Exemplo n.º 1
0
class CoapServerConnector(CoAP):
    def __init__(self):
        self.path = ""
        self.config = ConfigUtil(
            '../../../config/ConnectedDevicesConfig.props')
        self.host = self.config.getProperty(ConfigConst.COAP_DEVICE_SECTION,
                                            ConfigConst.HOST_KEY)
        self.port = int(
            self.config.getProperty(ConfigConst.COAP_DEVICE_SECTION,
                                    ConfigConst.PORT_KEY))
        CoAP.__init__(self, (self.host, self.port))
Exemplo n.º 2
0
class SmtpClientConnector(object):
    '''
    configure the remote server to send alert email;
        whenever an email is sent, log out the success message
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.config = ConfigUtil(
            '../../../config/ConnectedDevicesConfig.props')
        #         self.config = ConfigUtil('/Users/apple/workspace/iot-device/config/ConnectedDevicesConfig.props')
        self.config.loadConfig()
        logging.info('Configuration data...\n' + str(self.config))

    def publishMessage(self, topic, data):
        host = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION,
                                       ConfigConst.HOST_KEY)
        port = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION,
                                       ConfigConst.PORT_KEY)
        fromAddr = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION,
                                           ConfigConst.FROM_ADDRESS_KEY)
        toAddr = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION,
                                         ConfigConst.TO_ADDRESS_KEY)
        authToken = self.config.getProperty(ConfigConst.SMTP_CLOUD_SECTION,
                                            ConfigConst.USER_AUTH_TOKEN_KEY)
        #         nominalTemp = self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE, 'nominalTemp')
        #         if(SensorData.getValue()):
        #             actuate = ActuatorData()

        msg = MIMEMultipart()
        msg['From'] = fromAddr
        msg['To'] = toAddr
        msg['Subject'] = topic
        msgBody = str(data)

        msg.attach(MIMEText(msgBody))

        msgText = msg.as_string()

        smtpServer = smtplib.SMTP_SSL(host, port)
        smtpServer.ehlo()
        smtpServer.login(fromAddr, authToken)
        smtpServer.sendmail(fromAddr, toAddr, msgText)
        smtpServer.close()
        logging.info('Sending Successful!\n\n')
class TempSensorAdaptor(object):
    '''
    classdocs
    '''
    rateInSec = 10.0
    alertDiff = 10.0
    curDegree = 0.0
    nominalTemp = 10.0
    sense=None

    '''
    Initial all the variable I need
    '''
    def __init__(self, a, r):
        '''
        Constructor
        '''
        self.sense = SenseHat()
        self.tempActuatorEmulator = TempActuatorEmulator()
        self.alertDiff = a
        self.rateInSec = r
        self.sensorData = SensorData
        self.connector = SmtpClientConnector()
        self.config = ConfigUtil('../../../config/ConnectedDevicesConfig.props')
        self.config.loadConfig()
        self.nominalTemp = self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE, 'nominalTemp')
#         self.nominalTemp = self.config.getProperty()
        _thread.start_new_thread(self.run())
    
    '''
    Connect to senseHat and obtain the environment temperature.
    '''
    def readTempFromSH(self):
        self.sense.clear()
        self.curDegree = self.sense.get_temperature()
    
    '''
    Store the temperature into SenseData.
    '''
    def addTempToSensorData(self):
        self.sensorData.addValue(self.sensorData, self.curDegree)
        now = datetime.datetime.now()
        print(str(now) + "\t" + str(self.curDegree))
        print(str(now) + "\t" + str(self.sensorData.getAvgValue(self.sensorData)))
        print("\n")
    
    '''
    Determine whether or not the particular temperature is higher or lower than the average temperature and then alert the user by sending email.
    '''
    def alert(self):
        if (abs(self.sensorData.getValue(self.sensorData) - self.sensorData.getAvgValue(self.sensorData)) >= self.alertDiff):
            # Start to send email
            logging.info('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggering alert...')
            print("Starting sending email...")
            output = self.sensorData.__str__(self.sensorData)
            self.connector.publishMessage("Excessive Temp", output)
    
    '''
    Determine whether or not the new temperature value is higher or lower than the nominal temperature which is set in ConnectedDevicesConfig
    '''
    def adjust(self):
        if self.sensorData.getValue(self.sensorData) < float(self.nominalTemp) or self.sensorData.getValue(self.sensorData) > float(self.nominalTemp):
            self.actuatorData = ActuatorData()
            self.actuatorData.updateData(1, 1, 0, "adjust", self.sensorData.getValue(self.sensorData))
            self.tempActuatorEmulator.processMessage(self.actuatorData)
    
    '''
    Start a new thread to do the task.
    '''
    def run(self):
        while True:
            self.readTempFromSH()
            self.addTempToSensorData()
            self.alert()
            self.adjust()
            sleep(self.rateInSec)
Exemplo n.º 4
0
'''
Created on Mar 19, 2019

@author: GANESHRAM KANAKSABAI
'''

from labs.module07.CoapServerConnector import CoapServerConnector  # importing CoapServerConnector

from labbenchstudios.common.ConfigUtil import ConfigUtil  # importing ConfigUtil
from labbenchstudios.common import ConfigConst  # importing ConfigConst

config = ConfigUtil('../data/ConnectedDevicesConfig.props')
config.loadConfig()

host = config.getProperty(ConfigConst.COAP_DEVICE_SECTION,
                          ConfigConst.HOST_KEY)
port = int(
    config.getProperty(ConfigConst.COAP_DEVICE_SECTION, ConfigConst.PORT_KEY))

server = CoapServerConnector(host, port, config)

server.start()  # starting the action of the server