| | |
| | | using System.Net.Sockets; |
| | | using System.Threading; |
| | | using System.Net; |
| | | using Shared; |
| | | using HDL_ON.Entity; |
| | | |
| | | namespace HDL_ON.DAL.Net |
| | | { |
| | |
| | | lock (tcpClient.m_Buffer) |
| | | { |
| | | //tcpClient.m_Buffer.Add(buf); |
| | | var tcpDataString = System.Text.Encoding.UTF8.GetString(buf); |
| | | AnalysisTcpData(tcpDataString); |
| | | var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length); |
| | | if (!string.IsNullOrEmpty(tcpDataString)) |
| | | { |
| | | AnalysisTcpData(socketClient, tcpDataString); |
| | | } |
| | | |
| | | MainPage.Log(tcpDataString); |
| | | } |
| | | } |
| | |
| | | Thread.Sleep(100); |
| | | } |
| | | } |
| | | List<Entity.FunctionOid> tcpAddFunctionOidObjects = new List<Entity.FunctionOid>(); |
| | | string tcpFunctionOidJsonString = ""; |
| | | /// <summary> |
| | | /// 处理tcp数据 |
| | | /// </summary> |
| | | void AnalysisTcpData(string tcpDataString) |
| | | void AnalysisTcpData(Socket socket, string tcpDataString) |
| | | { |
| | | MainPage.Log($"tcpDataString: {tcpDataString}"); |
| | | var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(tcpDataString); |
| | | if(obj == null) |
| | | if (obj == null) |
| | | { |
| | | return; |
| | | } |
| | | var tcpCommand = obj.GetValue("command").ToString(); |
| | | var tcpType = obj.GetValue("type").ToString(); |
| | | |
| | | |
| | | switch (tcpCommand) |
| | | { |
| | | /* |
| | | case "search":// 适用于搜索 |
| | | case "get_list"://获取列表 |
| | | case "read":// 用于依次读取相关类型信息状态 |
| | | case "set_list":// 修改列表 |
| | | case "write":// 用于依次控制相关类型信息 |
| | | case "delete":// 用于删除相关操作 |
| | |
| | | case "find":// 设备定位 |
| | | case "get":// 用于获取sid当前状态 |
| | | case "set":// 用于控制sid相关参数 |
| | | */ |
| | | */ |
| | | case "add":// 用于增加功能 |
| | | switch (tcpType) |
| | | { |
| | | case "device_oid":// 用于原生态设备读写相关操作 |
| | | tcpAddFunctionOidObjects = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Entity.FunctionOid>>(obj.GetValue("objects").ToString()); |
| | | |
| | | tcpFunctionOidJsonString = tcpDataString; |
| | | break; |
| | | case "device_sid": |
| | | var addSidFunction = Newtonsoft.Json.JsonConvert.DeserializeObject<TcpAddFunctionSidObject>(tcpDataString); |
| | | Shared.Application.RunOnMainThread(() => { |
| | | var tipDialog = new HDL_ON.UI.UpdataTcpResidenceDataDialog(); |
| | | tipDialog.ShowDialog(addSidFunction.objects, tcpAddFunctionOidObjects); |
| | | Application.RunOnMainThread(() => |
| | | { |
| | | var tipDialog = new UI.UpdataTcpResidenceDataDialog(); |
| | | var tcpLocalFunction = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Function>>(Newtonsoft.Json.JsonConvert.SerializeObject(addSidFunction.objects)); |
| | | tipDialog.ShowDialog(tcpLocalFunction, tcpFunctionOidJsonString); |
| | | }); |
| | | break; |
| | | } |
| | | break; |
| | | case "read":// 用于依次读取相关类型信息状态 |
| | | case "get_list"://获取列表 |
| | | switch (tcpType) |
| | | { |
| | | /* |
| | |
| | | case "global":// 用于组播搜索A设备 |
| | | */ |
| | | case "device_oid":// 用于原生态设备读写相关操作 |
| | | Dictionary<string, object> dic_oid = new Dictionary<string, object>(); |
| | | dic_oid.Add("from_oid", null); |
| | | dic_oid.Add("to_oid", null); |
| | | dic_oid.Add("time_stamp", null); |
| | | dic_oid.Add("type", "device_oid"); |
| | | dic_oid.Add("command", "get_list_response"); |
| | | |
| | | List<object> oidObjList = new List<object>(); |
| | | foreach (var d01 in Entity.DB_ResidenceData.functionList.GetAllDeviceFunctionList()) |
| | | var bytes = FileUtils.ReadFile("AProtocolData_FunctionOid"); |
| | | //var jsonString = System.Text.Encoding.UTF8.GetString(bytes); |
| | | //if (!string.IsNullOrEmpty(jsonString)) |
| | | if (bytes.Length > 0) |
| | | { |
| | | Dictionary<string, object> d0 = new Dictionary<string, object>(); |
| | | d0.Add("oid", d01.sid); |
| | | d0.Add("status", "online"); |
| | | d0.Add("device_name", d01.name); |
| | | d0.Add("device_model","HDL MD0602.523"); |
| | | d0.Add("device_mac", "123456789067890"); |
| | | d0.Add("hw_info", "STM32F104"); |
| | | d0.Add("fw_version","HDL_V04.01U"); |
| | | oidObjList.Add(d0); |
| | | socket.Send(bytes); |
| | | MainPage.Log($"send oid list to 8586 prot :{ System.Text.Encoding.UTF8.GetString(bytes)}"); |
| | | } |
| | | dic_oid.Add("objects", oidObjList); |
| | | Newtonsoft.Json.JsonConvert.SerializeObject(dic_oid); |
| | | break; |
| | | case "device_sid":// 用于功能模型读写操作 |
| | | Dictionary<string, object> dic_sid = new Dictionary<string, object>(); |
| | | dic_sid.Add("from_oid", null); |
| | | dic_sid.Add("to_oid", null); |
| | | dic_sid.Add("time_stamp", null); |
| | | dic_sid.Add("type", "device_sid"); |
| | | dic_sid.Add("command", "get_list_response"); |
| | | var sendSidObj = new TcpAddFunctionSidObject(); |
| | | sendSidObj.from_oid = ""; |
| | | sendSidObj.to_oid = ""; |
| | | sendSidObj.time_stamp = ""; |
| | | sendSidObj.type = "device_sid"; |
| | | sendSidObj.command = "get_list_response"; |
| | | //转换成bus需要的数据格式 |
| | | var localFunction = DB_ResidenceData.functionList.GetAllFunction(); |
| | | var localFunctionString = Newtonsoft.Json.JsonConvert.SerializeObject(localFunction); |
| | | var tcpFunction = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TcpObject>>(localFunctionString); |
| | | |
| | | List<object> sidObjList = new List<object>(); |
| | | foreach (var d01 in Entity.DB_ResidenceData.functionList.GetAllDeviceFunctionList()) |
| | | { |
| | | Dictionary<string, object> d0 = new Dictionary<string, object>(); |
| | | d0.Add("sid", d01.sid); |
| | | d0.Add("name", d01.name); |
| | | d0.Add("type", d01.functionType); |
| | | sidObjList.Add(d0); |
| | | } |
| | | dic_sid.Add("objects", sidObjList); |
| | | Newtonsoft.Json.JsonConvert.SerializeObject(dic_sid); |
| | | sendSidObj.objects.AddRange(tcpFunction); |
| | | var sendSidJson = Newtonsoft.Json.JsonConvert.SerializeObject(sendSidObj); |
| | | var sendSidBytes = System.Text.Encoding.UTF8.GetBytes(sendSidJson); |
| | | socket.Send(sendSidBytes, sendSidBytes.Length, SocketFlags.OutOfBand); |
| | | MainPage.Log($"send sid list to 8586 prot :{sendSidJson}"); |
| | | break; |
| | | } |
| | | break; |
| | |
| | | |
| | | public class TcpAddFunctionSidObject |
| | | { |
| | | public List<Entity.Function> objects; |
| | | public List<TcpObject> objects = new List<TcpObject>(); |
| | | |
| | | public string type;//device_sid |
| | | public string from_oid;//": null, |
| | | public string to_oid;//": null, |
| | | public string time_stamp;//": null, |
| | | public string command;//": "add" |
| | | public string from_oid = ""; |
| | | public string to_oid = ""; |
| | | public string time_stamp = ""; |
| | | public string type = ""; |
| | | public string command = ""; |
| | | } |
| | | |
| | | //public class SidObject |
| | | //{ |
| | | // public List<Entity.Trait> function = new List<Entity.Trait>(); |
| | | // public string sid;//": "000101E10FEB7212040100010700", |
| | | // public string name;//": "HVAC-1" |
| | | //} |
| | | |
| | | public class TcpObject |
| | | { |
| | | public string sid = "0301011234567801012301230123"; |
| | | |
| | | /// <summary> |
| | | /// A协议功能的特性 |
| | | /// 如:是AC功能:特性:on_off,mode,fan,temperature |
| | | /// attri |
| | | /// </summary> |
| | | public List<Trait> function = new List<Trait>(); |
| | | |
| | | /// <summary> |
| | | /// 备注 |
| | | /// </summary> |
| | | public string name; |
| | | } |
| | | |
| | | } |