From d53e6af2c5f17838fa79659614b15a2a1f383399 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期五, 31 三月 2023 10:04:58 +0800
Subject: [PATCH] 1

---
 Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs |  244 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 213 insertions(+), 31 deletions(-)

diff --git a/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs b/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
index 996ecc4..4beb6ca 100644
--- a/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
+++ b/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using Shared;
 using Shared.IO;
+using Shared.SimpleControl;
 
 namespace Shared
 {
@@ -10,6 +11,40 @@
     /// </summary>
     public class HDLLinkUtlis
     {
+        /// <summary>
+        /// 鐢熸垚閫昏緫sid鏂规硶
+        /// </summary>
+        public static string NewSceneSid ()
+        {
+            string sceneId = "";
+            try {
+                string sOidBeginsWith = "000101";//鍘傚晢 + 閫氳鏂瑰紡
+               
+                string sTimeSpan = "00000000";
+                long sTimeSp = ConvertDateTimeLong (); //浠�2020骞�1鏈�1鏃ョ畻鍑虹殑鏃堕棿鎴�0.1s涓哄崟浣�
+                ConvertIntToByteArray (sTimeSp, ref sTimeSpan);
+              
+                if (sTimeSpan.Length > 8) {
+                    sTimeSpan = sTimeSpan.Substring (0, 8);
+                }
+              
+                sceneId = sOidBeginsWith + sTimeSpan;
+
+                sceneId += "0A";
+                sceneId += "0A01";
+                int maxId = 1;
+
+                Random random = new Random (Guid.NewGuid ().GetHashCode ());
+                maxId = random.Next (65535);
+
+                sceneId += (maxId).ToString ("X4");
+                sceneId += "0000";
+            } catch {
+                return sceneId;
+            }
+            return sceneId;
+        }
+
         /// <summary>
         /// 
         /// </summary>
@@ -38,7 +73,7 @@
         public static long ConvertDateTimeLong ()
         {
             System.DateTime startTime = TimeZoneInfo.ConvertTimeToUtc (new System.DateTime (2020, 1, 1));
-            long l = (long)(Math.Round ((DateTime.Now - startTime).TotalSeconds, 1) * 10);
+            long l = (long)(Math.Round ((DateTime.Now - startTime).TotalMilliseconds, 1) / 10);
             if (l <= LastTime) l = LastTime + 1;
             LastTime = l;
             return l;
@@ -82,6 +117,82 @@
         }
         #endregion
 
+        /// <summary>
+        /// 涓�閿皢鏈湴鍥炶矾璁惧鍜屽満鏅暟鎹浆鎹�
+        /// 鐒跺悗涓婁紶oid銆乻id銆佷互鍙婂満鏅垪琛�
+        /// </summary>
+        /// <returns></returns>
+        public void OneclickUpload (bool isMigrate = false)
+        {
+            if (isMigrate) {
+                var res = false;
+                res = UploadOidAndSidList ();
+                if (res) {
+                    res = GetFunctionList ();
+                    if (res) {
+                        res = UploadSecneList ();
+                    }
+                }
+                return;
+            }
+            MainPage.Loading.Start ("Uploading...");
+            System.Threading.Tasks.Task.Run (() => {
+                try {
+                    var res = false;
+                    res = UploadOidAndSidList ();
+                    if (res) {
+                        res = GetFunctionList ();
+                        if (res) {
+                            res = UploadSecneList ();
+                        }
+                    }
+
+                    if (res) {
+                        Utlis.ShowAlertOnMainThread (Language.StringByID (SimpleControl.R.MyInternationalizationString.SuccessfullySynchronizedToTheCloud));
+                    } else {
+                        Utlis.ShowAlertOnMainThread (Language.StringByID (SimpleControl.R.MyInternationalizationString.FailedToSyncToTheCloud));
+                    }
+
+                } catch {
+
+                } finally {
+                    Application.RunOnMainThread (() => {
+                        MainPage.Loading.Hide ();
+                    });
+                }
+            });
+
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <returns></returns>
+        public bool GetFunctionList ()
+        {
+            var res = false;
+            try {
+                var revertObj = HttpServerRequest.Current.GetDeviceList ();
+                if (revertObj.Code == StateCode.SUCCESS) {
+                    var devcieFunctionRes = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieFunctionRes> (revertObj.Data.ToString ());
+                    if (devcieFunctionRes != null && devcieFunctionRes.list != null && devcieFunctionRes.list.Count > 0) {
+                        CommonConfig.Current.FunctionList = devcieFunctionRes.list;
+                    } else {
+                        CommonConfig.Current.FunctionList = new List<Function> ();
+                    }
+                    CommonConfig.Current.Save ();
+                    res = true;
+                } else {
+                    //鎻愮ず閿欒
+                    IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
+                }
+            } catch {
+
+            }
+
+            return res;
+        }
+
         #region 鈻� 涓婁紶鍦烘櫙鍒楄〃___________________________
         /// <summary>
         /// 涓婁紶鍦烘櫙鍒楄〃
@@ -100,15 +211,16 @@
                         hdlLinkSceneList.Add (mHDLLinkScene);
                     }
                 }
-                //2.涓婁紶鍦烘櫙
+            
+                //鍏ㄩ噺鍚屾鍦烘櫙
                 res = UploadSceneList (hdlLinkSceneList);
+                
 
             } catch (Exception ex){
                 Utlis.WriteLine ("catch :" + ex.ToString ());
             }
             return res;
         }
-
 
         /// <summary>
         /// 鑾峰彇鎵�鏈夊満鏅垪琛�
@@ -134,16 +246,29 @@
                 }
             }
 
-            foreach (var list in globalSceneFileList) {
-                var tempScene = Scene.GetSceneByFilePath (list);
+            foreach (var sceneFilePath in globalSceneFileList) {
+                var tempScene = Scene.GetSceneByFilePath (sceneFilePath);
                 if (tempScene != null) {
+                    if (string.IsNullOrEmpty (tempScene.Sid)) {
+                        //濡傛灉Sid涓虹┖锛岄噸鏂扮敓鎴愬苟淇濆瓨
+                        tempScene.Sid = NewSceneSid ();
+                        tempScene.Save (sceneFilePath);
+                        Utlis.WriteLine ("鐢熸垚鏂扮殑鍦烘櫙id锛� " + tempScene.Sid);
+
+                    }
                     targetSceneList.Add (tempScene);
                 }
             }
 
-            foreach (var list in RoomsSceneFileList) {
-                var tempScene = Scene.GetSceneByFilePath (list);
+            foreach (var roomSceneFilePath in RoomsSceneFileList) {
+                var tempScene = Scene.GetSceneByFilePath (roomSceneFilePath);
                 if (tempScene != null) {
+                    if (string.IsNullOrEmpty (tempScene.Sid)) {
+                        //濡傛灉Sid涓虹┖锛岄噸鏂扮敓鎴愬苟淇濆瓨
+                        tempScene.Sid = NewSceneSid ();
+                        tempScene.Save (roomSceneFilePath);
+                        Utlis.WriteLine ("鐢熸垚鏂扮殑鍦烘櫙id锛� " + tempScene.Sid);
+                    }
                     targetSceneList.Add (tempScene);
                 }
             }
@@ -152,14 +277,14 @@
         }
 
         /// <summary>
-        /// 
+        /// 鍏ㄩ噺鍚屾鍦烘櫙
         /// </summary>
         /// <param name="mScene"></param>
         bool UploadSceneList (List<HDLLinkScene> hdlLinkSceneList)
         {
             var res = false;
             try {
-                var revertObj = HttpServerRequest.Current.AddScene (hdlLinkSceneList);
+                var revertObj = HttpServerRequest.Current.SecneSyncList (hdlLinkSceneList);
                 if (revertObj.Code == StateCode.SUCCESS) {
                     res = true;
                 } else {
@@ -171,6 +296,26 @@
             return res;
         }
 
+        ///// <summary>
+        ///// 
+        ///// </summary>
+        ///// <param name="mScene"></param>
+        //bool UpdateSceneList (List<HDLLinkScene> updateLinkSceneList)
+        //{
+        //    var res = false;
+        //    try {
+        //        var revertObj = HttpServerRequest.Current.EditScene (updateLinkSceneList);
+        //        if (revertObj.Code == StateCode.SUCCESS) {
+        //            res = true;
+        //        } else {
+        //            IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
+        //        }
+        //    } catch {
+
+        //    }
+        //    return res;
+        //}
+
         /// <summary>
         /// 鍘熺敓鍗忚Scene 杞崲鎴� HDLLinkScene鏁版嵁鏍煎紡
         /// </summary>
@@ -179,8 +324,7 @@
         HDLLinkScene GetHDLLinkScene (Scene mScene)
         {
             var mHDLLinkScene = new HDLLinkScene ();
-            //mHDLLinkScene.sid = mHDLLinkScene.NewSid();
-            mHDLLinkScene.sid = mScene.SceneID;
+            mHDLLinkScene.sid = mScene.Sid;
             mHDLLinkScene.name = mScene.Name;
             if (UserConfig.Instance.CheckWhetherGatewayIdNotNull ()) {
                 mHDLLinkScene.gatewayId = UserConfig.Instance.HomeGateway.gatewayId;
@@ -199,13 +343,18 @@
                         var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDimming> (jsonInfo);
                         var mSceneFunction = new SceneFunction ();
                         mSceneFunction.sid = function.sid;
-                        //1.Brightness
+                        //1.on_off
                         var status = new List<SceneFunctionStatus> ();
+                        status.Add (new SceneFunctionStatus () {
+                            key = FunctionAttributeKey.OnOff,
+                            value = commonNew.CurrentBrightness > 0 ? "on" : "off",
+                        });
+                        //2.Brightness
                         status.Add (new SceneFunctionStatus () {
                             key = FunctionAttributeKey.Brightness,
                             value = commonNew.CurrentBrightness.ToString(),
                         });
-                        //2.Delay
+                        //3.Delay
                         status.Add (new SceneFunctionStatus () {
                             key = FunctionAttributeKey.Delay,
                             value = (commonNew.DelayTimeHeight * 256 + commonNew.DelayTimeLow).ToString (),
@@ -258,19 +407,24 @@
                         var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (jsonInfo);
                         var mSceneFunction = new SceneFunction ();
                         mSceneFunction.sid = function.sid;
-                        //1.Brightness
+                        //1.on_off
                         var status = new List<SceneFunctionStatus> ();
+                        status.Add (new SceneFunctionStatus () {
+                            key = FunctionAttributeKey.OnOff,
+                            value = commonNew.CurrentBrightness > 0 ? "on" : "off",
+                        });
+                        //2.Brightness
                         status.Add (new SceneFunctionStatus () {
                             key = FunctionAttributeKey.Brightness,
                             value = commonNew.CurrentBrightness.ToString (),
                         });
-                        //2.Delay
+                        //3.Delay
                         status.Add (new SceneFunctionStatus () {
                             key = FunctionAttributeKey.Delay,
                             value = (commonNew.DelayTimeHeigh * 256 + commonNew.DelayTimeLow).ToString (),
                         });
 
-                        //3.rgb
+                        //4.rgb
                         status.Add (new SceneFunctionStatus () {
                             key = FunctionAttributeKey.RGB,
                             value = commonNew.RStatus + "," + commonNew.GStatus + "," + commonNew.BStatus,
@@ -297,7 +451,7 @@
                         var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (jsonInfo);
                         var mSceneFunction = new SceneFunction ();
                         mSceneFunction.sid = function.sid;
-                        //1.Brightness
+                        //1.OnOff
                         var status = new List<SceneFunctionStatus> ();
                         status.Add (new SceneFunctionStatus () {
                             key = FunctionAttributeKey.OnOff,
@@ -510,6 +664,13 @@
                 IotCloud currentProject = new IotCloud ();
                 var list = GetAllDeviceList ();
                 ConvertOidListToIotStruct (UserConfig.Instance.CurrentRegion.Id, UserConfig.Instance.HomeGateway.gatewayId, list, ref currentProject);
+                //2022-06-21 13:10:07 mac涓嶈兘涓虹┖锛屽惁鍒欎簯绔湁闂
+                foreach (var bean in currentProject.modules.devices) {
+                    if (string.IsNullOrEmpty (bean.mac)) {
+                        bean.mac = bean.oid;
+                    }
+                }
+
                 Utlis.WriteLine ("list: " + list.Count + "currentProject : " + currentProject.functions.devices.Count);
 
                 var revertObj = HttpServerRequest.Current.UploadDeviceOidList (currentProject.modules);
@@ -523,8 +684,8 @@
                 } else {
                     IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
                 }
-            } catch {
-
+            } catch(Exception EX) {
+                Utlis.WriteLine ("catch: " + EX.ToString());
             }
             return res;
         }
@@ -653,7 +814,7 @@
                         Oid tmpOid = new Oid ();
                         tmpOid.protocolType = "bus";
                         tmpOid.device_name = tmp.Type.ToString ();
-                        tmpOid.oid = FormingNewOid (tmp.Type);
+                        tmpOid.oid = FormingNewOid (tmp);
                         tmpOid.addresses = addresses;
                         tmpOid.device_model = tmp.Type.ToString ();
                         tmpOid.fw_version = "";
@@ -664,11 +825,11 @@
 
                         functionLists.AddRange (ConvertToSidListToIotCloud (tmpOid.oid, tmp));
 
-                        Utlis.WriteLine ("鏂扮殑妯″潡锛屾柊鐨勭殑OID锛�" + tmpOid.addresses + " deviceType:" + tmpOid.deviceType);
+                        //Utlis.WriteLine ("鏂扮殑妯″潡锛屾柊鐨勭殑OID锛�" + tmpOid.addresses + " deviceType:" + tmpOid.deviceType);
                     } else {
                         //3.2瀛樺湪鍒欏彇涔嬪墠鐨刼id 
                         functionLists.AddRange (ConvertToSidListToIotCloud (deviceOid.oid, tmp));
-                        Utlis.WriteLine ("鐩稿悓鐨勬ā鍧楋紝鍙栦箣鍓嶇殑OID锛�" + deviceOid.addresses + " deviceType:" + deviceOid.deviceType);
+                        //Utlis.WriteLine ("鐩稿悓鐨勬ā鍧楋紝鍙栦箣鍓嶇殑OID锛�" + deviceOid.addresses + " deviceType:" + deviceOid.deviceType);
                     }
 
                 }
@@ -727,23 +888,30 @@
         /// <summary>
         /// 鐢熸垚璁惧Oid
         /// </summary>
-        /// <param name="iDeviceType"></param>
+        /// <param name="common"></param>
         /// <returns></returns>
-        public string FormingNewOid (DeviceType deviceType)
+        public string FormingNewOid (Common common)
         {
             string sOid = "";
             try {
                 //1.鐢熸垚 鍘傚晢 + 閫氳鏂瑰紡 
                 string sOidBeginsWith = "000101";//鍘傚晢 + 閫氳鏂瑰紡
-                //2.鐢熸垚浜у搧鏃堕棿鎴�
-                long sTimeSp = ConvertDateTimeLong (); //浠�2020骞�1鏈�1鏃ョ畻鍑虹殑鏃堕棿鎴�0.1s涓哄崟浣�
+
+                //****************************
+                ////2.鐢熸垚浜у搧鏃堕棿鎴�
+                //long sTimeSp = ConvertDateTimeLong (); //浠�2020骞�1鏈�1鏃ョ畻鍑虹殑鏃堕棿鎴�0.1s涓哄崟浣�
+                //string sTimeSpan = "";
+                //ConvertIntToByteArray (sTimeSp, ref sTimeSpan);
+                //if (sTimeSpan.Length != 8) return sOid;
+                ////寤惰繜10ms
+
+                //2.鏂规浜岋細鐢熸垚浜у搧鏃堕棿鎴筹紙浜戠涓嶈В鏋愶級鍗板害APP鎶婂綋鍓嶄綅缃敼涓�(瀛愮綉鍙枫�佽澶囧彿銆佸ぇ绫诲皬绫�)琛ㄧず锛屼繚璇佽澶囧湪瀛愮綉鍙疯澶囧彿涓嶅彉鐨勬儏鍐典笅 oid鐢熸垚涔熶笉鍙�
                 string sTimeSpan = "";
-                ConvertIntToByteArray (sTimeSp, ref sTimeSpan);
-                if (sTimeSpan.Length != 8) return sOid;
-                //寤惰繜10ms
+                sTimeSpan = common.SubnetID.ToString ("X2") + common.DeviceID.ToString ("X2") + common.DeviceTypeString;
+                //****************************
 
                 //3.鐢熸垚浜у搧绫诲埆
-                var productType = GetProductType (deviceType);
+                var productType = GetProductType (common.Type);
                 sOid = sOidBeginsWith + sTimeSpan + productType;
 
                 return sOid;
@@ -938,7 +1106,7 @@
                 //2.閫氶亾鍙�
                 string loopIDStr = loopCommon.LoopID.ToString ("X4");
                 //3.澶у皬绫诲埆
-                string bigMinStr = loopCommon.BigClass.ToString ("X2") + loopCommon.MinClass.ToString ("X2");
+                string bigMinStr = loopCommon.DeviceTypeString;
                 //4.鎷兼帴
                 sSid = sOid + mPhysicalModelType + loopIDStr + bigMinStr;
                 return sSid;
@@ -2216,6 +2384,20 @@
         /// 鍥哄畾鐨勫簭鍙�
         /// </summary>
         public int fixedSerialNumber = int.MaxValue;
+
+        /// <summary>
+        /// 澶х被灏忕被
+        /// </summary>
+        [Newtonsoft.Json.JsonIgnore]
+        public string DeviceTypeString {
+            get {
+                if (!string.IsNullOrEmpty (sid) && sid.Length >= 28) {
+                    return sid.Substring (sid.Length - 4, 4);
+                } else {
+                    return "0000";
+                }
+            }
+        }
     }
 
 

--
Gitblit v1.8.0