Esempio n. 1
0
class TempActuatorEmulator():
    actuatorData = None
    senseHatLedActivator = None
    simpleLedActivator = None

    #Constructor
    def __init__(self):
        self.actuatorData = ActuatorData()
        self.senseHatLedActivator = SenseHatLedActivator()
        self.simpleLedActivator = SimpleLedActivator()

    #generate a message and run the activator
    def processMessage(self, ActuatorData):
        #print('processMessage...')
        self.actuatorData.updateData(ActuatorData)
        self.simpleLedActivator.setEnableLedFlag(True)
        if self.actuatorData.getCommand() == 0:
            #print('create msg-------')
            msg = "Temperature is " + str(
                abs(self.actuatorData.getValue())
            ) + " degree lower than nominal temperature, open the cool function"
        if self.actuatorData.getCommand() == 1:
            msg = "Temperature is " + str(
                self.actuatorData.getValue()
            ) + " degree higher than nominal temperature, open the heat function"
        self.senseHatLedActivator.setEnableLedFlag(True)
        self.senseHatLedActivator.setDisplayMessage(msg)
        #print('before run')
        self.senseHatLedActivator.run()
Esempio n. 2
0
class TempActuatorEmulator():
    actuatorData = None
    senseHatLedActivator = None
    simpleLedActivator = None

    def __init__(self):
        '''
        Constructor
        '''
        self.actuatorData = ActuatorData()
        self.senseHatLedActivator = SenseHatLedActivator()
        self.simpleLedActivator = SimpleLedActivator()

    def processMessage(self, ActuatorData):
        self.actuatorData.updateData(ActuatorData)
        self.simpleLedActivator.setEnableLedFlag(True)
        if self.actuatorData.getCommand() == 0:
            message = "Temperature need to raise " + str(
                abs(self.actuatorData.getValue()))
        if self.actuatorData.getCommand() == 1:
            message = "Temperature need to lower " + str(
                self.actuatorData.getValue())
        self.senseHatLedActivator.setEnableLedFlag(True)
        self.senseHatLedActivator.setDisplayMessage(message)
        self.senseHatLedActivator.run()
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
Esempio n. 4
0
 def updateActuator(self,ActuatorData):
     #print("Actuator Name "+ str(ActuatorData.getValue() ))
     if(ActuatorData.getName()=="HumidityI2C"):
         self.Humidityi2cdata = str(ActuatorData.getValue())
     elif(ActuatorData.getName()=="HumiditySenseHat"):
         self.HumiditySenseHat = str(ActuatorData.getValue())
         #print("Actuator Name "+ str(ActuatorData.getValue()))
     message = "Temp -> I2C =" + self.Humidityi2cdata + " SenseHat ="+ self.HumiditySenseHat
     logging.info(message)
     #print("msg : " +message)
     self.hat.updateLed(message)
     return True
Esempio n. 5
0
 def toJsonFromActuatorData(self,
                            actuatorData: ActuatorData.ActuatorData) -> str:
     '''
     Convert from ActuatorData instance to JSON
     '''
     #Converting the actuatorData to a dictionary
     jsonData = {
         "command": actuatorData.getCommand(),
         "name": actuatorData.getName(),
         "value": actuatorData.getValue()
     }
     #dumping the json data and returning it
     jsonStr = json.dumps(jsonData)
     #Logging and writing to file
     self.writeActuatorDataToFile("Created actuatorData JSON" + jsonStr)
     return jsonStr
Esempio n. 6
0
 def updateActuator(ActuatorData):
     hat = SenseHatLedActivator() 
     message ="Current = " + str(ActuatorData.getValue()) + " " +str(ActuatorData.getCommand()) +" <3"
     hat.updateLed(message)
     return True
Esempio n. 7
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):

        #instantiate ActuatorData
        self.actuatorData = ActuatorData()

        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):

        #set the reference to the variables to none to release any resources
        self.actuatorData = None

        pass

    """
	Tests the getCommand() method of ActuatorData module. Checks whether the command is updating appropriately, and doesn't break if the command is set to None
	"""

    def testGetCommand(self):

        #test the command when nothing has been set
        self.assertEqual('Not set', self.actuatorData.getCommand(),
                         "Command was not set")

        #test the command when set to 'INCREASE TEMP'
        self.actuatorData.setCommand('INCREASE TEMP')
        self.assertEqual('INCREASE TEMP', self.actuatorData.getCommand(),
                         "Command was set to INCREASE TEMP")

        #test the command is set to 'Not Set' when set to None
        self.actuatorData.setCommand(None)
        self.assertEqual('Not set', self.actuatorData.getCommand(),
                         "Command was not set")

        pass

    """
	Tests the setCommand() method of ActuatorData module. Checks whether the command is updating appropriately, and doesn't break if the name is set to None
	"""

    def testSetCommand(self):

        #test the command when set to None
        self.actuatorData.setCommand(None)
        self.assertEqual('Not set', self.actuatorData.getCommand(),
                         "Command was not set")

        #test the command when set to 'INCREASE TEMP'
        self.actuatorData.setCommand('INCREASE TEMP')
        self.assertEqual('INCREASE TEMP', self.actuatorData.getCommand(),
                         "Command was set to INCREASE TEMP")

        pass

    """
	Tests the getValue() method of ActuatorData module. Checks whether the value is updating appropriately, and doesn't break if the name is set to None
	"""

    def testGetValue(self):

        #test the name when nothing has been set
        self.assertEqual('Not set', self.actuatorData.getValue(),
                         "Name was not set")

        #test the name when set to None
        self.actuatorData.setValue(None)
        self.assertEqual('Not set', self.actuatorData.getValue(),
                         "Name was not set")

        #test the name when set to redArrowInc
        self.actuatorData.setValue('My Pixel')
        self.assertEqual('My Pixel', self.actuatorData.getValue(),
                         "Name was set to My Pixel")

        pass

    """
	Tests the setValue() method of ActuatorData module. Checks whether the value is updating appropriately, and doesn't break if the name is set to None
	"""

    def testSetValue(self):

        #test the name when nothing has been set
        self.assertEqual('Not set', self.actuatorData.getValue(),
                         "Name was not set")

        #test the name when set to None
        self.actuatorData.setValue(None)
        self.assertEqual('Not set', self.actuatorData.getValue(),
                         "Name was not set")

        #test the name when set to redArrowInc
        self.actuatorData.setValue('My Pixel')
        self.assertEqual('My Pixel', self.actuatorData.getValue(),
                         "Name was set to My Pixel")

        pass

    """
	Tests the getName() method of ActuatorData module. Checks whether the name is updating appropriately, and doesn't break if the name is set to None
	"""

    def testGetName(self):

        #test the name when nothing has been set
        self.assertEqual('Not set', self.actuatorData.getName(),
                         "Name was not set")

        #test the name when set to None
        self.actuatorData.setName(None)
        self.assertEqual('Not set', self.actuatorData.getName(),
                         "Name was not set")

        #test the name when set to 'Temperature'
        self.actuatorData.setName('Temperature')
        self.assertEqual('Temperature', self.actuatorData.getName(),
                         "Name was set to Temperature")

    """
	Tests the setName() method of ActuatorData module. Checks whether the name is updating appropriately, and doesn't break if the name is set to None
	"""

    def testSetName(self):

        #test the name when set to None
        self.actuatorData.setName(None)
        self.assertEqual('Not set', self.actuatorData.getName(),
                         "Name was not set")

        #test the command when set to 'INCREASE TEMP'
        self.actuatorData.setName('Temperature')
        self.assertEqual('Temperature', self.actuatorData.getName(),
                         "Name was set to Temperature")

        pass
Esempio n. 8
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