From 33656fa9abd15ed00a390024b439568d2713ca1a Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期日, 06 十二月 2020 17:44:36 +0800
Subject: [PATCH] 2020-12-06 1.修改挤下线判断标记。2.修改房间和场景加载云端图片路径

---
 HDL_ON/DAL/DriverLayer/Control.cs |  202 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 148 insertions(+), 54 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control.cs b/HDL_ON/DAL/DriverLayer/Control.cs
index 131f154..aeb874a 100644
--- a/HDL_ON/DAL/DriverLayer/Control.cs
+++ b/HDL_ON/DAL/DriverLayer/Control.cs
@@ -35,6 +35,27 @@
                 return _control;
             }
         }
+        int _msg_id = 1;
+        /// <summary>
+        /// 閫氳ID
+        /// </summary>
+        public int msg_id
+        {
+            get
+            {
+                return _msg_id++;
+            }
+        }
+        /// <summary>
+        /// 鑾峰彇13浣嶆椂闂存埑
+        /// </summary>
+        /// <returns></returns>
+        public string Get_TimeStamp()
+        {
+            long t = DateTime.Now.Ticks / 10000;
+            return t.ToString();
+        }
+
         bool _gatewayOnline = false;
         /// <summary>
         /// 缃戝叧鏄惁鍦ㄧ嚎
@@ -52,9 +73,31 @@
             }
         }
         /// <summary>
+        /// 缃戝叧閫氳ID
+        /// </summary>
+        public string GatewayId;
+
+        bool _isRemote = false;
+        /// <summary>
         /// 鏄惁涓鸿繙绋嬭繛鎺�
         /// </summary>
-        public bool IsRemote = false;
+        public bool IsRemote {
+            get
+            {
+                return _isRemote;
+            }
+            set
+            {
+                _isRemote = value;
+                if(value)
+                {
+                    DAL.Mqtt.MqttClient.InitState();
+                }else
+                {
+                    DAL.Mqtt.MqttClient.DisConnectRemote();
+                }
+            }
+        }
         /// <summary>
         /// 閫氳鍦板潃IP
         /// </summary>
@@ -99,6 +142,18 @@
             UdpSocket._BusSocket.Start(port);
         }
         /// <summary>
+        /// 鍏抽棴udp
+        /// </summary>
+        public void CloseUdp()
+        {
+            if (myUdp != null)
+            {
+                myUdp = null;
+            }
+            UdpSocket._BusSocket.Stop();
+        }
+
+        /// <summary>
         /// 鎵撳紑tcp瀹㈡湇绔�
         /// </summary>
         public void OpenTcpClent()
@@ -123,20 +178,25 @@
         /// </summary>
         public void SearchLoaclGateway()
         {
-
-#if DEBUG
-            DB_ResidenceData.residenceData.GatewayType = 1;
-#endif
             OpenUdp(DB_ResidenceData.residenceData.GatewayType == 0 ? 6000 : 8585);
-
-            if (DB_ResidenceData.residenceData.GatewayType == 0)
+            new System.Threading.Thread(() =>
             {
-                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)
-            {
-                myUdp.SearchLocalGateway();
-            }
+                for (int i = 0; i < 5; i++)
+                {
+                    if (GatewayOnline)
+                        break;
+                    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)
+                    {
+                        myUdp.SearchLocalGateway();
+                    }
+                    System.Threading.Thread.Sleep(500);
+                }
+            })
+            { IsBackground = true }.Start();
         }
 
         /// <summary>
@@ -166,28 +226,46 @@
         {
             function.usageCount++;
             function.refreshTime = DateTime.Now;
-            if (DB_ResidenceData.residenceData.GatewayType == 0)
+            DAL.Server.HttpServerRequest httpServer = new DAL.Server.HttpServerRequest();
+            //杩滅▼閫氳
+            if (!Ins.IsRemote)
             {
-                try
-                {
-                    if(myUdp != null)
-                    {
-                        myUdp.WriteBusData(function, commandDictionary);
-                    }
-                }
-                catch (Exception ex)
-                {
-                    MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}");
-                }
+                //ALink鎺у埗銆丅us鎺у埗浣跨敤鍚屼竴涓帴鍙f帶鍒讹紝鐢变簯绔礋璐hВ鏋�
+                var apiControlData = function.GetApiControlData(commandDictionary);
+                var actionObjs = new List<ApiAlinkControlActionObj>();
+                actionObjs.Add(apiControlData);
+                var pack = httpServer.ControlDevice(actionObjs);
+                MainPage.Log($"{pack.Code}:{pack.Data}");
             }
+            //鏈湴閫氳
             else
             {
-                if (myTcpClient != null)
+                //ALink鎺у埗
+                if (DB_ResidenceData.residenceData.GatewayType == 0)
                 {
-                    var sendDataObj = function.GetControlSendData(commandDictionary);
-                    var sendJson = Newtonsoft.Json.JsonConvert.SerializeObject(sendDataObj);
-                    var sendBytes = System.Text.Encoding.UTF8.GetBytes(sendJson);
-                    myTcpClient.SendMessage(sendBytes);
+                    try
+                    {
+                        if (myUdp != null)
+                        {
+                            myUdp.WriteBusData(function, commandDictionary);
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        MainPage.Log($"鍙戦�佹暟鎹紓甯�: {ex.Message}");
+                    }
+                }
+                //Bus鎺у埗
+                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);
+                        //MainPage.Log($"鍙戦�佹湰鍦癏DL-Link鏁版嵁{functionControlDataJson}\r\n");
+                    }
                 }
             }
         }
@@ -235,12 +313,13 @@
         /// </summary>
         public byte[] ConvertSendBodyData(string topic, string bodyDataString)
         {
-            string topicString = "Topic:" + topic + "/\r\n";
+            string topicString = "Topic:" + topic + "\r\n";
             byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString);
-            string lenghtString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n";
+            string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n";
 
-            string sendDataString = topicString + lenghtString + bodyDataString;
+            string sendDataString = topicString + lengthString + bodyDataString;
             byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString);
+            MainPage.Log($"杞崲HDL-Link鏁版嵁\r\n{sendDataString}\r\n");
 
             return sendDataBytes;
         }
@@ -248,16 +327,20 @@
         /// 杞崲鎺ユ敹鍒扮殑鏁版嵁
         /// </summary>
         /// <returns></returns>
-        public string ConvertReceiveData(byte[] bytes)
+        public void ConvertReceiveData(byte[] receiveBytes)
         {
-            string receiveString = "";
+            var reString = Encoding.UTF8.GetString(receiveBytes);
+            AnalysisReceiveData(reString);
+        }
+        /// <summary>
+        /// 杞崲鎺ユ敹鍒扮殑鏁版嵁
+        /// </summary>
+        /// <returns></returns>
+        public LocalCommunicationData AnalysisReceiveData(string receiveString)
+        {
+            LocalCommunicationData receiveObj = new LocalCommunicationData();
 
-            string topic = "";
-            int lenght = 0;
-
-            var reString = Encoding.UTF8.GetString(bytes);
-            var res = reString.Split("\r\n\r\n");
-            
+            var res = receiveString.Split("\r\n\r\n");
 
             if (res.Length == 2)
             {
@@ -269,30 +352,41 @@
                     switch (key[0])
                     {
                         case "Topic":
-                            topic = key[1];
+                            receiveObj.Topic = key[1];
                             break;
-                        case "Lenght":
-                            lenght = Convert.ToInt32(key[1]);
+                        case "Length":
+                            receiveObj.Length = Convert.ToInt32(key[1]);
                             break;
                     }
                 }
+                //楠岃瘉鏈夋晥鏁版嵁闀垮害
+                //if (res[1].Length != receiveObj.Length)
+                //{
+                //    MainPage.Log($"鏀跺埌鏁版嵁鍖呴暱搴︿笉澶�");
+                //    return receiveObj;
+                //}
+                receiveObj.BodyDataString = res[1];
 
-                switch (topic)
+                var bodyJObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(res[1]);
+                if (bodyJObj == null)
                 {
-                    case CommunicationTopic.SearchLoaclGateway:
-                        var jt = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(res[1]);
-                        if (jt != null)
+                    return receiveObj;
+                }
+                switch (receiveObj.Topic)
+                {
+                    case CommunicationTopic.SearchLoaclGatewayReply:
+                        var device = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceModule>(bodyJObj.objects.ToString());
+                        if (device.device_mac == DB_ResidenceData.residenceData.residenceGatewayMAC)
                         {
-                            var device = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.DeviceModule>(jt.objects.ToString());
-                            //Newtonsoft.Json.Linq.JObject.FromObject(jt.objects);
+                            Ins.GatewayOnline = true;
+                            Ins.GatewayId = device.gatewayId;
+                            reportIp = "239.0.168.188";// device.ip_address;
+                            Ins.IsRemote = false;
                         }
                         break;
                 }
-
             }
-            return receiveString;
+            return receiveObj;
         }
-
-
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0