陈嘉乐
2020-12-08 a75f121124e90619b2853ad079ae0a733366b022
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,24 +75,9 @@
                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;