Exemplo n.º 1
0
class HumiditySensorSimTaskTest(unittest.TestCase):
    """
	This test case class contains very basic unit tests for
	HumiditySensorSimTask. It should not be considered complete,
	but serve as a starting point for the student implementing
	additional functionality within their Programming the IoT
	environment.
	"""
    @classmethod
    def setUpClass(self):
        logging.basicConfig(
            format='%(asctime)s:%(module)s:%(levelname)s:%(message)s',
            level=logging.DEBUG)
        logging.info("Testing HumiditySensorSimTask class...")
        self.hSimTask = HumiditySensorSimTask()

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testGenerateTelemetry(self):
        sd = self.hSimTask.generateTelemetry()

        if sd:
            logging.info("SensorData: " + str(sd))
        else:
            logging.warning("SensorData is None.")

    def testGetTelemetryValue(self):
        val = self.hSimTask.getTelemetryValue()
        logging.info("Humidity data: %f", val)
        self.assertGreater(val, 0.0)
Exemplo n.º 2
0
 def __init__(self,
              useEmulator: bool = False,
              pollRate: int = 5,
              allowConfigOverride: bool = True):
     self.useEmulator = useEmulator
     self.pollRate = pollRate
     self.allowConfigOverride = allowConfigOverride
     self.dataMsgListener = IDataMessageListener()
     self.scheduler = BackgroundScheduler()
     self.scheduler.add_job(self.handleTelemetry,
                            'interval',
                            seconds=self.pollRate)
     # whether use Emulator
     if self.useEmulator is True:
         logging.info("Use Emulator")
         # load the Humidity emulator
         humidityModule = __import__(
             'programmingtheiot.cda.emulated.HumiditySensorEmulatorTask',
             fromlist=['HumiditySensorEmulatorTask'])
         heClazz = getattr(humidityModule, 'HumiditySensorEmulatorTask')
         self.humidityEmulator = heClazz()
         # load the Pressure emulator
         pressureModule = __import__(
             'programmingtheiot.cda.emulated.PressureSensorEmulatorTask',
             fromlist=['PressureSensorEmulatorTask'])
         heClazz = getattr(pressureModule, 'PressureSensorEmulatorTask')
         self.pressureEmulator = heClazz()
         # load the Temp emulator
         tempModule = __import__(
             'programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask',
             fromlist=['TemperatureSensorEmulatorTask'])
         heClazz = getattr(tempModule, 'TemperatureSensorEmulatorTask')
         self.tempEmulator = heClazz()
     else:
         logging.info("Use Simulator")
         self.dataGenerator = SensorDataGenerator()
         configUtil = ConfigUtil()
         #define data range
         humidityFloor = configUtil.getFloat(
             ConfigConst.CONSTRAINED_DEVICE,
             ConfigConst.HUMIDITY_SIM_FLOOR_KEY,
             SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
         humidityCeiling = configUtil.getFloat(
             ConfigConst.CONSTRAINED_DEVICE,
             ConfigConst.HUMIDITY_SIM_CEILING_KEY,
             SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)
         pressureFloor = configUtil.getFloat(
             ConfigConst.CONSTRAINED_DEVICE,
             ConfigConst.PRESSURE_SIM_FLOOR_KEY,
             SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
         pressureCeiling = configUtil.getFloat(
             ConfigConst.CONSTRAINED_DEVICE,
             ConfigConst.PRESSURE_SIM_CEILING_KEY,
             SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)
         tempFloor = configUtil.getFloat(
             ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY,
             SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
         tempCeiling = configUtil.getFloat(
             ConfigConst.CONSTRAINED_DEVICE,
             ConfigConst.TEMP_SIM_CEILING_KEY,
             SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)
         #generate dataset
         self.humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(
             minValue=humidityFloor,
             maxValue=humidityCeiling,
             useSeconds=False)
         self.pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(
             minValue=pressureFloor,
             maxValue=pressureCeiling,
             useSeconds=False)
         self.tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(
             minValue=tempFloor, maxValue=tempCeiling, useSeconds=False)
         #create task with data
         self.htask = HumiditySensorSimTask(dataSet=self.humidityData)
         self.ptask = PressureSensorSimTask(dataSet=self.pressureData)
         self.ttask = TemperatureSensorSimTask(dataSet=self.tempData)
Exemplo n.º 3
0
class SensorAdapterManager(object):
    """
    Shell representation of class for student implementation.
    
    """
    def __init__(self,
                 useEmulator: bool = False,
                 pollRate: int = 5,
                 allowConfigOverride: bool = True):
        self.useEmulator = useEmulator
        self.pollRate = pollRate
        self.allowConfigOverride = allowConfigOverride
        self.dataMsgListener = IDataMessageListener()
        self.scheduler = BackgroundScheduler()
        self.scheduler.add_job(self.handleTelemetry,
                               'interval',
                               seconds=self.pollRate)
        # whether use Emulator
        if self.useEmulator is True:
            logging.info("Use Emulator")
            # load the Humidity emulator
            humidityModule = __import__(
                'programmingtheiot.cda.emulated.HumiditySensorEmulatorTask',
                fromlist=['HumiditySensorEmulatorTask'])
            heClazz = getattr(humidityModule, 'HumiditySensorEmulatorTask')
            self.humidityEmulator = heClazz()
            # load the Pressure emulator
            pressureModule = __import__(
                'programmingtheiot.cda.emulated.PressureSensorEmulatorTask',
                fromlist=['PressureSensorEmulatorTask'])
            heClazz = getattr(pressureModule, 'PressureSensorEmulatorTask')
            self.pressureEmulator = heClazz()
            # load the Temp emulator
            tempModule = __import__(
                'programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask',
                fromlist=['TemperatureSensorEmulatorTask'])
            heClazz = getattr(tempModule, 'TemperatureSensorEmulatorTask')
            self.tempEmulator = heClazz()
        else:
            logging.info("Use Simulator")
            self.dataGenerator = SensorDataGenerator()
            configUtil = ConfigUtil()
            #define data range
            humidityFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
            humidityCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)
            pressureFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
            pressureCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)
            tempFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
            tempCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.TEMP_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)
            #generate dataset
            self.humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(
                minValue=humidityFloor,
                maxValue=humidityCeiling,
                useSeconds=False)
            self.pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(
                minValue=pressureFloor,
                maxValue=pressureCeiling,
                useSeconds=False)
            self.tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(
                minValue=tempFloor, maxValue=tempCeiling, useSeconds=False)
            #create task with data
            self.htask = HumiditySensorSimTask(dataSet=self.humidityData)
            self.ptask = PressureSensorSimTask(dataSet=self.pressureData)
            self.ttask = TemperatureSensorSimTask(dataSet=self.tempData)

    def handleTelemetry(self):
        if self.useEmulator is False:
            #use simulator
            self.dataMsgListener.handleSensorMessage(
                self.htask.generateTelemetry())
            logging.info(
                "Simulated humidity data: name=%s, timeStamp=%s, curValue=%f",
                self.htask.sensorData.getName(),
                self.htask.sensorData.getTimeStamp(),
                self.htask.sensorData.getValue())
            self.dataMsgListener.handleSensorMessage(
                self.ptask.generateTelemetry())
            logging.info(
                "Simulated pressure data: name=%s, timeStamp=%s, curValue=%f",
                self.ptask.sensorData.getName(),
                self.ptask.sensorData.getTimeStamp(),
                self.ptask.sensorData.getValue())
            self.dataMsgListener.handleSensorMessage(
                self.ttask.generateTelemetry())
            logging.info(
                "Simulated temp data: name=%s, timeStamp=%s, curValue=%f",
                self.ttask.sensorData.getName(),
                self.ttask.sensorData.getTimeStamp(),
                self.ttask.sensorData.getValue())
        else:
            #use emulator
            data = self.humidityEmulator.generateTelemetry()
            self.dataMsgListener.handleSensorMessage(data)
            logging.info(
                "Emulator humidity data: name=%s, timeStamp=%s, curValue=%f",
                data.getName(), data.getTimeStamp(), data.getValue())
            data = self.pressureEmulator.generateTelemetry()
            self.dataMsgListener.handleSensorMessage(data)
            logging.info(
                "Emulator pressure data: name=%s, timeStamp=%s, curValue=%f",
                data.getName(), data.getTimeStamp(), data.getValue())
            data = self.tempEmulator.generateTelemetry()
            self.dataMsgListener.handleSensorMessage(data)
            logging.info(
                "Emulator temp data: name=%s, timeStamp=%s, curValue=%f",
                data.getName(), data.getTimeStamp(), data.getValue())

    def setDataMessageListener(self, listener: IDataMessageListener) -> bool:
        if listener is not None:
            self.dataMsgListener = listener

    def startManager(self):
        self.scheduler.start()

    def stopManager(self):
        self.scheduler.shutdown()
Exemplo n.º 4
0
 def setUpClass(self):
     logging.basicConfig(
         format='%(asctime)s:%(module)s:%(levelname)s:%(message)s',
         level=logging.DEBUG)
     logging.info("Testing HumiditySensorSimTask class...")
     self.hSimTask = HumiditySensorSimTask()
Exemplo n.º 5
0
	def __init__(self, useEmulator: bool = False, pollRate: int = 15, allowConfigOverride: bool = True):
		"""
		Initialization of class.
		Create an instance of SensorAdapterManager
		"""
		self.useEmulator = useEmulator
		self.pollRate = pollRate
		self.allowConfigOverride = allowConfigOverride
		self.dataMsgListener = None
		self.scheduler = BackgroundScheduler()
		self.scheduler.add_job(self.handleTelemetry, 'interval', seconds = self.pollRate)
		
		configUtil = ConfigUtil()
		
		"""
		If self.useEmulator is true, we will use Emulator to generate sensor data
		Else we will use SensorDataGenerator
		"""
		if self.useEmulator :
			logging.info("---> Emulators will be used ")
			
			
			
			if configUtil.getBoolean(section= ConfigConst.CONSTRAINED_DEVICE, key= ConfigConst.ENABLE_SENSE_HAT_KEY):
				logging.info("---> SMbus will be used ")
				self.humidityI2cSensorAdapterTask = HumidityI2cSensorAdapterTask()
				self.pressureI2cSensorAdapterTask = PressureI2cSensorAdapterTask()
				self.temperatureI2cSensorAdapterTask = TemperatureI2cSensorAdapterTask()
			else:
				# load the Humidity emulator
				humidityModule = __import__('programmingtheiot.cda.emulated.HumiditySensorEmulatorTask', fromlist = ['HumiditySensorEmulatorTask'])
				heClazz = getattr(humidityModule, 'HumiditySensorEmulatorTask')
				self.humidityEmulator = heClazz()
			
				pressureModule = __import__('programmingtheiot.cda.emulated.PressureSensorEmulatorTask', fromlist = ['PressureSensorEmulatorTask'])
				peClazz = getattr(pressureModule, 'PressureSensorEmulatorTask')
				self.pressureEmulator = peClazz()
			
				temperatureModule = __import__('programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask', fromlist = ['TemperatureSensorEmulatorTask'])
				teClazz = getattr(temperatureModule, 'TemperatureSensorEmulatorTask')
				self.temperatureEmulator =teClazz()
				
				##add by miaoyao for final project
				soilHumidityModule = __import__('programmingtheiot.cda.emulated.SoilHumiditySensorEmulatorTask', fromlist = ['SoilHumiditySensorEmulatorTask'])
				shClazz = getattr(soilHumidityModule, 'SoilHumiditySensorEmulatorTask')
				self.soilHumidityEmulator = shClazz()
				

			
		else:
			logging.info("---> Simulators will be used ")
			self.dataGenerator = SensorDataGenerator()

			

			humidityFloor = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.HUMIDITY_SIM_FLOOR_KEY, SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
			humidityCeiling = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.HUMIDITY_SIM_CEILING_KEY, SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)
			
			pressureFloor = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.PRESSURE_SIM_FLOOR_KEY, SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
			pressureCeiling = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.PRESSURE_SIM_CEILING_KEY, SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)
			
			tempFloor = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY, SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
			tempCeiling = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_CEILING_KEY, SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)
			
			humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(minValue = humidityFloor, maxValue = humidityCeiling, useSeconds = False)
			pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(minValue = pressureFloor, maxValue = pressureCeiling, useSeconds = False)
			tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(minValue = tempFloor, maxValue = tempCeiling, useSeconds = False)
			
			
			
			
			self.humiditySensorSimTask = HumiditySensorSimTask(dataSet= humidityData)
			self.pressureSensorSimTask = PressureSensorSimTask(dataSet= pressureData)
			self.temperatureSensorSimTask = TemperatureSensorSimTask(dataSet= tempData)
Exemplo n.º 6
0
class SensorAdapterManager(object):
	"""
	Shell representation of class for student implementation.
	
	"""

	def __init__(self, useEmulator: bool = False, pollRate: int = 15, allowConfigOverride: bool = True):
		"""
		Initialization of class.
		Create an instance of SensorAdapterManager
		"""
		self.useEmulator = useEmulator
		self.pollRate = pollRate
		self.allowConfigOverride = allowConfigOverride
		self.dataMsgListener = None
		self.scheduler = BackgroundScheduler()
		self.scheduler.add_job(self.handleTelemetry, 'interval', seconds = self.pollRate)
		
		configUtil = ConfigUtil()
		
		"""
		If self.useEmulator is true, we will use Emulator to generate sensor data
		Else we will use SensorDataGenerator
		"""
		if self.useEmulator :
			logging.info("---> Emulators will be used ")
			
			
			
			if configUtil.getBoolean(section= ConfigConst.CONSTRAINED_DEVICE, key= ConfigConst.ENABLE_SENSE_HAT_KEY):
				logging.info("---> SMbus will be used ")
				self.humidityI2cSensorAdapterTask = HumidityI2cSensorAdapterTask()
				self.pressureI2cSensorAdapterTask = PressureI2cSensorAdapterTask()
				self.temperatureI2cSensorAdapterTask = TemperatureI2cSensorAdapterTask()
			else:
				# load the Humidity emulator
				humidityModule = __import__('programmingtheiot.cda.emulated.HumiditySensorEmulatorTask', fromlist = ['HumiditySensorEmulatorTask'])
				heClazz = getattr(humidityModule, 'HumiditySensorEmulatorTask')
				self.humidityEmulator = heClazz()
			
				pressureModule = __import__('programmingtheiot.cda.emulated.PressureSensorEmulatorTask', fromlist = ['PressureSensorEmulatorTask'])
				peClazz = getattr(pressureModule, 'PressureSensorEmulatorTask')
				self.pressureEmulator = peClazz()
			
				temperatureModule = __import__('programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask', fromlist = ['TemperatureSensorEmulatorTask'])
				teClazz = getattr(temperatureModule, 'TemperatureSensorEmulatorTask')
				self.temperatureEmulator =teClazz()
				
				##add by miaoyao for final project
				soilHumidityModule = __import__('programmingtheiot.cda.emulated.SoilHumiditySensorEmulatorTask', fromlist = ['SoilHumiditySensorEmulatorTask'])
				shClazz = getattr(soilHumidityModule, 'SoilHumiditySensorEmulatorTask')
				self.soilHumidityEmulator = shClazz()
				

			
		else:
			logging.info("---> Simulators will be used ")
			self.dataGenerator = SensorDataGenerator()

			

			humidityFloor = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.HUMIDITY_SIM_FLOOR_KEY, SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
			humidityCeiling = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.HUMIDITY_SIM_CEILING_KEY, SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)
			
			pressureFloor = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.PRESSURE_SIM_FLOOR_KEY, SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
			pressureCeiling = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.PRESSURE_SIM_CEILING_KEY, SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)
			
			tempFloor = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY, SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
			tempCeiling = configUtil.getFloat(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_CEILING_KEY, SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)
			
			humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(minValue = humidityFloor, maxValue = humidityCeiling, useSeconds = False)
			pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(minValue = pressureFloor, maxValue = pressureCeiling, useSeconds = False)
			tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(minValue = tempFloor, maxValue = tempCeiling, useSeconds = False)
			
			
			
			
			self.humiditySensorSimTask = HumiditySensorSimTask(dataSet= humidityData)
			self.pressureSensorSimTask = PressureSensorSimTask(dataSet= pressureData)
			self.temperatureSensorSimTask = TemperatureSensorSimTask(dataSet= tempData)

	def handleTelemetry(self):
		"""
		handle the Telemetry
		
		"""
		#humidityVal = self.humiditySensorSimTask.getTelemetryValue()
		#pressureVal = self.pressureSensorSimTask.getTelemetryValue()
		#tempVal = self.temperatureSensorSimTask .getTelemetryValue()
		configUtil = ConfigUtil()
		
		if self.useEmulator :
			if configUtil.getBoolean(section= ConfigConst.CONSTRAINED_DEVICE, key= ConfigConst.ENABLE_SENSE_HAT_KEY):
				humiditySd = self.humidityI2cSensorAdapterTask.generateTelemetry()
				pressureSd = self.pressureI2cSensorAdapterTask.generateTelemetry()
				tempSd = self.temperatureI2cSensorAdapterTask.generateTelemetry()
			else:
				humiditySd = self.humidityEmulator.generateTelemetry()
				pressureSd = self.pressureEmulator.generateTelemetry()
				tempSd = self.temperatureEmulator .generateTelemetry()
				soilHumiditySd = self.soilHumidityEmulator.generateTelemetry()
		else :
			
			humiditySd = self.humiditySensorSimTask.generateTelemetry()
			pressureSd = self.pressureSensorSimTask.generateTelemetry()
			tempSd = self.temperatureSensorSimTask .generateTelemetry()
		
		if self.dataMsgListener :
			self.dataMsgListener.handleSensorMessage(humiditySd)
			self.dataMsgListener.handleSensorMessage(pressureSd)
			self.dataMsgListener.handleSensorMessage(tempSd)
			self.dataMsgListener.handleSensorMessage(soilHumiditySd)
		
		
		logging.info(' >>>>>>>>> Humidity is < %s >,  SoilHumidity is <%s>,  Pressure is < %s >,  Temperature is < %s >.', str(humiditySd.getValue()), str(soilHumiditySd.getValue()), str(pressureSd.getValue()), str(tempSd.getValue()))
		
	def setDataMessageListener(self, listener: IDataMessageListener) -> bool:
		"""
		set Data MessageListener which is used for monitor the Sensor
		
		@return bool
		"""
		if listener:
			self.dataMsgListener = listener
			
	
	def startManager(self):
		"""
		Start the SensorAdapterManager. 
		
		"""
		logging.info("---> Started SensorAdapterManager.")
		self.scheduler.start()
		
	def stopManager(self):
		"""
		Stop the SensorAdapterManager. 
		
		"""
		self.scheduler.shutdown()
		logging.info("---> Stopped SensorAdapterManager.")
Exemplo n.º 7
0
    def __init__(self,
                 useEmulator: bool = False,
                 pollRate: int = 5,
                 allowConfigOverride: bool = True):
        self.useEmulator = useEmulator
        self.pollRate = pollRate

        self.dataMsgListener = 0
        self.allowConfigOverride = allowConfigOverride

        self.scheduler = BackgroundScheduler()
        self.scheduler.add_job(self.handleTelemetry,
                               'interval',
                               seconds=self.pollRate)

        if (self.useEmulator == True):
            logging.info("Emulators are being used")
            humidityModule = __import__(
                'programmingtheiot.cda.emulated.HumiditySensorEmulatorTask',
                fromlist=['HumiditySensorEmulatorTask'])
            heClazz = getattr(humidityModule, 'HumiditySensorEmulatorTask')
            self.humidityEmulator = heClazz()

            pressureModule = __import__(
                'programmingtheiot.cda.emulated.PressureSensorEmulatorTask',
                fromlist=['PressureSensorEmulatorTask'])
            heClazz = getattr(pressureModule, 'PressureSensorEmulatorTask')
            self.pressureEmulator = heClazz()

            tempModule = __import__(
                'programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask',
                fromlist=['TemperatureSensorEmulatorTask'])
            heClazz = getattr(tempModule, 'TemperatureSensorEmulatorTask')
            self.tempEmulator = heClazz()

        else:
            logging.info("Simulators are being used")
            self.dataGenerator = SensorDataGenerator()
            configUtil = ConfigUtil()

            humidity_floor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
            humidity_ceiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)

            pressure_Floor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
            pressure_ceiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)

            temp_floor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
            temp_ceiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.TEMP_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)

            self.humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(
                minValue=humidity_floor,
                maxValue=humidity_ceiling,
                useSeconds=False)

            self.pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(
                minValue=pressure_Floor,
                maxValue=pressure_ceiling,
                useSeconds=False)

            self.tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(
                minValue=temp_floor, maxValue=temp_ceiling, useSeconds=False)

            self.humiditySensorSimTask = HumiditySensorSimTask(
                self.humidityData)
            self.pressureSensorSimTask = PressureSensorSimTask(
                self.pressureData)
            self.temperatureSensorSimTask = TemperatureSensorSimTask(
                self.tempData)

            logging.info("Simulated Humidity Sensor Sim Task Value is: %s ",
                         self.humiditySensorSimTask.LatestSensorData)
            logging.info("Simulated Pressure Sensor Sim Task Value is : %s ",
                         self.pressureSensorSimTask.LatestSensorData)
            logging.info("Simulated Temperature Sensor Sim Task Value is: %s ",
                         self.temperatureSensorSimTask.LatestSensorData)
            self.handleTelemetry()
        pass
Exemplo n.º 8
0
class SensorAdapterManager(object):
    """
	Shell representation of class for student implementation.
	

	Handling All Sensors - invoking respective classes depending on input : Simulator or Emulator
	
	Sending respective commands to store Data/Values either from Simulator or Emulator
	
	
	
	"""
    def __init__(self,
                 useEmulator: bool = False,
                 pollRate: int = 5,
                 allowConfigOverride: bool = True):
        self.useEmulator = useEmulator
        self.pollRate = pollRate

        self.dataMsgListener = 0
        self.allowConfigOverride = allowConfigOverride

        self.scheduler = BackgroundScheduler()
        self.scheduler.add_job(self.handleTelemetry,
                               'interval',
                               seconds=self.pollRate)

        if (self.useEmulator == True):
            logging.info("Emulators are being used")
            humidityModule = __import__(
                'programmingtheiot.cda.emulated.HumiditySensorEmulatorTask',
                fromlist=['HumiditySensorEmulatorTask'])
            heClazz = getattr(humidityModule, 'HumiditySensorEmulatorTask')
            self.humidityEmulator = heClazz()

            pressureModule = __import__(
                'programmingtheiot.cda.emulated.PressureSensorEmulatorTask',
                fromlist=['PressureSensorEmulatorTask'])
            heClazz = getattr(pressureModule, 'PressureSensorEmulatorTask')
            self.pressureEmulator = heClazz()

            tempModule = __import__(
                'programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask',
                fromlist=['TemperatureSensorEmulatorTask'])
            heClazz = getattr(tempModule, 'TemperatureSensorEmulatorTask')
            self.tempEmulator = heClazz()

        else:
            logging.info("Simulators are being used")
            self.dataGenerator = SensorDataGenerator()
            configUtil = ConfigUtil()

            humidity_floor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
            humidity_ceiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)

            pressure_Floor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
            pressure_ceiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)

            temp_floor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
            temp_ceiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.TEMP_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)

            self.humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(
                minValue=humidity_floor,
                maxValue=humidity_ceiling,
                useSeconds=False)

            self.pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(
                minValue=pressure_Floor,
                maxValue=pressure_ceiling,
                useSeconds=False)

            self.tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(
                minValue=temp_floor, maxValue=temp_ceiling, useSeconds=False)

            self.humiditySensorSimTask = HumiditySensorSimTask(
                self.humidityData)
            self.pressureSensorSimTask = PressureSensorSimTask(
                self.pressureData)
            self.temperatureSensorSimTask = TemperatureSensorSimTask(
                self.tempData)

            logging.info("Simulated Humidity Sensor Sim Task Value is: %s ",
                         self.humiditySensorSimTask.LatestSensorData)
            logging.info("Simulated Pressure Sensor Sim Task Value is : %s ",
                         self.pressureSensorSimTask.LatestSensorData)
            logging.info("Simulated Temperature Sensor Sim Task Value is: %s ",
                         self.temperatureSensorSimTask.LatestSensorData)
            self.handleTelemetry()
        pass

    """
	Displaying sensor values on console by calling Simulator/Emulator methods as per useEmulator flag
	"""

    def handleTelemetry(self):
        if (self.useEmulator == False):
            self.humiditySensorSimTask.generateTelemetry()
            logging.info("Simulated Humidity Sensor value is %s ",
                         self.humiditySensorSimTask.getTelemetryValue())
            self.pressureSensorSimTask.generateTelemetry()
            logging.info("Simulated Pressure Sensor value is %s ",
                         self.pressureSensorSimTask.getTelemetryValue())
            self.temperatureSensorSimTask.generateTelemetry()
            logging.info("Simulated Temperature Sensor value is %s ",
                         self.temperatureSensorSimTask.getTelemetryValue())
        elif (self.useEmulator == True):
            self.humidityEmulator.generateTelemetry()
            logging.info("Emulated Humidity Sensor value is %s ",
                         self.humidityEmulator.getTelemetryValue())
            self.pressureEmulator.generateTelemetry()
            logging.info("Emulated Pressure Sensor value is %s ",
                         self.pressureEmulator.getTelemetryValue())
            self.tempEmulator.generateTelemetry()
            logging.info("Emulated Temperature Sensor value is %s ",
                         self.tempEmulator.getTelemetryValue())
        pass

    def setDataMessageListener(self, listener: IDataMessageListener) -> bool:
        if (listener != None):
            self.dataMsgListener = listener
        pass

    def startManager(self):
        self.scheduler.start()
        pass

    def stopManager(self):
        self.scheduler.shutdown()
        pass
    def __init__(self,
                 useEmulator: bool = False,
                 enableSenseHAT: bool = False,
                 pollRate: int = 5,
                 allowConfigOverride: bool = True):
        """
        Init the SensorAdapterManager, if using simulator, setup data sets and sim tasks for simulation

        :param useEmulator: Whether use Emulator
        :param pollRate: Interval seconds for polling sensor data
        :param allowConfigOverride: If allow to override config
        """
        logging.info("SensorAdapterManager is initializing...")
        # Init basic config variables
        self.useEmulator = useEmulator
        self.pollRate = pollRate
        self.allowConfigOverride = allowConfigOverride
        # Init data message listener
        self.dataMsgListener: IDataMessageListener = None
        # Init scheduler
        self.scheduler = BackgroundScheduler()
        self.scheduler.add_job(self.handleTelemetry,
                               'interval',
                               seconds=self.pollRate)

        configUtil = ConfigUtil()
        self.enableSenseHAT = enableSenseHAT

        if self.enableSenseHAT is True:
            logging.info("SensorAdapterManager is using SenseHAT.")
            self.humiditySensorI2cTask = HumidityI2cSensorAdapterTask()
            self.pressureSensorI2cTask = PressureI2cSensorAdapterTask()
            self.temperatureSensorI2cTask = TemperatureI2cSensorAdapterTask()
            pass
        elif self.useEmulator is True:
            logging.info("SensorAdapterManager is using emulator.")

            humidityModule = __import__(
                'programmingtheiot.cda.emulated.HumiditySensorEmulatorTask',
                fromlist=['HumiditySensorEmulatorTask'])
            huClass = getattr(humidityModule, 'HumiditySensorEmulatorTask')
            self.humidityEmulator = huClass()

            pressureModule = __import__(
                'programmingtheiot.cda.emulated.PressureSensorEmulatorTask',
                fromlist=['PressureSensorEmulatorTask'])
            prClass = getattr(pressureModule, 'PressureSensorEmulatorTask')
            self.pressureEmulator = prClass()

            tempModule = __import__(
                'programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask',
                fromlist=['TemperatureSensorEmulatorTask'])
            teClass = getattr(tempModule, 'TemperatureSensorEmulatorTask')
            self.tempEmulator = teClass()
        else:
            logging.info("SensorAdapterManager is using simulators.")
            self.dataGenerator = SensorDataGenerator()

            humidityFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
            humidityCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)
            pressureFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
            pressureCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)
            tempFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
            tempCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.TEMP_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)
            humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(
                minValue=humidityFloor,
                maxValue=humidityCeiling,
                useSeconds=False)
            pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(
                minValue=pressureFloor,
                maxValue=pressureCeiling,
                useSeconds=False)
            tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(
                minValue=tempFloor, maxValue=tempCeiling, useSeconds=False)
            self.humiditySensorSimTask = HumiditySensorSimTask(
                dataSet=humidityData,
                minVal=humidityFloor,
                maxVal=humidityCeiling)
            self.pressureSensorSimTask = PressureSensorSimTask(
                dataSet=pressureData,
                minVal=pressureFloor,
                maxVal=pressureCeiling)
            self.temperatureSensorSimTask = TemperatureSensorSimTask(
                dataSet=tempData, minVal=tempFloor, maxVal=tempCeiling)
        pass
class SensorAdapterManager(object):
    """
    Manager to manage all sensor tasks, trigger and sensor tasks periodically and handle their results
    According to config, manager also manage sim data set generation
    """
    def __init__(self,
                 useEmulator: bool = False,
                 enableSenseHAT: bool = False,
                 pollRate: int = 5,
                 allowConfigOverride: bool = True):
        """
        Init the SensorAdapterManager, if using simulator, setup data sets and sim tasks for simulation

        :param useEmulator: Whether use Emulator
        :param pollRate: Interval seconds for polling sensor data
        :param allowConfigOverride: If allow to override config
        """
        logging.info("SensorAdapterManager is initializing...")
        # Init basic config variables
        self.useEmulator = useEmulator
        self.pollRate = pollRate
        self.allowConfigOverride = allowConfigOverride
        # Init data message listener
        self.dataMsgListener: IDataMessageListener = None
        # Init scheduler
        self.scheduler = BackgroundScheduler()
        self.scheduler.add_job(self.handleTelemetry,
                               'interval',
                               seconds=self.pollRate)

        configUtil = ConfigUtil()
        self.enableSenseHAT = enableSenseHAT

        if self.enableSenseHAT is True:
            logging.info("SensorAdapterManager is using SenseHAT.")
            self.humiditySensorI2cTask = HumidityI2cSensorAdapterTask()
            self.pressureSensorI2cTask = PressureI2cSensorAdapterTask()
            self.temperatureSensorI2cTask = TemperatureI2cSensorAdapterTask()
            pass
        elif self.useEmulator is True:
            logging.info("SensorAdapterManager is using emulator.")

            humidityModule = __import__(
                'programmingtheiot.cda.emulated.HumiditySensorEmulatorTask',
                fromlist=['HumiditySensorEmulatorTask'])
            huClass = getattr(humidityModule, 'HumiditySensorEmulatorTask')
            self.humidityEmulator = huClass()

            pressureModule = __import__(
                'programmingtheiot.cda.emulated.PressureSensorEmulatorTask',
                fromlist=['PressureSensorEmulatorTask'])
            prClass = getattr(pressureModule, 'PressureSensorEmulatorTask')
            self.pressureEmulator = prClass()

            tempModule = __import__(
                'programmingtheiot.cda.emulated.TemperatureSensorEmulatorTask',
                fromlist=['TemperatureSensorEmulatorTask'])
            teClass = getattr(tempModule, 'TemperatureSensorEmulatorTask')
            self.tempEmulator = teClass()
        else:
            logging.info("SensorAdapterManager is using simulators.")
            self.dataGenerator = SensorDataGenerator()

            humidityFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_HUMIDITY)
            humidityCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.HUMIDITY_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_HUMIDITY)
            pressureFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_ENV_PRESSURE)
            pressureCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.PRESSURE_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_ENV_PRESSURE)
            tempFloor = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEMP_SIM_FLOOR_KEY,
                SensorDataGenerator.LOW_NORMAL_INDOOR_TEMP)
            tempCeiling = configUtil.getFloat(
                ConfigConst.CONSTRAINED_DEVICE,
                ConfigConst.TEMP_SIM_CEILING_KEY,
                SensorDataGenerator.HI_NORMAL_INDOOR_TEMP)
            humidityData = self.dataGenerator.generateDailyEnvironmentHumidityDataSet(
                minValue=humidityFloor,
                maxValue=humidityCeiling,
                useSeconds=False)
            pressureData = self.dataGenerator.generateDailyEnvironmentPressureDataSet(
                minValue=pressureFloor,
                maxValue=pressureCeiling,
                useSeconds=False)
            tempData = self.dataGenerator.generateDailyIndoorTemperatureDataSet(
                minValue=tempFloor, maxValue=tempCeiling, useSeconds=False)
            self.humiditySensorSimTask = HumiditySensorSimTask(
                dataSet=humidityData,
                minVal=humidityFloor,
                maxVal=humidityCeiling)
            self.pressureSensorSimTask = PressureSensorSimTask(
                dataSet=pressureData,
                minVal=pressureFloor,
                maxVal=pressureCeiling)
            self.temperatureSensorSimTask = TemperatureSensorSimTask(
                dataSet=tempData, minVal=tempFloor, maxVal=tempCeiling)
        pass

    def handleTelemetry(self):
        """
        Handle received sensor data
        If it is using simulator, got sensor data from sim tasks
        """
        logging.info("SensorAdapterManager is trying to get Telemetries...")
        if self.enableSenseHAT is True:
            humidityTelemetry = self.humiditySensorI2cTask.generateTelemetry()
            logging.info("SenserHAT I2c humidity data: %s" %
                         humidityTelemetry.__str__())

            pressureTelemetry = self.pressureSensorI2cTask.generateTelemetry()
            logging.info("SenserHAT I2c pressure data: %s" %
                         pressureTelemetry.__str__())

            tempTelemetry = self.temperatureSensorI2cTask.generateTelemetry()
            logging.info("SenserHAT I2c temperature data: %s" %
                         tempTelemetry.__str__())

            pass
        elif self.useEmulator is False:
            humidityTelemetry = self.humiditySensorSimTask.generateTelemetry()
            logging.info("Simulated humidity data: %s" %
                         humidityTelemetry.__str__())
            self.dataMsgListener.handleSensorMessage(humidityTelemetry)

            pressureTelemetry = self.pressureSensorSimTask.generateTelemetry()
            logging.info("Simulated pressure data: %s" %
                         pressureTelemetry.__str__())
            self.dataMsgListener.handleSensorMessage(pressureTelemetry)

            tempTelemetry = self.temperatureSensorSimTask.generateTelemetry()
            logging.info("Simulated temperature data: %s" %
                         tempTelemetry.__str__())
            self.dataMsgListener.handleSensorMessage(tempTelemetry)
        else:
            humidityTelemetry = self.humidityEmulator.generateTelemetry()
            logging.info("Emulated humidity data: %s" %
                         humidityTelemetry.__str__())
            self.dataMsgListener.handleSensorMessage(humidityTelemetry)

            pressureTelemetry = self.pressureEmulator.generateTelemetry()
            logging.info("Emulated pressure data: %s" %
                         pressureTelemetry.__str__())
            self.dataMsgListener.handleSensorMessage(pressureTelemetry)

            tempTelemetry = self.tempEmulator.generateTelemetry()
            logging.info("Emulated temperature data: %s" %
                         tempTelemetry.__str__())
            self.dataMsgListener.handleSensorMessage(tempTelemetry)
        pass

    def setDataMessageListener(self, listener: IDataMessageListener) -> bool:
        """
        Set DataMessageListener for manager
        :param listener: Given listener
        :return: If setup listen successfully
        """
        if listener is None:
            logging.info("Given DataMessageListener is invalid!")
            return False
        logging.info("Set %s as DataMessageListener." % listener.__str__())
        self.dataMsgListener = listener
        return True
        pass

    def startManager(self):
        """
        Start SensorAdapterManager
        """
        if not self.scheduler.running:
            self.scheduler.start()
            logging.info("Started SensorAdapterManager.")
        pass

    def stopManager(self):
        """
        Stop SensorAdapterManager
        """
        if self.scheduler.running:
            self.scheduler.shutdown()
            logging.info("Stopped SensorAdapterManager.")
        pass