| | |
| | | using System.Collections.Generic; |
| | | using Shared; |
| | | using Shared.IO; |
| | | using Shared.SimpleControl; |
| | | |
| | | namespace Shared |
| | | { |
| | |
| | | /// </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> |
| | |
| | | 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; |
| | |
| | | } |
| | | #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> |
| | | /// 上传场景列表 |
| | |
| | | hdlLinkSceneList.Add (mHDLLinkScene); |
| | | } |
| | | } |
| | | //2.上传场景 |
| | | |
| | | //全量同步场景 |
| | | res = UploadSceneList (hdlLinkSceneList); |
| | | |
| | | |
| | | } catch (Exception ex){ |
| | | Utlis.WriteLine ("catch :" + ex.ToString ()); |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 获取所有场景列表 |
| | |
| | | } |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /// <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 { |
| | |
| | | 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> |
| | |
| | | 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; |
| | |
| | | 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 (), |
| | |
| | | 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, |
| | |
| | | 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, |
| | |
| | | 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); |
| | |
| | | } else { |
| | | IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code); |
| | | } |
| | | } catch { |
| | | |
| | | } catch(Exception EX) { |
| | | Utlis.WriteLine ("catch: " + EX.ToString()); |
| | | } |
| | | return res; |
| | | } |
| | |
| | | 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 = ""; |
| | |
| | | |
| | | 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); |
| | | } |
| | | |
| | | } |
| | |
| | | /// <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; |
| | |
| | | //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; |
| | |
| | | /// 固定的序号 |
| | | /// </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"; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |