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); } 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); 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 ..."); }