From 1293fea19c6a2b558986856e301d5218c1979692 Mon Sep 17 00:00:00 2001 From: wei <kaede@kaededeMacBook-Air.local> Date: 星期五, 27 十一月 2020 14:08:43 +0800 Subject: [PATCH] Revert "20201127 接受bus数据修改完成,搜索网关、一端口修改完成" --- HDL_ON/DAL/DriverLayer/Control.cs | 155 +++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 112 insertions(+), 43 deletions(-) diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs index 743e402..131f154 100644 --- a/HDL_ON/DAL/DriverLayer/Control.cs +++ b/HDL_ON/DAL/DriverLayer/Control.cs @@ -1,15 +1,16 @@ using System; using System.Collections.Generic; +using System.Text; using HDL_ON.Entity; namespace HDL_ON.DriverLayer { - /// <summary> /// 閫氳鏂瑰紡 /// </summary> public enum CommunicationMode { + none, /// <summary> /// 鏈湴udp /// </summary> @@ -20,12 +21,10 @@ tcp_local_client, } - - public class Control { static Control _control; - public static Control ins + public static Control Ins { get { @@ -36,10 +35,26 @@ return _control; } } + bool _gatewayOnline = false; /// <summary> /// 缃戝叧鏄惁鍦ㄧ嚎 /// </summary> - public bool gatewayOnline = false; + public bool GatewayOnline { + get + { + return _gatewayOnline; + } + set + { + _gatewayOnline = value; + //淇敼涓婚〉杩炴帴鐘舵�� + UI.HomePage.LoadEvent_CheckLinkStatus(); + } + } + /// <summary> + /// 鏄惁涓鸿繙绋嬭繛鎺� + /// </summary> + public bool IsRemote = false; /// <summary> /// 閫氳鍦板潃IP /// </summary> @@ -55,7 +70,7 @@ /// <summary> /// 鏈湴udp /// </summary> - public Control_Udp_Bus myUdp = null; + public Control_Udp myUdp = null; /// <summary> /// 閫氳鏂瑰紡 @@ -72,63 +87,54 @@ } /// <summary> - /// 鏀瑰彉閫氳鏂瑰紡 + /// 鎵撳紑Udp /// </summary> - public void ChangeCommunicationMode(CommunicationMode communicationMode) + public void OpenUdp(int port) { - if (this.communicationMode == communicationMode) + if (myUdp == null) { - return; + myUdp = new Control_Udp(); } - this.communicationMode = communicationMode; - - - switch (this.communicationMode) + UdpSocket._BusSocket.Start(port); + } + /// <summary> + /// 鎵撳紑tcp瀹㈡湇绔� + /// </summary> + public void OpenTcpClent() + { + if (myTcpClient == null) { - case CommunicationMode.local_BusUdp: - if (myUdp == null) - { - myUdp = new Control_Udp_Bus(); - } - UdpSocket._BusSocket.Start(); - if (myTcpClient != null) - { - myTcpClient.Close(); - myTcpClient = null; - } - break; - case CommunicationMode.tcp_local_client: - if (myTcpClient == null) - { - myTcpClient = new Control_TcpClient(reportIp); - myTcpClient.Connect(); - } - if (myUdp != null) - { - UdpSocket._BusSocket.Stop(); - myUdp = null; - } - break; + myTcpClient = new Control_TcpClient(reportIp); + myTcpClient.Connect(); } } + + ///// <summary> + ///// 鏀瑰彉閫氳鏂瑰紡 + ///// </summary> + //public void ChangeCommunicationMode(CommunicationMode communicationMode) + //{ + // this.communicationMode = communicationMode; + //} /// <summary> /// 鎼滅储鏈湴缃戝叧鍒楄〃 /// </summary> public void SearchLoaclGateway() { - ChangeCommunicationMode(CommunicationMode.local_BusUdp); + +#if DEBUG + DB_ResidenceData.residenceData.GatewayType = 1; +#endif + OpenUdp(DB_ResidenceData.residenceData.GatewayType == 0 ? 6000 : 8585); + if (DB_ResidenceData.residenceData.GatewayType == 0) { myUdp.ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) }); } else if (DB_ResidenceData.residenceData.GatewayType == 1) { - UdpSocket._BusSocket.SearchNetDeviceAction = (revIp) => - { - MainPage.Log($"鎼滅储鍒扮綉缁滆澶囷細{revIp}"); - }; myUdp.SearchLocalGateway(); } } @@ -224,6 +230,69 @@ sendSidObj.type = "device_sid"; sendSidObj.command = "get_list_response"; } + /// <summary> + /// 杞崲閫氳鏁版嵁 + /// </summary> + public byte[] ConvertSendBodyData(string topic, string bodyDataString) + { + string topicString = "Topic:" + topic + "/\r\n"; + byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString); + string lenghtString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n"; + + string sendDataString = topicString + lenghtString + bodyDataString; + byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString); + + return sendDataBytes; + } + /// <summary> + /// 杞崲鎺ユ敹鍒扮殑鏁版嵁 + /// </summary> + /// <returns></returns> + public string ConvertReceiveData(byte[] bytes) + { + string receiveString = ""; + + string topic = ""; + int lenght = 0; + + var reString = Encoding.UTF8.GetString(bytes); + var res = reString.Split("\r\n\r\n"); + + + if (res.Length == 2) + { + var topics = res[0].Split("\r\n"); + MainPage.Log(res[1]); + foreach (var ts in topics) + { + var key = ts.Split(":"); + switch (key[0]) + { + case "Topic": + topic = key[1]; + break; + case "Lenght": + lenght = Convert.ToInt32(key[1]); + break; + } + } + + switch (topic) + { + case CommunicationTopic.SearchLoaclGateway: + var jt = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(res[1]); + if (jt != null) + { + var device = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.DeviceModule>(jt.objects.ToString()); + //Newtonsoft.Json.Linq.JObject.FromObject(jt.objects); + } + break; + } + + } + return receiveString; + } + } } \ No newline at end of file -- Gitblit v1.8.0