1) Json 파일 읽기
파일 내용 - device.json { "deviceInfo":[ {"device":"DEVICE_069", "hostname":"127.0.0.1", "port":9010}, {"device":"DEVICE_083", "hostname":"127.0.0.1", "port":9020}, {"device":"DEVICE_015", "hostname":"127.0.0.1", "port":9030} ] } 코드에서 파일 읽기 class DeviceInfo { public string device = null; public string hostname = null; public int port = 0; } static Dictionary<string, DeviceInfo> dicDeviceInfo = new Dictionary<string, DeviceInfo>(); JObject jObj = JObject.Parse(File.ReadAllText(".\\INFO\\DEVICE.JSON")); foreach (JObject device in jObj["deviceInfo"]) { DeviceInfo deviceInfo = device.ToObject<DeviceInfo>(); dicDeviceInfo.Add(deviceInfo.device, deviceInfo); } 파일 내용 - server_command.json { "serverCommandInfo":[ {"command":"CMD_001", "forwardCommand":"CMD_001_A"}, {"command":"CMD_002", "forwardCommand":"CMD_002_B"} ] } 코드에서 파일읽기 static Dictionary<string, string> CmdInfo = new Dictionary<string, string>(); JObject jObj = JObject.Parse(File.ReadAllText(".\\INFO\\SERVER_COMMAND.JSON")); foreach (JObject cmd in jObj["serverCommandInfo"]) { CmdInfo[cmd["command"].ToString()] = cmd["forwardCommand"].ToString(); } } 코드로 Json 파일 쓰기 using (StreamWriter file = File.CreateText("sample.json")) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(file, jsonObj); } 코드로 Json 파일 쓰기 2 string path = Application.StartupPath + @"\Config.json"; string[] userList = new string[4] { "USER1", "USER2", "USER3", "USER4" }; string users = string.Empty; JObject configData = new JObject( new JProperty("IP", "127.0.0.1"), new JProperty("PORT", "8088"), new JProperty("DATABASE", "DB TEST"), new JProperty("ID", "TestID"), new JProperty("PASSWORD", "1234") ); // Jarray 로 추가 configData.Add("USERS", JArray.FromObject(userList)); // 파일 생성 후 쓰기 File.WriteAllText(path, configData.ToString()); |
[시나리오][Server] Request 송신 (URI:http://127.0.0.1:8010/fromServer, Body:{"command":"CMD_001","targetDevice":["DEVICE_069"],"param":"fe303904"}) [시나리오][DEVICE_069] Request 수신 (URI:http://127.0.0.1:9010/fromEdge, Body:{ "command": "CMD_001_A", "param": "fe303904" }) [시나리오][DEVICE_069] Response 송신 (Status Code:200, Body:{"result":["20b95f9c"]}) [시나리오][Server] Response 수신 (Status Code:200, Body:{ "result": [ "20b95f9c" ] }, Elapsed time:1,380ms) [시나리오][Server] Request 송신 (URI:http://127.0.0.1:8010/fromServer, Body:{"command":"CMD_002","targetDevice":["DEVICE_083","DEVICE_015"],"param":"ce3c39e1"}) [시나리오][DEVICE_083] Request 수신 (URI:http://127.0.0.1:9020/fromEdge, Body:{ "command": "CMD_002_B", "param": "ce3c39e1" }) [시나리오][DEVICE_083] Response 송신 (Status Code:200, Body:{"result":["c8d4fc55"]}) [시나리오][DEVICE_015] Request 수신 (URI:http://127.0.0.1:9030/fromEdge, Body:{ "command": "CMD_002_B", "param": "ce3c39e1" }) [시나리오][DEVICE_015] Response 송신 (Status Code:200, Body:{"result":["9604d2cd"]}) [시나리오][Server] Response 수신 (Status Code:200, Body:{ "result": [ "c8d4fc55", "9604d2cd" ] StreamReader sr = new StreamReader(context.Request.InputStream); JObject body = JObject.Parse(sr.ReadToEnd()); sr.Close(); strRes = sendToDevices(body["command"].ToString(), body["param"].ToString(), body["targetDevice"].ToObject<List<string>>()); |
'프로그래밍 > C#' 카테고리의 다른 글
기본내용 - 네트워크 (0) | 2025.06.01 |
---|---|
기본내용 (0) | 2025.06.01 |
sample #2 .. No 5 (0) | 2025.06.01 |
sample #2 .. No 4 (0) | 2025.06.01 |
sample #2 .. No 3 (0) | 2025.05.31 |