void ProcessCycleStatistics(MqttApplicationMessage applicationMessage) { string json = applicationMessage.ConvertPayloadToString(); try { var model = JsonConvert.DeserializeObject <BoilerInfoModel>(json); model.Sensors.Add(new Sensor() { SensorId = Sensor.SensorIdForBoilerRequired, Last = model.BoilerRequired, Time = model.CycleStart, Average = model.BoilerRequired }); model.Sensors.Add(new Sensor() { SensorId = Sensor.SensorIdForBoilerState, Last = model.IsBoilerOn ? 0.4 : 0, Time = model.CycleStart, Average = model.IsBoilerOn ? 0.4 : 0 }); model.ReciveTime = DateTime.UtcNow; readerWriterLockSlim.EnterWriteLock(); try { boilerInfoModel = model; SaveValidValues(model); if (boilerInfoModel.ReciveTime.Subtract(lastFullModelRecieved).TotalMinutes >= 10) { if (infoModelsHistory.Count >= QueueCapacity) { infoModelsHistory.Dequeue(); } SubstituteEmptyValues(model); infoModelsHistory.Enqueue(model); lastFullModelRecieved = boilerInfoModel.ReciveTime; } if (voltageModelsHistory.Count >= QueueCapacity * 2) { voltageModelsHistory.Dequeue(); } model = new BoilerInfoModel() { ReciveTime = DateTime.UtcNow }; var sensor = boilerInfoModel.Sensors.First(s => s.SensorId == 103); model.Sensors = new List <Sensor>() { (Sensor)sensor.Clone() }; if (voltageModelsHistory.Count == 0 || Math.Abs(voltageModelsHistory.Peek().Sensors[0].Last - sensor.Last) > 0.005 || sensor.Time.Subtract(voltageModelsHistory.Peek().Sensors[0].Time).TotalSeconds > 5) { voltageModelsHistory.Enqueue(model); } } finally { readerWriterLockSlim.ExitWriteLock(); } } catch { } }
void SaveValidValues(BoilerInfoModel model) { foreach (var sensor in model.Sensors) { if (!sensor.IsInvalid || sensor.Last != BoilerInfoModel.DefaultValue) { if (!lastValues.TryAdd(sensor.SensorId, sensor.Last)) { lastValues[sensor.SensorId] = sensor.Last; } } } }
void SubstituteEmptyValues(BoilerInfoModel model) { for (int i = 0; i < model.Sensors.Count; i++) { if (model.Sensors[i].IsInvalid || model.Sensors[i].Last == BoilerInfoModel.DefaultValue) { if (lastValues.TryGetValue(model.Sensors[i].SensorId, out double value)) { var sensor = (Sensor)model.Sensors[i].Clone(); sensor.Last = value; model.Sensors[i] = sensor; } } } }