Exemple #1
0
    def receivedStateChange(self, serverTime, sensorId, state, dataType,
        sensorData):

        self.serverTime = serverTime

        # search sensor in list of known sensors
        # => if not known return failure
        sensor = None
        for tempSensor in self.sensors:
            if tempSensor.sensorId == sensorId:
                sensor = tempSensor
                break
        if not sensor:
            logging.error("[%s]: Sensor for state change " % self.fileName
                + "not known.")

            return False

        # when events are activated
        # => create state change event
        if self.eventsLifeSpan != 0:
            utcTimestamp = int(time.time())
            tempStateEvent = EventStateChange(utcTimestamp)
            tempStateEvent.state = state
            tempStateEvent.description = sensor.description
            tempStateEvent.dataType = sensor.dataType
            tempStateEvent.data = sensor.data

            for node in self.nodes:
                if node.nodeId == sensor.nodeId:
                    tempStateEvent.hostname = node.hostname
                    self.events.append(tempStateEvent)
                    break
            if not tempStateEvent.hostname:
                logging.error("[%s]: Unable to find corresponding " 
                    % self.fileName
                    + "node to sensor for state change event.")

        # Change sensor state.
        sensor.state = state
        sensor.lastStateUpdated = serverTime

        if dataType == sensor.dataType:
            sensor.data = sensorData
        else:
            logging.error("[%s]: Sensor data type different. "
                % self.fileName
                + "Skipping data assignment.")

        return True
    def receivedSensorAlert(self, serverTime, sensorAlert):

        self.serverTime = serverTime
        timeReceived = int(time.time())
        self.sensorAlerts.append(sensorAlert)

        # when events are activated
        # => create events
        if self.eventsLifeSpan != 0:

            # create sensor alert event
            tempEvent = EventSensorAlert(timeReceived)
            tempEvent.description = sensorAlert.description
            tempEvent.state = sensorAlert.state
            tempEvent.dataType = sensorAlert.dataType
            tempEvent.sensorData = sensorAlert.sensorData

            tempEvent.alertLevels = list(sensorAlert.alertLevels)
            self.events.append(tempEvent)

            # When rules are not activated and change state flag is set.
            # => Create state change event.
            if not sensorAlert.rulesActivated and sensorAlert.changeState:
                tempStateEvent = EventStateChange(timeReceived)
                tempStateEvent.state = sensorAlert.state

                # Only store data for this state change event if the sensor
                # alert carries the latest data of the sensor.
                if sensorAlert.hasLatestData:
                    tempStateEvent.dataType = sensorAlert.dataType
                    tempStateEvent.data = sensorAlert.sensorData
                else:
                    tempStateEvent.dataType = SensorDataType.NONE

                triggeredSensor = None
                for sensor in self.sensors:
                    if sensor.sensorId == sensorAlert.sensorId:
                        tempStateEvent.description = sensor.description
                        triggeredSensor = sensor
                        break
                if not triggeredSensor is None:
                    for node in self.nodes:
                        if node.nodeId == sensor.nodeId:
                            tempStateEvent.hostname = node.hostname
                            self.events.append(tempStateEvent)
                            break
                    if tempStateEvent.hostname is None:
                        logging.error("[%s]: Unable to find corresponding " %
                                      self.fileName +
                                      "node to sensor for state change event.")
                else:
                    logging.error(
                        "[%s]: Unable to find corresponding " % self.fileName +
                        "sensor to sensor alert for state change event.")

        # If rules are not activated (and therefore the sensor alert was
        # only triggered by one distinct sensor).
        # => Update information in sensor which triggered the sensor alert.
        if not sensorAlert.rulesActivated:
            found = False
            for sensor in self.sensors:
                if sensor.sensorId == sensorAlert.sensorId:
                    sensor.lastStateUpdated = serverTime

                    # Only update sensor state information if the flag
                    # was set in the received message.
                    if sensorAlert.changeState:
                        sensor.state = sensorAlert.state

                    # Only update sensor data information if the flag
                    # was set in the received message.
                    if sensorAlert.hasLatestData:
                        if sensorAlert.dataType == sensor.dataType:
                            sensor.data = sensorAlert.sensorData
                        else:
                            logging.error(
                                "[%s]: Sensor data type different. " %
                                self.fileName + "Skipping data assignment.")

                    found = True
                    break
            if not found:
                logging.error("[%s]: Sensor of sensor alert " % self.fileName +
                              "not known.")

                return False

        return True
Exemple #3
0
	def receivedSensorAlert(self, serverTime, sensorAlert):

		self.serverTime = serverTime
		timeReceived = int(time.time())
		self.sensorAlerts.append(sensorAlert)

		# when events are activated
		# => create events
		if self.eventsLifeSpan != 0:

			# create sensor alert event
			tempEvent = EventSensorAlert(timeReceived)
			tempEvent.description = sensorAlert.description
			tempEvent.state = sensorAlert.state
			tempEvent.dataType = sensorAlert.dataType
			tempEvent.sensorData = sensorAlert.sensorData

			tempEvent.alertLevels = list(sensorAlert.alertLevels)
			self.events.append(tempEvent)

			# When rules are not activated and change state flag is set.
			# => Create state change event.
			if not sensorAlert.rulesActivated and sensorAlert.changeState:
				tempStateEvent = EventStateChange(timeReceived)
				tempStateEvent.state = sensorAlert.state

				# Only store data for this state change event if the sensor
				# alert carries the latest data of the sensor.
				if sensorAlert.hasLatestData:
					tempStateEvent.dataType = sensorAlert.dataType
					tempStateEvent.data = sensorAlert.sensorData
				else:
					tempStateEvent.dataType = SensorDataType.NONE

				triggeredSensor = None
				for sensor in self.sensors:
					if sensor.sensorId == sensorAlert.sensorId:
						tempStateEvent.description = sensor.description
						triggeredSensor = sensor
						break
				if not triggeredSensor is None:
					for node in self.nodes:
						if node.nodeId == sensor.nodeId:
							tempStateEvent.hostname = node.hostname
							self.events.append(tempStateEvent)
							break
					if tempStateEvent.hostname is None:
						logging.error("[%s]: Unable to find corresponding " 
							% self.fileName
							+ "node to sensor for state change event.")
				else:
					logging.error("[%s]: Unable to find corresponding " 
						% self.fileName
						+ "sensor to sensor alert for state change event.")

		# If rules are not activated (and therefore the sensor alert was
		# only triggered by one distinct sensor).
		# => Update information in sensor which triggered the sensor alert.
		if not sensorAlert.rulesActivated:
			found = False
			for sensor in self.sensors:
				if sensor.sensorId == sensorAlert.sensorId:
					sensor.lastStateUpdated = serverTime

					# Only update sensor state information if the flag
					# was set in the received message.
					if sensorAlert.changeState:
						sensor.state = sensorAlert.state
					
					# Only update sensor data information if the flag
					# was set in the received message.
					if sensorAlert.hasLatestData:
						if sensorAlert.dataType == sensor.dataType:
							sensor.data = sensorAlert.sensorData
						else:
							logging.error("[%s]: Sensor data type different. "
								% self.fileName
								+ "Skipping data assignment.")

					found = True
					break
			if not found:
				logging.error("[%s]: Sensor of sensor alert " % self.fileName
					+ "not known.")

				return False

		return True