class SensorDataManager:
    #Default Constructor
    def __init__(self):
        self.actuator = ActuatorData()
        self.sensorAdaptor = MultiSensorAdaptor()
        self.config = ConfigUtil()
        self.config.loadConfig("../../../config/ConnectedDevicesConfig.props")
        self.threshold = float(self.config.getValue("device", "nominalTemp"))
        self.actuatorOP = MultiActuatorAdaptor.getInstance()

    def run(self):

        self.sensorAdaptor.getSensorData()

    #Method for evaluating the sensor values and create decision for actation
    def hadleSensorData(self, SensorData):
        self.actuator.max_value = SensorData.max_value
        self.actuator.min_value = SensorData.min_value
        self.actuator.readings_number = SensorData.readings_number
        self.actuator.value = SensorData.getCurrentValue()
        self.actuator.avgTemp = (SensorData.total_value /
                                 SensorData.readings_number)
        self.actuator.total_value = SensorData.total_value
        self.actuator.setName(SensorData.getName())
        self.actuatorOP.updateActuator(self.actuator)
        #print("Value check - " + str(self.actuator.value))
        self.sendNotification()
        return True

    #Triggering mail based on the command value. The mail contains the relevant information.
    def sendNotification(self):

        try:
            logging.info(self.actuator.getName() + "Temperature: \nTime: " +
                         str(self.actuator.timestamp) + " Value :" +
                         str(self.actuator.getValue()))
            mail = SmtpClientConnector()
            #Creating mail body text
            data = "Reading from:" + self.actuator.getName(
            ) + "\nTime: " + str(
                self.actuator.timestamp) + "\ncurrent : " + str(
                    self.actuator.getValue()) + "\nAverage :" + str(
                        self.actuator.getAverageValue()) + "\nSamples :" + str(
                            self.actuator.readings_number) + "\nMin: " + str(
                                self.actuator.min_value) + "\nMax :" + str(
                                    self.actuator.max_value)
            mail.publishMessage(
                "Humidity Alert Python -" + self.actuator.getName(), data)
            logging.info('\nMail sent')
            return True
        except Exception as e:
            logging.info(e)
            print(e)
            #If the mailing fails, the method returns false
            return False
예제 #2
0
class ActuatorDataTest(unittest.TestCase):
    """
	Use this to setup your tests. This is where you may want to load configuration
	information (if needed), initialize class-scoped variables, create class-scoped
	instances of complex objects, initialize any requisite connections, etc.
	"""
    def setUp(self):
        self.sensor = ActuatorData()
        self.sensor.addValue(11)
        self.sensor.setCommand("Temp")
        pass

    """
	Use this to tear down any allocated resources after your tests are complete. This
	is where you may want to release connections, zero out any long-term data, etc.
	"""

    def tearDown(self):
        pass

    """
	Place your comments describing the test here.
	"""

    def testSomething(self):
        pass

    '@Test'

    def testgetCurrent(self):
        self.assertGreaterEqual(
            self.sensor.getValue(), 0.0,
            'Current temperature value coming less than 0')
        pass

    '@Test'

    def testgetAvg(self):
        self.assertGreaterEqual(self.sensor.getAverageValue(), 0.0,
                                'Avg coming less than 0')
        pass

    '@Test'

    def testgetMin(self):
        self.assertGreaterEqual(self.sensor.getMivValue(), 0.0,
                                'Min coming less than 0')
        pass

    '@Test'

    def testgetMax(self):
        self.assertGreaterEqual(self.sensor.getMaxValue(), 0.0,
                                'Max coming less than 0')
        pass

    '@Test'

    def testgetCount(self):
        self.assertGreaterEqual(self.sensor.getCount(), 0.0,
                                'sample count coming less than 0')
        pass

    '@Test'

    def testgetCommand(self):
        self.assertEqual(self.sensor.getCommand(), "Temp",
                         "Returned value did not match")
        pass
class SensorDataManager:
    #Default Constructor
    def __init__(self):
        self.actuator = ActuatorData()
        self.sensorAdaptor = TempSensorAdaptor()
        self.config = ConfigUtil()
        self.config.loadConfig("../../../config/ConnectedDevicesConfig.props")
        self.threshold = float(self.config.getValue("device", "nominalTemp"))
        self.actuatorOP = TempActuatorAdaptor()

    def run(self):

        self.sensorAdaptor.getSensorData()

    #Method for evaluating the sensor values and create decision for actation
    def hadleSensorData(self, SensorData):
        self.actuator.max_value = SensorData.max_value
        self.actuator.min_value = SensorData.min_value
        self.actuator.readings_number = SensorData.readings_number
        self.actuator.value = SensorData.getCurrentValue()
        self.actuator.avgTemp = (SensorData.total_value /
                                 SensorData.readings_number)
        self.actuator.total_value = SensorData.total_value
        if (self.threshold > self.actuator.value):
            self.actuator.setCommand("Raise Temp")
        elif (self.threshold < self.actuator.value):
            self.actuator.setCommand("Decrease Temp")
        elif (self.threshold == self.actuator.value):
            self.actuator.setCommand("Temp Stable")
        self.actuatorOP.updateActuator(self.actuator)
        self.sendNotification()
        return True

    #Triggering mail based on the command value. The mail contains the relevant information.
    def sendNotification(self):

        try:
            if (self.actuator.getCommand() == "Raise Temp"):
                #logging.info('Current Temperature lower than threshold: ' + str(self.threshold) + ' Lowering Temp')
                logging.info(
                    "Current Temperature lower than threshold: \n \nTemperature: \nTime: "
                    + str(self.actuator.timestamp) + "\ncurrent : " +
                    str(self.actuator.getValue()) + "\nAverage :" +
                    str(self.actuator.getAverageValue()) + "\nSamples :" +
                    str(self.actuator.readings_number) + "\nMin: " +
                    str(self.actuator.min_value) + "\nMax :" +
                    str(self.actuator.max_value))
                mail = SmtpClientConnector()
                #Creating mail body text
                data = "Current Temperature lower than threshold: \n \nTemperature: \nTime: " + str(
                    self.actuator.timestamp) + "\ncurrent : " + str(
                        self.actuator.getValue()) + "\nAverage :" + str(
                            self.actuator.getAverageValue()
                        ) + "\nSamples :" + str(
                            self.actuator.readings_number) + "\nMin: " + str(
                                self.actuator.min_value) + "\nMax :" + str(
                                    self.actuator.max_value)
                mail.publishMessage("Temperature Alert Python", data)
                logging.info('\nMail sent')

            elif (self.actuator.getCommand() == "Decrease Temp"):
                logging.info(
                    "Current Temperature higher than threshold: \n \nTemperature: \nTime: "
                    + str(self.actuator.timestamp) + "\ncurrent : " +
                    str(self.actuator.getValue()) + "\nAverage :" +
                    str(self.actuator.getAverageValue()) + "\nSamples :" +
                    str(self.actuator.readings_number) + "\nMin: " +
                    str(self.actuator.min_value) + "\nMax :" +
                    str(self.actuator.max_value))
                mail = SmtpClientConnector()
                #Creating mail body text
                data = "Current Temperature higher than threshold: \n \nTemperature: \nTime: " + str(
                    self.actuator.timestamp) + "\ncurrent : " + str(
                        self.actuator.getValue()) + "\nAverage :" + str(
                            self.actuator.getAverageValue()
                        ) + "\nSamples :" + str(
                            self.actuator.readings_number) + "\nMin: " + str(
                                self.actuator.min_value) + "\nMax :" + str(
                                    self.actuator.max_value)
                mail.publishMessage("Temperature Alert Python", data)
                logging.info('\nMail sent')
            return True
        except Exception as e:
            logging.info(e)
            #If the mailing fails, the method returns false
            return False