CrabtreeOn,印度客户定制APP,迁移2.0平台版本
Crabtree/SmartHome/HDL/Common/HDLLinkUtlis.cs
@@ -10,6 +10,60 @@
    /// </summary>
    public class HDLLinkUtlis
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="curtainStatus"></param>
        /// <returns></returns>
        public static string GetCurtainStatusKey (CurtainStatus curtainStatus) {
            if(curtainStatus == CurtainStatus.Open) {
                return "on";
            } else if (curtainStatus == CurtainStatus.Close) {
                return "off";
            } else if (curtainStatus == CurtainStatus.Stop) {
                return "stop";
            } else {
                return "off";
            }
        }
        #region 生成4位byte 时间戳
        private static long LastTime = 0;
        /// <summary>
        /// DateTime时间格式转换为13位带毫秒的Unix时间戳
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        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);
            if (l <= LastTime) l = LastTime + 1;
            LastTime = l;
            return l;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="m"></param>
        /// <param name="strTmp"></param>
        /// <returns></returns>
        public static bool ConvertIntToByteArray (long m, ref string strTmp)
        {
            strTmp = "00000000";
            byte [] arry = new byte [4];
            arry [0] = (byte)(m & 0xFF);
            arry [1] = (byte)((m & 0xFF00) >> 8);
            arry [2] = (byte)((m & 0xFF0000) >> 16);
            arry [3] = (byte)((m & 0xFF000000) >> 24);
            strTmp = arry [0].ToString ("X2") + arry [1].ToString ("X2") + arry [2].ToString ("X2") + arry [3].ToString ("X2");
            return true;
        }
        #endregion
        #region ■ Current___________________________
        /// <summary>
        /// 通用方法
@@ -28,11 +82,425 @@
        }
        #endregion
        #region ■ 上传场景列表___________________________
        /// <summary>
        /// 上传场景列表
        /// </summary>
        public bool UploadSecneList ()
        {
            var res = false;
            try {
                //1.加载所有场景并转换
                var allSecneList = GetAllSecneList ();
                var hdlLinkSceneList = new List<HDLLinkScene> ();
                foreach (var sence in allSecneList) {
                    var mHDLLinkScene = GetHDLLinkScene (sence);
                    //1.1成功转换的场景才添加到准备上传列表
                    if(mHDLLinkScene != null && mHDLLinkScene.functions != null && mHDLLinkScene.functions.Count > 0) {
                        hdlLinkSceneList.Add (mHDLLinkScene);
                    }
                }
                //2.上传场景
                res = UploadSceneList (hdlLinkSceneList);
            } catch (Exception ex){
                Utlis.WriteLine ("catch :" + ex.ToString ());
            }
            return res;
        }
        /// <summary>
        /// 获取所有场景列表
        /// </summary>
        /// <returns></returns>
        public List<Scene> GetAllSecneList ()
        {
            List<Scene> targetSceneList = new List<Scene> ();
            //// 找出需要显示的场景
            // 1.全局场景
            var globalSceneFileList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (Scene.GlobalSceneFilePath)));
            if (globalSceneFileList == null) {
                globalSceneFileList = new List<string> ();
            }
            // 2.房间场景
            List<string> RoomsSceneFileList = new List<string> ();
            foreach (var r in Room.Lists) {
                if (string.IsNullOrEmpty (r.Name)) {
                    continue;
                }
                if (r != null) {
                    RoomsSceneFileList.AddRange (r.SceneFilePathList);
                }
            }
            foreach (var list in globalSceneFileList) {
                var tempScene = Scene.GetSceneByFilePath (list);
                if (tempScene != null) {
                    targetSceneList.Add (tempScene);
                }
            }
            foreach (var list in RoomsSceneFileList) {
                var tempScene = Scene.GetSceneByFilePath (list);
                if (tempScene != null) {
                    targetSceneList.Add (tempScene);
                }
            }
            // 所有场景
            return targetSceneList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mScene"></param>
        bool UploadSceneList (List<HDLLinkScene> hdlLinkSceneList)
        {
            var res = false;
            try {
                var revertObj = HttpServerRequest.Current.AddScene (hdlLinkSceneList);
                if (revertObj.Code == StateCode.SUCCESS) {
                    res = true;
                } else {
                    IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
                }
            } catch {
            }
            return res;
        }
        /// <summary>
        /// 原生协议Scene 转换成 HDLLinkScene数据格式
        /// </summary>
        /// <param name="mScene"></param>
        /// <returns></returns>
        HDLLinkScene GetHDLLinkScene (Scene mScene)
        {
            var mHDLLinkScene = new HDLLinkScene ();
            //mHDLLinkScene.sid = mHDLLinkScene.NewSid();
            mHDLLinkScene.sid = mScene.SceneID;
            mHDLLinkScene.name = mScene.Name;
            if (UserConfig.Instance.CheckWhetherGatewayIdNotNull ()) {
                mHDLLinkScene.gatewayId = UserConfig.Instance.HomeGateway.gatewayId;
            }
            var functions = new List<SceneFunction> ();
            if (!mScene.busScene) {
                foreach (var deviceFilePath in mScene.DeviceFilePathList) {
                    var jsonInfo = System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath));
                    var common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (jsonInfo);
                    if (common == null) continue;
                    var function = CommonUtlis.Current.CommonToFunction (common, CommonConfig.Current.FunctionList);
                    if (function == null) continue;
                    if (common.Type == DeviceType.LightDimming) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDimming> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.Brightness
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Brightness,
                            value = commonNew.CurrentBrightness.ToString(),
                        });
                        //2.Delay
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Delay,
                            value = (commonNew.DelayTimeHeight * 256 + commonNew.DelayTimeLow).ToString (),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                    } else if (common.Type == DeviceType.LightEnergySocket) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightEnergySocket> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.on_off
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = commonNew.CurrentBrightness > 0 ? "on" : "off",
                        });
                        //2.Delay
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Delay,
                            value = (commonNew.DelayTimeHeight * 256 + commonNew.DelayTimeLow).ToString (),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                    } else if (common.Type == DeviceType.LightEnergySwitch) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightEnergySwitch> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.on_off
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = commonNew.CurrentBrightness > 0 ? "on" : "off",
                        });
                        //2.Delay
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Delay,
                            value = (commonNew.DelayTimeHeight * 256 + commonNew.DelayTimeLow).ToString(),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                    } else if (common.Type == DeviceType.LightRGB) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.Brightness
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Brightness,
                            value = commonNew.CurrentBrightness.ToString (),
                        });
                        //2.Delay
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Delay,
                            value = (commonNew.DelayTimeHeigh * 256 + commonNew.DelayTimeLow).ToString (),
                        });
                        //3.rgb
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.RGB,
                            value = commonNew.RStatus + "," + commonNew.GStatus + "," + commonNew.BStatus,
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                        //var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                        ////if (device == null) {
                        ////    mSendCount--;
                        ////    continue;
                        ////}
                        //if (device == null) {
                        //    //replyBytes = new byte [] { 0x00 };
                        //} else {
                        //    var mSceneFunction = new SceneFunction ();
                        //    functions.Add (mSceneFunction);
                        //    replyBytes = Control.ControlBytesSendHasReturn (Command.SetLogicLoopColor, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, device.CurrentBrightness, 254, device.DelayTimeHeigh,device.DelayTimeLow,
                        //                            3,device.RStatus,device.GStatus,device.BStatus,0,0});
                        //}
                    } else if (common.Type == DeviceType.LightSwitch) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.Brightness
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = commonNew.CurrentBrightness > 0 ? "on" : "off",
                        });
                        //2.Delay
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Delay,
                            value = (commonNew.DelayTimeHeight * 256 + commonNew.DelayTimeLow).ToString (),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                    } else if (common.Type == DeviceType.CurtainModel) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainModel> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.on_off stop
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = GetCurtainStatusKey(commonNew.Status),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                    } else if (common.Type == DeviceType.CurtainRoller) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainRoller> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.percent
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Percent,
                            value = commonNew.CurtainProress.ToString(),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                    } else if (common.Type == DeviceType.CurtainTrietex) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainTrietex> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.percent
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Percent,
                            value = commonNew.CurtainProress.ToString (),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                    } else if (common.Type == DeviceType.HVAC || common.Type == DeviceType.ACInfrared) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<HVAC> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.on_off
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = commonNew.Power > 0 ? "on" : "off",
                        });
                        //2.mode
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Mode,
                            value = commonNew.SetModeAttribute,
                        });
                        //3.fan
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.FanSpeed,
                            value = commonNew.SetFanSpeedAttribute,
                        });
                        //4.SetTemp
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.SetTemp,
                            value = commonNew.SetTemperature.ToString(),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                        //replyBytes = Control.ControlBytesSendHasReturn (Command.SetACMode, device.SubnetID, device.DeviceID, new byte [] {
                        //                    device.LoopID,
                        //                    device.TemperatureMode,
                        //                    device.IndoorTemperature,
                        //                    device.CoolTemperature,
                        //                    device.HeatTemperature,
                        //                    device.AutoTemperature,
                        //                    device.ChuShiTemperature,
                        //                    device.RealModeAndFanSpeed,
                        //                    device.Power,
                        //                    device.SetMode,
                        //                    device.SetFanSpeed,
                        //                    device.SetTemperature,
                        //                    device.ShaoFanMode});
                    } else if (common.Type == DeviceType.FoolHeat) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<FoolHeat> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.on_off
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = commonNew.Status > 0 ? "on" : "off",
                        });
                        //2.mode
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.Mode,
                            value = commonNew.SetModeAttribute,
                        });
                        //3.SetTemp
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.SetTemp,
                            value = commonNew.WorkingTemperature.ToString (),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                        //var device = Newtonsoft.Json.JsonConvert.DeserializeObject<FoolHeat> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                        ////if (device == null) {
                        ////    mSendCount--;
                        ////    continue;
                        ////}
                        //if (device == null) {
                        //    //replyBytes = new byte [] { 0x00 };
                        //} else {
                        //    var mSceneFunction = new SceneFunction ();
                        //    functions.Add (mSceneFunction);
                            //replyBytes = Control.ControlBytesSendHasReturn (Command.SetFoolHeat, device.SubnetID, device.DeviceID,
                            //                                            new byte [] { device.LoopID, (byte)(device.Status + device.WorkingMode * 16), 0,device.WorkingMode,device.NormalTemperature, device.DayTemperature,
                            //   device.NightTemperature, device.AwayTemperature , 0, 0 });
                        //}
                    } else if (common.Type == DeviceType.FanModule) {
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<FanModule> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.on_off
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = commonNew.WindSpeed > 0 ? "on" : "off",
                        });
                        //2.FanSpeedPercent
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.FanSpeedPercent,
                            value = commonNew.WindSpeed.ToString(),
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                        //replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { device.LoopID, (byte)device.WindSpeed });
                    } else if (common.Type == DeviceType.LogicModule) {
                        //replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, device.SubnetID, device.DeviceID, new byte [] {
                        //device.AreaID,device.AreaSceneID});
                    } else if (common.Type == DeviceType.UniversalDevice) {//2020-09-02 增加通用开关
                        var commonNew = Newtonsoft.Json.JsonConvert.DeserializeObject<UniversalDevice> (jsonInfo);
                        var mSceneFunction = new SceneFunction ();
                        mSceneFunction.sid = function.sid;
                        //1.on_off
                        var status = new List<SceneFunctionStatus> ();
                        status.Add (new SceneFunctionStatus () {
                            key = FunctionAttributeKey.OnOff,
                            value = commonNew.SendBytes [1] > 0 ? "on" : "off",
                        });
                        mSceneFunction.status = status;
                        functions.Add (mSceneFunction);
                        //replyBytes = Control.ControlBytesSendHasReturn (Command.SetCommonSwitch, device.SubnetID, device.DeviceID, new byte [] { device.SendBytes [0], device.SendBytes [1] });
                    }
                }
            } else {
            }
            mHDLLinkScene.functions = functions;
            return mHDLLinkScene;
        }
        #endregion
        #region ■ 上传设备列表___________________________
        /// <summary>
        /// 上传设备列表
        /// </summary>
        /// <returns></returns>
        public bool UploadOidAndSidList ()
@@ -70,87 +538,7 @@
            List<Common> TargetList = new List<Common> ();
            //找出需要显示的设备
            var filesList = FileUtils.ReadFiles ().FindAll ((obj) => {
                string [] str = obj.Split ('_');
                return obj.StartsWith ("Equipment_") && str.Length == 5;
            });
            var localEquipments = filesList.FindAll ((obj) => {
                string typeString = obj.Split ('_') [1];
                return (
                #region light
                    typeString.ToString () == DeviceType.LightCCT.ToString () ||
                typeString.ToString () == DeviceType.LightRGB.ToString () ||
                typeString.ToString () == DeviceType.LightDALI.ToString () ||
                typeString.ToString () == DeviceType.LightRGBW.ToString () ||
                typeString.ToString () == DeviceType.LightLogic.ToString () ||
                typeString.ToString () == DeviceType.LightSwitch.ToString () ||
                typeString.ToString () == DeviceType.LightDimming.ToString () ||
                typeString.ToString () == DeviceType.LightMixSwitch.ToString () ||
                typeString.ToString () == DeviceType.LightRGBandCCT.ToString () ||
                typeString.ToString () == DeviceType.LightMixDimming.ToString () ||
                typeString.ToString () == DeviceType.LightEnergySwitch.ToString () ||
                typeString.ToString () == DeviceType.LightEnergySocket.ToString () ||
                typeString.ToString () == DeviceType.LightSwitchSocket.ToString ()
                || typeString.ToString () == DeviceType.DMX48.ToString ()
                #endregion
                #region light
                    || typeString.ToString () == DeviceType.SensorCH4.ToString () ||
                typeString.ToString () == DeviceType.SensorCO2.ToString () ||
                typeString.ToString () == DeviceType.SensorLPG.ToString () ||
                typeString.ToString () == DeviceType.SensorCOH2.ToString () ||
                typeString.ToString () == DeviceType.SensorPM25.ToString () ||
                typeString.ToString () == DeviceType.SensorTVOC.ToString () ||
                typeString.ToString () == DeviceType.SensorPower.ToString () ||
                typeString.ToString () == DeviceType.SensorSmoke.ToString () ||
                typeString.ToString () == DeviceType.SensorWater.ToString () ||
                typeString.ToString () == DeviceType.SensorWeight.ToString () ||
                typeString.ToString () == DeviceType.SensorCurrent.ToString () ||
                typeString.ToString () == DeviceType.SensorVoltage.ToString ()
                || typeString.ToString () == DeviceType.SensorRainfall.ToString ()
                || typeString.ToString () == DeviceType.SensorVelocity.ToString ()
                || typeString.ToString () == DeviceType.SensorMenciAndwindowMagnetic.ToString ()
                || typeString.ToString () == DeviceType.SensorMobileDetection.ToString ()
                || typeString.ToString () == DeviceType.SensorLiquidPressure.ToString ()
                || typeString.ToString () == DeviceType.SensorVibration.ToString ()
                || typeString.ToString () == DeviceType.SensorLiquidFlow.ToString ()
                || typeString.ToString () == DeviceType.SensorLiquidDepth.ToString ()
                || typeString.ToString () == DeviceType.SensorTemperature.ToString ()
                || typeString.ToString () == DeviceType.SensorHeightLength.ToString ()
                || typeString.ToString () == DeviceType.SensorIllumination.ToString ()
                || typeString.ToString () == DeviceType.SensorWindPressure.ToString ()
                || typeString.ToString () == DeviceType.SensorHumidity.ToString ()
                #endregion
                #region curtain
                    || typeString.ToString () == DeviceType.CurtainModel.ToString ()
                || typeString.ToString () == DeviceType.CurtainRoller.ToString ()
                || typeString.ToString () == DeviceType.CurtainTrietex.ToString ()
                #endregion
                #region ac
                    || typeString.ToString () == DeviceType.ACPanel.ToString ()
                || typeString.ToString () == DeviceType.ACDevice.ToString ()
                || typeString.ToString () == DeviceType.ACInfrared.ToString ()
                || typeString.ToString () == DeviceType.ACCoolmaster.ToString ()
                || typeString.ToString () == DeviceType.CustomAC.ToString ()
                || typeString.ToString () == DeviceType.HVAC.ToString ()
                #endregion
                #region foolheat
                    || typeString.ToString () == DeviceType.FoolHeat.ToString ()
                || typeString.ToString () == DeviceType.FoolHeatPanel.ToString ()
                #endregion
                #region
                    || typeString.ToString () == DeviceType.InfraredMode.ToString ()
                || typeString.ToString () == DeviceType.DoorLock.ToString ()
                || typeString.ToString () == DeviceType.FanModule.ToString ()
                || typeString.ToString () == DeviceType.FreshAir.ToString ()
                || typeString.ToString () == DeviceType.InfraredTV.ToString ()
                || typeString.ToString () == DeviceType.UniversalDevice.ToString ()
                || typeString.ToString () == DeviceType.MusicModel.ToString ()
                || typeString.ToString () == DeviceType.SecurityModule.ToString ()
                || typeString.ToString () == DeviceType.LogicModule.ToString ()
                || typeString.ToString () == DeviceType.SecurityPanel.ToString ()
                #endregion
                    );
            });
            var localEquipments = CommonUtlis.Current.GetAllLocalEquipments ();
            foreach (string deviceFilePath in localEquipments) {
                try {
@@ -581,13 +969,66 @@
                tmp.name = loopCommon.Name;
                tmp.attributes = new List<Attribute> ();
                tmp.omodel = loopCommon.Type.ToString ();
                //继电器开关类
                if (loopCommon.Type == DeviceType.LightSwitch
                if (loopCommon.Type == DeviceType.UniversalDevice) {
                    //通用开关
                    tmp.spk = SPK.UniversalDevice;
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
                    tempAttribute.data_type = "string";
                    tempAttribute.value = new List<string> ();
                    tempAttribute.value.Add ("on");
                    tempAttribute.value.Add ("off");
                    tempAttribute.max = 1;
                    tempAttribute.min = 0;
                    tmp.attributes.Add (tempAttribute);
                    #endregion
                }else if (loopCommon.Type == DeviceType.FanModule ) {
                    //风扇
                    tmp.spk = SPK.ElectricFan;
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
                    tempAttribute.data_type = "string";
                    tempAttribute.value = new List<string> ();
                    tempAttribute.value.Add ("on");
                    tempAttribute.value.Add ("off");
                    tempAttribute.max = 1;
                    tempAttribute.min = 0;
                    tmp.attributes.Add (tempAttribute);
                    #endregion
                    #region volume_level
                    Attribute tempSpeedAttribute = new Attribute ();
                    tempSpeedAttribute.key = FunctionAttributeKey.FanSpeedPercent;
                    tempSpeedAttribute.data_type = "integer";
                    tempSpeedAttribute.value = new List<string> ();
                    tempSpeedAttribute.max = 8;
                    tempSpeedAttribute.min = 0;
                    tmp.attributes.Add (tempSpeedAttribute);
                    #endregion
                } else if (loopCommon.Type == DeviceType.LightSwitch
                    || loopCommon.Type == DeviceType.LightEnergySwitch
                    || loopCommon.Type == DeviceType.LightEnergySocket
                    || loopCommon.Type == DeviceType.LightSwitchSocket
                    || loopCommon.Type == DeviceType.LightMixSwitch) {
                    tmp.spk = "light.switch";
                    //继电器开关类
                    tmp.spk = SPK.LightSwitch;
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
                    tempAttribute.data_type = "string";
                    tempAttribute.value = new List<string> ();
                    tempAttribute.value.Add ("on");
                    tempAttribute.value.Add ("off");
                    tempAttribute.max = 1;
                    tempAttribute.min = 0;
                    tmp.attributes.Add (tempAttribute);
                    #endregion
                }else if (loopCommon.Type == DeviceType.LightEnergySocket
                    || loopCommon.Type == DeviceType.LightSwitchSocket) {
                    //电器 插座
                    tmp.spk = SPK.ElectricSocket;
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
@@ -603,7 +1044,8 @@
                    || loopCommon.Type == DeviceType.LightCCT
                    || loopCommon.Type == DeviceType.LightDALI) {
                    //调光类
                    tmp.spk = "light.dimming";
                    //tmp.spk = "light.dimming";
                    tmp.spk = SPK.LightDimming;
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
@@ -644,7 +1086,8 @@
                    if (loopCommon.Type == DeviceType.LightCCT || loopCommon.Type == DeviceType.LightDALI)//色温类别
                    {
                        tmp.spk = "light.cct";
                        //tmp.spk = "light.cct";
                        tmp.spk = SPK.LightCCT;
                        #region cct
                        Attribute tempAttribute2 = new Attribute ();
                        tempAttribute2.key = "cct";
@@ -663,7 +1106,8 @@
                    || loopCommon.Type == DeviceType.LightRGBandCCT
                    || loopCommon.Type == DeviceType.DMX48) {
                    tmp.spk = "light.rgb";
                    tmp.spk = SPK.LightRGB;
                    //tmp.spk = "light.rgb";
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
@@ -689,7 +1133,8 @@
                    if (loopCommon.Type == DeviceType.LightRGBW) {
                        tmp.spk = "light.rgbw";
                        //tmp.spk = "light.rgbw";
                        tmp.spk = SPK.LightRGBW;
                        #region color
                        Attribute tempAttribute3 = new Attribute ();
                        tempAttribute3.key = "rgb";
@@ -730,7 +1175,8 @@
                    }
                } else if (loopCommon.Type == DeviceType.CurtainModel || loopCommon.Type == DeviceType.CurtainRoller || loopCommon.Type == DeviceType.CurtainTrietex) {
                    tmp.spk = "curtain.switch";
                    //tmp.spk = "curtain.switch";
                    tmp.spk = SPK.CurtainSwitch;
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
@@ -745,7 +1191,8 @@
                    #endregion
                    if (loopCommon.Type == DeviceType.CurtainRoller || loopCommon.Type == DeviceType.CurtainTrietex)//所有卷帘控制器 
                    {
                        tmp.spk = "curtain.trietex";
                        tmp.spk = SPK.CurtainTrietex;
                        //tmp.spk = "curtain.trietex";
                        #region openlevel
                        Attribute tempAttribute1 = new Attribute ();
                        tempAttribute1.key = "percent";
@@ -765,7 +1212,8 @@
                   || loopCommon.Type == DeviceType.HVAC) {
                    //空调类
                    tmp.spk = "ac.standard";
                    tmp.spk = SPK.AcStandard;
                    //tmp.spk = "ac.standard";
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
@@ -870,7 +1318,8 @@
                    #endregion
                } else if (loopCommon.Type == DeviceType.FoolHeat
                    || loopCommon.Type == DeviceType.FoolHeatPanel) {
                    tmp.spk = "floorHeat.standard";
                    tmp.spk = SPK.FloorHeatStandard;
                    //tmp.spk = "floorHeat.standard";
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
                    tempAttribute.key = "on_off";
@@ -940,7 +1389,8 @@
                } else if (loopCommon.Type == DeviceType.MusicA31
                    || loopCommon.Type == DeviceType.MusicModel
                    || loopCommon.Type == DeviceType.MusicPanel) {
                    tmp.spk = "music.standard";
                    tmp.spk = SPK.MusicStandard;
                    //tmp.spk = "music.standard";
                    // on_off  volume song_step  audio  list_channel  mode  song_num  special_song  volume_level
                    #region on_off
                    Attribute tempAttribute = new Attribute ();
@@ -1125,7 +1575,8 @@
                } else if (loopCommon.BigClass == 5) {
                    //传感器
                    if (loopCommon.Type == DeviceType.SensorMobileDetection) {
                        tmp.spk = "sensor.pir";
                        tmp.spk = SPK.SensorPir;
                        //tmp.spk = "sensor.pir";
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1159,7 +1610,8 @@
                        tmp.attributes.Add (tempAttribute2);
                        #endregion
                    }else if (loopCommon.Type == DeviceType.SensorTemperature) {
                        tmp.spk = "sensor.temperature";
                        tmp.spk = SPK.SensorTemperature;
                        //tmp.spk = "sensor.temperature";
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1206,7 +1658,8 @@
                        //tmp.attributes.Add(tempAttribute3);
                        //#endregion
                    }else if (loopCommon.Type == DeviceType.SensorIllumination) {
                        tmp.spk = "sensor.light";
                        //tmp.spk = "sensor.light";
                        tmp.spk = SPK.SensorLight;
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1246,7 +1699,8 @@
                        tmp.attributes.Add (tempAttribute3);
                        #endregion
                    } else if (loopCommon.Type == DeviceType.SensorPM25) {
                        tmp.spk = "sensor.pm25";
                        //tmp.spk = "sensor.pm25";
                        tmp.spk = SPK.SensorPm25;
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1286,7 +1740,8 @@
                        tmp.attributes.Add (tempAttribute3);
                        #endregion
                    }else if (loopCommon.Type == DeviceType.SensorHumidity) {
                        tmp.spk = "sensor.humidity";
                        //tmp.spk = "sensor.humidity";
                        tmp.spk = SPK.SensorHumidity;
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1307,7 +1762,8 @@
                        tmp.attributes.Add (tempAttribute1);
                        #endregion
                    }else if (loopCommon.Type == DeviceType.SensorTVOC) {
                        tmp.spk = "sensor.tvoc";
                        //tmp.spk = "sensor.tvoc";
                        tmp.spk = SPK.SensorTVOC;
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1328,7 +1784,8 @@
                        tmp.attributes.Add (tempAttribute1);
                        #endregion
                    }else if (loopCommon.Type == DeviceType.SensorCO2) {
                        tmp.spk = "sensor.co2";
                        //tmp.spk = "sensor.co2";
                        tmp.spk = SPK.SensorCO2;
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1349,7 +1806,8 @@
                        tmp.attributes.Add (tempAttribute1);
                        #endregion
                    }else if (loopCommon.Type == DeviceType.Sensor) {
                        tmp.spk = "dryContact.standard";
                        //tmp.spk = "dryContact.standard";
                        tmp.spk = SPK.DryContact;
                        #region on_off
                        Attribute tempAttribute = new Attribute ();
                        tempAttribute.key = "enable";
@@ -1414,6 +1872,9 @@
            } catch { }
            return tmp;
        }
        #endregion
        #region 获取属性
@@ -1487,42 +1948,11 @@
        //#endregion
        #region 生成4位byte 时间戳
        private long LastTime = 0;
        /// <summary>
        /// DateTime时间格式转换为13位带毫秒的Unix时间戳
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public long ConvertDateTimeLong ()
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (new System.DateTime (2020, 1, 1));
            long l = (long)(Math.Round ((DateTime.Now - startTime).TotalSeconds, 1) * 10);
            if (l <= LastTime) l = LastTime + 1;
            LastTime = l;
            return l;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="m"></param>
        /// <param name="strTmp"></param>
        /// <returns></returns>
        public bool ConvertIntToByteArray (long m, ref string strTmp)
        {
            strTmp = "00000000";
            byte [] arry = new byte [4];
            arry [0] = (byte)(m & 0xFF);
            arry [1] = (byte)((m & 0xFF00) >> 8);
            arry [2] = (byte)((m & 0xFF0000) >> 16);
            arry [3] = (byte)((m & 0xFF000000) >> 24);
            strTmp = arry [0].ToString ("X2") + arry [1].ToString ("X2") + arry [2].ToString ("X2") + arry [3].ToString ("X2");
            return true;
        }
        #endregion
    }
    #region ■ 设备列表相关___________________________
    [Serializable]
    public class BaseCloudFeedback
    {
@@ -1877,6 +2307,11 @@
        /// value
        /// </summary>
        public const string Value = "value";
        /// <summary>
        /// 风扇档位
        /// </summary>
        public const string FanSpeedPercent = "fan_speed_percent";
    }
    /// <summary>
@@ -2008,6 +2443,11 @@
        /// 家电、风扇
        /// </summary>
        public const string ElectricFan = "electrical.fan";
        /// <summary>
        /// 其它、通用开关
        /// </summary>
        public const string UniversalDevice = "other.common";
    }
@@ -2079,98 +2519,104 @@
    //    public Sids devices { get; set; } //设备列表
    //}
}
//sid组成部分:
//sid
//来源   厂商代码   通讯方式   产品时间戳   产品类别   物模型类   通道号   大小类别
//1byte   1byte   1byte   4byte   1byte   2byte   2byte   2byte
//oid组成部分:
//来源   厂商代码   通讯方式   产品时间戳   产品类别
//1byte   1 byte   1 byte   4 byte   1byte
//其中各部分代码列表:
//来源   1byte   编号   描述
//      00   默认原生态系统数据
//      01   网关或者其他A设备
//      02   调试软件
//      03   APP应用程序
//      04   第三方网关或者平台
//厂商代码   1byte   编号   描述
//      01   HDL
//      02
    //sid组成部分:
    //sid
    //来源   厂商代码   通讯方式   产品时间戳   产品类别   物模型类   通道号   大小类别
    //1byte   1byte   1byte   4byte   1byte   2byte   2byte   2byte
    //oid组成部分:
    //来源   厂商代码   通讯方式   产品时间戳   产品类别
    //1byte   1 byte   1 byte   4 byte   1byte
    //其中各部分代码列表:
    //来源   1byte   编号   描述
    //      00   默认原生态系统数据
    //      01   网关或者其他A设备
    //      02   调试软件
    //      03   APP应用程序
    //      04   第三方网关或者平台
//通讯方式   1byte   编号   描述
//      01   HDL Bus
//      02   Zigbee
//      03   KNX
//      04   Z-Wave
    //厂商代码   1byte   编号   描述
    //      01   HDL
    //      02
//产品时间戳   4bytes   以2020年1月1日算出的时间戳0.1s为单位
    //通讯方式   1byte   编号   描述
    //      01   HDL Bus
    //      02   Zigbee
    //      03   KNX
    //      04   Z-Wave
//产品类别   1byte   编号   描述
//      01   调光器
//      02   继电器
//      03   干接点模块
//      04   传感器
//      05   面板
//      06   RCU
//      07   网关
//      08   红外发射
//      09   Android屏
//      10   场景
//      11   音乐播放器
//      12   232/485转换器
//      21   自动化
//      22   安防防区
//      14   窗帘模块
//      15   HVAC
//      16   地热模块
    //产品时间戳   4bytes   以2020年1月1日算出的时间戳0.1s为单位
//物模型类型   2bytes   编号   描述
//      01   开关类   01   开关
//            02   插座
//            03
//      02   照明   01   开关
//            02   调光
//            03   色温(CCT)
//            04   RGB
//      03   遮阳   01   窗帘控制器
//            02   百叶窗
//            03   开合帘
//            04   卷帘
//            05    推窗器
//            06    投影幕
//      04   面板   01   按键面板
//      05   传感器   01   移动探测
//            02   温度传感器
//            03   湿度传感器
//            04   照度传感器
//            05   TVOC
//            06   PM2.5
//            07   CO2
//            08   毫米波传感器
//            09
//            10
//            11   烟雾传感器
//            25   干接点
//      07   恒温器   01   空调
//            02   风扇
//            03   毛细空调
//      08   地热   01   地热模块
//      09   背景音乐   01   音乐播放器
//            02   Sonos
//      10   场景   01   场景
//            02   电影场景
//      19   新风   01   新风
    //产品类别   1byte   编号   描述
    //      01   调光器
    //      02   继电器
    //      03   干接点模块
    //      04   传感器
    //      05   面板
    //      06   RCU
    //      07   网关
    //      08   红外发射
    //      09   Android屏
    //      10   场景
    //      11   音乐播放器
    //      12   232/485转换器
    //      21   自动化
    //      22   安防防区
    //      14   窗帘模块
    //      15   HVAC
    //      16   地热模块
//13   能源   01   电表
//            02   水表
//            03   燃气
//      16   电器   01   风扇
//            02   TV
//      20   安防   01   安防
//      21   自动化   01   自动化
//大类别   1bytes   (预留)
//小类别   1byte   (预留)
    //物模型类型   2bytes   编号   描述
    //      01   开关类   01   开关
    //            02   插座
    //            03
    //      02   照明   01   开关
    //            02   调光
    //            03   色温(CCT)
    //            04   RGB
    //      03   遮阳   01   窗帘控制器
    //            02   百叶窗
    //            03   开合帘
    //            04   卷帘
    //            05    推窗器
    //            06    投影幕
    //      04   面板   01   按键面板
    //      05   传感器   01   移动探测
    //            02   温度传感器
    //            03   湿度传感器
    //            04   照度传感器
    //            05   TVOC
    //            06   PM2.5
    //            07   CO2
    //            08   毫米波传感器
    //            09
    //            10
    //            11   烟雾传感器
    //            25   干接点
    //      07   恒温器   01   空调
    //            02   风扇
    //            03   毛细空调
    //      08   地热   01   地热模块
    //      09   背景音乐   01   音乐播放器
    //            02   Sonos
    //      10   场景   01   场景
    //            02   电影场景
    //      19   新风   01   新风
    //13   能源   01   电表
    //            02   水表
    //            03   燃气
    //      16   电器   01   风扇
    //            02   TV
    //      20   安防   01   安防
    //      21   自动化   01   自动化
    //大类别   1bytes   (预留)
    //小类别   1byte   (预留)
    #endregion
}