def __init__(self): """Initialize the bus and sensor info and start monitoring sensor states""" self.sensorMutex = RLock() self.realTimeMutex = RLock() self.exiting = Event() self.onDataChanged = None self.systemData = [] self.currentSystemState = [] self.currentRealTimeData = {} self.queuedRealTimeData = {} self.disabledSensors = {} self.disabledSensorTable = "disabled_sensors" checkAllBus() self.gpio = GPIO() self.downloadSpeed = DownloadSpeed(Config(APP_SETTINGS)) self.downloadSpeed.getDownloadSpeed() manager.addDeviceInstance("GPIO", "GPIO", "GPIO", self.gpio, [], "system") manager.loadJsonDevices("rest") results = DbManager.Select(self.disabledSensorTable) if results: for row in results: self.disabledSensors[row[0]] = 1 self.realTimeMonitorRunning = False self.pluginManager = PluginManager(self.OnPluginChange) self.pluginManager.load_plugins() self.InitCallbacks() self.StartMonitoring()
def __init__(self): self.sensorMutex = RLock() self.systemMutex = RLock() self.continueMonitoring = False self.onDataChanged = None self.onSystemInfo = None self.currentBusInfo = self.previousBusInfo = None self.currentSensorsInfo = self.previousSensorsInfo = None self.currentSystemInfo = self.previousSystemInfo = None self.cpuLoadValues = {} self.disabledSensors = {} self.sensorsRefreshCount = 0 self.retrievingSystemInfo = False self.disabledSensorTable = "disabled_sensors" self.systemInfoRefreshList = [] checkAllBus() self.gpio = GPIO() manager.addDeviceInstance("GPIO", "GPIO", "GPIO", self.gpio, [], "system") manager.loadJsonDevices("rest") results = DbManager.Select(self.disabledSensorTable) if results: for row in results: self.disabledSensors[row[0]] = 1 self.StartMonitoring()
def testSensorCallback(self): debug('testSensorCallback') self.previousSystemData = None self.currentSystemData = None self.done = False SensorsClientTest.client.SetDataChanged(self.OnDataChanged) actuator_channel = GPIO().pins[10] sensor_channel = GPIO().pins[11] sensors = { 'actuator': { 'description': 'Digital Output', 'device': 'DigitalActuator', 'args': { 'gpio': 'GPIO', 'invert': False, 'channel': actuator_channel }, 'name': 'test_actuator' }, 'sensor': { 'description': 'Digital Input', 'device': 'DigitalSensor', 'args': { 'gpio': 'GPIO', 'invert': False, 'channel': sensor_channel }, 'name': 'testdevice' } } for sensor in sensors.values(): SensorsClientTest.client.AddSensor(sensor['name'], sensor['description'], sensor['device'], sensor['args']) for i in range(35): sleep(1) if self.done: break info('Changed items: {}'.format([ x for x in self.currentSystemData if x not in self.previousSystemData ])) self.assertNotEqual(self.previousSystemData, self.currentSystemData) for sensor in sensors.values(): self.assertTrue( SensorsClientTest.client.RemoveSensor(sensor['name']))
def setGPIOInstance(self): if not self.gpio: if self.gpioname != "GPIO": self.gpio = instance.deviceInstance(self.gpioname) else: self.gpio = GPIO() if self.gpio: self.gpio.setFunction(self.channel, GPIO.IN)
def __init__(self, client): """Initialize the bus and sensor info and start monitoring sensor states""" self.cloudClient = client self.sensorMutex = RLock() self.realTimeMutex = RLock() self.exiting = Event() self.onDataChanged = None self.systemData = [] self.currentSystemState = [] self.currentRealTimeData = {} self.queuedRealTimeData = {} self.disabledSensors = {} self.disabledSensorTable = "disabled_sensors" checkAllBus() self.gpio = GPIO() # self.downloadSpeed = DownloadSpeed(Config(APP_SETTINGS)) # self.downloadSpeed.getDownloadSpeed() manager.addDeviceInstance("GPIO", "GPIO", "GPIO", self.gpio, [], "system") manager.loadJsonDevices("rest") if not DYNAMIC_DEVICES: warn("loadJsonDevices is None") for sensor in sensors.values(): # info('--------{} {} {}'.format(sensor['name'], sensor['description'], sensor['device'])) self.AddSensor(sensor['name'], sensor['description'], sensor['device'], sensor['args']) # # info(DYNAMIC_DEVICES) self.config = Config(APP_SETTINGS) self.clientId = self.config.get('Agent', 'ClientID', None) self.mqtt_dis_prefix = self.config.get('Agent', 'MQTT_DIS_PREFIX', "homeassistant") self.serial = self.cloudClient.hardware.Serial for name, device in DYNAMIC_DEVICES.items(): for type in device['type']: if type in ['DAC', 'ADC']: continue topic, message = self.AddMQTTSensorDevice(name, type, device) if self.cloudClient: info("{} {}".format(topic, message)) self.cloudClient.EnqueuePacket(message, topic) # info(mqttsensor) results = DbManager.Select(self.disabledSensorTable) if results: for row in results: self.disabledSensors[row[0]] = 1 self.realTimeMonitorRunning = False self.pluginManager = PluginManager(self.OnPluginChange) self.pluginManager.load_plugins() self.InitCallbacks() self.StartMonitoring()
def testBusInfo(self): bus = { item['channel']: item['value'] for item in SensorsClientTest.client.BusInfo() } info('Bus info: {}'.format(bus)) for pin in GPIO().pins: self.assertIn('sys:gpio:{};function'.format(pin), bus) self.assertIn('sys:gpio:{};value'.format(pin), bus)
def testSensors(self): debug('testSensors') #Test adding a sensor channel = GPIO().pins[8] testSensor = { 'description': 'Digital Input', 'device': 'DigitalSensor', 'args': { 'gpio': 'GPIO', 'invert': False, 'channel': channel }, 'name': 'testdevice' } SensorsClientTest.client.RemoveSensor( testSensor['name'] ) #Attempt to remove device if it already exists from a previous test compareKeys = ('args', 'description', 'device') retValue = SensorsClientTest.client.AddSensor( testSensor['name'], testSensor['description'], testSensor['device'], testSensor['args']) self.assertTrue(retValue) retrievedSensor = next(obj for obj in manager.getDeviceList() if obj['name'] == testSensor['name']) for key in compareKeys: self.assertEqual(testSensor[key], retrievedSensor[key]) #Test updating a sensor editedSensor = testSensor editedSensor['args']['channel'] = GPIO().pins[5] retValue = SensorsClientTest.client.EditSensor( editedSensor['name'], editedSensor['description'], editedSensor['device'], editedSensor['args']) self.assertTrue(retValue) retrievedSensor = next(obj for obj in manager.getDeviceList() if obj['name'] == editedSensor['name']) for key in compareKeys: self.assertEqual(editedSensor[key], retrievedSensor[key]) #Test removing a sensor retValue = SensorsClientTest.client.RemoveSensor(testSensor['name']) self.assertTrue(retValue) deviceNames = [device['name'] for device in manager.getDeviceList()] self.assertNotIn(testSensor['name'], deviceNames)
def __init__(self): """Initialize the bus and sensor info and start monitoring sensor states""" self.sensorMutex = RLock() self.exiting = Event() self.onDataChanged = None self.onSystemInfo = None self.systemData = [] self.currentSystemState = [] self.disabledSensors = {} self.disabledSensorTable = "disabled_sensors" checkAllBus() self.gpio = GPIO() manager.addDeviceInstance("GPIO", "GPIO", "GPIO", self.gpio, [], "system") manager.loadJsonDevices("rest") results = DbManager.Select(self.disabledSensorTable) if results: for row in results: self.disabledSensors[row[0]] = 1 self.StartMonitoring()
def testSetValue(self): self.setChannelFunction(GPIO().pins[7], 'OUT') self.setChannelValue(GPIO().pins[7], 1) self.setChannelValue(GPIO().pins[7], 0)
def testSetFunction(self): self.setChannelFunction(GPIO().pins[7], 'IN') self.setChannelFunction(GPIO().pins[7], 'OUT')
def testSensorInfo(self): actuator_channel = GPIO().pins[9] light_switch_channel = GPIO().pins[9] sensors = { 'actuator': { 'description': 'Digital Output', 'device': 'DigitalActuator', 'args': { 'gpio': 'GPIO', 'invert': False, 'channel': actuator_channel }, 'name': 'test_actuator' }, 'light_switch': { 'description': 'Light Switch', 'device': 'LightSwitch', 'args': { 'gpio': 'GPIO', 'invert': True, 'channel': light_switch_channel }, 'name': 'test_light_switch' }, 'MCP3004': { 'description': 'MCP3004', 'device': 'MCP3004', 'args': { 'chip': '0' }, 'name': 'test_MCP3004' }, 'distance': { 'description': 'Analog Distance Sensor', 'device': 'DistanceSensor', 'args': { 'adc': 'test_MCP3004', 'channel': 0 }, 'name': 'test_distance' } } for sensor in sensors.values(): SensorsClientTest.client.AddSensor(sensor['name'], sensor['description'], sensor['device'], sensor['args']) SensorsClientTest.client.SensorsInfo() #Test setting sensor values self.setSensorValue(sensors['actuator'], 1) self.setSensorValue(sensors['actuator'], 0) self.setSensorValue(sensors['light_switch'], 1) self.setSensorValue(sensors['light_switch'], 0) #Test getting analog value channel = 'dev:{}'.format(sensors['distance']['name']) retrievedSensorInfo = next( obj for obj in SensorsClientTest.client.SensorsInfo() if obj['channel'] == channel) self.assertGreaterEqual(retrievedSensorInfo['value'], 0.0) self.assertLessEqual(retrievedSensorInfo['value'], 1.0) for sensor in sensors.values(): self.assertTrue( SensorsClientTest.client.RemoveSensor(sensor['name']))
def testSetFunction(self): debug('testSetFunciton') self.setChannelFunction(GPIO().pins[7], 'IN') self.setChannelFunction(GPIO().pins[7], 'OUT')
def testSensorInfo(self): debug('testSensorInfo') self.config = Config(APP_SETTINGS) self.location = self.config.get('Agent', 'Location', "house0_room0_") actuator_channel = GPIO().pins[10] light_switch_channel = GPIO().pins[11] adcSensors = {} for sensor in adcSensors.values(): info('--------{} {} {}'.format(sensor['name'], sensor['description'], sensor['device'])) SensorsClientTest.client.AddSensor(sensor['name'], sensor['description'], sensor['device'], sensor['args']) sensors = { 'PCF8591': { 'description': 'PCF8591', 'index': 0, 'device': 'PCF8591', 'args': {}, 'name': 'adc' }, 'distance': { 'description': 'distance', 'index': 1, 'device': 'VL6180X', 'args': {}, 'name': self.location + 'distance' }, 'object_temperature': { 'description': 'ir_body_temperature', 'index': 2, 'device': 'MLX90614', 'args': { 'obj_temp': True }, 'name': self.location + 'ir_body' }, 'amb_temperature': { 'description': 'ir_climate_temperature', 'index': 3, 'device': 'MLX90614', 'args': { 'obj_temp': False }, 'name': self.location + 'ir_climate' }, 'luminosity': { 'description': 'luminosity', 'index': 4, 'device': 'GY30', 'args': {}, 'name': self.location + 'luminosity' }, 'co2': { 'description': 'co2', 'index': 5, 'device': 'CO2Sensor', 'args': { 'adc': 'adc', 'channel': 3 }, 'name': self.location + 'gas' }, 'h2s': { 'description': 'h2s', 'index': 6, 'device': 'MQSensor', 'args': { 'adc': 'adc', 'channel': 2 }, 'name': self.location + 'gas' }, 'nh3': { 'description': 'nh3', 'index': 6, 'device': 'MQSensor', 'args': { 'adc': 'adc', 'channel': 4 }, 'name': self.location + 'gas' }, 'climate': { 'description': 'climate', 'index': 7, 'device': 'BME280', 'args': { 'temperature': True, 'pressure': True, 'humidity': True }, 'name': self.location + 'climate' }, } for sensor in sensors.values(): # info("sensors:{}".format(sensor)) SensorsClientTest.client.RemoveSensor(sensor['name']) for sensor in sensors.values(): info('--------{} {} {}'.format(sensor['name'], sensor['description'], sensor['device'])) SensorsClientTest.client.AddSensor(sensor['name'], sensor['description'], sensor['device'], sensor['args']) # SensorsClientTest.client.SensorsInfo() #Test setting sensor values # self.setSensorValue(sensors['actuator'], 1) # self.setSensorValue(sensors['actuator'], 0) # self.setSensorValue(sensors['light_switch'], 1) # self.setSensorValue(sensors['light_switch'], 0) #Test getting analog value # channel = 'dev:{}'.format(sensors['MQ']['name']) # info(" channel -----> {} ".format(channel)) count = 0 while count < 1: info("new loop for SensorsInfo") sleep(5) for obj in SensorsClientTest.client.SensorsInfo(): info(obj) count = count + 1