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(self, func): if (func == 'getall'): return {k: v for k, v in self.config.iteritems()} elif (func == 'get'): item = bh.get_string('item') if not item: return {'error': 'Item not set'} if item in self.config: return self.config[item] elif (func == 'set'): item = bh.get_string('item') if not item: return {'error': 'Item not set'} if not item in self.config: return {'error': 'Item not found'} value = bh.get_string('value') if item == 'password' and not value == '': value = generate_password_hash(value) self.config[item] = value result = self.config.validate(self.validator) return bh.success_response() bh.raise404()
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(self, func): if (func == 'getall'): return {k:v for k, v in self.config.iteritems()} elif (func == 'get'): item = bh.get_string('item') if not item: return { 'error' : 'Item not set' } if item in self.config: return self.config[item] elif (func == 'set'): item = bh.get_string('item') if not item: return { 'error' : 'Item not set' } if not item in self.config: return { 'error' : 'Item not found' } value = bh.get_string('value') if item == 'password' and not value == '': value = generate_password_hash(value) self.config[item] = value result = self.config.validate(self.validator) return bh.success_response() bh.raise404()
def route_devices(self, func): if not func == 'list': bh.raise404() supportedMethods = self.get_supported_methods() self.load_devices() return { 'device': [ self.device_to_dict(device, supportedMethods, False) for k, device in self.devices.iteritems() ]}
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 device_set_parameter(self, device, attr): if (attr == 'parameter'): resp = device.set_parameter(bh.get_string('parameter'), bh.get_string('value')) elif attr in ['name', 'model', 'protocol']: value = bh.get_string(attr) if value is None: return "Attribute \"" + attr + "\" not found" resp = device.__setattr__(attr, value) else: bh.raise404() if resp: return TELLSTICK_SUCCESS else: return TELLSTICK_ERROR_NOT_FOUND
def route_all(self, out_format, ftype, func): print request if ftype in self.callbacks: if (self.check_apikey()): func = func.strip().lower() resp = self.callbacks[ftype](func) else: resp = { 'error': 'key not valid' } return bh.format_response(resp, out_format, ftype, self.config['pretty_print']) bh.raise404()
def route_all(self, out_format, ftype, func): print request if ftype in self.callbacks: if (self.check_apikey()): func = func.strip().lower() resp = self.callbacks[ftype](func) else: resp = {'error': 'key not valid'} return bh.format_response(resp, out_format, ftype, self.config['pretty_print']) bh.raise404()
def route_all(self, out_format, ftype, func): if ftype in self.allroutes: funcs = self.allroutes[ftype] funcName = func.strip().lower() if funcName in funcs: if self.check_apikey(): func = funcs[funcName] args = self.get_inputs(func['inputs']) resp = func['fn'](funcName, *args) else: resp = { 'error': 'key not valid' } return bh.format_response(resp, out_format, ftype, self.config['pretty_print']) bh.raise404()
def route_all(self, out_format, ftype, func): if ftype in self.allroutes: funcs = self.allroutes[ftype] funcName = func.strip().lower() if funcName in funcs: if self.check_apikey(): func = funcs[funcName] args = self.get_inputs(func['inputs']) resp = func['fn'](funcName, *args) else: resp = {'error': 'key not valid'} return bh.format_response(resp, out_format, ftype, self.config['pretty_print']) bh.raise404()
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 route_clients(self, func): if not func == 'list': bh.raise404() return { 'client': [self.get_client_info()] }