def scenario_edit(id): # Fetch all known actions actions = [] cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('action.list') res = cli.request('scenario', msg.get(), timeout=10) if res is not None: res = res.get_data() if 'result' in res: res = json.loads(res['result']) actions = res.keys() # Fetch all known tests tests = [] cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('test.list') res = cli.request('scenario', msg.get(), timeout=10) if res is not None: res = res.get_data() if 'result' in res: res = json.loads(res['result']) tests = res.keys() # TODO laod the json for this scenario # if not exists send None == new jso = None return render_template('scenario_edit.html', mactive = "scenario", actions = actions, tests = tests, json = jso)
def _mdp_reply_config_set(self, data): """ Reply to config.set MQ req @param data : MQ req message """ print "#################" msg = MQMessage() msg.set_action('config.result') status = True msg_data = data.get_data() if 'type' not in msg_data: status = False reason = "Config set : missing 'type' field : {0}".format(data) if msg_data['type'] != "plugin": status = False reason = "Config set not available for type={0}".format( msg_data['type']) if 'name' not in msg_data: status = False reason = "Config set : missing 'name' field : {0}".format(data) if 'host' not in msg_data: status = False reason = "Config set : missing 'host' field : {0}".format(data) if 'data' not in msg_data: status = False reason = "Config set : missing 'data' field : {0}".format(data) if status == False: self.log.error(reason) else: reason = "" type = msg_data['type'] name = msg_data['name'] host = msg_data['host'] data = msg_data['data'] msg.add_data('type', type) msg.add_data('name', name) msg.add_data('host', host) try: # we add a configured key set to true to tell the UIs and plugins that there are some configuration elements self._db.set_plugin_config(name, host, "configured", True) for key in msg_data['data']: self._db.set_plugin_config(name, host, key, data[key]) self.publish_config_updated(type, name, host) except: reason = "Error while setting configuration for '{0} {1} on {2}' : {3}".format( type, name, host, traceback.format_exc()) status = False self.log.error(reason) msg.add_data('status', status) msg.add_data('reason', reason) self.log.debug(msg.get()) self.reply(msg.get())
def _mdp_reply_config_set(self, data): """ Reply to config.set MQ req @param data : MQ req message """ print "#################" msg = MQMessage() msg.set_action('config.result') status = True msg_data = data.get_data() if 'type' not in msg_data: status = False reason = "Config set : missing 'type' field : {0}".format(data) if msg_data['type'] != "plugin": status = False reason = "Config set not available for type={0}".format(msg_data['type']) if 'name' not in msg_data: status = False reason = "Config set : missing 'name' field : {0}".format(data) if 'host' not in msg_data: status = False reason = "Config set : missing 'host' field : {0}".format(data) if 'data' not in msg_data: status = False reason = "Config set : missing 'data' field : {0}".format(data) if status == False: self.log.error(reason) else: reason = "" type = msg_data['type'] name = msg_data['name'] host = msg_data['host'] data = msg_data['data'] msg.add_data('type', type) msg.add_data('name', name) msg.add_data('host', host) try: # we add a configured key set to true to tell the UIs and plugins that there are some configuration elements self._db.set_plugin_config(name, host, "configured", True) for key in msg_data['data']: self._db.set_plugin_config(name, host, key, data[key]) self.publish_config_updated(type, name, host) except: reason = "Error while setting configuration for '{0} {1} on {2}' : {3}".format(type, name, host, traceback.format_exc()) status = False self.log.error(reason) msg.add_data('status', status) msg.add_data('reason', reason) self.log.debug(msg.get()) self.reply(msg.get())
def scenario_edit(id): default_json = '{"type":"dom_condition","id":"1","deletable":false}' # laod the json if id == 0: name = "ikke" jso = default_json else: # TODO laod from DB jso = default_json name = "new" # create a form class F(Form): sid = HiddenField("id", default=id) sname = TextField("name", default=name) sjson = HiddenField("json") submit = SubmitField("Send") pass form = F() if request.method == 'POST' and form.validate(): print request.form flash(gettext("Changes saved"), "success") return redirect("/scenario") pass else: # Fetch all known actions actions = [] cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('action.list') res = cli.request('scenario', msg.get(), timeout=10) if res is not None: res = res.get_data() if 'result' in res: res = json.loads(res['result']) actions = res.keys() # Fetch all known tests tests = [] cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('test.list') res = cli.request('scenario', msg.get(), timeout=10) if res is not None: res = res.get_data() if 'result' in res: res = json.loads(res['result']) tests = res.keys() # ouput return render_template('scenario_edit.html', mactive = "scenario", form = form, name = name, actions = actions, tests = tests, jso = jso)
def _mdp_reply(self, plugin, hostname, key, value, element=None): msg = MQMessage() msg.setaction( 'config.result' ) msg.add_data('plugin', plugin) msg.add_data('hostname', hostname) msg.add_data('key', key) msg.add_data('value', value) if element: msg.add_data('element', element) print msg.get() self.reply(msg.get())
def _mdp_reply(self, plugin, hostname, key, value, element=None): msg = MQMessage() msg.setaction('config.result') msg.add_data('plugin', plugin) msg.add_data('hostname', hostname) msg.add_data('key', key) msg.add_data('value', value) if element: msg.add_data('element', element) print msg.get() self.reply(msg.get())
def _mdp_reply_config_delete(self, data): """ Reply to config.delete MQ req Delete all the config items for the given type, name and host @param data : MQ req message """ msg = MQMessage() msg.set_action('config.result') status = True msg_data = data.get_data() if 'type' not in msg_data: status = False reason = "Config request : missing 'type' field : {0}".format(data) if msg_data['type'] != "plugin": status = False reason = "Config request not available for type={0}".format( msg_data['type']) if 'name' not in msg_data: status = False reason = "Config request : missing 'name' field : {0}".format(data) if 'host' not in msg_data: status = False reason = "Config request : missing 'host' field : {0}".format(data) if status == False: self.log.error(reason) else: reason = "" type = msg_data['type'] name = msg_data['name'] host = msg_data['host'] msg.add_data('type', type) msg.add_data('name', name) msg.add_data('host', host) try: self._db.del_plugin_config(name, host) self.log.info(u"Delete config for {0} {1}".format(type, name)) self.publish_config_updated(type, name, host) except: status = False reason = "Error while deleting configuration for '{0} {1} on {2} : {3}".format( type, name, host, traceback.format_exc()) self.log.error(reason) msg.add_data('reason', reason) msg.add_data('status', status) self.log.debug(msg.get()) self.reply(msg.get())
def _mdp_reply_config_delete(self, data): """ Reply to config.delete MQ req Delete all the config items for the given type, name and host @param data : MQ req message """ msg = MQMessage() msg.set_action('config.result') status = True msg_data = data.get_data() if 'type' not in msg_data: status = False reason = "Config request : missing 'type' field : {0}".format(data) if msg_data['type'] != "plugin": status = False reason = "Config request not available for type={0}".format(msg_data['type']) if 'name' not in msg_data: status = False reason = "Config request : missing 'name' field : {0}".format(data) if 'host' not in msg_data: status = False reason = "Config request : missing 'host' field : {0}".format(data) if status == False: self.log.error(reason) else: reason = "" type = msg_data['type'] name = msg_data['name'] host = msg_data['host'] msg.add_data('type', type) msg.add_data('name', name) msg.add_data('host', host) try: self._db.del_plugin_config(name, host) self.log.info(u"Delete config for {0} {1}".format(type, name)) self.publish_config_updated(type, name, host) except: status = False reason = "Error while deleting configuration for '{0} {1} on {2} : {3}".format(type, name, host, traceback.format_exc()) self.log.error(reason) msg.add_data('reason', reason) msg.add_data('status', status) self.log.debug(msg.get()) self.reply(msg.get())
def query(self, plugin, key, element='', nb_test=QUERY_CONFIG_NUM_TRY): ''' Ask the config system for the value. Calling this function will make your program wait until it got an answer @param plugin : the plugin of the item requesting the value, must exists in the config database @param element : the name of the element which requests config, None if it's a technolgy global parameter @param key : the key to fetch corresponding value, if it's an empty string, all the config items for this technology will be fetched ''' msg = MQMessage() msg._action = 'config.get' msg._data = { 'plugin': plugin, 'key': key, 'element': element, 'hostname': get_sanitized_hostname() } ret = self.cli.request('dbmgr', msg.get(), timeout=QUERY_CONFIG_WAIT) if ret is None: return None else: if 'value' in ret._data.keys(): return ret._data['value'] else: return None
def test_XplCmd(self, parameters = {}, statParams = None): """Send an Xpl cmd with statics parameters by request, if necessary start testcase listener for ack message and check if insert in database is ok. """ if self.get_return_confirmation() : schema, data, statResult = self.get_XplStat_fromAck(statParams) th = threading.Thread(None, self.assert_Xpl_Stat_Ack_Wait, "th_test_0110_xpl-ack_from_{0}".format(self.command_name), (schema, data, statResult)) th.start() time.sleep(1) else : print (u"No ack required for {0}".format(self.command_name)) if self._device and self.command_id : cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('cmd.send') msg.add_data('cmdid', self.command_id) msg.add_data('cmdparams', parameters) print (u"Send xpl_cmnd {0}".format(self.command_name)) cli.request('xplgw', msg.get(), timeout=10) # if self.get_return_confirmation() : # self.assert_get_last_command_in_db(statResult) # else : # print (u"No ack required for {0}".format(self.command_name)) return True else : return False
def _mdp_reply_plugin_stop(self, data): """ Stop the plugin @param data : MQ req message First, send the MQ Rep to 'ack' the request Then, change the plugin status to STATUS_STOP_REQUEST Then, quit the plugin by calling force_leave(). This should make the plugin send a STATUS_STOPPED if all is ok Notice that no check is done on the MQ req content : we need nothing in it as it is directly addressed to a plugin """ # check if the message is for us content = data.get_data() if content['name'] != self._name or content[ 'host'] != self.get_sanitized_hostname(): return ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('plugin.stop.result') status = True reason = "" msg.add_data('status', status) msg.add_data('reason', reason) self.reply(msg.get()) ### Change the plugin status self._set_status(STATUS_STOP_REQUEST) ### Try to stop the plugin # if it fails, the manager should try to kill the plugin self.force_leave()
def _mdp_reply_plugin_start(self, data): """ Reply on the MQ @param data : msg REQ received """ msg = MQMessage() msg.set_action('plugin.start.result') if 'name' not in data.get_data().keys(): status = False reason = "Plugin startup request : missing 'name' field" self.log.error(reason) else: name = data.get_data()['name'] msg.add_data('name', name) # try to start the plugin pid = self._plugins[name].start() if pid != 0: status = True reason = "" else: status = False reason = "Plugin '{0}' startup failed".format(name) msg.add_data('status', status) msg.add_data('reason', reason) self.reply(msg.get())
def get_device_list(self, quit_if_no_device=False): """ Request the dbmgr component over MQ to get the devices list for this client @param quit_if_no_device: if True, exit the plugin if there is no devices """ self.log.info(u"Retrieve the devices list for this client...") mq_client = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('device.get') msg.add_data('type', 'plugin') msg.add_data('name', self._name) msg.add_data('host', self.get_sanitized_hostname()) result = mq_client.request('dbmgr', msg.get(), timeout=10) if not result: self.log.error(u"Unable to retrieve the device list") self.force_leave() return [] else: device_list = result.get_data()['devices'] if device_list == []: self.log.warn(u"There is no device created for this client") if quit_if_no_device: self.log.warn( u"The developper requested to stop the client if there is no device created" ) self.force_leave() return [] for a_device in device_list: self.log.info(u"- id : {0} / name : {1} / device type id : {2}".format(a_device['id'], \ a_device['name'], \ a_device['device_type_id'])) # log some informations about the device # notice that even if we are not in the XplPlugin class we will display xpl related informations : # for some no xpl plugins, there will just be nothing to display. # first : the stats self.log.info(u" xpl_stats features :") for a_xpl_stat in a_device['xpl_stats']: self.log.info(u" - {0}".format(a_xpl_stat)) self.log.info(u" Static Parameters :") for a_feature in a_device['xpl_stats'][a_xpl_stat][ 'parameters']['static']: self.log.info(u" - {0} = {1}".format( a_feature['key'], a_feature['value'])) self.log.info(u" Dynamic Parameters :") for a_feature in a_device['xpl_stats'][a_xpl_stat][ 'parameters']['dynamic']: self.log.info(u" - {0}".format(a_feature['key'])) # then, the commands self.log.info(u" xpl_commands features :") for a_xpl_cmd in a_device['xpl_commands']: self.log.info(u" - {0}".format(a_xpl_cmd)) self.log.info(u" + Parameters :") for a_feature in a_device['xpl_commands'][a_xpl_cmd][ 'parameters']: self.log.info(u" - {0} = {1}".format( a_feature['key'], a_feature['value'])) self.devices = device_list return device_list
def check_config(type, name, host, key, exp_value): cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('config.get') msg.add_data('type', type) msg.add_data('host', host) msg.add_data('name', name) msg.add_data('key', key) result = cli.request('dbmgr', msg.get(), timeout=10) if result: data = result.get_data() if 'status' in data: if not data['status']: print(result.get()) raise RuntimeError("DbMgr did not return status true on a config.set for {0}-{1}.{2} : {3} = {4}".format(type, name, host, key, value)) else: if 'value' in data: if data['value'] != exp_value: print(result.get()) raise RuntimeError("The returned value is not the expected value for {0}-{1}.{2} : {3} = {4} but received {5}".format(type, name, host, key, exp_value, data['value'])) else: return True else: print(result.get()) raise RuntimeError("DbMgr did not return a value on a config.set for {0}-{1}.{2} : {3} = {4}".format(type, name, host, key, value)) else: print(result.get()) raise RuntimeError("DbMgr did not return a status on a config.set for {0}-{1}.{2} : {3} = {4}".format(type, name, host, key, value)) else: raise RuntimeError("Error while setting configuration for {0}-{1}.{2} : {3} = {4}".format(type, name, host, key, value))
def WSCommandSend(self, data): cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('cmd.send') msg.add_data('cmdid', data['command_id']) msg.add_data('cmdparams', data['parameters']) return cli.request('xplgw', msg.get(), timeout=10).get()
def request_stop(self): """ Request the plugin to stop """ print(u"Request plugin to stop : '{0}' on '{1}'".format( self.name, self.host)) cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('plugin.stop.do') msg.add_data('name', self.name) msg.add_data('host', self.host) result = cli.request("plugin-{0}.{1}".format(self.name, self.host), msg.get(), timeout=30) if result: msgid, content = result.get() content = json.loads(content) print(u"Response : {0}".format(content)) if content['status']: print(u"Plugin stopped") return True else: print(u"Error : plugin not stopped") return False else: raise RuntimeError( "MQ Timeout when requesting to stop the plugin (the plugin didn't stop itself)" )
def client_devices_new(client_id): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('client.detail.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: detaila = res.get_data() data = detaila[client_id]['data'] else: data = {} if type(data["device_types"]) is not dict: dtypes = {} else: dtypes = list(data["device_types"].keys()) products = {} if "products" in data: for prod in data["products"]: products[prod["name"]] = prod["type"] return render_template('client_device_new.html', device_types = dtypes, products = products, clientid = client_id, mactve="clients", active = 'devices' )
def client_devices_edit(client_id, did): with app.db.session_scope(): device = app.db.get_device_sql(did) MyForm = model_form(Device, \ base_class=Form, \ db_session=app.db.get_session(), exclude=['params', 'commands', 'sensors', 'address', 'xpl_commands', 'xpl_stats', 'device_type_id', 'client_id', 'client_version']) form = MyForm(request.form, device) if request.method == 'POST' and form.validate(): # save it app.db.update_device(did, \ d_name=request.form['name'], \ d_description=request.form['description'], \ d_reference=request.form['reference']) # message the suer flash(gettext("Device saved"), 'success') # reload stats req = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action( 'reload' ) resp = req.request('xplgw', msg.get(), 100) # redirect return redirect("/client/{0}/dmg_devices/known".format(client_id)) else: return render_template('client_device_edit.html', form = form, clientid = client_id, mactive="clients", active = 'devices' )
def client_devices_edit(client_id, did): with app.db.session_scope(): device = app.db.get_device_sql(did) MyForm = model_form(Device, \ base_class=Form, \ db_session=app.db.get_session(), exclude=['params', 'commands', 'sensors', 'address', 'xpl_commands', 'xpl_stats', 'device_type_id', 'client_id', 'client_version']) form = MyForm(request.form, device) if request.method == 'POST' and form.validate(): # save it app.db.update_device(did, \ d_name=request.form['name'], \ d_description=request.form['description'], \ d_reference=request.form['reference']) # message the suer flash(gettext("Device saved"), 'success') # reload stats req = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('reload') resp = req.request('xplgw', msg.get(), 100) # redirect return redirect("/client/{0}/devices/known".format(client_id)) else: return render_template('client_device_edit.html', form=form, clientid=client_id, mactve="clients", active='devices')
def client_devices_new(client_id): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('client.detail.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: detaila = res.get_data() data = detaila[client_id]['data'] else: data = {} if type(data["device_types"]) is not dict: dtypes = {} else: dtypes = list(data["device_types"].keys()) products = {} if "products" in data: for prod in data["products"]: products[prod["name"]] = prod["type"] return render_template('client_device_new.html', device_types=dtypes, products=products, clientid=client_id, mactve="clients", active='devices')
def _mdp_reply_plugin_stop(self, data): """ Stop the plugin @param data : MQ req message First, send the MQ Rep to 'ack' the request Then, change the plugin status to STATUS_STOP_REQUEST Then, quit the plugin by calling force_leave(). This should make the plugin send a STATUS_STOPPED if all is ok Notice that no check is done on the MQ req content : we need nothing in it as it is directly addressed to a plugin """ # check if the message is for us content = data.get_data() if content['name'] != self._name or content['host'] != self.get_sanitized_hostname(): return ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('plugin.stop.result') status = True reason = "" msg.add_data('status', status) msg.add_data('reason', reason) self.reply(msg.get()) ### Change the plugin status self._set_status(STATUS_STOP_REQUEST) ### Try to stop the plugin # if it fails, the manager should try to kill the plugin self.force_leave()
def request_startup(self): """ Request the plugin to start over the manager """ print( u"Request plugin startup to the manager for '{0}' on '{1}'".format( self.name, self.host)) cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('plugin.start.do') msg.add_data('name', self.name) msg.add_data('host', self.host) result = cli.request('manager', msg.get(), timeout=10) if result: msgid, content = result.get() content = json.loads(content) print(u"Response from the manager : {0}".format(content)) if content['status']: print(u"Plugin started") return True else: print(u"Error : plugin not started") return False else: raise RuntimeError( "MQ Timeout when requesting manager to start the plugin")
def configure(type, name, host, key, value): cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('config.set') msg.add_data('type', type) msg.add_data('host', host) msg.add_data('name', name) msg.add_data('data', {key: value}) result = cli.request('dbmgr', msg.get(), timeout=10) if result: data = result.get_data() if 'status' in data: if not data['status']: print(result.get()) raise RuntimeError( "DbMgr did not return status true on a config.set for {0}-{1}.{2} : {3} = {4}" .format(type, name, host, key, value)) else: return True else: print(result.get()) raise RuntimeError( "DbMgr did not return a status on a config.set for {0}-{1}.{2} : {3} = {4}" .format(type, name, host, key, value)) else: raise RuntimeError( "Error while setting configuration for {0}-{1}.{2} : {3} = {4}". format(type, name, host, key, value))
def on_message(self, msg, content=None): if not content: # this is a websocket message jsons = json.loads(msg) if 'action' in jsons and 'data' in jsons: cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action(str(jsons['action'])) msg.set_data(jsons['data']) if 'dst' in jsons: print cli.request(str(jsons['dst']), msg.get(), timeout=10).get() else: print cli.request('manager', msg.get(), timeout=10).get() else: # this is a mq message for cli in AdminWebSocket.clients: cli.write_message({"msgid": msg, "content": content})
def reload_stats(self): self.log.debug(u"=============== reload stats") req = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action( 'reload' ) resp = req.request('xplgw', msg.get(), 100) self.log.debug(u"Reply from xplgw: {0}".format(resp)) self.log.debug(u"=============== reload stats END")
def _mdp_reply_helper_help(self, data): content = data.get_data() if 'command' in contens.keys(): if content['command'] in self.helpers.keys(): msg = MQMessage() msg.set_action('helper.help.result') msg.add_data('help', self.helpers[content['command']]['help']) self.reply(msg.get())
def _mdp_reply_helper_help(self, data): content = data.get_data() if 'command' in contens.keys(): if content['command'] in self.helpers.keys(): msg = MQMessage() msg.set_action('helper.help.result') msg.add_data('help', self.helpers[content['command']]['help']) self.reply(msg.get())
def _mdp_reply_packages_detail(self): """ Reply on the MQ """ msg = MQMessage() msg.set_action('package.detail.result') for pkg in self._packages: msg.add_data(pkg, self._packages[pkg].get_json()) self.reply(msg.get())
def scenario_blocks_actions(): """ Blockly.Blocks['dom_action_log'] = { init: function() { this.setColour(160); this.appendDummyInput() .appendField('Log Message') .appendField(new Blockly.FieldTextInput("<message to log>"), "message"); this.setPreviousStatement(true, "null"); this.setNextStatement(true, "null"); this.setTooltip(''); this.setInputsInline(false); this.contextMenu = false; } }; """ js = "" cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('action.list') res = cli.request('scenario', msg.get(), timeout=10) if res is not None: res = res.get_data() if 'result' in res: res = json.loads(res['result']) for act, params in res.iteritems(): print act print params p = [] jso = "" for par, parv in params['parameters'].iteritems(): print par print parv papp = "this.appendDummyInput().appendField('{0}')".format(parv['description']) if parv['type'] == 'string': jso = '{0}, "{1}": "\'+ block.getFieldValue(\'{1}\') + \'" '.format(jso, par) papp = "{0}.appendField(new Blockly.FieldTextInput('{1}'), '{2}');".format(papp, parv['default'],par) elif parv['type'] == 'integer': jso = '{0}, "{1}": \'+ block.getFieldValue(\'{1}\') + \' '.format(jso, par) papp = "{0}.appendField(new Blockly.FieldTextInput('{1}'), '{2}');".format(papp, parv['default'],par) else: papp = "{0};".format(papp) p.append(papp) add = """Blockly.Blocks['{0}'] = {{ init: function() {{ this.setHelpUrl(''); this.setColour(160); this.appendDummyInput().appendField("{0}"); {1} this.setPreviousStatement(true, "null"); this.setNextStatement(true, "null"); this.setTooltip('{2}'); this.setInputsInline(false); }} }}; """.format(act, '\n'.join(p), params['description'], jso) js = '{0}\n\r{1}'.format(js, add) return Response(js, content_type='text/javascript; charset=utf-8')
def _mdp_reply_devices_create_result(self, data): status = True reason = False result = False # get the filled package json params = data.get_data()['data'] # get the json cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('device_types.get') msg.add_data('device_type', params['device_type']) res = cli.request('manager', msg.get(), timeout=10) del cli if res is None: status = False reason = "Manager is not replying to the mq request" pjson = res.get_data() if pjson is None: status = False reason = "No data for {0} found by manager".format(params['device_type']) pjson = pjson[params['device_type']] if pjson is None: status = False reason = "The json for {0} found by manager is empty".format(params['device_type']) if status: # call the add device function res = self._db.add_full_device(params, pjson) if not res: status = False reason = "DB failed" else: status = True reason = False result = res msg = MQMessage() msg.set_action('device.create.result') if reason: msg.add_data('reason', reason) if result: msg.add_data('result', result) msg.add_data('status', status) self.log.debug(msg.get()) self.reply(msg.get())
def loadDevices(cls): logger.info("MQ: Loading Devices info") Device.clean() msg = MQMessage() msg.set_action('client.list.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: _datac = res.get_data() else: _datac = {} session = Session() for client in _datac.itervalues(): # for each plugin client, we request the list of devices if client["type"] == "plugin": msg = MQMessage() msg.set_action('device.get') msg.add_data('type', 'plugin') msg.add_data('name', client["name"]) msg.add_data('host', client["host"]) res = cli.request('dbmgr', msg.get(), timeout=10) if res is not None: _datad = res.get_data() else: _datad = {} if 'devices' in _datad: for device in _datad["devices"]: d = Device(id=device["id"], name=device["name"], type=device["device_type_id"], reference=device["reference"]) session.add(d) if "commands" in device: for ref, command in device["commands"].iteritems(): c = Command(id=command["id"], name=command["name"], device_id=device["id"], reference=ref, return_confirmation=command["return_confirmation"]) session.add(c) c.datatypes = "" for param in command["parameters"]: p = CommandParam(command_id=c.id, key=param["key"], datatype_id=param["data_type"]) session.add(p) c.datatypes += param["data_type"] session.add(c) if "sensors" in device: for ref, sensor in device["sensors"].iteritems(): s = Sensor(id=sensor["id"], name=sensor["name"], device_id=device["id"], reference=ref, datatype_id=sensor["data_type"], last_value=sensor["last_value"], last_received=sensor["last_received"]) session.add(s) session.commit() session.flush()
def _mdp_reply_device_new_get(self, data): """ Return a list of new devices detected @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('device.new.result') msg.add_data('devices', self.new_devices) self.reply(msg.get())
def _load_client_to_xpl_target(self): cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('client.list.get') response = cli.request('manager', msg.get(), timeout=10) if response: self._parse_xpl_target(response.get_data()) else: self.log.error(u"Updating client list was not successfull, no response from manager")
def _mdp_reply_helper_list(self, data): """ Return a list of supported helpers @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('helper.list.result') msg.add_data('actions', self.helpers.keys()) self.reply(msg.get())
def client_devices_delete(client_id, did): with app.db.session_scope(): app.db.del_device(did) # reload stats req = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('reload') resp = req.request('xplgw', msg.get(), 100) return redirect("/client/{0}/devices/known".format(client_id))
def _mdp_reply_device_new_get(self, data): """ Return a list of new devices detected @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('device.new.result') msg.add_data('devices', self.new_devices) self.reply(msg.get())
def client_devices_delete(client_id, did): with app.db.session_scope(): app.db.del_device(did) # reload stats req = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action( 'reload' ) resp = req.request('xplgw', msg.get(), 100) return redirect("/client/{0}/devices/known".format(client_id))
def _mdp_reply_helper_list(self, data): """ Return a list of supported helpers @param data : MQ req message """ ### Send the ack over MQ Rep msg = MQMessage() msg.set_action('helper.list.result') msg.add_data('actions', self.helpers.keys()) self.reply(msg.get())
def _mdp_reply_clients_detail(self): """ Reply on the MQ """ msg = MQMessage() msg.set_action('client.detail.result') clients = self._clients.get_detail() for key in clients: msg.add_data(key, clients[key]) self.reply(msg.get())
def __init__(self, server_interfaces, server_port): """ Initiate DbHelper, Logs and config Then, start HTTP server and give it initialized data @param server_interfaces : interfaces of HTTP server @param server_port : port of HTTP server """ Plugin.__init__(self, name='admin') # logging initialization self.log.info(u"Admin Server initialisation...") self.log.debug(u"locale : %s %s" % locale.getdefaultlocale()) try: try: cfg_rest = Loader('admin') config_rest = cfg_rest.load() conf_rest = dict(config_rest[1]) self.interfaces = conf_rest['interfaces'] self.port = conf_rest['port'] # if rest_use_ssl = True, set here path for ssl certificate/key self.use_ssl = conf_rest['use_ssl'] self.key_file = conf_rest['ssl_certificate'] self.cert_file = conf_rest['ssl_key'] except KeyError: # default parameters self.interfaces = server_interfaces self.port = server_port self.use_ssl = False self.key_file = "" self.cert_file = "" self.clean_json = False self.log.info(u"Configuration : interfaces:port = %s:%s" % (self.interfaces, self.port)) # get all datatypes cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('datatype.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: self.datatypes = res.get_data()['datatypes'] else: self.datatypes = {} # Launch server, stats self.log.info(u"Admin Initialisation OK") self.add_stop_cb(self.stop_http) self.server = None self.start_http() # calls the tornado.ioloop.instance().start() ### Component is ready self.ready(0) IOLoop.instance().start() except: self.log.error(u"%s" % self.get_exception())
def orphans_delete(did): with app.db.session_scope(): app.db.del_device(did) # reload stats req = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('reload') resp = req.request('xplgw', msg.get(), 100) flash(gettext("Device deleted"), "success") return redirect("/orphans")
def orphans_delete(did): with app.db.session_scope(): app.db.del_device(did) # reload stats req = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action( 'reload' ) resp = req.request('xplgw', msg.get(), 100) flash(gettext("Device deleted"), "success") return redirect("/orphans")
def _load_conversions(self): print "============================ conversion" cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('client.conversion.get') response = cli.request('manager', msg.get(), timeout=10) if response: self._parse_conversions(response.get_data()) else: self.log.error(u"Updating client conversion list was not successfull, no response from manager")
def __init__(self, server_interfaces, server_port): """ Initiate DbHelper, Logs and config Then, start HTTP server and give it initialized data @param server_interfaces : interfaces of HTTP server @param server_port : port of HTTP server """ XplPlugin.__init__(self, name = 'admin', nohub = True) # logging initialization self.log.info(u"Admin Server initialisation...") self.log.debug(u"locale : %s %s" % locale.getdefaultlocale()) try: try: cfg_rest = Loader('admin') config_rest = cfg_rest.load() conf_rest = dict(config_rest[1]) self.interfaces = conf_rest['interfaces'] self.port = conf_rest['port'] # if rest_use_ssl = True, set here path for ssl certificate/key self.use_ssl = conf_rest['use_ssl'] self.key_file = conf_rest['ssl_certificate'] self.cert_file = conf_rest['ssl_key'] except KeyError: # default parameters self.interfaces = server_interfaces self.port = server_port self.use_ssl = False self.key_file = "" self.cert_file = "" self.clean_json = False self.log.info(u"Configuration : interfaces:port = %s:%s" % (self.interfaces, self.port)) # get all datatypes cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('datatype.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: self.datatypes = res.get_data()['datatypes'] else: self.datatypes = {} # Launch server, stats self.log.info(u"Admin Initialisation OK") self.add_stop_cb(self.stop_http) self.server = None self.start_http() # calls the tornado.ioloop.instance().start() ### Component is ready self.ready(0) IOLoop.instance().start() except : self.log.error(u"%s" % self.get_exception())
def set(self, plugin, key, value): ''' Send a xpl message to set value for a param @param technology : the technology of the item @param key : the key to set corresponding value, @param value : the value to set ''' msg = MQMessage() msg._action = 'config.set' self.cli.request('dbmgr', msg.get(), timeout=QUERY_CONFIG_WAIT)
def set(self, plugin, key, value): """ Send a xpl message to set value for a param @param technology : the technology of the item @param key : the key to set corresponding value, @param value : the value to set """ msg = MQMessage() msg._action = "config.set" self.cli.request("dbmgr", msg.get(), timeout=QUERY_CONFIG_WAIT)
def _load_client_to_xpl_target(self): cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('client.list.get') response = cli.request('manager', msg.get(), timeout=10) if response: data = response.get_data() for cli in data: self.client_xpl_map[cli] = data[cli]['xpl_source'] else: self.log.error(u"Updating client list was not successfull, no response from manager")
def on_mdp_request(self, msg): # XplPlugin handles MQ Req/rep also XplPlugin.on_mdp_request(self, msg) if msg.get_action() == "reload": self.load() msg = MQMessage() msg.set_action( 'reload.result' ) self.reply(msg.get()) elif msg.get_action() == "cmd.send": self._send_xpl_command(msg)
def on_mdp_request(self, msg): # XplPlugin handles MQ Req/rep also XplPlugin.on_mdp_request(self, msg) if msg.get_action() == "reload": self.load() msg = MQMessage() msg.set_action('reload.result') self.reply(msg.get()) elif msg.get_action() == "cmd.send": self._send_xpl_command(msg)
def _load_client_to_xpl_target(self): cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('client.list.get') response = cli.request('manager', msg.get(), timeout=10) if response: self._parse_xpl_target(response.get_data()) else: self.log.error( u"Updating client list was not successfull, no response from manager" )
def _load_conversions(self): print "============================ conversion" cli = MQSyncReq(self.zmq) msg = MQMessage() msg.set_action('client.conversion.get') response = cli.request('manager', msg.get(), timeout=10) if response: self._parse_conversions(response.get_data()) else: self.log.error( u"Updating client conversion list was not successfull, no response from manager" )
def clients(): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('client.list.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: client_list = res.get_data() else: client_list = {} return render_template('clients.html', mactve="clients", overview_state="collapse", clients=client_list)
def do_action(self, condition, tests): cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('cmd.send') msg.add_data('cmdid', self._params['cmdid']) msg.add_data('cmdparams', self._params['cmdparams']) # do the request res = cli.request('xplgw', msg.get(), timeout=10) if res: data = res.get_data() if not data['status']: self._log.error( "Command sending to XPL gw failed: {0}".format(res)) else: self._log.error("XPL gw did not respond")
def client_devices_detected(client_id): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('device.new.get') res = cli.request(str(client_id), msg.get(), timeout=10) if res is not None: data = res.get_data() devices = data['devices'] else: devices = {} return render_template('client_detected.html', devices=devices, clientid=client_id, mactve="clients", active='devices')
def _mdp_reply_devices_result(self, data): """ Reply to device.get MQ req @param data : MQ req message """ msg = MQMessage() msg.set_action('device.result') status = True msg_data = data.get_data() if 'type' not in msg_data: status = False reason = "Devices request : missing 'type' field : {0}".format( data) if 'name' not in msg_data: status = False reason = "Devices request : missing 'name' field : {0}".format( data) if 'host' not in msg_data: status = False reason = "Devices request : missing 'host' field : {0}".format( data) if status == False: self.log.error(reason) else: reason = "" type = msg_data['type'] #if type == "plugin": # type = DMG_VENDOR_ID name = msg_data['name'] host = msg_data['host'] dev_list = self._db.list_devices_by_plugin("{0}-{1}.{2}".format( type, name, host)) #dev_json = json.dumps(dev_list, cls=domogik_encoder(), check_circular=False), dev_json = dev_list print(dev_json) msg.add_data('status', status) msg.add_data('reason', reason) msg.add_data('type', type) msg.add_data('name', name) msg.add_data('host', host) msg.add_data('devices', dev_json) self.reply(msg.get())
def client_detail(client_id): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('client.detail.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: detaila = res.get_data() detail = detaila[client_id] else: detail = {} return render_template('client.html', loop={'index': 1}, clientid=client_id, data=detail, mactve="clients", active='home')
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