using System; using System.Collections.Generic; using Newtonsoft.Json.Linq; namespace ZigBee.Device { [System.Serializable] public class AC : CommonDevice { public AC() { this.Type = DeviceType.Thermostat; } /// ///当前室内温度 /// [Newtonsoft.Json.JsonIgnore] public int currentLocalTemperature = 0; /// /// 当前加热度数 /// [Newtonsoft.Json.JsonIgnore] public int currentHeatingSetpoint = 0; /// /// 当前制冷度数 /// [Newtonsoft.Json.JsonIgnore] public int currentCoolingSetpoint = 0; /// /// 当前自动温度 /// [Newtonsoft.Json.JsonIgnore] public int currentAutoSetpoint; /// /// 恒温设备当前模式 /// 恒温设备具备功能,SystemMode Attribute Values如下 /// 0:Off /// 1:Auto /// 3:Cool /// 4:Heat /// 5:Emergency heating /// 6:Precooling /// 7:Fan only /// 8:Dry /// 9:Sleep /// [Newtonsoft.Json.JsonIgnore] public int currentSystemMode = 0; /// /// 风扇当前模式 /// 0:Off /// 1:Low /// 2:Medium /// 3:High /// 4:On /// 5:Auto /// 6:Smart /// [Newtonsoft.Json.JsonIgnore] public int currentFanMode = 0; /// /// 风扇zz扫风当前模式 /// 0:1挡 /// 1:2 /// 2:3 /// 3:4 /// 4:5 /// 7:Auto /// [Newtonsoft.Json.JsonIgnore] public int currentFanSwingMode = 0; /// /// 自定义空调支持的模式(默认全开) /// 数组索引 -> 0:制冷 1:制热 2:送风 3:除湿 4:自动 /// 值 -> 1:代表使用 0:代表不使用 /// public int[] listSupportMode = new int[5] { 1, 1, 1, 1, 1 }; /// /// 是否启用摆风功能 false:不使用摆风功能 true:使用摆风功能 /// public bool UseSwingFunction = true; /// /// 过滤网清洗状态 /// [Newtonsoft.Json.JsonIgnore] public bool CleanStatu = false; /// /// 获取当前室内温度. /// AttriButeId=0 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadLocalTemperature() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.LocalTemperature); } /// /// 获取当前制冷度数. /// AttriButeId=17 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadCoolingSetpoint() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.CoolingSetpoint); } /// /// 获取当前加热度数. /// AttriButeId=18 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadHeatingSetpoint() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.HeatingSetpoint); } /// /// 获取当前自动度数. /// AttriButeId=18 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadAutoSetpoint() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.AutoSetpoint); } /// /// 获取恒温设备类型(******测试设备返回的AttriBute属性是空) /// AttriButeId=27 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadControlSequenceOfOperation() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.ControlSequenceOfOperation); } /// /// 获取恒温设备当前工作模式 /// AttriButeId=28 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadSystemMode() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.SystemMode); } /// /// 获取风扇模式。 /// AttriButeId=0 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadFanMode() { ReadAttri(Device.Cluster_ID.FanControl, AttriButeId.FanMode); } /// /// 获取风扇拥有的工作模式种类. /// AttriButeId=1 (先调用发现属性接口,查看设备是否支持该接口的属性) /// public void ReadFanModeSequence() { ReadAttri(Device.Cluster_ID.FanControl, AttriButeId.FanModeSequence); } /// /// 获取空调自定义设置的支持模式 /// public void ReadModeSupport() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.AcModeSupport); } /// /// 过滤网清洗状态 /// public void ReadCleanStatu() { ReadAttri(Device.Cluster_ID.Thermostat, AttriButeId.CleanStatu); } /// /// 获取是否启用空调摆风模式的状态(打开 或者 关闭) /// public void ReadUseSwingFunctionStatu() { ReadAttri(Device.Cluster_ID.FanControl, AttriButeId.UseAcSwingFunctionStatu); } /// /// 扫风模式 /// public enum FanSwingMode { /// /// 一档 /// First = 0, /// /// 二挡 /// Second = 1, /// /// 三挡 /// Thrid = 2, /// /// 四挡 /// Fourth = 3, /// /// 五档 /// Fifth = 4, /// /// 自动 /// Auto = 7 } /// /// 获取扫风模式 /// public void ReadSystemFansSwingMode() { ReadAttri(Device.Cluster_ID.FanSwing, AttriButeId.FanSwing); } /// /// 设置扫风模式 /// /// /// public async System.Threading.Tasks.Task SetFanSwingAsyncMode(FanSwingMode fanSwing) { if (Gateway == null) { return null; } return await System.Threading.Tasks.Task.Run(async () => { SetWritableValueResponAllData d = null; Action action = (topic, message) => { var gatewayID = topic.Split('/')[0]; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); if (topic == gatewayID + "/" + "Error_Respon") { var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (temp == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" }; } else { d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) }; } } if (topic == gatewayID + "/" + "SetWritableValue_Respon") { var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (tempData == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" }; } else { d = new SetWritableValueResponAllData { setWritableValueResponData = tempData }; DebugPrintLog($"UI收到通知后的主题_{ topic}"); } } }; Gateway.Actions += action; DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString()); try { var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 514 }, { "Command", 120 } }; var data = new JObject { { "Undivided", 0 }, { "AttributeId", 4096 }, { "AttributeDataType", 48 }, { "AttributeData", (int)fanSwing } }; jObject.Add("Data", data); Gateway.Send("SetWritableValue", jObject.ToString()); } catch { } var dateTime = DateTime.Now; while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime) { await System.Threading.Tasks.Task.Delay(10); if (d != null) { break; } } if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime) { d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" }; } Gateway.Actions -= action; DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString()); return d; }); } /// ///设置制冷度数. /// public async System.Threading.Tasks.Task SetCoolingTemperatureAsync(int temperature) { if (Gateway == null) { return null; } return await System.Threading.Tasks.Task.Run(async () => { SetWritableValueResponAllData d = null; Action action = (topic, message) => { var gatewayID = topic.Split('/')[0]; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); if (topic == gatewayID + "/" + "Error_Respon") { var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (temp == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" }; } else { d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) }; } } if (topic == gatewayID + "/" + "SetWritableValue_Respon") { var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (tempData == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" }; } else { d = new SetWritableValueResponAllData { setWritableValueResponData = tempData }; DebugPrintLog($"UI收到通知后的主题_{ topic}"); } } }; Gateway.Actions += action; DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString()); try { var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } }; var data = new JObject { { "Undivided", 0 }, { "AttributeId", 17 }, { "AttributeDataType", 41 }, { "AttributeData", (int)temperature } }; jObject.Add("Data", data); Gateway.Send("SetWritableValue", jObject.ToString()); } catch { } var dateTime = DateTime.Now; while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime) { await System.Threading.Tasks.Task.Delay(10); if (d != null) { break; } } if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime) { d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" }; } Gateway.Actions -= action; DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString()); return d; }); } /// ///设置制热度数. /// public async System.Threading.Tasks.Task SetHeatingTemperatureAsync(int temperature) { if (Gateway == null) { return null; } return await System.Threading.Tasks.Task.Run(async () => { SetWritableValueResponAllData d = null; Action action = (topic, message) => { var gatewayID = topic.Split('/')[0]; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); if (topic == gatewayID + "/" + "Error_Respon") { var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (temp == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" }; } else { d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) }; } } if (topic == gatewayID + "/" + "SetWritableValue_Respon") { var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (tempData == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" }; } else { d = new SetWritableValueResponAllData { setWritableValueResponData = tempData }; DebugPrintLog($"UI收到通知后的主题_{ topic}"); } } }; Gateway.Actions += action; DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString()); try { var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } }; var data = new JObject { { "Undivided", 0 }, { "AttributeId", 18 }, { "AttributeDataType", 41 }, { "AttributeData", (int)temperature } }; jObject.Add("Data", data); Gateway.Send("SetWritableValue", jObject.ToString()); } catch { } var dateTime = DateTime.Now; while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime) { await System.Threading.Tasks.Task.Delay(10); if (d != null) { break; } } if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime) { d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" }; } Gateway.Actions -= action; DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString()); return d; }); } /// ///设置自动度数. /// public async System.Threading.Tasks.Task SetAutoTemperatureAsync(int temperature) { if (Gateway == null) { return null; } return await System.Threading.Tasks.Task.Run(async () => { SetWritableValueResponAllData d = null; Action action = (topic, message) => { var gatewayID = topic.Split('/')[0]; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); if (topic == gatewayID + "/" + "Error_Respon") { var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (temp == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" }; } else { d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) }; } } if (topic == gatewayID + "/" + "SetWritableValue_Respon") { var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (tempData == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" }; } else { d = new SetWritableValueResponAllData { setWritableValueResponData = tempData }; DebugPrintLog($"UI收到通知后的主题_{ topic}"); } } }; Gateway.Actions += action; DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString()); try { var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } }; var data = new JObject { { "Undivided", 0 }, { "AttributeId", 4096 }, { "AttributeDataType", 41 }, { "AttributeData", (int)temperature } }; jObject.Add("Data", data); Gateway.Send("SetWritableValue", jObject.ToString()); } catch { } var dateTime = DateTime.Now; while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime) { await System.Threading.Tasks.Task.Delay(10); if (d != null) { break; } } if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime) { d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" }; } Gateway.Actions -= action; DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString()); return d; }); } /// ///设置恒温器设备当前工作模式. /// public async System.Threading.Tasks.Task SetSystemModeAsync(AcMode acMode) { if (Gateway == null) { return null; } return await System.Threading.Tasks.Task.Run(async () => { SetWritableValueResponAllData d = null; Action action = (topic, message) => { var gatewayID = topic.Split('/')[0]; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); if (topic == gatewayID + "/" + "Error_Respon") { var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (temp == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" }; } else { d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) }; } } if (topic == gatewayID + "/" + "SetWritableValue_Respon") { var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (tempData == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" }; } else { d = new SetWritableValueResponAllData { setWritableValueResponData = tempData }; DebugPrintLog($"UI收到通知后的主题_{ topic}"); } } }; Gateway.Actions += action; DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString()); try { var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } }; var data = new JObject { { "Undivided", 0 }, { "AttributeId", 28 }, { "AttributeDataType", 48 }, { "AttributeData", (int)acMode } }; jObject.Add("Data", data); Gateway.Send("SetWritableValue", jObject.ToString()); } catch { } var dateTime = DateTime.Now; while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime) { await System.Threading.Tasks.Task.Delay(10); if (d != null) { break; } } if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime) { d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" }; } Gateway.Actions -= action; DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString()); return d; }); } public enum AcMode { /// /// 关闭模式(测试恒温面板时发现:2,5,6,7,8,9都是可以打开的) /// Off = 0, /// /// 自动模式 /// Auto = 1, /// /// 制冷模式 /// Cool = 3, /// /// 制热模式 /// Heat = 4, /// /// 紧急制热模式 /// EmergencyHeating = 5, /// /// 预冷模式 /// Precooling = 6, /// /// 只有风速模式 /// FanOnly = 7, /// /// 干燥模式 /// Dry = 8, /// /// 睡眠模式 /// Sleep = 9 } /// ///设置恒温器设备当前风速. /// public async System.Threading.Tasks.Task SetFanModeAsync(FanMode fanMode) { if (Gateway == null) { return null; } return await System.Threading.Tasks.Task.Run(async () => { SetWritableValueResponAllData d = null; Action action = (topic, message) => { var gatewayID = topic.Split('/')[0]; var jobject = Newtonsoft.Json.Linq.JObject.Parse(message); if (topic == gatewayID + "/" + "Error_Respon") { var temp = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (temp == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" }; } else { d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) }; } } if (topic == gatewayID + "/" + "SetWritableValue_Respon") { var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject(jobject["Data"].ToString()); if (tempData == null) { d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" }; } else { d = new SetWritableValueResponAllData { setWritableValueResponData = tempData }; DebugPrintLog($"UI收到通知后的主题_{ topic}"); } } }; Gateway.Actions += action; DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString()); try { var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 514 }, { "Command", 120 } }; var data = new JObject { { "Undivided", 0 }, { "AttributeId", 0 }, { "AttributeDataType", 48 }, { "AttributeData", (int)fanMode } }; jObject.Add("Data", data); Gateway.Send("SetWritableValue", jObject.ToString()); } catch { } var dateTime = DateTime.Now; while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime) { await System.Threading.Tasks.Task.Delay(10); if (d != null) { break; } } if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime) { d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" }; } Gateway.Actions -= action; DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString()); return d; }); } public enum FanMode { /// /// 关闭风速 /// Off = 0, /// /// 低风 /// Low = 1, /// /// 中风 /// Medium = 2, /// /// 高风 /// High = 3, /// /// 打开风速 /// On = 4, /// /// 自动 /// Auto = 5, /// /// 聪明fe /// Smart = 6, } /// ///设置恒温设备加热或制冷度数(没有测试成功) /// mode要调节的模式 0:加热;1:制冷;2:自动调节 ///temperature:要变化的度数,单位:0.1℃ /// public void SetTemperature(int mode, int temperature) { var jObject = new JObject { { "DeviceAddr", DeviceAddr}, { "Epoint", DeviceEpoint}, { "Cluster_ID",513}, { "Command", 0}, { "SendMode", 2 } }; var data = new JObject { { "Mode", mode }, { "Amount", temperature } }; jObject.Add("Data", data); Gateway?.Send("DeviceControl", jObject.ToString()); } /// /// 关闭 /// /// The close. public async System.Threading.Tasks.Task Close() { return await SetSystemModeAsync(AcMode.Off); } /// /// 开启 /// /// The open. /// Ac mode. public async System.Threading.Tasks.Task Open(AcMode acMode = AcMode.Cool) { return await SetSystemModeAsync(acMode); } } }