| | |
| | | if (value) |
| | | { |
| | | MainPage.Log($"网关在线,刷新设备状态"); |
| | | FunctionList.List.ReadAllFunctionStatus(); |
| | | new System.Threading.Thread(() => |
| | | { |
| | | System.Threading.Thread.Sleep(1000); |
| | | FunctionList.List.ReadAllFunctionStatus(); |
| | | }) |
| | | { IsBackground = true, Priority = System.Threading.ThreadPriority.AboveNormal }.Start(); |
| | | } |
| | | } |
| | | } |
| | |
| | | /// <summary> |
| | | /// 网关通讯ID |
| | | /// </summary> |
| | | public string GatewayId; |
| | | public string GatewayId =""; |
| | | |
| | | bool _isRemote = false; |
| | | /// <summary> |
| | |
| | | /// <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(); |
| | | } |
| | | |
| | |
| | | /// </summary> |
| | | public void SearchLoaclGateway() |
| | | { |
| | | OpenUdp(DB_ResidenceData.residenceData.GatewayType == 0 ? 6000 : 8585); |
| | | OpenUdp(DB_ResidenceData.Instance.GatewayType == 0 ? 6000 : 8585); |
| | | new System.Threading.Thread(() => |
| | | { |
| | | for (int i = 0; i < 5; i++) |
| | | { |
| | | if (GatewayOnline) |
| | | break; |
| | | if (DB_ResidenceData.residenceData.GatewayType == 0) |
| | | 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.residenceData.GatewayType == 1) |
| | | else if (DB_ResidenceData.Instance.GatewayType == 1) |
| | | { |
| | | myUdp.SearchLocalGateway(); |
| | | new Control_Udp().SearchLocalGateway(); |
| | | } |
| | | System.Threading.Thread.Sleep(500); |
| | | } |
| | |
| | | /// </summary> |
| | | public void ControlScene(Scene scene) |
| | | { |
| | | if(myUdp!=null) |
| | | { |
| | | myUdp.ControlBusScenes(scene); |
| | | } |
| | | if(myTcpClient!= null) |
| | | if (Ins.IsRemote || DB_ResidenceData.Instance.GatewayType == 1) |
| | | { |
| | | ControlAProtocolScene(scene); |
| | | } |
| | | else |
| | | { |
| | | if (DB_ResidenceData.Instance.GatewayType == 0) |
| | | { |
| | | new Control_Udp().ControlBusScenes(scene); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | function.usageCount++; |
| | | function.refreshTime = DateTime.Now; |
| | | DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest(); |
| | | |
| | | //远程通讯 |
| | | if (Ins.IsRemote) |
| | | { |
| | | DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest(); |
| | | //ALink控制、Bus控制使用同一个接口控制,由云端负责解析 |
| | | var apiControlData = function.GetApiControlData(commandDictionary); |
| | | var actionObjs = new List<ApiAlinkControlActionObj>(); |
| | | actionObjs.Add(apiControlData); |
| | | var pack = httpServer.ControlDevice(actionObjs); |
| | | MainPage.Log($"{pack.Code}:{pack.Data}"); |
| | | //MainPage.Log($"{pack.Code}:{pack.Data}"); |
| | | } |
| | | //本地通讯 |
| | | else |
| | | { |
| | | //Bus控制 |
| | | if (DB_ResidenceData.residenceData.GatewayType == 0) |
| | | if (DB_ResidenceData.Instance.GatewayType == 0) |
| | | { |
| | | 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); |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | else |
| | | { |
| | | if (DB_ResidenceData.residenceData.GatewayType == 0) |
| | | if (DB_ResidenceData.Instance.GatewayType == 0) |
| | | { |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | //} |
| | | |
| | | /// <summary> |
| | | /// 控制a协议场景 |
| | | /// a协议控制场景 |
| | | /// </summary> |
| | | /// <param name="scene"></param> |
| | | static void ControlAProtocolScene(Scene scene) |
| | | { |
| | | var sendSidObj = new AProtocolSendingObject(); |
| | | sendSidObj.time_stamp = ""; |
| | | sendSidObj.type = "device_sid"; |
| | | sendSidObj.command = "get_list_response"; |
| | | if (Ins.IsRemote) |
| | | { |
| | | var pm = new HDL_ON.DAL.Server.HttpServerRequest(); |
| | | pm.ExecuteScene(scene.userSceneId); |
| | | } |
| | | else |
| | | { |
| | | Dictionary<string, string> keys = new Dictionary<string, string>(); |
| | | keys.Add("sid", scene.sid); |
| | | var aLinkData = new AlinkReadFunctionStatusObj() |
| | | { |
| | | id = Ins.msg_id.ToString(), |
| | | objects = new List<Dictionary<string, string>>() |
| | | { |
| | | keys |
| | | }, |
| | | time_stamp = Utlis.GetTimestamp() |
| | | }; |
| | | var aLinkJson = Newtonsoft.Json.JsonConvert.SerializeObject(aLinkData); |
| | | var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlScene, aLinkJson); |
| | | new Control_Udp().SendLocalHdlLinkData(sendBytes,aLinkData.id); |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 转换通讯数据 |
| | |
| | | if (res.Length == 2) |
| | | { |
| | | var topics = res[0].Split("\r\n"); |
| | | MainPage.Log(res[1]); |
| | | //MainPage.Log(res[1]); |
| | | foreach (var ts in topics) |
| | | { |
| | | var key = ts.Split(":"); |
| | |
| | | if (receiveObj.Topic == CommunicationTopic.SearchLoaclGatewayReply) |
| | | { |
| | | var device = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceModule>(bodyJObj.objects.ToString()); |
| | | if (device.device_mac == DB_ResidenceData.residenceData.residenceGatewayMAC) |
| | | if (device.device_mac == DB_ResidenceData.Instance.residenceGatewayMAC) |
| | | { |
| | | Ins.GatewayOnline = true; |
| | | if(!string.IsNullOrEmpty( device.gatewayId)) |
| | |
| | | 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}"); |
| | | if(Control.Ins.myUdp!= null) |
| | | { |
| | | //重置重启udp判断调整 |
| | | Ins.myUdp.controlLostCount = 0; |
| | | } |
| | | var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<AlinkFunctionStatusObj>(revString); |
| | | if (temp != null) |
| | | { |
| | | var allLocalFuntion = FunctionList.List.GetAllDeviceFunctionList(); |
| | | Control_Udp.ReceiveRepeatManager(temp.id,usefulBytes); |
| | | var allLocalFuntion = FunctionList.List.GetDeviceFunctionList(); |
| | | foreach (var updateTemp in temp.objects) |
| | | { |
| | | try |
| | | { |
| | | bool hadChange = false;//状态有变化再更新界面 |
| | | //bool hadChange = false;//状态有变化再更新界面 |
| | | var updataObj = allLocalFuntion.Find((obj) => obj.sid == updateTemp.sid); |
| | | if (updataObj != null) |
| | | { |
| | |
| | | continue; |
| | | } |
| | | locatAttr.curValue = attr.value; |
| | | hadChange = true; |
| | | //hadChange = true; |
| | | } |
| | | } |
| | | } |
| | | if (!hadChange)//状态有变化再更新界面 |
| | | { |
| | | continue; |
| | | } |
| | | //2020-12-13 如果控制时,立即更新界面即状态,收到数据不用在处理,目前只修改设备状态,界面没有更新,还是需要接收数据更新 |
| | | //if (!hadChange)//状态有变化再更新界面 |
| | | //{ |
| | | // continue; |
| | | //} |
| | | //更新界面状态 |
| | | switch (updataObj.spk) |
| | | { |