From 50b7b7a486f56fab27c83b59eb45673c6d9f0935 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期三, 24 二月 2021 18:33:57 +0800
Subject: [PATCH] 2021-02-24 1.新增场景上传。2.优化

---
 Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs |  125 ++++++++++++++++++++++++++++++++++++-----
 1 files changed, 109 insertions(+), 16 deletions(-)

diff --git a/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs b/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
index 996ecc4..0359a0d 100644
--- a/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
+++ b/Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
@@ -11,6 +11,40 @@
     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>
         /// <param name="curtainStatus"></param>
@@ -101,7 +135,13 @@
                     }
                 }
                 //2.涓婁紶鍦烘櫙
+                //2.1涓嶅瓨鍦紝闇�瑕佹柊涓婁紶鐨�
                 res = UploadSceneList (hdlLinkSceneList);
+                //2.2宸插瓨鍦紝闇�瑕佹洿鏂扮殑
+                //if (res) {
+                //    var updateLinkSceneList = new List<HDLLinkScene> ();
+                //    res = UploadSceneList (updateLinkSceneList);
+                //}
 
             } catch (Exception ex){
                 Utlis.WriteLine ("catch :" + ex.ToString ());
@@ -134,16 +174,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);
                 }
             }
@@ -172,6 +225,26 @@
         }
 
         /// <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>
         /// <param name="mScene"></param>
@@ -179,8 +252,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;
@@ -653,7 +725,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 = "";
@@ -727,23 +799,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 +1017,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 +2295,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