def route_device(self, func): if (func == 'add'): id = '' resp = self.add_device() if type(resp) is td.Device: id = resp.id resp = TELLSTICK_SUCCESS return self.map_response(resp, id) else: """ With the only function that does not require ID out of the way, determine the device we want to interact with """ id = bh.get_int('id') self.load_devices() if (self.devices.has_key(id)): device = self.devices[id] if (func == 'info'): return self.device_to_dict(device, self.get_supported_methods(), True) elif (func[:3] == 'set'): resp = self.device_set_parameter(device, func[3:]) elif (func == 'command'): resp = self.device_command(device, bh.get_int('method'), bh.get_int('value')) else: resp = self.device_command(device, func, bh.get_int('level')) if resp is None: bh.raise404() else: resp = "Device " + "\"" + str(id) + "\" not found!" return self.map_response(resp)
def route_client(self, func): if not func == 'info': bh.raise404() clientid = clientid = bh.get_int('id') if (clientid != self.config['client_id']): return { "error" : "Client \"" + str(clientid) + "\" not found!" } return self.get_client_info()
def route_sensors(self, func): if not func == 'list': bh.raise404() self.load_sensors() includeIgnored = True if bh.get_int('includeignored') == 1 else False return { 'sensor': [ self.sensor_to_dict(sensor, False) for id, sensor in self.sensors.iteritems() if includeIgnored or int(sensor.ignore) == 0 ]}
def route_sensor(self, func): # The ID should be an integer, but we store them in the dictionary as # strings, so treat as such id = str(bh.get_int('id')) self.load_sensors() resp = TELLSTICK_SUCCESS if (self.sensors.has_key(id)): sensor = self.sensors[id] if (func == 'info'): return self.sensor_to_dict(sensor, True) elif (func == 'setignore'): sensor.ignore = 1 if bh.get_int('ignore') == 1 else 0 elif (func == 'setname'): sensor.name = bh.get_string('name') if resp is None: bh.raise404() else: resp = "Sensor " + "\"" + str(id) + "\" not found!" return self.map_response(resp)
def add_device(self): if (self.config['editable'] is False): return "Client is not editable" clientid = bh.get_int('id') if (clientid != self.config['client_id']): return "Client \"" + str(clientid) + "\" not found!" try: return self.core.add_device( bh.get_string('name'), bh.get_string('protocol'), bh.get_string('model')) except Exception as e: return e
def get_supported_methods(self): return bh.get_int('supportedMethods') or 0