public async Task OnReceiveData(string data) { var codes = new List <string>(); if (data.IndexOf("01 50 01 01 01") == 0) { codes = data.Split(' ').ToList(); if (!stateobjs.Any(p => p.Id == codes[5])) { var obj = new HvacStateObject(); obj.Id = codes[5]; obj.Switch = (SwitchState)int.Parse(codes[6]); obj.TemperatureSet = int.Parse(codes[7], System.Globalization.NumberStyles.HexNumber); obj.Mode = (WorkMode)int.Parse(codes[8]); obj.Fan = (Fanspeed)int.Parse(codes[9]); obj.CurrentTemperature = int.Parse(codes[10], System.Globalization.NumberStyles.HexNumber); stateobjs.Add(obj); var message = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/Status") .WithPayload(JsonConvert.SerializeObject(obj)) .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message); var message1 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/Current") .WithPayload(obj.CurrentTemperature.ToString()) .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message1); if (obj.Switch == SwitchState.open) { if (obj.Mode == WorkMode.Cool) { var message2 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/State") .WithPayload("COOL") .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message2); } else if (obj.Mode == WorkMode.Heat) { var message2 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/State") .WithPayload("HEAT") .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message2); } } else { var message2 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/State") .WithPayload("OFF") .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message2); } var message3 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/TargetSet") .WithPayload(obj.TemperatureSet.ToString()) .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message3); } else { var obj = stateobjs.FirstOrDefault(p => p.Id == codes[5]); obj.Switch = (SwitchState)int.Parse(codes[6]); obj.TemperatureSet = int.Parse(codes[7], System.Globalization.NumberStyles.HexNumber); obj.Mode = (WorkMode)int.Parse(codes[8]); obj.Fan = (Fanspeed)int.Parse(codes[9]); obj.CurrentTemperature = int.Parse(codes[10], System.Globalization.NumberStyles.HexNumber); var message = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/Status") .WithPayload(JsonConvert.SerializeObject(obj)) .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message); var message1 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/Current") .WithPayload(obj.CurrentTemperature.ToString()) .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message1); if (obj.Switch == SwitchState.open) { if (obj.Mode == WorkMode.Cool) { var message2 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/State") .WithPayload("COOL") .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message2); } else if (obj.Mode == WorkMode.Heat) { var message2 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/State") .WithPayload("HEAT") .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message2); } } else { var message2 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/State") .WithPayload("OFF") .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message2); } var message3 = new MqttApplicationMessageBuilder().WithTopic("Home/Sanling/" + codes[5] + "/TargetSet") .WithPayload(obj.TemperatureSet.ToString()) .WithAtLeastOnceQoS() .Build(); await _mqttHelper.Publish(message3); if (obj.Id == "02") { if (obj.Switch == SwitchState.open) { await _lightHelper.SetBackgroudLight("0D", "25", 1); } else { await _lightHelper.SetBackgroudLight("0D", "25", 0); } } if (obj.Id == "03") { if (obj.Switch == SwitchState.open) { await _lightHelper.SetBackgroudLight("0D", "26", 1); } else { await _lightHelper.SetBackgroudLight("0D", "26", 0); } } } await _lightHelper.UpdateACPanel(); } }
public async Task UpdateStateObject(HvacStateObject target) { //_logger.LogInformation("updateStateObject"); var obj = stateobjs.FirstOrDefault(p => p.Id == target.Id); if (obj != null) { if (obj.Switch != target.Switch) { obj.Switch = target.Switch; if (obj.Id == "02") { if (obj.Switch == SwitchState.open) { await _lightHelper.OpenWorkroomAC(); } else { await _lightHelper.CloseWorkroomAC(); } } else if (obj.Id == "03") { if (obj.Switch == SwitchState.open) { await _lightHelper.OpenLivingroomAC(); } else { await _lightHelper.CloseLivingroomAC(); } } else { if (obj.Switch == SwitchState.open) { await TurnOnAC(int.Parse(obj.Id)); } else { await TurnOffAC(int.Parse(obj.Id)); } } } else if (obj.Mode != target.Mode) { await SetMode(int.Parse(obj.Id), (WorkMode)target.Mode); await _lightHelper.UpdateACPanel(); } else if (obj.Fan != target.Fan) { obj.Fan = target.Fan; await SetFanspeed(int.Parse(obj.Id), (Fanspeed)target.Fan); await _lightHelper.UpdateACPanel(); } else if (obj.TemperatureSet != target.TemperatureSet) { obj.TemperatureSet = target.TemperatureSet; await SetTemperature(int.Parse(obj.Id), target.TemperatureSet); await _lightHelper.UpdateACPanel(); } } //_logger.LogInformation("... End SyncStateObject ..."); }