def _sensorAlertsOffHandler(self, incomingMessage): logging.debug("[%s]: Received sensor alerts off." % self.fileName) # sending sensor alerts off response logging.debug("[%s]: Sending sensor alerts off " % self.fileName + "response message.") try: payload = {"type": "response", "result": "ok"} utcTimestamp = int(time.time()) message = {"clientTime": utcTimestamp, "message": "sensoralertsoff", "payload": payload} self.client.send(json.dumps(message)) except Exception as e: logging.exception("[%s]: Sending sensor alerts " % self.fileName + "off response failed.") return False # stop all alerts for alert in self.alerts: # stop alert in an own thread to not block this one alertStopProcess = AsynchronousAlertExecuter(alert) # set thread to daemon # => threads terminates when main thread terminates alertStopProcess.daemon = True alertStopProcess.stopAlert = True alertStopProcess.start() return True
def _sensorAlertsOffHandler(self): logging.debug("[%s]: Received sensor alerts off." % self.fileName) # acknowledge sensor alerts off logging.debug("[%s]: Sending SENSORALERTSOFF OK message." % self.fileName) try: self.client.send("SENSORALERTSOFF OK\r\n") except Exception as e: logging.exception("[%s]: Sending sensor alerts " % self.fileName + "off acknowledgement failed.") return False # stop all alerts for alert in self.alerts: # stop alert in an own thread to not block this one alertStopProcess = AsynchronousAlertExecuter(alert) # set thread to daemon # => threads terminates when main thread terminates alertStopProcess.daemon = True alertStopProcess.stopAlert = True alertStopProcess.start() return True
def _sensorAlertsOffHandler(self, incomingMessage): logging.debug("[%s]: Received sensor alerts off." % self.fileName) # sending sensor alerts off response logging.debug("[%s]: Sending sensor alerts off " % self.fileName + "response message.") try: payload = {"type": "response", "result": "ok"} message = {"clientTime": int(time.time()), "message": "sensoralertsoff", "payload": payload} self.client.send(json.dumps(message)) except Exception as e: logging.exception("[%s]: Sending sensor alerts " % self.fileName + "off response failed.") return False # stop all alerts for alert in self.alerts: # stop alert in an own thread to not block this one alertStopProcess = AsynchronousAlertExecuter(alert) # set thread to daemon # => threads terminates when main thread terminates alertStopProcess.daemon = True alertStopProcess.stopAlert = True alertStopProcess.start() return True
def _sensorAlertHandler(self, data): logging.debug("[%s]: Received sensor alert." % self.fileName) # extract data from sensor alert message try: splittedData = data.split() sensorId = int(splittedData[1]) state = int(splittedData[2]) alertLevel = int(splittedData[3]) except Exception as e: logging.exception("[%s]: Receiving sensor alert " % self.fileName + "failed.") return False # acknowledge sensor alert logging.debug("[%s]: Sending SENSORALERT OK message." % self.fileName) try: self.client.send("SENSORALERT OK\r\n") except Exception as e: logging.exception("[%s]: Sending sensor alert " % self.fileName + "acknowledgement failed.") return False # trigger all alerts that have the same alert level for alert in self.alerts: if alertLevel in alert.alertLevels: # trigger alert in an own thread to not block this one alertTriggerProcess = AsynchronousAlertExecuter(alert) # set thread to daemon # => threads terminates when main thread terminates alertTriggerProcess.daemon = True alertTriggerProcess.triggerAlert = True alertTriggerProcess.start() return True
def _sensorAlertHandler(self, incomingMessage): logging.info("[%s]: Received sensor alert." % self.fileName) # extract sensor alert values sensorAlert = SensorAlert() sensorAlert.timeReceived = int(time.time()) try: if not self._checkMsgServerTime( incomingMessage["serverTime"], incomingMessage["message"]): logging.error("[%s]: Received serverTime invalid." % self.fileName) return False if not self._checkMsgAlertLevels( incomingMessage["payload"]["alertLevels"], incomingMessage["message"]): logging.error("[%s]: Received alertLevels invalid." % self.fileName) return False if not self._checkMsgDescription( incomingMessage["payload"]["description"], incomingMessage["message"]): logging.error("[%s]: Received description invalid." % self.fileName) return False if not self._checkMsgRulesActivated( incomingMessage["payload"]["rulesActivated"], incomingMessage["message"]): logging.error("[%s]: Received rulesActivated invalid." % self.fileName) return False if not self._checkMsgSensorId( incomingMessage["payload"]["sensorId"], incomingMessage["message"]): logging.error("[%s]: Received sensorId invalid." % self.fileName) return False if not self._checkMsgState( incomingMessage["payload"]["state"], incomingMessage["message"]): logging.error("[%s]: Received state invalid." % self.fileName) return False if not self._checkMsgHasOptionalData( incomingMessage["payload"]["hasOptionalData"], incomingMessage["message"]): logging.error("[%s]: Received hasOptionalData invalid." % self.fileName) return False if incomingMessage["payload"]["hasOptionalData"]: if not self._checkMsgOptionalData( incomingMessage["payload"]["optionalData"], incomingMessage["message"]): logging.error("[%s]: Received optionalData invalid." % self.fileName) return False if not self._checkMsgSensorDataType( incomingMessage["payload"]["dataType"], incomingMessage["message"]): logging.error("[%s]: Received dataType invalid." % self.fileName) return False if incomingMessage["payload"]["dataType"] != SensorDataType.NONE: if not self._checkMsgSensorData( incomingMessage["payload"]["data"], incomingMessage["payload"]["dataType"], incomingMessage["message"]): logging.error("[%s]: Received data invalid." % self.fileName) return False if not self._checkMsgHasLatestData( incomingMessage["payload"]["hasLatestData"], incomingMessage["message"]): logging.error("[%s]: Received hasLatestData invalid." % self.fileName) return False if not self._checkMsgChangeState( incomingMessage["payload"]["changeState"], incomingMessage["message"]): logging.error("[%s]: Received changeState invalid." % self.fileName) return False sensorAlert.sensorId = incomingMessage["payload"]["sensorId"] sensorAlert.alertLevels = incomingMessage["payload"]["alertLevels"] sensorAlert.state = incomingMessage["payload"]["state"] sensorAlert.description = incomingMessage["payload"]["description"] sensorAlert.changeState = incomingMessage["payload"]["changeState"] sensorAlert.rulesActivated = \ incomingMessage["payload"]["rulesActivated"] # parse received data (if data transfer is activated) sensorAlert.optionalData = None sensorAlert.hasOptionalData = \ incomingMessage["payload"]["hasOptionalData"] if sensorAlert.hasOptionalData: sensorAlert.optionalData = incomingMessage[ "payload"]["optionalData"] sensorAlert.changeState = incomingMessage["payload"]["changeState"] sensorAlert.hasLatestData = \ incomingMessage["payload"]["hasLatestData"] sensorAlert.dataType = incomingMessage["payload"]["dataType"] sensorAlert.sensorData = None if sensorAlert.dataType != SensorDataType.NONE: sensorAlert.sensorData = incomingMessage["payload"]["data"] except Exception as e: logging.exception("[%s]: Received sensor alert " % self.fileName + "invalid.") # send error message back try: utcTimestamp = int(time.time()) message = {"clientTime": utcTimestamp, "message": incomingMessage["message"], "error": "received sensor alert invalid"} self.client.send(json.dumps(message)) except Exception as e: pass return False # sending sensor alert response logging.debug("[%s]: Sending sensor alert " % self.fileName + "response message.") try: payload = {"type": "response", "result": "ok"} utcTimestamp = int(time.time()) message = {"clientTime": utcTimestamp, "message": "sensoralert", "payload": payload} self.client.send(json.dumps(message)) except Exception as e: logging.exception("[%s]: Sending sensor alert " % self.fileName + "response failed.") return False # trigger all alerts that have the same alert level atLeastOnceTriggered = False for alert in self.alerts: for alertLevel in sensorAlert.alertLevels: if alertLevel in alert.alertLevels: atLeastOnceTriggered = True # trigger alert in an own thread to not block this one alertTriggerProcess = AsynchronousAlertExecuter(alert) alertTriggerProcess.sensorAlert = sensorAlert # set thread to daemon # => threads terminates when main thread terminates alertTriggerProcess.daemon = True alertTriggerProcess.triggerAlert = True alertTriggerProcess.start() break # Write to log file if no alert was triggered for received sensorAlert. if not atLeastOnceTriggered: alertLevelsStr = ", ".join(map(str, sensorAlert.alertLevels)) logging.info("[%s]: No alert triggered for alertLevels: %s." % (self.fileName, alertLevelsStr)) return True
def _sensorAlertHandler(self, incomingMessage): logging.debug("[%s]: Received sensor alert." % self.fileName) # extract sensor alert values try: alertLevels = incomingMessage["payload"]["alertLevels"] # check if alertLevels is a list if not isinstance(alertLevels, list): # send error message back try: message = {"clientTime": int(time.time()), "message": message["message"], "error": "alertLevels not of type list"} self.client.send(json.dumps(message)) except Exception as e: pass return False # check if all elements of the alertLevels list # are of type int if not all(isinstance(item, int) for item in alertLevels): # send error message back try: message = {"clientTime": int(time.time()), "message": message["message"], "error": "alertLevels items not of type int"} self.client.send(json.dumps(message)) except Exception as e: pass return False description = str(incomingMessage["payload"]["description"]) # parse received data (if data transfer is activated) data = None dataTransfer = bool(incomingMessage["payload"]["dataTransfer"]) if dataTransfer: data = incomingMessage["payload"]["data"] # check if data is of type dict # => if not ignore it if not isinstance(data, dict): logging.warning("[%s]: Received data in sensor alert " % self.fileName + "not valid. Ignoring it.") data = None dataTransfer = False except Exception as e: logging.exception("[%s]: Received sensor alert " % self.fileName + "invalid.") # send error message back try: message = {"clientTime": int(time.time()), "message": incomingMessage["message"], "error": "received sensor alert invalid"} self.client.send(json.dumps(message)) except Exception as e: pass return False # sending sensor alert response logging.debug("[%s]: Sending sensor alert " % self.fileName + "response message.") try: payload = {"type": "response", "result": "ok"} message = {"clientTime": int(time.time()), "message": "sensoralert", "payload": payload} self.client.send(json.dumps(message)) except Exception as e: logging.exception("[%s]: Sending sensor alert " % self.fileName + "response failed.") return False # trigger all alerts that have the same alert level for alert in self.alerts: for alertLevel in alertLevels: if alertLevel in alert.alertLevels: # trigger alert in an own thread to not block this one alertTriggerProcess = AsynchronousAlertExecuter(alert) alertTriggerProcess.sensorDescription = description alertTriggerProcess.dataTransfer = dataTransfer alertTriggerProcess.data = data # set thread to daemon # => threads terminates when main thread terminates alertTriggerProcess.daemon = True alertTriggerProcess.triggerAlert = True alertTriggerProcess.start() return True
def _sensorAlertHandler(self, incomingMessage): logging.debug("[%s]: Received sensor alert." % self.fileName) # extract sensor alert values try: alertLevels = incomingMessage["payload"]["alertLevels"] # check if alertLevels is a list if not isinstance(alertLevels, list): # send error message back try: message = { "clientTime": int(time.time()), "message": message["message"], "error": "alertLevels not of type list" } self.client.send(json.dumps(message)) except Exception as e: pass return False # check if all elements of the alertLevels list # are of type int if not all(isinstance(item, int) for item in alertLevels): # send error message back try: message = { "clientTime": int(time.time()), "message": message["message"], "error": "alertLevels items not of type int" } self.client.send(json.dumps(message)) except Exception as e: pass return False state = int(incomingMessage["payload"]["state"]) description = str(incomingMessage["payload"]["description"]) # parse received data (if data transfer is activated) data = None dataTransfer = bool(incomingMessage["payload"]["dataTransfer"]) if dataTransfer: data = incomingMessage["payload"]["data"] # check if data is of type dict # => if not ignore it if not isinstance(data, dict): logging.warning("[%s]: Received data in sensor alert " % self.fileName + "not valid. Ignoring it.") data = None dataTransfer = False except Exception as e: logging.exception("[%s]: Received sensor alert " % self.fileName + "invalid.") # send error message back try: message = { "clientTime": int(time.time()), "message": incomingMessage["message"], "error": "received sensor alert invalid" } self.client.send(json.dumps(message)) except Exception as e: pass return False # sending sensor alert response logging.debug("[%s]: Sending sensor alert " % self.fileName + "response message.") try: payload = {"type": "response", "result": "ok"} message = { "clientTime": int(time.time()), "message": "sensoralert", "payload": payload } self.client.send(json.dumps(message)) except Exception as e: logging.exception("[%s]: Sending sensor alert " % self.fileName + "response failed.") return False # trigger all alerts that have the same alert level for alert in self.alerts: for alertLevel in alertLevels: if alertLevel in alert.alertLevels: # trigger alert in an own thread to not block this one alertTriggerProcess = AsynchronousAlertExecuter(alert) alertTriggerProcess.sensorDescription = description alertTriggerProcess.state = state alertTriggerProcess.dataTransfer = dataTransfer alertTriggerProcess.data = data # set thread to daemon # => threads terminates when main thread terminates alertTriggerProcess.daemon = True alertTriggerProcess.triggerAlert = True alertTriggerProcess.start() return True