예제 #1
0
 public void LogLowBatteryStateOnSensor(DoorWindowSensorModel doorWindowSensor)
 {
     _dbContext.DoorWindowSensorActivity.Add(new DoorWindowSensorActivity
     {
         SensorId          = doorWindowSensor.Id,
         ExecutionDateTime = DateTime.Now,
         State             = doorWindowSensor.State,
         LowBattery        = null
     });
     _dbContext.SaveChanges();
 }
예제 #2
0
        public void LogUpdateOnLightswitch(LightSwitchModel lightSwitch)
        {
            bool addNewRecord = true;

            // Check if at least a predefined ammount of time passed since the last record with the same state was inserted.
            // There can be the case when the same state is being triggered for a light switch multiple times in a short time span.
            // We don't want to log all of those events.
            var latestLightSwitchRecord = _dbContext.LightSwitches
                                          .OrderByDescending(l => l.ExecutionDateTime)
                                          .FirstOrDefault();

            if (latestLightSwitchRecord != null && latestLightSwitchRecord.State == lightSwitch.State)
            {
                TimeSpan timeElapsedSinceLastRecord = DateTime.Now.Subtract(latestLightSwitchRecord.ExecutionDateTime);
                TimeSpan buffer = new TimeSpan(0, 0, _lightSwitchOptions.NoLogUpdateInterval);

                if (timeElapsedSinceLastRecord < buffer)
                {
                    addNewRecord = false;
                }
            }

            if (addNewRecord)
            {
                _dbContext.LightSwitches.Add(new LightSwitch
                {
                    LightSwitchId     = lightSwitch.Id,
                    ExecutionDateTime = DateTime.Now,
                    State             = lightSwitch.State
                });
                _dbContext.SaveChanges();
            }
        }
예제 #3
0
 public void LogValue(ExternalWallSocketModel wallSocket)
 {
     _dbContext.ExternalWallSockets.Add(new ExternalWallSocket {
         ExternalWallSocketId = wallSocket.Id,
         ExecutionDateTime    = DateTime.Now,
         Voltage       = wallSocket.Voltage,
         Current       = wallSocket.Current,
         Frequency     = wallSocket.Frequency,
         EnergyCounter = wallSocket.EnergyCounter
     });
     _dbContext.SaveChanges();
 }