| | |
| | | /// <summary> |
| | | /// 本地udp |
| | | /// </summary> |
| | | public Control_Udp myUdp = null; |
| | | public Control_Udp myUdp1 = null; |
| | | |
| | | /// <summary> |
| | | /// 通讯方式 |
| | |
| | | /// </summary> |
| | | public void OpenUdp(int port) |
| | | { |
| | | if (myUdp == null) |
| | | { |
| | | myUdp = new Control_Udp(); |
| | | } |
| | | |
| | | UdpSocket._BusSocket.Start(port); |
| | | } |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | public void CloseUdp() |
| | | { |
| | | if (myUdp != null) |
| | | { |
| | | myUdp = null; |
| | | } |
| | | UdpSocket._BusSocket.Stop(); |
| | | } |
| | | |
| | |
| | | break; |
| | | if (DB_ResidenceData.Instance.GatewayType == 0) |
| | | { |
| | | myUdp.ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) }); |
| | | new Control_Udp().ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) }); |
| | | } |
| | | else if (DB_ResidenceData.Instance.GatewayType == 1) |
| | | { |
| | | myUdp.SearchLocalGateway(); |
| | | new Control_Udp().SearchLocalGateway(); |
| | | } |
| | | System.Threading.Thread.Sleep(500); |
| | | } |
| | |
| | | { |
| | | if (DB_ResidenceData.Instance.GatewayType == 0) |
| | | { |
| | | myUdp.ControlBusScenes(scene); |
| | | new Control_Udp().ControlBusScenes(scene); |
| | | } |
| | | } |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | if (myUdp != null) |
| | | { |
| | | myUdp.WriteBusData(function, commandDictionary); |
| | | } |
| | | new Control_Udp().WriteBusData(function, commandDictionary); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | //ALink控制 |
| | | else |
| | | { |
| | | if (myUdp != null) |
| | | { |
| | | var functionControlDataObj = function.GetGatewayAlinkControlData(commandDictionary); |
| | | var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj); |
| | | var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson); |
| | | Ins.myUdp.SendLocalHdlLinkData(sendBytes); |
| | | } |
| | | var functionControlDataObj = function.GetGatewayAlinkControlData(commandDictionary); |
| | | var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj); |
| | | var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson); |
| | | new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id); |
| | | } |
| | | } |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | Ins.myUdp.ReadBusData(function); |
| | | new Control_Udp().ReadBusData(function); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | }; |
| | | var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(readDataObj); |
| | | var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ReadStatus, functionControlDataJson); |
| | | Ins.myUdp.SendLocalHdlLinkData(sendBytes); |
| | | new Control_Udp().SendLocalHdlLinkData(sendBytes,readDataObj.id); |
| | | } |
| | | } |
| | | } |
| | |
| | | }; |
| | | var aLinkJson = Newtonsoft.Json.JsonConvert.SerializeObject(aLinkData); |
| | | var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlScene, aLinkJson); |
| | | Ins.myUdp.SendLocalHdlLinkData(sendBytes); |
| | | new Control_Udp().SendLocalHdlLinkData(sendBytes,aLinkData.id); |
| | | } |
| | | } |
| | | /// <summary> |
| | |
| | | else if(receiveObj.Topic == CommunicationTopic.ct.ReadStatus+"_reply" || |
| | | receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus) |
| | | { |
| | | UpdataFunctionStatus(receiveObj.BodyDataString); |
| | | //TODO 暂时不传正确的数据上去,如果后面要优化前面这些代码 |
| | | UpdataFunctionStatus(receiveObj.BodyDataString,null); |
| | | } |
| | | } |
| | | return receiveObj; |
| | |
| | | /// A协议数据 |
| | | /// </summary> |
| | | /// <param name="updateBytes"></param> |
| | | public void UpdataFunctionStatus(string revString) |
| | | public void UpdataFunctionStatus(string revString,byte []usefulBytes) |
| | | { |
| | | MainPage.Log($"A协议更新状态:{revString}"); |
| | | var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<AlinkFunctionStatusObj>(revString); |
| | | if (temp != null) |
| | | { |
| | | Control_Udp.ReceiveRepeatManager(temp.id,usefulBytes); |
| | | var allLocalFuntion = FunctionList.List.GetDeviceFunctionList(); |
| | | foreach (var updateTemp in temp.objects) |
| | | { |
| | |
| | | /// 所有对一端口的控制都会放到这个集合里 |
| | | /// </summary> |
| | | static List<Control_Udp> controlList = new List<Control_Udp>(50); |
| | | |
| | | |
| | | public System.Net.IPEndPoint EndPoint |
| | | { |
| | |
| | | /// <summary> |
| | | /// 重发验证 |
| | | /// </summary> |
| | | public void ReceiveRepeatManager(string receiveFlag) |
| | | public static void ReceiveRepeatManager(string receiveFlag,byte []usefulBytes) |
| | | { |
| | | for (int i = 0; i < controlList.Count; i++) |
| | | { |
| | |
| | | { |
| | | for (int i = 0; i < controlList.Count;) |
| | | { |
| | | if (controlList[i] == null || 3 <= controlList[i].Packet.HaveSendCount) |
| | | if (controlList[i] == null || 3 <= controlList[i].packet.HaveSendCount) |
| | | { |
| | | controlList.RemoveAt(i); |
| | | } |
| | |
| | | void run() |
| | | { |
| | | allDone.Set(); |
| | | Packet.HaveSendCount = 4; |
| | | packet.HaveSendCount = 4; |
| | | } |
| | | |
| | | //数据发送处理 |
| | |
| | | new System.Threading.Thread(() => |
| | | { |
| | | System.Threading.Thread.Sleep(1000); |
| | | UdpSocket._BusSocket.Start(6000); |
| | | UdpSocket._BusSocket.Start(UdpSocket._BusSocket.Port); |
| | | controlLostCount = 0; |
| | | }) |
| | | { IsBackground = true }.Start(); |
| | |
| | | try |
| | | { |
| | | //MainPage.Log("发送数据:" + SendFlag); |
| | | UdpSocket._BusSocket.AsyncBeginSend(Packet); |
| | | Packet.HaveSendCount--; |
| | | UdpSocket._BusSocket.AsyncBeginSend(packet); |
| | | packet.HaveSendCount--; |
| | | |
| | | //这里是重发两次 |
| | | while (Packet.HaveSendCount < 3) |
| | | while (packet.HaveSendCount < 3) |
| | | { |
| | | if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks) |
| | | if (packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks) |
| | | { |
| | | MainPage.Log("重发数据:" + SendFlag); |
| | | UdpSocket._BusSocket.AsyncBeginSend(Packet); |
| | | UdpSocket._BusSocket.AsyncBeginSend(packet); |
| | | controlLostCount++; |
| | | } |
| | | System.Threading.Thread.Sleep(100); |
| | |
| | | /// <summary> |
| | | /// 当前数据包 |
| | | /// </summary> |
| | | Packet Packet; |
| | | Packet packet; |
| | | /// <summary> |
| | | /// 记录发送数据包 |
| | | /// </summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | Packet = new Packet(target.SendBytes, target.IPEndPoint); |
| | | Packet.HaveSendCount = 3 - sendCount; |
| | | packet = new Packet(target.SendBytes, target.IPEndPoint); |
| | | packet.HaveSendCount = 3 - sendCount; |
| | | |
| | | signPacket(target); |
| | | |
| | | System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount)); |
| | | thread.IsBackground = true; |
| | | thread.Start(Packet); |
| | | thread.Start(packet); |
| | | |
| | | if (isWait) |
| | | { |
| | |
| | | |
| | | var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.SearchLoaclGateway, bodyString); |
| | | //组播发送 |
| | | Packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585)); |
| | | Packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断 |
| | | packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585)); |
| | | packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断 |
| | | System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount)); |
| | | thread.IsBackground = true; |
| | | thread.Start(Packet); |
| | | thread.Start(packet); |
| | | //wait();不需要等待 |
| | | } |
| | | catch (Exception ex) |
| | |
| | | MainPage.Log($"Send bus data error {ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 发送udp A协议数据 |
| | | /// </summary> |
| | | public void SendLocalHdlLinkData(byte[] sendBytes) |
| | | public void SendLocalHdlLinkData(byte[] sendBytes,string id, bool isWait=false) |
| | | { |
| | | Packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585)); |
| | | Packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断 |
| | | packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585)); |
| | | sendFlag = id; |
| | | System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount)); |
| | | thread.IsBackground = true; |
| | | thread.Start(Packet); |
| | | //wait();不需要等待 |
| | | thread.Start(packet); |
| | | if (isWait) |
| | | { |
| | | wait(); |
| | | } |
| | | //MainPage.Log($"发送Hdl-Link数据,IP:{Control.Ins.reportIp}:8585"); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]); |
| | | break; |
| | | case Command.ReadRemark: |
| | | DriverLayer.Control.Ins.myUdp.ReceiveReadRemark(usefulBytes); |
| | | new Control_Udp().ReceiveReadRemark(usefulBytes); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | DriverLayer.Control.Ins.myUdp.ReceiveRepeatManager(receiveFlag); |
| | | Control_Udp.ReceiveRepeatManager(receiveFlag,usefulBytes); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | |
| | | //本地Socket |
| | | private Socket busSocket; |
| | | int _port = 0; |
| | | public int Port = 0; |
| | | /// <summary> |
| | | /// 启动Socket接收和发送功能 |
| | | /// </summary> |
| | |
| | | { |
| | | if (IsRunning) |
| | | { |
| | | if (port == _port) |
| | | if (port == Port) |
| | | { |
| | | return; |
| | | } |
| | |
| | | } |
| | | } |
| | | if (port != 0) |
| | | _port = port; |
| | | if (_port == 0) |
| | | Port = port; |
| | | if (Port == 0) |
| | | return; |
| | | |
| | | busSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); |
| | | busSocket.EnableBroadcast = true; |
| | | try { |
| | | //busSocket.Bind (new IPEndPoint (IPAddress.Any, 6000)); |
| | | busSocket.Bind(new IPEndPoint(IPAddress.Any, _port)); |
| | | busSocket.Bind(new IPEndPoint(IPAddress.Any, Port)); |
| | | |
| | | busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188"))); |
| | | } |
| | |
| | | { |
| | | //A协议数据处理 |
| | | var revString = Encoding.UTF8.GetString(bytes); |
| | | Control.Ins.UpdataFunctionStatus(revString); |
| | | Control.Ins.UpdataFunctionStatus(revString,null); |
| | | } |
| | | } |
| | | } |
| | |
| | | { |
| | | int RandKey = new Random().Next(0, 255); |
| | | var textByte = (byte)iCode; |
| | | Control.Ins.myUdp.ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0); |
| | | new Control_Udp().ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0); |
| | | } |
| | | public void ControlTV(int number) |
| | | { |
| | |
| | | { |
| | | textByte = 18; |
| | | } |
| | | Control.Ins.myUdp.ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0); |
| | | new Control_Udp().ControlBytesSend(Command.InfraredControl, bus_Data.SubnetID, bus_Data.DeviceID, new byte[] { 1, bus_Data.loopId, textByte, 0, 0, (byte)RandKey }, 0); |
| | | } |
| | | } |
| | | } |
| | |
| | | using Newtonsoft.Json; |
| | | using HDL_ON.Entity; |
| | | using System.Collections.Generic; |
| | | |
| | | using HDL_ON.DriverLayer; |
| | | |
| | | namespace HDL_ON.UI |
| | | { |
| | |
| | | deviceIpList.Clear(); |
| | | contentView.RemoveAll(); |
| | | //DriverLayer.Control.ins.ChangeCommunicationMode(DriverLayer.CommunicationMode.local_BusUdp); |
| | | // UdpSocket._BusSocket.SearchNetDeviceAction = (jto) => |
| | | // { |
| | | // var device = JsonConvert.DeserializeObject<DeviceModule>(jto.ToString()); |
| | | // if (device != null) |
| | | // { |
| | | // lock (deviceIpList) |
| | | // { |
| | | // if (!deviceIpList.Contains(device.ip_address)) |
| | | // { |
| | | // deviceIpList.Add(device.ip_address); |
| | | // MainPage.Log($"搜索到网络设备:{device.ip_address}"); |
| | | // Application.RunOnMainThread(() => |
| | | // { |
| | | //#if DEBUG |
| | | //#else |
| | | // if(device.gateway_type== 4) |
| | | //#endif |
| | | // { |
| | | // LoadRow(device); |
| | | // } |
| | | // }); |
| | | // } |
| | | // else |
| | | // { |
| | | // MainPage.Log($"已加载该IP设备:{device.ip_address}"); |
| | | // } |
| | | // } |
| | | // } |
| | | // }; |
| | | DriverLayer.Control.Ins.myUdp.SearchLocalGateway(); |
| | | // UdpSocket._BusSocket.SearchNetDeviceAction = (jto) => |
| | | // { |
| | | // var device = JsonConvert.DeserializeObject<DeviceModule>(jto.ToString()); |
| | | // if (device != null) |
| | | // { |
| | | // lock (deviceIpList) |
| | | // { |
| | | // if (!deviceIpList.Contains(device.ip_address)) |
| | | // { |
| | | // deviceIpList.Add(device.ip_address); |
| | | // MainPage.Log($"搜索到网络设备:{device.ip_address}"); |
| | | // Application.RunOnMainThread(() => |
| | | // { |
| | | //#if DEBUG |
| | | //#else |
| | | // if(device.gateway_type== 4) |
| | | //#endif |
| | | // { |
| | | // LoadRow(device); |
| | | // } |
| | | // }); |
| | | // } |
| | | // else |
| | | // { |
| | | // MainPage.Log($"已加载该IP设备:{device.ip_address}"); |
| | | // } |
| | | // } |
| | | // } |
| | | // }; |
| | | new Control_Udp().SearchLocalGateway(); |
| | | contentView.EndHeaderRefreshing(); |
| | | }; |
| | | |