def device_globals(did): #- if static field == 1 => this is a static param #- if static field == 0 and no sensor id is defined => this is a device param => value will be filled in #- if statis == 0 and it has a sensor id => its a dynamic param device = urlHandler.db.get_device(did) js = get_device_params(device['device_type_id']) for x in urlHandler.db.get_xpl_command_by_device_id(did): for p in js['global']: if p["xpl"] is True: urlHandler.db.add_xpl_command_param(cmd_id=x.id, key=p['key'], value=request.form.get( p['key'])) for x in urlHandler.db.get_xpl_stat_by_device_id(did): for p in js['global']: if p["xpl"] is True: #urlHandler.db.add_xpl_stat_param(statid=x.id, key=p['key'], value=request.form.get(p['key']), static=True) urlHandler.db.add_xpl_stat_param(statid=x.id, key=p['key'], value=request.form.get( p['key']), static=True, type=p['type']) for p in js['global']: if p["xpl"] is not True: urlHandler.db.add_device_param(did, p["key"], request.form.get(p['key']), p["type"]) urlHandler.reload_stats() return 200, "{}"
def put(self, did): b = urlHandler.db.update_device( did, request.form.get('name'), request.form.get('description'), request.form.get('reference'), ) urlHandler.reload_stats() return 200, urlHandler.db.get_device(did)
def post(self): """ Create a new device Get all the clients details Finally, call the database function to create the device and give it the device types list and clients details : they will be used to fill the database as the json structure is recreated in the database """ cli = MQSyncReq(urlHandler.zmq_context) #self.log.info(u"Device creation request for {0} {1} on {2} : name = '{3}', device_type = '{4}', reference = '{5}'".format(request.form.get('type'), request.form.get('id'), request.form.get('host'), request.form.get('device_type'), request.form.get('reference'))) #urlHandler.log.info("Device creation request for {0} {1} on {2} : name = '{3}', device_type = '{4}', reference = '{5}'".format(request.form.get('type'), request.form.get('id'), request.form.get('host'), request.form.get('device_type'), request.form.get('reference'))) # get the client details msg = MQMessage() msg.set_action('client.detail.get') res = cli.request('manager', msg.get(), timeout=10) if res is None: return 500, "Error while getting the clients details" # create the full client id : #if request.form.get('type') == "plugin": # client_id = "{0}-{1}.{2}".format(DMG_VENDOR_ID, request.form.get('id'), request.form.get('host')) #else: # client_id = "{0}-{1}.{2}".format(request.form.get('type'), request.form.get('id'), request.form.get('host')) # get the corresponding json all_clients_data = res.get_data() # extract the interesting part of the json (just the client part) if all_clients_data.has_key(request.form.get('client_id')): client_data = all_clients_data[request.form.get( 'client_id')]['data'] else: return 500, "Error : there is no client id named '{0}'".format( request.form.get('client_id')) # create the device in database # notice that we don't give any address for the device as this will be done with another url later created_device = urlHandler.db.add_device_and_commands( name=request.form.get('name'), device_type=request.form.get('device_type'), client_id=request.form.get('client_id'), description=request.form.get('description'), reference=request.form.get('reference'), client_data=client_data) urlHandler.reload_stats() return 201, created_device
def post(self): """ Create a new device Get all the clients details Finally, call the database function to create the device and give it the device types list and clients details : they will be used to fill the database as the json structure is recreated in the database """ cli = MQSyncReq(urlHandler.zmq_context) #self.log.info(u"Device creation request for {0} {1} on {2} : name = '{3}', device_type = '{4}', reference = '{5}'".format(request.form.get('type'), request.form.get('id'), request.form.get('host'), request.form.get('device_type'), request.form.get('reference'))) #urlHandler.log.info("Device creation request for {0} {1} on {2} : name = '{3}', device_type = '{4}', reference = '{5}'".format(request.form.get('type'), request.form.get('id'), request.form.get('host'), request.form.get('device_type'), request.form.get('reference'))) # get the client details msg = MQMessage() msg.set_action('client.detail.get') res = cli.request('manager', msg.get(), timeout=10) if res is None: return 500, "Error while getting the clients details" # create the full client id : #if request.form.get('type') == "plugin": # client_id = "{0}-{1}.{2}".format(DMG_VENDOR_ID, request.form.get('id'), request.form.get('host')) #else: # client_id = "{0}-{1}.{2}".format(request.form.get('type'), request.form.get('id'), request.form.get('host')) # get the corresponding json all_clients_data = res.get_data() # extract the interesting part of the json (just the client part) if all_clients_data.has_key(request.form.get('client_id')): client_data = all_clients_data[request.form.get('client_id')]['data'] else: return 500, "Error : there is no client id named '{0}'".format(request.form.get('client_id')) # create the device in database # notice that we don't give any address for the device as this will be done with another url later created_device = urlHandler.db.add_device_and_commands( name=request.form.get('name'), device_type=request.form.get('device_type'), client_id=request.form.get('client_id'), description=request.form.get('description'), reference=request.form.get('reference'), client_data=client_data ) urlHandler.reload_stats() return 201, created_device
def device_xplstat_params(did): cmd = urlHandler.db.get_xpl_stat(device_id) if cmd == None: # ERROR return # get the device dev = urlHandler.db.get_device(cmd.device_id) if dev == None: # ERROR return # get the device_type dt = urlHandler.db.get_device_type_by_id(dev.device_type_id) if dt == None: # ERROR return # get the json cli = MQSyncReq(urlHandler.zmq_context) msg = MQMessage() msg.set_action('device_types.get') msg.add_data('device_type', dev.device_type_id) res = cli.request('manager', msg.get(), timeout=10) if res is None: return "Bad device type" pjson = res.get_data() if pjson['json_version'] < 2: # ERROR return # get the json device params for this command if pjson['xpl_stats'][cmd.name] is None: # ERROR return for p in pjson['xpl_stats'][cmd.name]['parameters']['device']: if request.form.get(p['key']) is None: # ERROR return # go and add the param urlHandler.db.add_xpl_stat_param(cmd_id=cmd.id, key=p['key'], value=request.form.get(['key'])) urlHandler.reload_stats() return 204, ""
def device_globals(did): #- if static field == 1 => this is a static param #- if static field == 0 and no sensor id is defined => this is a device param => value will be filled in #- if statis == 0 and it has a sensor id => its a dynamic param device = urlHandler.db.get_device(did) js = get_device_params(device['device_type_id']) for x in urlHandler.db.get_xpl_command_by_device_id(did): for p in js['global']: if p["xpl"] is True: urlHandler.db.add_xpl_command_param(cmd_id=x.id, key=p['key'], value=request.form.get(p['key'])) for x in urlHandler.db.get_xpl_stat_by_device_id(did): for p in js['global']: if p["xpl"] is True: #urlHandler.db.add_xpl_stat_param(statid=x.id, key=p['key'], value=request.form.get(p['key']), static=True) urlHandler.db.add_xpl_stat_param(statid=x.id, key=p['key'], value=request.form.get(p['key']), static=True, type=p['type']) for p in js['global']: if p["xpl"] is not True: urlHandler.db.add_device_param(did, p["key"], request.form.get(p['key']), p["type"]) urlHandler.reload_stats() return 200, "{}"
def post(self): """ Create a new device Get all the clients details Finally, call the database function to create the device and give it the device types list and clients details : they will be used to fill the database as the json structure is recreated in the database """ print "==========" print request.form type(json.loads(request.form.get('params'))) cli = MQSyncReq(urlHandler.zmq_context) msg = MQMessage() msg.set_action('device.create') msg.set_data({'data': json.loads(request.form.get('params'))}) res = cli.request('dbmgr', msg.get(), timeout=10) if res is not None: data = res.get_data() if data["status"]: urlHandler.reload_stats() return 201, data["result"] else: return 500, data["reason"] else: return 500, "DbMgr did not respond on the device.create, check the logs" return 201, null
def put(self, did): """ @api {put} /device/<id> Update a specifick device @apiName putDevice @apiGroup Device @apiParamTitle (url) Url parameters @apiParam (url) {Number} id The deviceId to update @apiParamTitle (data) Post parameters @apiParam (data) {String} name The new name for the device @apiParam (data) {String} description The new description for the device @apiParam (data) {String} reference The new reference for the device @apiSuccess {json} result The json representation of the updated device @apiSuccessExample Success-Response: HTTTP/1.1 200 OK { "xpl_stats": { "get_temp": { "json_id": "get_temp", "schema": "sensor.basic", "id": 4, "parameters": { "dynamic": [ { "ignore_values": "", "sensor_name": "temp", "key": "current" } ], "static": [ { "type": "integer", "value": "2", "key": "device" }, { "type": null, "value": "temp", "key": "type" }, { "type": null, "value": "c", "key": "units" } ] }, "name": "get_temp" }, ... }, "commands": { ... }, "description": "Test Temp", "reference": "VMB1TS", "xpl_commands": { ... }, "client_id": "plugin-velbus.igor", "device_type_id": "velbus.temp", "sensors": { "temp": { "value_min": 21.875, "data_type": "DT_Temp", "incremental": false, "id": 4, "reference": "temp", "conversion": "", "name": "temp_sensor", "last_received": 1410857216, "timeout": 0, "formula": null, "last_value": "29.1875", "value_max": 37.4375 } }, "parameters": { ... }, "id": 3, "name": "Temp elentrik" } @apiErrorExample Error-Response: HTTTP/1.1 404 Not Found """ b = urlHandler.db.update_device( did, request.form.get('name'), request.form.get('description'), request.form.get('reference'), ) urlHandler.reload_stats() return 200, urlHandler.db.get_device(did)
def post(self): """ @api {post} /device Create a device @apiName postDevice @apiGroup Device @apiParamTitle (data) Post parameters @apiParam (data) {Json} params The populated device params json string @apiSuccess {json} result The json representation of the created device @apiSuccessExample Success-Response: HTTTP/1.1 201 Created { "xpl_stats": { "get_temp": { "json_id": "get_temp", "schema": "sensor.basic", "id": 4, "parameters": { "dynamic": [ { "ignore_values": "", "sensor_name": "temp", "key": "current" } ], "static": [ { "type": "integer", "value": "2", "key": "device" }, { "type": null, "value": "temp", "key": "type" }, { "type": null, "value": "c", "key": "units" } ] }, "name": "get_temp" }, ... }, "commands": { ... }, "description": "Test Temp", "reference": "VMB1TS", "xpl_commands": { ... }, "client_id": "plugin-velbus.igor", "device_type_id": "velbus.temp", "sensors": { "temp": { "value_min": 21.875, "data_type": "DT_Temp", "incremental": false, "id": 4, "reference": "temp", "conversion": "", "name": "temp_sensor", "last_received": 1410857216, "timeout": 0, "formula": null, "last_value": "29.1875", "value_max": 37.4375 } }, "parameters": { ... }, "id": 3, "name": "Temp elentrik" } @apiErrorExample Error-Response: HTTTP/1.1 500 Internal Server Error """ cli = MQSyncReq(urlHandler.zmq_context) msg = MQMessage() msg.set_action('device.create') msg.set_data({'data': json.loads(request.form.get('params'))}) res = cli.request('dbmgr', msg.get(), timeout=10) if res is not None: data = res.get_data() if data["status"]: urlHandler.reload_stats() return 201, data["result"] else: return 500, data["reason"] else: return 500, "DbMgr did not respond on the device.create, check the logs" return 201, None
def delete(self, did): b = urlHandler.db.del_device(did) urlHandler.reload_stats() return 200, b