CrabtreeOn,印度客户定制APP,迁移2.0平台版本
1
wxr
2023-03-31 d53e6af2c5f17838fa79659614b15a2a1f383399
Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using Shared;
using Shared.IO;
using Shared.SimpleControl;
namespace Shared
{
@@ -72,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;
@@ -116,6 +117,82 @@
        }
        #endregion
        /// <summary>
        /// 一键将本地回路设备和场景数据转换
        /// 然后上传oid、sid、以及场景列表
        /// </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>
        /// 上传场景列表
@@ -134,21 +211,16 @@
                        hdlLinkSceneList.Add (mHDLLinkScene);
                    }
                }
                //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 ());
            }
            return res;
        }
        /// <summary>
        /// 获取所有场景列表
@@ -205,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 {
@@ -224,25 +296,25 @@
            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 {
        ///// <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;
        }
        //    }
        //    return res;
        //}
        /// <summary>
        /// 原生协议Scene 转换成 HDLLinkScene数据格式
@@ -271,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 (),
@@ -330,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,
@@ -369,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,
@@ -582,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);
@@ -595,8 +684,8 @@
                } else {
                    IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
                }
            } catch {
            } catch(Exception EX) {
                Utlis.WriteLine ("catch: " + EX.ToString());
            }
            return res;
        }
@@ -736,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存在则取之前的oid 
                        functionLists.AddRange (ConvertToSidListToIotCloud (deviceOid.oid, tmp));
                        Utlis.WriteLine ("相同的模块,取之前的OID:" + deviceOid.addresses + " deviceType:" + deviceOid.deviceType);
                        //Utlis.WriteLine ("相同的模块,取之前的OID:" + deviceOid.addresses + " deviceType:" + deviceOid.deviceType);
                    }
                }