def _send_notification(self, notification_sensor, reading): last_notification = self.notification_persistence \ .get_newest_notification(reading.name) if last_notification is None: send_notification = True elif reading.timestamp > last_notification.timestamp + \ timedelta(days=1): send_notification = True else: send_notification = False if send_notification: try: self.sms_notification.send_notification( notification_sensor.get_notification_text(reading.value)) notification_event = NotificationEvent() notification_event.sensor_id = reading.id notification_event.timestamp = datetime.now() self.notification_persistence.create(notification_event) logger.info(reading.name + ' - Sent Notification') except IOError: logger.exception(reading.name + ' - Failed to send notification') else: logger.info(reading.name + ' - No Notification Sent')
def test_get_newest_notification_returns_newest_record(self, sqlalchemy): sensor_service = SensorReadingPersistenceService() reading = AnalogSensorReading() reading.name = 'Test Sensor' sensor_service.create(reading) old_notification = NotificationEvent() old_notification.sensor_id = reading.id old_notification.timestamp = datetime(2015, 1, 1) new_notification = NotificationEvent() new_notification.sensor_id = reading.id new_notification.timestamp = datetime(2016, 1, 1) service = NotificationPersistenceService() # Insert in reverse order to confirm order by service.create(new_notification) service.create(old_notification) newest_timestamp = new_notification.timestamp db_timestamp = service.get_newest_notification(reading.name).timestamp assert db_timestamp == newest_timestamp
def test_get_newest_notification_excludes_other_sensors(self, sqlalchemy): reading_service = SensorReadingPersistenceService() notification_service = NotificationPersistenceService() included_sensor = DigitalSensorReading() included_sensor.name = 'included' reading_service.create(included_sensor) excluded_sensor = DigitalSensorReading() excluded_sensor.name = 'excluded' reading_service.create(excluded_sensor) excluded_notification = NotificationEvent() excluded_notification.sensor_id = excluded_sensor.id excluded_notification.timestamp = datetime(2016, 1, 1) notification_service.create(excluded_notification) included_notification = NotificationEvent() included_notification.sensor_id = included_sensor.id included_notification.timestamp = datetime(2015, 1, 1) notification_service.create(included_notification) assert notification_service.get_newest_notification('included') \ .timestamp == included_notification.timestamp