| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Text; |
| | | using HDL_ON.Entity; |
| | | |
| | | namespace HDL_ON.DriverLayer |
| | |
| | | /// <summary> |
| | | /// 打开Udp |
| | | /// </summary> |
| | | public void OpenUdp() |
| | | public void OpenUdp(int port) |
| | | { |
| | | if (myUdp == null) |
| | | { |
| | | myUdp = new Control_Udp(); |
| | | } |
| | | |
| | | UdpSocket._BusSocket.Start(); |
| | | UdpSocket._BusSocket.Start(port); |
| | | } |
| | | /// <summary> |
| | | /// 打开tcp客服端 |
| | |
| | | /// </summary> |
| | | public void SearchLoaclGateway() |
| | | { |
| | | OpenUdp(); |
| | | |
| | | #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) }); |
| | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | } |
| | | } |