| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Text; |
| | | using HDL_ON.DAL; |
| | | using HDL_ON.DAL.Net; |
| | | using HDL_ON.Entity; |
| | | using Shared; |
| | | using Shared.Net; |
| | | using Newtonsoft.Json; |
| | | using Newtonsoft.Json.Linq; |
| | | |
| | | namespace HDL_ON |
| | | { |
| | | public partial class Control |
| | | { |
| | | public DateTime LatestDateTime = DateTime.Now; |
| | | /// <summary> |
| | | /// 控制失败次数 |
| | | /// </summary> |
| | | public static int controlLostCount = 0; |
| | | |
| | | /// <summary> |
| | | /// 所有微信对一端口的控制都会放到这个集合里 |
| | |
| | | /// <summary> |
| | | /// 发送数据,不需要等待回复 |
| | | /// </summary> |
| | | static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null) |
| | | public static void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3, System.Net.IPEndPoint ipEndpoint = null) |
| | | { |
| | | Control control = new Control(); |
| | | control.Send(new Target() |
| | |
| | | DeviceID = deviceID, |
| | | AddData = gatewayBytes, |
| | | }, sendCount, false); |
| | | MainPage.Log(command.ToString() + "::" + CommonPage.EndPoint.ToString()); |
| | | |
| | | } |
| | | |
| | | public static byte[] ReadGatewayIPAddress() |
| | | { |
| | | Control control = new Control(); |
| | | control.ReadGatewayIP(); |
| | | return control.UsefulBytes; |
| | | } |
| | | |
| | | static DAL.Net.TcpListener tcpListener; |
| | | |
| | | /// <summary> |
| | | /// 000E 搜索回复 |
| | | /// </summary> |
| | |
| | | return; |
| | | } |
| | | |
| | | //if (!isHttpListenerStart) |
| | | //{ |
| | | // //数据接收端口没打开,不回复 |
| | | // return; |
| | | //} |
| | | |
| | | var sendStr = MainPage.LoginUser.accountString; |
| | | |
| | | |
| | | //如果IP地址获取成功 显示最后一位 |
| | | var mAddressBytes = new Shared.Net.NetWiFi().IpAddress.GetAddressBytes(); |
| | | if (mAddressBytes != null && mAddressBytes.Length == 4) |
| | | if (tcpListener == null) |
| | | { |
| | | sendStr = (mAddressBytes[3] & 0xFF).ToString() + ":" + sendStr; |
| | | tcpListener = new DAL.Net.TcpListener(); |
| | | tcpListener.OpenServer(8586); |
| | | } |
| | | |
| | | if (usefullBytes.Length <= 2) |
| | | //if (!CommonPage.isHttpListenerStart) |
| | | //{ |
| | | // //数据接收端口没打开,不回复 |
| | | // CommonPage.InitHttpListener(); |
| | | //} |
| | | var sendStr = MainPage.LoginUser.accountString; |
| | | if (usefullBytes.Length == 0) |
| | | { |
| | | byte[] sendBytes = new byte[20]; |
| | | byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr); |
| | | Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length); |
| | | Control.ControlBytesSend(Command.ReadRemarkACK, 0xFF, 0xFF, sendBytes, 1); |
| | | |
| | | SenRemark(sendStr); |
| | | } |
| | | else |
| | | { |
| | | |
| | | for (int i = 2, len = usefullBytes.Length; i < len; i++) |
| | | bool isExit = false; |
| | | for (int i = 0, len = usefullBytes.Length; i < len; i++) |
| | | { |
| | | if (i % 2 == 0) |
| | | { |
| | | if ((usefullBytes[i] & 0xFF) == 0xFC |
| | | && (usefullBytes[i + 1] & 0xFF) == 0xFC |
| | | ) |
| | | if ((usefullBytes[i] & 0xFF) == 252 |
| | | && (usefullBytes[i + 1] & 0xFF) == 252) |
| | | { |
| | | |
| | | byte[] sendBytes = new byte[20]; |
| | | byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr); |
| | | //Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1); |
| | | Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length); |
| | | Control.ControlBytesSend(Command.ReadRemarkACK, 0xFF, 0xFF, sendBytes, 1); |
| | | |
| | | isExit = true; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (!isExit) |
| | | { |
| | | //不存在,代表没收到本机的发送,继续回复 |
| | | SenRemark(sendStr); |
| | | } |
| | | } |
| | | |
| | | } |
| | | catch { } |
| | | catch (Exception ex) { |
| | | Console.WriteLine($"回复bus搜索异常 : {ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 000F回复备注 |
| | | /// </summary> |
| | | /// <param name="sendStr"></param> |
| | | static void SenRemark(string sendStr) |
| | | { |
| | | byte[] sendBytes = new byte[20]; |
| | | byte[] b1 = CommonPage.MyEncodingGB2312.GetBytes(sendStr); |
| | | //Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1); |
| | | Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length); |
| | | |
| | | var control = new Control(); |
| | | control.Send(new Target() |
| | | { |
| | | IPEndPoint = CommonPage.EndPoint, |
| | | Command = Command.ReadRemarkACK, |
| | | SubnetID = 0xFF, |
| | | DeviceID = 0xFF, |
| | | AddData = sendBytes, |
| | | }, 0, false); //设置当前发送指令方式为:任何情况下本地发送、不限制、不加密明文发送 |
| | | } |
| | | /// <summary> |
| | | /// 处理接收回来的数据 |
| | | /// </summary> |
| | |
| | | /// <param name="remoteEndPoint">源网络套接字</param> |
| | | public static void ManagerReceive(byte subnetID, byte deviceID, Command command, byte targetSubnetID, byte targetDeviceID, byte[] usefulBytes, System.Net.EndPoint remoteEndPoint) |
| | | { |
| | | if (((System.Net.IPEndPoint)remoteEndPoint).Port == 6688) |
| | | { |
| | | |
| | | for (int i = 0; i < controlList.Count; i++) |
| | | { |
| | | try |
| | | { |
| | | var control = controlList[i]; |
| | | control.LatestDateTime = DateTime.Now; |
| | | control.UsefulBytes = usefulBytes;// |
| | | control.run(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine($"control error : {ex.Message}"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | try |
| | | { |
| | | string receiveFlag = string.Format("{0},{1},{2},", subnetID, deviceID, (int)command); |
| | |
| | | break; |
| | | case Command.ReadRemark: |
| | | //buspro 读备注回复 |
| | | Console.WriteLine("buspro 读备注回复"); |
| | | ReceiveReadRemark(usefulBytes); |
| | | break; |
| | | case Command.ReadLightAllLoopBrightnessACK: |
| | | receiveFlag += ""; |
| | | break; |
| | | //case Command.YIPanelDeviceInofACK: |
| | | // for (int i = 0; i < 4; i++) |
| | |
| | | //case Command.SetSensorPushValuesACK: |
| | | //case Command.ReadSensorPushValuesACK: |
| | | // receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]); |
| | | // break; |
| | | //case Command.ReadLightEquipmentAllLoopBrightnessACK: |
| | | // break; |
| | | //case Command.UpLoadInfraredACK: |
| | | // receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]); |
| | |
| | | //case Command.Remote3thACK: |
| | | // receiveFlag = string.Format("{0},", (int)command); |
| | | // break; |
| | | //case Command.ReadDeviceLoopInfoACK: |
| | | // if (usefulBytes[0] == 1) |
| | | // { |
| | | // receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[2]); ; |
| | | // } |
| | | // else |
| | | // receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]); |
| | | // break; |
| | | case Command.ReadDeviceLoopInfoACK: |
| | | if (usefulBytes[0] == 1) |
| | | { |
| | | receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[2]); ; |
| | | } |
| | | else |
| | | receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]); |
| | | break; |
| | | //case Command.SetDeviceLoopInfoACK: |
| | | //case Command.InfraredControlACK: |
| | | //case Command.SetSecurityByPassACK: |
| | |
| | | //case Command.ReadWirelessPanelButtonKeyACK: |
| | | //case Command.WriteWirelessPanelButtonKeyACK: |
| | | //case Command.ReadDryContactStatusACK: |
| | | //case Command.InstructionPanelKeyACK: |
| | | //case Command.ReadInstructionPanelKeyACK: |
| | | //case Command.ReadAnalogValueACK: |
| | | //case Command.ReadSensorTargetRemarkACK: |
| | | //case Command.SetHornTargetStateACK: |
| | | //case Command.ReadHornTargetStateACK: |
| | | // receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]); |
| | | // break; |
| | | case Command.InstructionPanelKeyACK: |
| | | case Command.ReadInstructionPanelKeyACK: |
| | | //case Command.ReadAnalogValueACK: |
| | | //case Command.ReadSensorTargetRemarkACK: |
| | | //case Command.SetHornTargetStateACK: |
| | | //case Command.ReadHornTargetStateACK: |
| | | receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]); |
| | | break; |
| | | //case Command.AssignedAddressACK: |
| | | //case Command.UpdataCurtainModelRunTimeACK: |
| | | //case Command.ReadCurtainStutasACK: |
| | |
| | | control.run(); |
| | | } |
| | | } |
| | | catch |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | Console.WriteLine($"control error : {ex.Message}"); |
| | | } |
| | | } |
| | | } |
| | |
| | | void managerSendCount(object o) |
| | | { |
| | | add(); |
| | | if (CommonPage.IsRemote) |
| | | if (MainPage.IsRemote) |
| | | { |
| | | MqttCommon.MqttRemoteSend(Packet.Bytes); |
| | | System.Console.WriteLine("远程发送数据:" + SendFlag); |
| | | Mqtt_Cloud.MqttRemoteSend(Packet.Bytes); |
| | | Packet.FlagDateTime = DateTime.Now; |
| | | Packet.HaveSendCount--; |
| | | //这里是重发两次 |
| | |
| | | { |
| | | if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= System.DateTime.Now.Ticks) |
| | | { |
| | | MqttCommon.MqttRemoteSend(Packet.Bytes); |
| | | Mqtt_Cloud.MqttRemoteSend(Packet.Bytes); |
| | | System.Console.WriteLine("重发远程发送数据:" + SendFlag); |
| | | Packet.FlagDateTime = DateTime.Now; |
| | | Packet.HaveSendCount++; |
| | | } |
| | |
| | | }
allDone.Set();
} |
| | | else |
| | | { |
| | | //Bus socket无法控制,重启机制 |
| | | if (controlLostCount > 10) |
| | | { |
| | | BusSocket.Stop(); |
| | | new System.Threading.Thread(() => |
| | | { |
| | | System.Threading.Thread.Sleep(1000); |
| | | BusSocket.Start(6000); |
| | | controlLostCount = 0; |
| | | }) |
| | | { IsBackground = true }.Start(); |
| | | } |
| | | |
| | | try |
| | | { |
| | | MainPage.Log("发送数据:" + SendFlag); |
| | |
| | | //这里是重发两次 |
| | | while (Packet.HaveSendCount < 3) |
| | | { |
| | | if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= System.DateTime.Now.Ticks) |
| | | if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks) |
| | | { |
| | | MainPage.Log("重发数据:" + SendFlag); |
| | | BusSocket.AsyncBeginSend(Packet); |
| | | controlLostCount++; |
| | | } |
| | | System.Threading.Thread.Sleep(100); |
| | | } |
| | |
| | | /// </summary> |
| | | Packet Packet; |
| | | |
| | | private void ini(Target target, bool send = true) |
| | | private void ini(Target target) |
| | | { |
| | | this.SendFlag = string.Format("{0},{1},{2},", target.SubnetID, target.DeviceID, (int)target.Command + 1); |
| | | |
| | |
| | | break; |
| | | case Command.SetLogicLoopColor: |
| | | this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]); |
| | | break; |
| | | case Command.ReadLightAllLoopBrightness: |
| | | this.SendFlag += ""; |
| | | break; |
| | | //case Command.YIPanelDeviceInof: |
| | | // for (int i = 0; i < 4; i++) |
| | |
| | | //case Command.Remote3th: |
| | | // this.SendFlag = string.Format("{0},", (int)target.Command + 1); |
| | | // break; |
| | | //case Command.ReadDeviceLoopInfo: |
| | | // if (target.AddData[0] == 1)//特殊处理灯光类,DMX模块每一个回路不固定小类,根据具体设置来回复 |
| | | // this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[2]); |
| | | // else |
| | | // this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]); |
| | | // break; |
| | | case Command.ReadDeviceLoopInfo: |
| | | if (target.AddData[0] == 1)//特殊处理灯光类,DMX模块每一个回路不固定小类,根据具体设置来回复 |
| | | this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[2]); |
| | | else |
| | | this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]); |
| | | break; |
| | | //case Command.SetDeviceLoopInfo: |
| | | //case Command.InfraredControl: |
| | | //case Command.SetSecurityByPass: |
| | |
| | | //case Command.SetCurtainModelStutas: |
| | | //case Command.DownloadInfrared: |
| | | //case Command.UpLoadInfrared: |
| | | //case Command.InstructionPanelKey: |
| | | //case Command.ReadInstructionPanelKey: |
| | | //case Command.ReadSensorTargetRemark: |
| | | //case Command.SetHornTargetState: |
| | | // this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[1]); |
| | | // break; |
| | | case Command.InstructionPanelKey: |
| | | case Command.ReadInstructionPanelKey: |
| | | //case Command.ReadSensorTargetRemark: |
| | | //case Command.SetHornTargetState: |
| | | this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[1]); |
| | | break; |
| | | //case Command.AssignedAddress: |
| | | //case Command.UpdataCurtainModelRunTime: |
| | | //case Command.ReadCurtainStatus: |
| | |
| | | //return; |
| | | break; |
| | | } |
| | | if (send) |
| | | { |
| | | System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount)); |
| | | thread.IsBackground = true; |
| | | thread.Start(Packet); |
| | | } |
| | | |
| | | System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount)); |
| | | thread.IsBackground = true; |
| | | thread.Start(Packet); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="target">发送对象</param> |
| | | /// <param name="sendCount">重发次数</param> |
| | | public void Send(Target target, int sendCount, bool isWait) |
| | | void Send(Target target, int sendCount, bool isWait) |
| | | { |
| | | try |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 读取网关IP |
| | | /// </summary> |
| | | void ReadGatewayIP() |
| | | { |
| | | try |
| | | { |
| | | var sendJob = new JObject {{ "command", "search" } }; |
| | | var SearchGateway = JsonConvert.SerializeObject(sendJob); |
| | | var SearchGatewayPayload = Encoding.ASCII.GetBytes(SearchGateway); |
| | | Packet = new Packet(SearchGatewayPayload, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("224.0.168.188"), 6688)); |
| | | System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount)); |
| | | thread.IsBackground = true; |
| | | thread.Start(Packet); |
| | | |
| | | wait(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | MainPage.Log($"Send bus data error {ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | } |