CrabtreeOn,印度客户定制APP,迁移2.0平台版本
JLChen
2021-02-24 50b7b7a486f56fab27c83b59eb45673c6d9f0935
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";
                }
            }
        }
    }