private async void dispatcherTimer_Tick(object sender, EventArgs e) { foreach (var sensors in this.SensorCollection) { DateTime now = DateTime.Now; var dueSensors = sensors.SensorEvents?. EventAlarms?. Count(t => (now - t.TimeRecieved).Seconds >= dueDelay); if (dueSensors > 0) { sensors.SensorEvents.EventAlarms.RemoveAll(t => (now - t.TimeRecieved).Seconds >= dueDelay); bool update = await cacheService.UpdateEntity(sensors.CurrentSensor); if (update) { UpdateSensorEvent?. Invoke(null, sensors); break; } } if (sensors.SensorEvents != null && (sensors.SensorEvents.EventAlarms != null && (sensors.SensorEvents != null && sensors.SensorEvents.EventAlarms.Count == 0))) { bool delete = await cacheService.RemoveEntity(sensors.CurrentSensor); if (delete) { SensorCollection.Remove(sensors); DeleteSensorEvent?. Invoke(null, sensors); break; } } } }
public void AddEvent(Sensor result, SensorStatus sensorStatus) { InvokerHelper.RunSave(async() => { SensorCollection currentSensorCollection; bool event_exist = false; //foreach (var sensor in SensorCollection) //{ // if (result.Id == sensor.CurrentSensor.Id) // { // event_exist = true; // break; // } //} Parallel.ForEach(SensorCollection, (sensor, state) => { if (result.Id == sensor.CurrentSensor.Id) { event_exist = true; state.Stop(); } }); if (IsSensorAlarming(result, sensorStatus)) { if (event_exist) { currentSensorCollection = SensorCollection.FirstOrDefault(t => t.CurrentSensor.Id == result.Id); if (currentSensorCollection != null) { bool canUpdate = await cacheService.UpdateEntity(currentSensorCollection.CurrentSensor); if (canUpdate) { currentSensorCollection.SensorEvents.EventAlarms?. Add(new EventAlarm(sensorStatus)); UpdateSensorEvent?. Invoke(null, currentSensorCollection); } } } else { currentSensorCollection = new SensorCollection() { CurrentSensor = result, FirstSensorStatus = sensorStatus, SensorEvents = new Event() { EventAlarms = new List <EventAlarm>() { new EventAlarm(sensorStatus) } } }; bool canSave = await cacheService.AddEntity(currentSensorCollection.CurrentSensor); if (canSave) { if (sensorStatus.StatusType == StatusType.Alarm) { SensorCollection.Add(currentSensorCollection); } else { SensorCollection.Insert(0, currentSensorCollection); } AddSensorEvent?. Invoke(null, currentSensorCollection); } } } }); }