From fe3b2466c68b5db70d38e78039703add3b8b1dfe Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期四, 31 十二月 2020 19:29:43 +0800
Subject: [PATCH] 合并了代码

---
 ZigbeeApp/Shared/Phone/ZigBee/Device/TemperatureSensor.cs |  545 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 540 insertions(+), 5 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Device/TemperatureSensor.cs b/ZigbeeApp/Shared/Phone/ZigBee/Device/TemperatureSensor.cs
index 1c9dc44..a7c3225 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Device/TemperatureSensor.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Device/TemperatureSensor.cs
@@ -1,12 +1,19 @@
 锘縰sing System;
 using System.Collections.Generic;
+using System.IO;
+using Newtonsoft.Json.Linq;
 
 namespace ZigBee.Device
 {
-    public class TemperatureSensor: CommonDevice
+    public class TemperatureSensor : CommonDevice
     {
+        public TemperatureSensor()
+        {
+            this.Type = DeviceType.TemperatureSensor;
+        }
+
         /// <summary>
-        /// 鍥犱负杩欎袱涓笢瑗垮叡鐢ㄥ悓涓�涓澶嘔D锛屾墍浠� 1锛氭俯搴︿紶鎰熷櫒  2:婀垮害浼犳劅鍣�
+        /// 鍥犱负杩欎袱涓笢瑗垮叡鐢ㄥ悓涓�涓澶嘔D锛屾墍浠� 1锛氭俯搴︿紶鎰熷櫒  2:婀垮害浼犳劅鍣�   
         /// </summary>
         public int SensorDiv = 1;
 
@@ -18,12 +25,72 @@
         /// 婀垮害
         /// </summary>
         public decimal currentHumidity = 0;
+        /// <summary>
+        /// 褰撳墠PM2.5鍊�
+        /// </summary>
+        public decimal currentPmData = 0;
+        /// <summary>
+        /// 褰撳墠CO2
+        /// </summary>
+        public decimal currentCO2 = 0;
+        /// <summary>
+        /// 寮�鍏夌姸鎬�:0=鍏�,1=寮�,
+        /// </summary>
+        [Newtonsoft.Json.JsonIgnore]
+        public int OnOffStatus = 0;
+        /// <summary>
+        /// 绌烘皵璐ㄩ噺浼犳劅鍣ㄦ椂闂村け鏁堝垪琛�
+        /// key:搴忓彿锛寁alue锛歀edTimeData
+        /// </summary>
+        [Newtonsoft.Json.JsonIgnore]
+        public Dictionary<int, LedTimeData> AirQualitySensorLedTimeList = new Dictionary<int, LedTimeData> { };
 
-        public TemperatureSensor()
+        #region 寮�鍏�
+        ///<summary >
+        ///寮�鍏虫帶鍒�(浠呯敤浜巆luster=6鐨勮澶囷級
+        /// <para>璁惧鏀寔cluster=6鐨勮澶囨墠鑳借皟鐢ㄨ鎺ュ彛</para>
+        /// <para>command鐨勫��</para>
+        ///<para>0 : 鍏抽棴</para>
+        ///<para>1: 鎵撳紑</para>
+        ///<para>2锛氬彇鍙�</para>
+        /// </summary>
+        public void SwitchControl(int command)
         {
-            this.Type = DeviceType.TemperatureSensor ;
+            try
+            {
+                var jobject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 6 }, { "Command", command }, { "SendMode", 2 } };
+                Gateway?.Send(("DeviceControl"), jobject.ToString());
+                System.Console.WriteLine("SwitchControl_鍙戦�佹暟鎹�" + "_" + jobject.ToString() + "_" + System.DateTime.Now.ToString());
+            }
+            catch { }
         }
- 
+
+        /// <summary>
+        /// 鍙戦�佽幏鍙栫伅鍏夌姸鎬佸懡浠�
+        /// </summary>
+        /// <param name="device">绐楀笜</param>
+        public void SendSwitchStatuComand()
+        {
+            var jObject = new Newtonsoft.Json.Linq.JObject
+            {
+                { "DeviceAddr",DeviceAddr },
+                { "Epoint",DeviceEpoint },
+                { "Cluster_ID", (int)Cluster_ID.Switch },
+                { "Command", 108 }
+            };
+            var attriBute = new Newtonsoft.Json.Linq.JArray
+            {
+               new Newtonsoft.Json.Linq.JObject
+               {
+                 { "AttriButeId", (int)AttriButeId.Switch}
+               }
+            };
+            var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
+            jObject.Add("Data", data);
+            Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
+        }
+        #endregion
+
         /// <summary>
         /// 璇诲彇娓�/婀垮害
         /// </summary>
@@ -38,5 +105,473 @@
                 ReadAttri(Device.Cluster_ID.RelativeHumidityMeasurement, AttriButeId.MeasuredValue);
             }
         }
+
+        /// <summary>
+        /// 绌烘皵璐ㄩ噺绛夌骇(1:浼橈紱2:鑹紱3锛氭薄鏌擄級
+        /// </summary>
+        /// <param name="tempera"></param>
+        /// <returns></returns>
+        public int AirQuality(TemperatureSensor tempera)
+        {
+            //绌烘皵璐ㄩ噺
+            int curQuality = 0;
+            //pm绛夌骇(1:浼橈紱2:鑹紱3锛氳交搴︽薄鏌擄紱4:閲嶅害姹℃煋锛涳級
+            int pmQuality = 0;
+            //co2绛夌骇(1:浼橈紙娓呮櫚锛夛紱2:鑹紙娴戞祳锛夛紱3锛氳交搴︽薄鏌擄紙缂烘哀锛夛紱4:閲嶅害姹℃煋锛堜弗閲嶇己姘э級锛涳級
+            int co2Quality = 0;
+            //娓╁害
+            string temperature = string.Empty;
+            //婀垮害
+            string humidity = string.Empty;
+            //PM2.5
+            string pm = string.Empty;
+            //CO2
+            string co2 = string.Empty;
+
+            if (tempera.currentPmData <= 35 && tempera.currentPmData >= 0)
+            {
+                pmQuality = 1;
+            }
+            else if (tempera.currentPmData <= 75 && tempera.currentPmData > 35)
+            {
+                pmQuality = 2;
+            }
+            else if (tempera.currentPmData <= 115 && tempera.currentPmData > 75)
+            {
+                pmQuality = 3;
+            }
+            else if (tempera.currentPmData > 115)
+            {
+                pmQuality = 4;
+            }
+            else
+            {
+                pmQuality = 0;
+            }
+
+            if (tempera.currentCO2 <= 1000 && tempera.currentCO2 >= 0)
+            {
+                co2Quality = 1;
+            }
+            else if (tempera.currentCO2 <= 2000 && tempera.currentCO2 > 1000)
+            {
+                co2Quality = 2;
+            }
+            else if (tempera.currentCO2 <= 5000 && tempera.currentCO2 > 2000)
+            {
+                co2Quality = 3;
+            }
+            else if (tempera.currentCO2 > 5000)
+            {
+                co2Quality = 4;
+            }
+            else
+            {
+                co2Quality = 0;
+            }
+
+            if (co2Quality == 1 && pmQuality == 1)
+            {
+                curQuality = 1;
+            }
+            else if (co2Quality == 2 || pmQuality == 2)
+            {
+                curQuality = 2;
+            }
+            else
+            {
+                curQuality = 3;
+            }
+
+            return curQuality;
+        }
+
+        #region led鏃堕棿
+        ///<summary >
+        /// 閰嶇疆led澶辫兘鏃堕棿
+        /// <para>number:搴忓彿</para> 
+        /// </summary>
+        public async System.Threading.Tasks.Task<ResponseAllData> SetAirQualitySensorLedTime(LedTimeData ledTimeData)
+        {
+            ResponseAllData result = null;
+            if (Gateway == null)
+            {
+                return result;
+            }
+            return await System.Threading.Tasks.Task.Run(async () =>
+            {
+                Action<string, string> action = (topic, message) =>
+          {
+              var gatewayID = topic.Split('/')[0];
+              var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
+
+              if (topic == gatewayID + "/" + "ZbDataPassthrough")
+              {
+                  var clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString());
+
+                  if (clientDataPassthroughResponseData != null)
+                  {
+                      if (clientDataPassthroughResponseData?.PassData != null)
+                      {
+                          var data = clientDataPassthroughResponseData.PassData;
+                          if (data.Length == 16)
+                          {
+                              var command = data[4].ToString() + data[5].ToString() + data[2].ToString() + data[3].ToString();
+                              if (command == "0002")
+                              {
+                                  var tempD = new ResponseData();
+                                  tempD.command = data[12].ToString() + data[13].ToString() + data[10].ToString() + data[11].ToString();
+                                  tempD.status = Convert.ToInt32(data[14].ToString() + data[15].ToString(), 16);
+                                  if (tempD.command == "0412")
+                                  {
+                                      result = new ResponseAllData { responseData = tempD };
+                                      System.Console.WriteLine($"UI鏀跺埌閫氱煡鍚庣殑涓婚_command:0400_{ topic}");
+                                  }
+
+                              }
+                          }
+                      }
+                  }
+              }
+          };
+
+                Gateway.Actions += action;
+                System.Console.WriteLine("ClientDataPassthrough_Actions 鍚姩" + System.DateTime.Now.ToString());
+
+                try
+                {
+                    var passData = SetAirQualitySensorLedTimeDataStr(ledTimeData);
+                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", 200 }, { "Cluster_ID", 64513 }, { "Command", 0 } };
+                    var data = new JObject { { "PassData", passData } };
+                    jObject.Add("Data", data);
+                    Gateway.Send(("ClientDataPassthrough"), jObject.ToString());
+                }
+                catch { }
+
+                var dateTime = DateTime.Now;
+                while ((DateTime.Now - dateTime).TotalMilliseconds < 9000)// WaitReceiveDataTime)
+                {
+                    await System.Threading.Tasks.Task.Delay(10);
+                    if (result != null)
+                    {
+                        break;
+                    }
+                }
+                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
+                {
+                    result = null;
+                }
+                Gateway.Actions -= action;
+                System.Console.WriteLine("ClientDataPassthrough_Actions 閫�鍑�" + System.DateTime.Now.ToString());
+
+                return result;
+            });
+        }
+
+        /// <summary>
+        ///閰嶇疆led澶辫兘鏃堕棿鏁版嵁
+        /// </summary>
+        string SetAirQualitySensorLedTimeDataStr(LedTimeData ledTimeData)
+        {
+            string data = "";
+            string dataLength = "10";
+            string dataComand1 = "12";
+            string dataComand2 = "04";
+            string dataSerialNum = "01";
+            string addDataLength = "6";
+            string serialNum = "";
+            string sEnable = "";
+            string startHour = "";
+            string startMin = "";
+            string endHour = "";
+            string endMin = "";
+            string remark = "";
+            try
+            {
+                if (ledTimeData == null)
+                {
+                    return "";
+                }
+
+                int dataLength1 = 10;
+                int addDataLength1 = 6;
+                if (!string.IsNullOrEmpty(ledTimeData.remark))
+                {
+                    var reamarkGwBytes = System.Text.Encoding.UTF8.GetBytes(ledTimeData.remark);
+                    int len = 36 < reamarkGwBytes.Length ? 36 : reamarkGwBytes.Length;
+                    dataLength1 += len;
+                    addDataLength1 += len;
+                }
+
+                dataLength = Convert.ToString(dataLength1, 16);
+                if (dataLength.Length == 1)
+                {
+                    dataLength = "0" + dataLength;
+                }
+
+                addDataLength = Convert.ToString(addDataLength1, 16);
+                if (addDataLength.Length == 1)
+                {
+                    addDataLength = "0" + addDataLength;
+                }
+
+                //鍥犱负鍙湁0锝�4涓簭鍙凤紝鎬诲叡5涓�
+                serialNum = "0" + ledTimeData.serialNum.ToString();
+
+                if (ledTimeData.enable == 1)
+                {
+                    sEnable = "01";
+                }
+                else
+                {
+                    sEnable = "00";
+                }
+
+                startHour = Convert.ToString(ledTimeData.startHour, 16);
+                if (startHour.Length == 1)
+                {
+                    startHour = "0" + startHour;
+                }
+
+                startMin = Convert.ToString(ledTimeData.startMin, 16);
+                if (startMin.Length == 1)
+                {
+                    startMin = "0" + startMin;
+                }
+
+                endHour = Convert.ToString(ledTimeData.endHour, 16);
+                if (endHour.Length == 1)
+                {
+                    endHour = "0" + endHour;
+                }
+
+                endMin = Convert.ToString(ledTimeData.endMin, 16);
+                if (endMin.Length == 1)
+                {
+                    endMin = "0" + endMin;
+                }
+
+                remark = StringToHexString(ledTimeData.remark);
+
+                data = dataLength + dataComand1 + dataComand2 + dataSerialNum + addDataLength +
+                  serialNum + sEnable + startHour + startMin + endHour + endMin + remark;
+            }
+            catch { };
+
+            return data;
+        }
+
+        private string StringToHexString(string s)
+        {
+            var reamarkGwBytes = System.Text.Encoding.UTF8.GetBytes(s);
+            int len = 36 < reamarkGwBytes.Length ? 36 : reamarkGwBytes.Length;
+            var bytes = new byte[len];
+            System.Array.Copy(reamarkGwBytes, 0, bytes, 0, len);
+            string result = string.Empty;
+            for (int i = 0; i < bytes.Length; i++)//閫愬瓧鑺傚彉涓�16杩涘埗瀛楃 
+            {
+                string temp = Convert.ToString(bytes[i], 16);
+                if (temp.Length == 1)
+                {
+                    temp = "0" + temp;
+                }
+                result += temp;
+            }
+            return result;
+        }
+
+        private string HexStringToString(string hs)
+        {
+            //浠�%鍒嗗壊瀛楃涓诧紝骞跺幓鎺夌┖瀛楃
+            string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
+            byte[] b = new byte[chars.Length];
+            //閫愪釜瀛楃鍙樹负16杩涘埗瀛楄妭鏁版嵁
+            for (int i = 0; i < chars.Length; i++)
+            {
+                b[i] = Convert.ToByte(chars[i], 16);
+            }
+            //鎸夌収鎸囧畾缂栫爜灏嗗瓧鑺傛暟缁勫彉涓哄瓧绗︿覆
+            return System.Text.Encoding.UTF8.GetString(b);
+        }
+
+        private string StringToHexString1(string s)
+        {
+            var bytes = new byte[36];
+            var reamarkGwBytes = System.Text.Encoding.UTF8.GetBytes(s);
+            System.Array.Copy(reamarkGwBytes, 0, bytes, 0, 36 < reamarkGwBytes.Length ? 36 : reamarkGwBytes.Length);
+            string result = string.Empty;
+            for (int i = 0; i < bytes.Length; i++)//閫愬瓧鑺傚彉涓�16杩涘埗瀛楃锛屼互%闅斿紑
+            {
+                result += "%" + Convert.ToString(bytes[i], 16);
+            }
+            return result;
+
+        }
+
+        private string HexStringToString1(string hs)
+        {
+            //浠�%鍒嗗壊瀛楃涓诧紝骞跺幓鎺夌┖瀛楃
+            string[] chars = hs.Split(new char[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
+            byte[] b = new byte[chars.Length];
+            //閫愪釜瀛楃鍙樹负16杩涘埗瀛楄妭鏁版嵁
+            for (int i = 0; i < chars.Length; i++)
+            {
+                b[i] = Convert.ToByte(chars[i], 16);
+            }
+            //鎸夌収鎸囧畾缂栫爜灏嗗瓧鑺傛暟缁勫彉涓哄瓧绗︿覆
+            return System.Text.Encoding.UTF8.GetString(b);
+        }
+
+        ///<summary >
+        /// 鑾峰彇led澶辫兘鏃堕棿
+        /// <para>number:搴忓彿</para> 
+        /// </summary>
+        public async System.Threading.Tasks.Task<LedTimeData> ReadAirQualitySensorLedTime(int number)
+        {
+            LedTimeData result = null;
+            if (Gateway == null)
+            {
+                return result;
+            }
+            return await System.Threading.Tasks.Task.Run(async () =>
+            {
+                Action<string, string> action = (topic, message) =>
+          {
+              var gatewayID = topic.Split('/')[0];
+              var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
+
+              if (topic == gatewayID + "/" + "ZbDataPassthrough")
+              {
+                  var clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString());
+
+                  if (clientDataPassthroughResponseData != null)
+                  {
+                      if (clientDataPassthroughResponseData?.PassData != null)
+                      {
+                          var data = clientDataPassthroughResponseData.PassData;
+                          var command = data[4].ToString() + data[5].ToString() + data[2].ToString() + data[3].ToString();
+                          if (command == "0413")
+                          {
+                              result = new LedTimeData();
+                              result.serialNum = Convert.ToInt32(data[10].ToString() + data[11].ToString(), 16);
+                              result.enable = Convert.ToInt32(data[12].ToString() + data[13].ToString(), 16);
+                              result.startHour = Convert.ToInt32(data[14].ToString() + data[15].ToString(), 16);
+                              result.startMin = Convert.ToInt32(data[16].ToString() + data[17].ToString(), 16);
+                              result.endHour = Convert.ToInt32(data[18].ToString() + data[19].ToString(), 16);
+                              result.endMin = Convert.ToInt32(data[20].ToString() + data[21].ToString(), 16);
+                              string remark = data.Substring(22, data.Length - 22);
+                              var bytes = new byte[remark.Length / 2];
+                              for (int i = 0; i < bytes.Length; i++)
+                              {
+                                  bytes[i] = (byte)Convert.ToInt32(remark.Substring(i * 2, 2), 16);
+                              }
+                              result.remark = System.Text.Encoding.UTF8.GetString(bytes);
+                          }
+                      }
+                  }
+              }
+          };
+
+                Gateway.Actions += action;
+                System.Console.WriteLine("ClientDataPassthrough_Actions 鍚姩" + System.DateTime.Now.ToString());
+
+                try
+                {
+                    var passData = ReadAirQualitySensorLedTimeDataStr(number);
+                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", 200 }, { "Cluster_ID", 64513 }, { "Command", 0 } };
+                    var data = new JObject { { "PassData", passData } };
+                    jObject.Add("Data", data);
+                    Gateway.Send(("ClientDataPassthrough"), jObject.ToString());
+                }
+                catch { }
+
+                var dateTime = DateTime.Now;
+                while ((DateTime.Now - dateTime).TotalMilliseconds < 9000)// WaitReceiveDataTime)
+                {
+                    await System.Threading.Tasks.Task.Delay(10);
+                    if (result != null)
+                    {
+                        break;
+                    }
+                }
+                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
+                {
+                    result = null;
+                }
+                Gateway.Actions -= action;
+                System.Console.WriteLine("ClientDataPassthrough_Actions 閫�鍑�" + System.DateTime.Now.ToString());
+
+                return result;
+            });
+        }
+
+        /// <summary>
+        /// 鑾峰彇led澶辫兘鏃堕棿
+        /// </summary>
+        string ReadAirQualitySensorLedTimeDataStr(int number)
+        {
+            string data = "";
+            string dataLength = "05";
+            string dataComand1 = "11";
+            string dataComand2 = "04";
+            string dataSerialNum = "01";
+            string addDataLength = "01";
+            string num = "";
+            try
+            {
+                if (number == 1)
+                {
+                    num = "01";
+                }
+                else
+                {
+                    num = "00";
+                }
+
+                data = dataLength + dataComand1 + dataComand2 + dataSerialNum + addDataLength +
+                  num;
+            }
+            catch { };
+
+            return data;
+        }
+
+        /// <summary>
+        /// 閰嶇疆led澶辫兘鏃堕棿鏁版嵁
+        /// </summary>
+        [System.Serializable]
+        public class LedTimeData
+        {
+            /// <summary>
+            /// 搴忓彿(姝e父搴忓彿0锝�4锛屽叡5涓級
+            /// </summary>
+            public int serialNum = -1;
+            /// <summary>
+            /// 浣胯兘浣�
+            /// <para>0-鏃犳晥 1--鏈夋晥</para>
+            /// </summary>
+            public int enable = 0;
+            /// <summary>
+            /// 璧峰鏃�
+            /// </summary>
+            public int startHour;
+            /// <summary>
+            /// 璧峰鍒�
+            /// </summary>
+            public int startMin;
+            /// <summary>
+            /// 缁撴潫鏃�
+            /// </summary>
+            public int endHour;
+            /// <summary>
+            /// 缁撴潫鍒�
+            /// </summary>
+            public int endMin;
+            /// <summary>
+            /// 澶囨敞
+            /// </summary>
+            public string remark;
+        }
+        #endregion
     }
 }

--
Gitblit v1.8.0