wxr
2020-12-08 145b157ba183ebf877c93ed2dbb01ec737c993d3
HDL_ON/UI/UI2/3-Intelligence/Automation/LogicMethod.cs
@@ -22,6 +22,47 @@
        {
            MainPage.BasePageView.RemoveViewByTag("Logic");
        }
        /// <summary> Converts an array of bytes into a formatted string of hex digits (ex: E4 CA B2)</summary>
        /// <param name="data"> The array of bytes to be translated into a string of hex digits. </param>
        /// <returns> Returns a well formatted string of hex digits with spacing. </returns>
        static string byteArrayToHexString(byte[] data)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (byte b in data)
            {
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            }
            return sb.ToString().ToUpper();
        }
        /**
  * int转byte[]
  * 该方法将一个int类型的数据转换为byte[]形式,因为int为32bit,而byte为8bit所以在进行类型转换时,知会获取低8位,
  * 丢弃高24位。通过位移的方式,将32bit的数据转换成4个8bit的数据。注意 &0xff,在这当中,&0xff简单理解为一把剪刀,
  * 将想要获取的8位数据截取出来。
  * @param i 一个int数字
  * @return byte[]
  */
        public static byte[] int2ByteArray(int i)
        {
            byte[] result = new byte[4];
            result[0] = (byte)((i >> 24) & 0xFF);
            result[1] = (byte)((i >> 16) & 0xFF);
            result[2] = (byte)((i >> 8) & 0xFF);
            result[3] = (byte)(i & 0xFF);
            return result;
        }
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        static int getTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return (int)ts.TotalSeconds;
        }
        /// <summary>
        /// 生成逻辑sid方法
        /// </summary>
@@ -34,38 +75,28 @@
                DateTime dt = DateTime.Now;
                DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(2020, 1, 1));
                long m = (long)((dt - startTime).TotalMilliseconds / 10);
                string sTimeSpan = "00000000";
                string sTimeSpan = byteArrayToHexString(int2ByteArray(getTimeStamp()));
                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 >> 24) & 0xFF);
                sTimeSpan = arry[0].ToString("X2") + arry[1].ToString("X2") + arry[2].ToString("X2") + arry[3].ToString("X2");
                if (sTimeSpan.Length > 8)
                {
                    sTimeSpan = sTimeSpan.Substring(0, 8);
                }
                else
                {
                    sTimeSpan = "00000000";
                }
                logicId = sOidBeginsWith + sTimeSpan;
                logicId += "0A";
                logicId += "0A01";
                //0A01 物模型为逻辑, 0001 表示 1 号逻辑功能
                int iTopLogicId = 1;
                logicId += "15";
                logicId += "1501";
                //1501 物模型为逻辑, 0001 表示 1 号逻辑功能
                int maxId = 1;
                Random random = new Random();
                iTopLogicId = random.Next(0, 255);
                iTopLogicId += random.Next(0, 255);
                logicId += iTopLogicId.ToString("X4");//逻辑号 两个byte
                logicId += "1100";
                for (int i = 0; i < Logic.LogicList.Count; i++)
                {
                    string s = Logic.LogicList[i].sid.Substring(20, 4);
                    int iThisSceneId = Convert.ToInt16(s, 16);
                    if (iThisSceneId > maxId)
                        maxId = iThisSceneId ;
                }
                logicId += (maxId+1).ToString("X4");//逻辑号 两个byte
                logicId += "0000";
            }
            catch
            {
@@ -73,53 +104,6 @@
            }
            return logicId;
        }
        /// <summary>
        /// 生成逻辑sid方法
        /// </summary>
        //public static string NewSid()
        //{
        //    String sLogicid = "";
        //    try
        //    {
        //        String sOidBeginsWith = "000101";//厂商 + 通讯方式
        //        //生成4个byte时间戳
        //        DateTime dt = DateTime.Now;
        //        long sTimeSp = GateWay.ConvertDateTimeLong(dt);
        //        string sTimeSpan = "";
        //        GateWay.ConvertIntToByteArray(sTimeSp, ref sTimeSpan);
        //        if (sTimeSpan.Length != 8) return sLogicid;
        //        //间隔10毫秒 错开时间戳
        //        HDLUDP.TimeBetwnNext(10);
        //        sLogicid = sOidBeginsWith + sTimeSpan;
        //        //1501 物模型为自动化, 0001 表示 1 号自动化功能
        //        sLogicid += "15";
        //        sLogicid += "1501";
        //        //自动化号自增
        //        int iTopSceneId = 1;
        //        if (myGateway.LogicResponse != null)
        //        {
        //            if (myGateway.LogicResponse.objects != null)
        //            {
        //                for (int i = 0; i < myGateway.LogicResponse.objects.Count; i++)
        //                {
        //                    string ccc = myGateway.LogicResponse.objects[i].sid.Substring(myGateway.LogicResponse.objects[i].sid.Length - 8, 4);
        //                    int iThisSceneId = Convert.ToInt16(myGateway.LogicResponse.objects[i].sid.Substring(myGateway.LogicResponse.objects[i].sid.Length - 8, 4), 16);
        //                    if (iThisSceneId >= iTopSceneId)
        //                        iTopSceneId = iThisSceneId + 1;
        //                }
        //            }
        //        }
        //        sLogicid += iTopSceneId.ToString("X4");//场景号 两个byte
        //                                               //大类小类没有用到 固定为0
        //        sLogicid += "0000";
        //        return sLogicid;
        //    }
        //    catch
        //    {
        //        return sLogicid;
        //    }
        //}
        /// <summary>
        /// 封装Dictionary对象
@@ -171,81 +155,7 @@
        /// <returns></returns>
        public static List<HDL_ON.Entity.Function> GetGatewayDeviceList()
        {
            List<HDL_ON.Entity.Function> list = new List<Entity.Function>();
            for (int i=0;i<10;i++) {
                HDL_ON.Entity.Function function = new Entity.Function();
                switch (i) {
                    case 0: {
                            function.sid = "1234560001212121020182";
                            function.name ="灯光";
                            list.Add(function);
                        }
                        break;
                    case 1: {
                            function.sid = "1234560001212121030182";
                            function.name = "窗帘";
                            list.Add(function);
                        } break;
                    case 2: {
                            function.sid = "1234560001212121040182";
                            function.name = "空调";
                            list.Add(function);
                        } break;
                    case 3:
                        {
                            function.sid = "1234560001212121010282";
                            function.name = "RGBW";
                            list.Add(function);
                        }
                        break;
                    case 4:
                        {
                            function.sid = "1234560001212121010382";
                            function.name = "CCT灯";
                            list.Add(function);
                        }
                        break;
                    case 5:
                        {
                            function.sid = "1234560001212121010482";
                            function.name = "开合帘";
                            list.Add(function);
                        }
                        break;
                    case 6:
                        {
                            function.sid = "1234560001212121010582";
                            function.name = "卷帘";
                            list.Add(function);
                        }
                        break;
                    case 7:
                        {
                            function.sid = "1234560001212121040282";
                            function.name = "地热";
                            list.Add(function);
                        }
                        break;
                    case 8:
                        {
                            function.sid = "1234560001212121020282";
                            function.name = "调关";
                            list.Add(function);
                        }
                        break;
                    case 9:
                        {
                            function.sid = "1234560001212121020482";
                            function.name = "RGB";
                            list.Add(function);
                        }
                        break;
                }
            }
            return list;
            //return HDL_ON.Entity.DB_ResidenceData.functionList.GetAllFunction();
            return HDL_ON.Entity.DB_ResidenceData.functionList.GetAllFunction();
        }
        /// <summary>
        /// 获取网关场景列表
@@ -352,15 +262,15 @@
                case FunctionType.Relay:
                case FunctionType.RGB:
                case FunctionType.RGBW:
                case FunctionType.CCT:
                case FunctionType.ColorTemperature:
                case FunctionType.Dimmer:
                    {
                        strPath = "LogicIcon/lightloguc.png";
                    }
                    break;
                case FunctionType.Curtain:
                case FunctionType.RollerCurtain:
                case FunctionType.TrietexCurtain:
                case FunctionType.RollingShutter:
                case FunctionType.MotorCurtain:
                    {
                        strPath = "LogicIcon/curtainlogic.png";
                    }
@@ -388,13 +298,13 @@
        {
            List<string> deviceStrTypeList = new List<string>(); 
            deviceStrTypeList.Clear();
            var lightjosn = deviceList.Find((device) => device.functionType == FunctionType.Relay || device.functionType == FunctionType.Dimmer|| device.functionType == FunctionType.CCT || device.functionType == FunctionType.RGB|| device.functionType == FunctionType.RGBW);
            var lightjosn = deviceList.Find((device) => device.functionType == FunctionType.Relay || device.functionType == FunctionType.Dimmer|| device.functionType == FunctionType.ColorTemperature || device.functionType == FunctionType.RGB|| device.functionType == FunctionType.RGBW);
            if (lightjosn != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.Lights));
            }
            var curtainjosn = deviceList.Find((device) => device.functionType ==FunctionType.Curtain|| device.functionType == FunctionType.TrietexCurtain|| device.functionType == FunctionType.RollerCurtain);
            var curtainjosn = deviceList.Find((device) => device.functionType ==FunctionType.Curtain|| device.functionType == FunctionType.MotorCurtain|| device.functionType == FunctionType.RollingShutter);
            if (curtainjosn != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.Curtain));
@@ -406,7 +316,7 @@
                deviceStrTypeList.Add(Language.StringByID(StringId.AC));
            }
            var floorHeating = deviceList.Find((device) => device.functionType == FunctionType.FloorHeating);
            if (ac != null)
            if (floorHeating != null)
            {
                deviceStrTypeList.Add(Language.StringByID(StringId.FloorHeating));
            }
@@ -427,13 +337,13 @@
                functionTypeList.Add(FunctionType.Dimmer);
                functionTypeList.Add(FunctionType.RGB);
                functionTypeList.Add(FunctionType.RGBW);
                functionTypeList.Add(FunctionType.CCT);
                functionTypeList.Add(FunctionType.ColorTemperature);
            }
            else if (deviceType == Language.StringByID(StringId.Curtain))
            {
                functionTypeList.Add(FunctionType.Curtain);
                functionTypeList.Add(FunctionType.RollerCurtain);
                functionTypeList.Add(FunctionType.TrietexCurtain);
                functionTypeList.Add(FunctionType.RollingShutter);
                functionTypeList.Add(FunctionType.MotorCurtain);
            }
            else if (deviceType == Language.StringByID(StringId.AC))
            {
@@ -459,10 +369,10 @@
                        deviceTypeList.Add(FunctionType.RGB);
                        deviceTypeList.Add(FunctionType.RGBW);
                        deviceTypeList.Add(FunctionType.Dimmer);
                        deviceTypeList.Add(FunctionType.CCT);
                        deviceTypeList.Add(FunctionType.ColorTemperature);
                        deviceTypeList.Add(FunctionType.Curtain);
                        deviceTypeList.Add(FunctionType.RollerCurtain);
                        deviceTypeList.Add(FunctionType.TrietexCurtain);
                        deviceTypeList.Add(FunctionType.RollingShutter);
                        deviceTypeList.Add(FunctionType.MotorCurtain);
                        deviceTypeList.Add(FunctionType.AC);
                        deviceTypeList.Add(FunctionType.FloorHeating);
                    }
@@ -473,10 +383,10 @@
                        deviceTypeList.Add(FunctionType.RGB);
                        deviceTypeList.Add(FunctionType.RGBW);
                        deviceTypeList.Add(FunctionType.Dimmer);
                        deviceTypeList.Add(FunctionType.CCT);
                        deviceTypeList.Add(FunctionType.ColorTemperature);
                        deviceTypeList.Add(FunctionType.Curtain);
                        deviceTypeList.Add(FunctionType.RollerCurtain);
                        deviceTypeList.Add(FunctionType.TrietexCurtain);
                        deviceTypeList.Add(FunctionType.RollingShutter);
                        deviceTypeList.Add(FunctionType.MotorCurtain);
                        deviceTypeList.Add(FunctionType.AC);
                        deviceTypeList.Add(FunctionType.FloorHeating);
                    }