Beispiel #1
0
 def RemoveCallbacks(self):
     """Remove callback function for all digital devices"""
     devices = manager.getDeviceList()
     for device in devices:
         sensor = instance.deviceInstance(device['name'])
         if 'DigitalSensor' in device['type'] and hasattr(sensor, 'removeCallback'):
             sensor.removeCallback()
Beispiel #2
0
 def InitCallbacks(self):
     """Set callback function for any digital devices that support them"""
     devices = manager.getDeviceList()
     for device in devices:
         sensor = instance.deviceInstance(device['name'])
         if 'DigitalSensor' in device['type'] and hasattr(sensor, 'setCallback'):
             debug('Set callback for {}'.format(sensor))
             sensor.setCallback(self.OnSensorChange, device)
             if not self.realTimeMonitorRunning:
                 ThreadPool.Submit(self.RealTimeMonitor)
 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)
Beispiel #4
0
 def SensorsInfo(self):
     """Return a list with current sensor states for all enabled sensors"""
     manager.deviceDetector()
     devices = manager.getDeviceList()
     sensors_info = []
     if devices is None:
         return sensors_info
     for device in devices:
         sensor = instance.deviceInstance(device['name'])
         if 'enabled' not in device or device['enabled'] == 1:
             sensor_types = {'Temperature': {'function': 'getCelsius', 'data_args': {'type': 'temp', 'unit': 'c'}},
                             'Humidity': {'function': 'getHumidityPercent', 'data_args': {'type': 'rel_hum', 'unit': 'p'}},
                             'Pressure': {'function': 'getPascal', 'data_args': {'type': 'bp', 'unit': 'pa'}},
                             'Luminosity': {'function': 'getLux', 'data_args': {'type': 'lum', 'unit': 'lux'}},
                             'Distance': {'function': 'getCentimeter', 'data_args': {'type': 'prox', 'unit': 'cm'}},
                             'ServoMotor': {'function': 'readAngle', 'data_args': {'type': 'analog_actuator'}},
                             'DigitalSensor': {'function': 'read', 'data_args': {'type': 'digital_sensor', 'unit': 'd'}},
                             'DigitalActuator': {'function': 'read', 'data_args': {'type': 'digital_actuator', 'unit': 'd'}},
                             'AnalogSensor': {'function': 'readFloat', 'data_args': {'type': 'analog_sensor'}},
                             'AnalogActuator': {'function': 'readFloat', 'data_args': {'type': 'analog_actuator'}}}
             # extension_types = {'ADC': {'function': 'analogReadAllFloat'},
             #                     'DAC': {'function': 'analogReadAllFloat'},
             #                     'PWM': {'function': 'pwmWildcard'},
             #                     'GPIOPort': {'function': 'wildcard'}}
             for device_type in device['type']:
                 try:
                     display_name = device['description']
                 except:
                     display_name = None
                 if device_type in sensor_types:
                     try:
                         sensor_type = sensor_types[device_type]
                         func = getattr(sensor, sensor_type['function'])
                         if len(device['type']) > 1:
                             channel = '{}:{}'.format(device['name'], device_type.lower())
                         else:
                             channel = device['name']
                         value = self.CallDeviceFunction(func)
                         cayennemqtt.DataChannel.add(sensors_info, cayennemqtt.DEV_SENSOR, channel, value=value, name=display_name, **sensor_type['data_args'])
                         if 'DigitalActuator' == device_type and value in (0, 1):
                             manager.updateDeviceState(device['name'], value)
                     except:
                         exception('Failed to get sensor data: {} {}'.format(device_type, device['name']))
                 # else:
                 #     try:
                 #         extension_type = extension_types[device_type]
                 #         func = getattr(sensor, extension_type['function'])
                 #         values = self.CallDeviceFunction(func)
                 #         for pin, value in values.items():
                 #             cayennemqtt.DataChannel.add(sensors_info, cayennemqtt.DEV_SENSOR, device['name'] + ':' + str(pin), cayennemqtt.VALUE, value, name=display_name)
                 #     except:
                 #         exception('Failed to get extension data: {} {}'.format(device_type, device['name']))
     info('Sensors info: {}'.format(sensors_info))
     return sensors_info
Beispiel #5
0
 def GetDevices(self):
     device_list = manager.getDeviceList()
     devices = []
     for dev in device_list:
         try:
             if len(dev['type']) == 0:
                 self.AppendToDeviceList(devices, dev, '')
             else:
                 for device_type in dev['type']:
                     self.AppendToDeviceList(devices, dev, device_type)
         except:
             exception("Failed to get device: {}".format(dev))
     return devices