Ejemplo n.º 1
0
    def setUp(self):

        #initialize the modules in Module 6
        self.deviceDataManager = DeviceDataManager()
        self.mqttClientConnector = MqttClientConnector()
        self.multiSensorAdaptor = MultiSensorAdaptor()
        self.tempSensorAdaptorTask = TempSensorAdaptorTask()
Ejemplo n.º 2
0
class MultiSensorAdaptor(threading.Thread):
    '''
    * Constructor function which sets MultiSensorAdaptor as a daemon thread
    '''
    def __init__(self):
        threading.Thread.__init__(self)
        MultiSensorAdaptor.setDaemon(self, True)
        logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
                            level=logging.INFO,
                            datefmt='%Y-%m-%d %H:%M:%S')
        logging.info("started logging")

    '''
    * Runnable function which starts two seperate threads for polling humidity data via SenseHat api & i2c bus
    '''

    def run(self):
        self.temp_sensor_object = TempSensorAdaptorTask(20)
        self.temp_sensor_object.start()  # Starting Threaded Class Object
        time.sleep(100)

    '''
    * Standard getter function for temp_sensor_object
      Output: temp_sensor_object(SensorData)   
    '''

    def getSensorobj(self):
        return self.temp_sensor_object

    '''
    * Standard getter function for humi_sensori2c_object
      Output: humi_sensori2c_object(SensorData)
    '''

    def geti2cobject(self):
        return self.humi_sensori2c_object

    '''
    * Standard getter function for humi_sensorAPI_object
      Output: humi_sensorAPI_object(SensorData)  
    '''

    def getAPIobject(self):
        return self.humi_sensorAPI_object
class MqttClientConnector(object):
    '''
    classdocs
    '''
    tempSensor = TempSensorAdaptorTask()
    sensorData = SensorData()
    dataUtil = DataUtil()
    client = mqtt.Client()

    def __init__(self):
        '''
        Constructor
        '''

    '''
    Callback function
    '''

    def _on_connect(self, client, userdata, flags, rc):
        print("Connected with result code " + str(rc))

    def _on_disconnect(self, client, userdata, rc):
        print("Dissconnected with result code: " + str(rc))

    def _on_message(self, client, userdata, msg):
        print(msg.topic + "" + str(msg.payload))

    def getSensorData(self):
        temp = self.tempSensor.getTemperature()
        self.sensorData.addValue(temp)
        jsonData = self.dataUtil.toJsonFromSensorData(self.sensorData)
        return jsonData

    '''
    Connect to remote mqtt broker:mqtt.eclipse.org
    publish msg
    '''

    def run(self):

        self.client.on_connect = self._on_connect
        self.client.on_message = self._on_message
        self.client.on_disconnect = self._on_disconnect
        self.client.connect("mqtt.eclipse.org", 1883, 60)

        jsonData = self.getSensorData()
        logging.info("SensorData befor publishing: " + jsonData)
        self.client.loop_start()

        self.client.publish("kai_test1", jsonData, 2, True)
        self.client.loop_stop()
        self.client.disconnect()
Ejemplo n.º 4
0
 def run(self):
     self.temp_sensor_object = TempSensorAdaptorTask(20)
     self.temp_sensor_object.start()  # Starting Threaded Class Object
     time.sleep(100)
Ejemplo n.º 5
0
 def adaptor(self):
     while True:
         running = TempSensorAdaptorTask()
         running.run()
Ejemplo n.º 6
0
class MultiSensorAdaptor(threading.Thread):
    '''
    classdocs
    '''
    rateInSec = 10
    enableTempSensor = False
    enableHumidSensor = False
    tempSensorData = SensorData()
    humidSensorData = SensorData()
    hi2cSensorData = SensorData()
    manager = SensorDataManager()
    tempsensor = TempSensorAdaptorTask()
    #     humiditysensor = HumiditySensorAdaptorTask()
    #     hi2csensor = HI2CSensorAdaptorTask()
    persistenceutil = PersistenceUtil()

    def __init__(self, rateInSec=10):
        threading.Thread.__init__(self)
        #self.sensorData = SensorData()
        self.rateInSec = rateInSec
        #self.time      = self.sensorData.timeStamp

    def run(self):
        while True:

            if self.enableTempSensor:
                self.tempSensorData.setName('Temp')
                self.tempSensorData.addValue(self.tempsensor.getTemperature())
                #print(self.sensorData.curValue)
                #self.manager.handleSensorData(self.sensorData)
                self.persistenceutil.writeSensorDataToRedis(
                    self.tempSensorData)
                sleep(self.rateInSec)

            if self.enableHumidSensor:
                self.humidSensorData.setName("Humid")
                self.humidSensorData.addValue(
                    self.humiditysensor.getHumidity())
                #print(str(datetime.now()) + "SenseHat API Humidity   " + str(self.curHumid))
                #print(str(datetime.now()) + "I2C Direct Humidity:    " + str(self.i2cHumid.getHumidityData()))
                #print(self.sensorData.curValue)
                #self.manager.handleSensorData(self.sensorData)
                self.persistenceutil.writeSensorDataToRedis(
                    self.humidSensorData)

                sleep(self.rateInSec)

            if self.enableHI2CSensor:
                #                 self.displayAccelerometerData()
                #                 self.displayMagnetometerData()
                #                 self.displayPressureData()
                #                 self.displayHumidityData()
                #                 self.getHumidityData()
                #                 print(str(datetime.now()) + "I2C Direct Humidity:    " + str(self.RH))
                self.hi2cSensorData.setName("I2C_Humid")
                self.hi2cSensorData.addValue(self.hi2csensor.getHumidityData())
                #self.manager.handleSensorData(self.sensorData)
                self.persistenceutil.writeSensorDataToRedis(
                    self.hi2cSensorData)

                sleep(self.rateInSec)
Ejemplo n.º 7
0
class MultiSensorAdaptor(threading.Thread):
    '''
    The MultiSensorAdaptor gets the temperature from the TempSensorAdaptorTask. It
    gets the amount of reading at a frequency indicated during instantiation
    '''
    #Instantiate TempSensorAdaptorTask
    tempSensorAdaptorTask = TempSensorAdaptorTask()
    
    #Instantiate MqttClientConnector
    mqttClientConnector = MqttClientConnector()
    
    #Disable the fetcher initially
    enableFetcher = False

    """
    The constructor is used to set the readings (numReadings) and the 
    frequency of readings (rateInSec) if provided, else defaults to 10 and 5 respectively
    """
    def __init__(self, numReadings = 10, rateInSec = 5):
        '''
        Constructor
        '''
        #Initialize the thread
        threading.Thread.__init__(self)
        
        #If provided numReadings and rateInSec
        self.numReadings = numReadings
        self.rateInSec = rateInSec
    
    """    
    This method takes in a MqttClientConnector reference and assigns it to its own
    MqttClientConnector
    """
    def setMqttClient(self, mqttClientConnector):
        
        #Check if the parameter passed is of the type mqttClientConnector, only set it then
        if(type(mqttClientConnector) == MqttClientConnector):
    
            #Set the reference of mqttClientConnector
            self.mqttClientConnector = mqttClientConnector
            
            #Return True for successful assignment
            return True
        
        #if its of any other type besides MqttClientConnector
        else:
            
            #return False for failure to assign
            return False
    
    """    
    This method polls the TempSensorAdaptor task to get the SensorData from it,
    it then calls the MQTTClientConnector to publish it MQTT
    """    
    def start(self):
        
        #Only run if there is a valid reference to mqttClientConnector and setMqttClientConnector has been called first
        if(self.mqttClientConnector!=None):
        
            #Connect the MQTT client
            self.mqttClientConnector.connect("broker.mqttdashboard.com", 1883)
            
            #Sleep for 1 second to let it connect
            sleep(1)
        
            #Data is not fetched if only 0 readings set:
            if self.numReadings == 0:
                    
                #Return false because fetcher didn't run 
                return False
                
            #Run the loop as many times as indicated in the numReadings variable
            while self.numReadings > 0:
                    
                #If the fetcher is enabled
                if self.enableFetcher:
                        
                    #Get the sensorData from the task
                    sensorData = self.tempSensorAdaptorTask.getTemperature()
                            
                    #Publish data to MQTT
                    self.mqttClientConnector.publishSensorCommand(sensorData, 2)
                        
                    #Decrement the number of readings by 1
                    self.numReadings -= 1
                    
                    #Sleep for time specified by user
                    sleep(self.rateInSec)
                        
                #If fetcher is disabled    
                else: 
                        
                    #Return false to indicate no readings taken
                    return False
                 
            #Disconnect from MQTT once done publishing all SensorData
            self.mqttClientConnector.client.disconnect()
                
            #Stop the MQTT loop that listens for MQTT events
            self.mqttClientConnector.client.loop_stop()
                
            #Fetcher is done running, return true to indicate readings were taken   
            return True
            
        #If MQTT client not connected        
        else:
                
            #Return false to indicate no readings were taken
            return False