def execute(self): # time on which the last full sensor states were sent # to the server lastFullStateSent = 0 # Get reference to server communication object. while self.connection is None: time.sleep(0.5) self.connection = self.globalData.serverComm self._isInitialized = True while True: # check if the client is connected to the server # => wait and continue loop until client is connected if not self.connection.isConnected(): time.sleep(0.5) continue # poll all sensors and check their states for sensor in self.sensors: oldState = sensor.getState() sensor.updateState() currentState = sensor.getState() # Check if a sensor alert is forced to send to the server. # => update already known state and continue sensorAlert = sensor.forceSendAlert() if sensorAlert: oldState = currentState asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorAlert = True asyncSenderProcess.sendSensorAlertSensorAlert = sensorAlert asyncSenderProcess.start() continue # check if the current state is the same # than the already known state => continue elif oldState == currentState: continue # check if the current state is an alert triggering state elif currentState == sensor.triggerState: # check if the sensor triggers a sensor alert # => send sensor alert to server if sensor.triggerAlert: logging.info("[%s]: Sensor alert " % self.fileName + "triggered by '%s'." % sensor.description) # Create sensor alert object to send to the server. sensorAlert = SensorAlert() sensorAlert.clientSensorId = sensor.id sensorAlert.state = 1 sensorAlert.hasOptionalData = sensor.hasOptionalData sensorAlert.optionalData = sensor.optionalData sensorAlert.changeState = sensor.changeState sensorAlert.hasLatestData = sensor.hasLatestData sensorAlert.dataType = sensor.sensorDataType sensorAlert.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorAlert = True asyncSenderProcess.sendSensorAlertSensorAlert = \ sensorAlert asyncSenderProcess.start() # if sensor does not trigger sensor alert # => just send changed state to server else: logging.debug("[%s]: State " % self.fileName + "changed by '%s'." % sensor.description) # Create state change object to send to the server. stateChange = StateChange() stateChange.clientSensorId = sensor.id stateChange.state = 1 stateChange.dataType = sensor.sensorDataType stateChange.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendStateChange = True asyncSenderProcess.sendStateChangeStateChange = \ stateChange asyncSenderProcess.start() # only possible situation left => sensor changed # back from triggering state to a normal state else: # check if the sensor triggers a sensor alert when # state is back to normal # => send sensor alert to server if sensor.triggerAlertNormal: logging.info("[%s]: Sensor alert " % self.fileName + "for back to normal state " + "triggered by '%s'." % sensor.description) # Create sensor alert object to send to the server. sensorAlert = SensorAlert() sensorAlert.clientSensorId = sensor.id sensorAlert.state = 0 sensorAlert.hasOptionalData = sensor.hasOptionalData sensorAlert.optionalData = sensor.optionalData sensorAlert.changeState = sensor.changeState sensorAlert.hasLatestData = sensor.hasLatestData sensorAlert.dataType = sensor.sensorDataType sensorAlert.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorAlert = True asyncSenderProcess.sendSensorAlertSensorAlert = \ sensorAlert asyncSenderProcess.start() # if sensor does not trigger sensor alert when # state is back to normal # => just send changed state to server else: logging.debug("[%s]: State " % self.fileName + "changed by '%s'." % sensor.description) # Create state change object to send to the server. stateChange = StateChange() stateChange.clientSensorId = sensor.id stateChange.state = 0 stateChange.dataType = sensor.sensorDataType stateChange.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendStateChange = True asyncSenderProcess.sendStateChangeStateChange = \ stateChange asyncSenderProcess.start() # Poll all sensors if they want to force an update that should # be send to the server. for sensor in self.sensors: stateChange = sensor.forceSendState() if stateChange: asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendStateChange = True asyncSenderProcess.sendStateChangeStateChange = stateChange asyncSenderProcess.start() # check if the last state that was sent to the server # is older than 60 seconds => send state update utcTimestamp = int(time.time()) if (utcTimestamp - lastFullStateSent) > 60: logging.debug("[%s]: Last state " % self.fileName + "timed out.") asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorsState = True asyncSenderProcess.start() # update time on which the full state update was sent lastFullStateSent = utcTimestamp time.sleep(0.5)
def execute(self): # time on which the last full sensor states were sent # to the server lastFullStateSent = 0 # Get reference to server communication object. while self.connection is None: time.sleep(0.5) self.connection = self.globalData.serverComm self._isInitialized = True while True: # check if the client is connected to the server # => wait and continue loop until client is connected if not self.connection.isConnected(): time.sleep(0.5) continue # poll all sensors and check their states for sensor in self.sensors: oldState = sensor.getState() sensor.updateState() currentState = sensor.getState() # Check if a sensor alert is forced to send to the server. # => update already known state and continue sensorAlert = sensor.forceSendAlert() if sensorAlert: oldState = currentState asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorAlert = True asyncSenderProcess.sendSensorAlertSensorAlert = sensorAlert asyncSenderProcess.start() continue # check if the current state is the same # than the already known state => continue elif oldState == currentState: continue # check if the current state is an alert triggering state elif currentState == sensor.triggerState: # check if the sensor triggers a sensor alert # => send sensor alert to server if sensor.triggerAlert: logging.info("[%s]: Sensor alert " % self.fileName + "triggered by '%s'." % sensor.description) # Create sensor alert object to send to the server. sensorAlert = SensorAlert() sensorAlert.clientSensorId = sensor.id sensorAlert.state = 1 sensorAlert.hasOptionalData = sensor.hasOptionalData sensorAlert.optionalData = sensor.optionalData sensorAlert.changeState = sensor.changeState sensorAlert.hasLatestData = sensor.hasLatestData sensorAlert.dataType = sensor.sensorDataType sensorAlert.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorAlert = True asyncSenderProcess.sendSensorAlertSensorAlert = \ sensorAlert asyncSenderProcess.start() # if sensor does not trigger sensor alert # => just send changed state to server else: logging.debug("[%s]: State " % self.fileName + "changed by '%s'." % sensor.description) # Create state change object to send to the server. stateChange = StateChange() stateChange.clientSensorId = sensor.id stateChange.state = 1 stateChange.dataType = sensor.sensorDataType stateChange.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendStateChange = True asyncSenderProcess.sendStateChangeStateChange = \ stateChange asyncSenderProcess.start() # only possible situation left => sensor changed # back from triggering state to a normal state else: # check if the sensor triggers a sensor alert when # state is back to normal # => send sensor alert to server if sensor.triggerAlertNormal: logging.info("[%s]: Sensor alert " % self.fileName + "for back to normal state " + "triggered by '%s'." % sensor.description) # Create sensor alert object to send to the server. sensorAlert = SensorAlert() sensorAlert.clientSensorId = sensor.id sensorAlert.state = 0 sensorAlert.hasOptionalData = sensor.hasOptionalData sensorAlert.optionalData = sensor.optionalData sensorAlert.changeState = sensor.changeState sensorAlert.hasLatestData = sensor.hasLatestData sensorAlert.dataType = sensor.sensorDataType sensorAlert.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorAlert = True asyncSenderProcess.sendSensorAlertSensorAlert = \ sensorAlert asyncSenderProcess.start() # if sensor does not trigger sensor alert when # state is back to normal # => just send changed state to server else: logging.debug("[%s]: State " % self.fileName + "changed by '%s'." % sensor.description) # Create state change object to send to the server. stateChange = StateChange() stateChange.clientSensorId = sensor.id stateChange.state = 0 stateChange.dataType = sensor.sensorDataType stateChange.sensorData = sensor.sensorData asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendStateChange = True asyncSenderProcess.sendStateChangeStateChange = \ stateChange asyncSenderProcess.start() # Poll all sensors if they want to force an update that should # be send to the server. for sensor in self.sensors: stateChange = sensor.forceSendState() if stateChange: asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendStateChange = True asyncSenderProcess.sendStateChangeStateChange = stateChange asyncSenderProcess.start() # check if the last state that was sent to the server # is older than 60 seconds => send state update if (time.time() - lastFullStateSent) > 60: logging.debug("[%s]: Last state " % self.fileName + "timed out.") asyncSenderProcess = AsynchronousSender( self.connection, self.globalData) # set thread to daemon # => threads terminates when main thread terminates asyncSenderProcess.daemon = True asyncSenderProcess.sendSensorsState = True asyncSenderProcess.start() # update time on which the full state update was sent lastFullStateSent = time.time() time.sleep(0.5)
def forceSendAlert(self): # Check if we have exceeded the threshold of failed calendar # retrieval attempts and create a sensor alert if we have. sensorAlert = None if (not self.inFailedState and self.failedCounter > self.maxFailedAttempts): logging.warning("[%s] Triggering sensor alert for " % self.fileName + "'%d' failed calendar fetching attempts." % self.failedCounter) sensorAlert = SensorAlert() sensorAlert.clientSensorId = self.id sensorAlert.state = 1 sensorAlert.hasOptionalData = True msg = "Failed more than %d times for '%s' " \ % (self.maxFailedAttempts, self.name) \ + "to retrieve calendar data." sensorAlert.optionalData = {"message": msg, "calendar": self.name, "type": "timeout"} sensorAlert.changeState = True sensorAlert.hasLatestData = False sensorAlert.dataType = SensorDataType.NONE self.state = self.triggerState self.inFailedState = True # If we are in a failed retrieval state and we could retrieve # calendar data again trigger a sensor alert for "normal". elif (self.inFailedState and self.failedCounter <= self.maxFailedAttempts): logging.warning("[%s] Fetching calendar succeeded after " % self.fileName + "multiple failed attempts. Triggering sensor alert.") sensorAlert = SensorAlert() sensorAlert.clientSensorId = self.id sensorAlert.state = 0 sensorAlert.hasOptionalData = True msg = "Calendar data for '%s' retrievable again." \ % self.name sensorAlert.optionalData = {"message": msg, "calendar": self.name, "type": "timeout"} sensorAlert.changeState = True sensorAlert.hasLatestData = False sensorAlert.dataType = SensorDataType.NONE self.state = 1 - self.triggerState self.inFailedState = False # If we have sensor alerts of reminder waiting # return the oldest of them. elif not self.reminderAlertQueue.empty(): try: sensorAlert = self.reminderAlertQueue.get(True, 2) except: pass return sensorAlert
def _processEventAlarms(self, event, eventDatetime): # Create current datetime object. currentUTCTime = time.time() currentDatetime = datetime.datetime.utcfromtimestamp(currentUTCTime) currentDatetime = currentDatetime.replace(tzinfo=pytz.UTC) for alarm in event.walk("VALARM"): if alarm.has_key("TRIGGER"): # Get time delta when reminder is triggered. trigger = alarm.get("TRIGGER") # Get time when reminder is triggered. # When trigger time is a delta, then calculate the actual time. if type(trigger.dt) == datetime.timedelta: triggerDatetime = eventDatetime + trigger.dt # When trigger time is an actual time, use this. elif type(trigger.dt) == datetime.datetime: triggerDatetime = trigger.dt # Use the same timezone as the event when # no is given. if triggerDatetime.tzinfo is None: triggerDatetime = triggerDatetime.replace( tzinfo=eventDatetime.tzinfo) # When trigger time is only a date, start at midnight, # however, use the same timezone as the event. elif type(trigger.dt) == datetime.date: triggerUTCTime = calendar.timegm(trigger.dt.timetuple()) triggerDatetime = datetime.datetime.utcfromtimestamp( triggerUTCTime) # Since we just overwrite the timezone here, we do not # care about conversion from UTC since the new datetime # object starts at midnight in the given timezone. triggerDatetime = triggerDatetime.replace( tzinfo=eventDatetime.tzinfo) else: logging.error("[%s] Error: Do not know how to handle " % self.fileName + "trigger type '%s'." % trigger.dt.__class__) continue # Uid of event is needed. uid = event.get("UID") # Get time when event starts. dtstart = event.get("DTSTART") # Check if we already triggered an alarm for the event # with this uid and the given alarm trigger time. if (uid, triggerDatetime) in self.alreadyTriggered: break # Check if the alarm trigger time lies in the past but not # more than 1 day. if((currentDatetime - self.timedelta1day) <= triggerDatetime <= currentDatetime): title = event.get("SUMMARY") # Get description if event has one. evDescription = "" if event.has_key("DESCRIPTION"): evDescription = event.get("DESCRIPTION") # Get location if event has one. location = "" if event.has_key("LOCATION"): location = event.get("LOCATION") # Create the utc unix timestamp for the start of the event. unixStart = datetime.datetime.utcfromtimestamp(0) unixStart = unixStart.replace(tzinfo=pytz.UTC) utcDtstart = int( (eventDatetime - unixStart).total_seconds()) # Create the utc unix timestamp for the reminder trigger. utcTrigger = int( (triggerDatetime - unixStart).total_seconds()) eventDateStr = time.strftime("%D %H:%M:%S", time.localtime(utcDtstart)) msg = "Reminder for event '%s' at %s" \ % (title, eventDateStr) # Create sensor alert. sensorAlert = SensorAlert() sensorAlert.clientSensorId = self.id sensorAlert.state = 1 sensorAlert.hasOptionalData = True sensorAlert.optionalData = {"message": msg, "calendar": self.name, "type": "reminder", "title": title, "description": evDescription, "location": location, "trigger": utcTrigger, "start": utcDtstart} sensorAlert.changeState = False sensorAlert.hasLatestData = False sensorAlert.dataType = SensorDataType.NONE self.reminderAlertQueue.put(sensorAlert) # Store the event uid and the alarm trigger time # as already triggered. self.alreadyTriggered.add( (uid, triggerDatetime) )
def forceSendAlert(self): # Check if we have exceeded the threshold of failed calendar # retrieval attempts and create a sensor alert if we have. sensorAlert = None if (not self.inFailedState and self.failedCounter > self.maxFailedAttempts): logging.warning( "[%s] Triggering sensor alert for " % self.fileName + "'%d' failed calendar fetching attempts." % self.failedCounter) sensorAlert = SensorAlert() sensorAlert.clientSensorId = self.id sensorAlert.state = 1 sensorAlert.hasOptionalData = True msg = "Failed more than %d times for '%s' " \ % (self.maxFailedAttempts, self.name) \ + "to retrieve calendar data." sensorAlert.optionalData = { "message": msg, "calendar": self.name, "type": "timeout" } sensorAlert.changeState = True sensorAlert.hasLatestData = False sensorAlert.dataType = SensorDataType.NONE self.state = self.triggerState self.inFailedState = True # If we are in a failed retrieval state and we could retrieve # calendar data again trigger a sensor alert for "normal". elif (self.inFailedState and self.failedCounter <= self.maxFailedAttempts): logging.warning( "[%s] Fetching calendar succeeded after " % self.fileName + "multiple failed attempts. Triggering sensor alert.") sensorAlert = SensorAlert() sensorAlert.clientSensorId = self.id sensorAlert.state = 0 sensorAlert.hasOptionalData = True msg = "Calendar data for '%s' retrievable again." \ % self.name sensorAlert.optionalData = { "message": msg, "calendar": self.name, "type": "timeout" } sensorAlert.changeState = True sensorAlert.hasLatestData = False sensorAlert.dataType = SensorDataType.NONE self.state = 1 - self.triggerState self.inFailedState = False # If we have sensor alerts of reminder waiting # return the oldest of them. elif not self.reminderAlertQueue.empty(): try: sensorAlert = self.reminderAlertQueue.get(True, 2) except: pass return sensorAlert
def _processEventAlarms(self, event, eventDatetime): # Create current datetime object. currentUTCTime = time.time() currentDatetime = datetime.datetime.utcfromtimestamp(currentUTCTime) currentDatetime = currentDatetime.replace(tzinfo=pytz.UTC) for alarm in event.walk("VALARM"): if alarm.has_key("TRIGGER"): # Get time delta when reminder is triggered. trigger = alarm.get("TRIGGER") # Get time when reminder is triggered. # When trigger time is a delta, then calculate the actual time. if type(trigger.dt) == datetime.timedelta: triggerDatetime = eventDatetime + trigger.dt # When trigger time is an actual time, use this. elif type(trigger.dt) == datetime.datetime: triggerDatetime = trigger.dt # Thunderbird uses the same timezone as the event when # no is given. if triggerDatetime.tzinfo is None: triggerDatetime = triggerDatetime.replace( tzinfo=eventDatetime.tzinfo) # When trigger time is only a date, start at midnight. elif type(trigger.dt) == datetime.date: triggerUTCTime = calendar.timegm(trigger.dt.timetuple()) triggerDatetime = datetime.datetime.utcfromtimestamp( triggerUTCTime) triggerDatetime = triggerDatetime.replace(tzinfo=pytz.UTC) else: logging.error("[%s] Error: Do not know how to handle " % self.fileName + "trigger type '%s'." % trigger.dt.__class__) continue # Uid of event is needed. uid = event.get("UID") # Get time when event starts. dtstart = event.get("DTSTART") # Check if we already triggered an alarm for the event # with this uid and the given alarm trigger time. if (uid, triggerDatetime) in self.alreadyTriggered: break # Check if the alarm trigger time lies in the past but not # more than 1 day. if ((currentDatetime - self.timedelta1day) <= triggerDatetime <= currentDatetime): title = event.get("SUMMARY") # Get description if event has one. evDescription = "" if event.has_key("DESCRIPTION"): evDescription = event.get("DESCRIPTION") # Get location if event has one. location = "" if event.has_key("LOCATION"): location = event.get("LOCATION") # Create the utc unix timestamp for the start of the event. unixStart = datetime.datetime.utcfromtimestamp(0) unixStart = unixStart.replace(tzinfo=pytz.UTC) utcDtstart = int( (eventDatetime - unixStart).total_seconds()) # Create the utc unix timestamp for the reminder trigger. utcTrigger = int( (triggerDatetime - unixStart).total_seconds()) eventDateStr = time.strftime("%D %H:%M:%S", time.localtime(utcDtstart)) msg = "Reminder for event '%s' at %s" \ % (title, eventDateStr) # Create sensor alert. sensorAlert = SensorAlert() sensorAlert.clientSensorId = self.id sensorAlert.state = 1 sensorAlert.hasOptionalData = True sensorAlert.optionalData = { "message": msg, "calendar": self.name, "type": "reminder", "title": title, "description": evDescription, "location": location, "trigger": utcTrigger, "start": utcDtstart } sensorAlert.changeState = False sensorAlert.hasLatestData = False sensorAlert.dataType = SensorDataType.NONE self.reminderAlertQueue.put(sensorAlert) # Store the event uid and the alarm trigger time # as already triggered. self.alreadyTriggered.add((uid, triggerDatetime))