def scenario_edit(id): default_json = '{"type":"dom_condition","id":"1","deletable":false}' # laod the json if int(id) == 0: name = "New scenario" jso = default_json dis = 0 else: with app.db.session_scope(): scen = app.db.get_scenario(id) jso = scen.json dis = scen.disabled name = scen.name jso.replace('\n', '').replace('\r', '') # create a form class F(Form): sid = HiddenField("id", default=id) sname = TextField("name", default=name) sdis = BooleanField("disabled", default=dis) sjson = HiddenField("json") submit = SubmitField("Send") pass form = F() if request.method == 'POST' and form.validate(): 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 = 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 = 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 on_message(self, msg, content=None): #print(ws_list.list()) """ This function is quite tricky It is called by both WebSocketHandler and MQASyncSub... """ try: ### websocket message (from the web) # there are the mesages send by the administration javascript part thanks to the ws.send(...) function if not content: jsons = json.loads(msg) # req/rep if 'mq_request' in jsons and 'data' in jsons: cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action(str(jsons['mq_request'])) msg.set_data(jsons['data']) if 'dst' in jsons: cli.request(str(jsons['dst']), msg.get(), timeout=10).get() else: cli.request('manager', msg.get(), timeout=10).get() # pub elif 'mq_publish' in jsons and 'data' in jsons: print("Publish : {0}".format(jsons['data'])) self.pub.send_event(jsons['mq_publish'], jsons['data']) ### MQ message (from domogik) # these are the published messages which are received here else: try: self.write_message({"msgid": msg, "content": content}) except WebSocketClosedError: self.close() except: print("Error : {0}".format(traceback.format_exc()))
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 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 scenario_edit(id): default_json = '{"type":"dom_condition","id":"1","deletable":false}' # laod the json if int(id) == 0: name = "New scenario" jso = default_json dis = 0 else: with app.db.session_scope(): scen = app.db.get_scenario(id) jso = scen.json dis = scen.disabled name = scen.name jso.replace('\n', '').replace('\r', '') # create a form class F(Form): sid = HiddenField("id", default=id) sname = TextField("name", default=name) sdis = BooleanField("disabled", default=dis) sjson = HiddenField("json") submit = SubmitField("Send") pass form = F() if request.method == 'POST' and form.validate(): 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 = 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 = 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 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 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 __init__(self, log = None, trigger = None): AbstractParameter.__init__(self, log, trigger) self.log = log #self.set_type("string") #self.add_expected_entry("sensor_id", "integer", "The sensor id to check") # first, let's get the devices list try: cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('device.get') json_devices = cli.request('dbmgr', msg.get(), timeout=10).get()[1] devices = json.loads(json_devices)['devices'] print(devices) sensors_list = [] for dev in devices: name = dev['name'] for sen_idx in dev['sensors']: sen_name = dev['sensors'][sen_idx]['name'] sen_id = dev['sensors'][sen_idx]['id'] sensors_list.append(['{0} : {1}'.format(name, sen_name), '{0}'.format(sen_id)]) print(sensors_list) except: self.log.error("Error while getting devices list : {0}".format(traceback.format_exc())) # then, et's configure our sensor id selector :) self.set_type("list") self.add_expected_entry("sensor_id", "list", "The sensor id to check") #list_sensors = [['sensor1', 'sensor2'], ['a', 'A']] self.set_list_of_values("sensor_id", sensors_list)
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_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, mactive="clients", active='devices')
def delete_configuration(type, name, host): cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('config.delete') msg.add_data('type', type) msg.add_data('host', host) msg.add_data('name', name) 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( "Timeout while deleting configuration for {0}-{1}.{2}".format( type, name, host))
def sendToMQ(self, message): try: ctx = zmq.Context() jsons = json.loads(message) # req/rep if 'mq_request' in jsons and 'data' in jsons: cli = MQSyncReq(ctx) msg = MQMessage() msg.set_action(str(jsons['mq_request'])) msg.set_data(jsons['data']) print(u"REQ : {0}".format(msg.get())) if 'dst' in jsons: dst = str(jsons['dst']) else: dst = 'manager' res = cli.request(dst, msg.get(), timeout=10) if res: print(res.get()) cli.shutdown() del cli # pub elif 'mq_publish' in jsons and 'data' in jsons: self.pub.send_event(jsons['mq_publish'], jsons['data']) except Exception as e: print(u"Error sending mq message: {0}".format(e))
def send_to_butler(self, command_line, widget_proxy, live): you = u"You > {0}".format(command_line) self.parent.value.set_values(self.parent.value.get() + [you]) self.parent.wMain.values = self.parent.value.get() self.parent.wMain.display() # handle special commands if command_line.lower() == "quit": IOLoop.instance().stop() sys.exit(0) # why is this needed ? elif command_line.lower() == "reload": try: cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('butler.reload.do') result = cli.request('butler', msg.get(), timeout=10).get() if result: msg = "*** action reload : {0}".format(result) self.parent.value.set_values(self.parent.value.get() + [msg]) except: msg = u"*** action reload : error (is butler component ok ?)" self.parent.value.set_values(self.parent.value.get() + [msg]) # handle butler else: self.parent.butler_cb(command_line, identity = "cli user", media = "chat", location = None, mood = None) self.parent.wMain.values = self.parent.value.get() self.parent.wMain.display()
class QueryMQ(): ''' Query to the mq to find the config ''' def __init__(self, zmq, log, host): ''' Init the query system and connect it to xPL network @param zmq : the zMQ context @param log : a Logger instance (usually took from self.log)) ''' self._zmq = zmq self._log = log self._host = host self._log.debug("Init config query(mq) instance") self.cli = MQSyncReq(self._zmq) def query(self, type, name, key = None): ''' Ask the config system for the value. Calling this function will make your program wait until it got an answer @param type : the client type @param name : the client name of the item requesting the value, must exists in the config database @param key : the key to fetch corresponding value @return : the value if key != None a dictionnary will all keys/values if key = None ''' msg = MQMessage() msg.set_action('config.get') msg.add_data('type', type) msg.add_data('name', name) msg.add_data('host', self._host) if key != None: msg.add_data('key', key) else: key = "*" self._log.info("Request query config for client {0} : key {1}".format(name, key)) ret = self.cli.request('dbmgr', msg.get(), timeout=QUERY_CONFIG_WAIT) ### no response from dbmgr if ret is None: self._log.error("Query config for client {0} on host {1}, key {2} : no response from dbmgr".format(name, self._host, key)) return None ### response from dbmgr else: dat = ret.get_data() if dat['status']: self._log.debug("Query config : successfull response : {0}".format(ret)) if key == "*": return dat['data'] else: val = dat['value'] # do some cast if val == "None": val = None return val else: self._log.error("Query config : error returned. Reason : {0}".format(dat['reason'])) return None
def __init__(self, log=None, trigger=None): AbstractParameter.__init__(self, log, trigger) self.log = log #self.set_type("string") #self.add_expected_entry("sensor_id", "integer", "The sensor id to check") # first, let's get the devices list try: cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('device.get') json_devices = cli.request('dbmgr', msg.get(), timeout=10).get()[1] devices = json.loads(json_devices)['devices'] print(devices) sensors_list = [] for dev in devices: name = dev['name'] for sen_idx in dev['sensors']: sen_name = dev['sensors'][sen_idx]['name'] sen_id = dev['sensors'][sen_idx]['id'] sensors_list.append([ '{0} : {1}'.format(name, sen_name), '{0}'.format(sen_id) ]) print(sensors_list) except: self.log.error("Error while getting devices list : {0}".format( traceback.format_exc())) # then, et's configure our sensor id selector :) self.set_type("list") self.add_expected_entry("sensor_id", "list", "The sensor id to check") #list_sensors = [['sensor1', 'sensor2'], ['a', 'A']] self.set_list_of_values("sensor_id", sensors_list)
def __init__(self, log, dbid, name, json, disabled): """ Create the instance @param log : A logger instance @param dbid : The id of this scenario in the db @param json : The json describing the scenario """ self._log = log self._name = name self._json = json self._disabled = disabled self.zmq = zmq.Context() # datatypes self.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: res = res.get_data() if 'datatypes' in res: self.datatypes = res['datatypes'] self._parsed_condition = None self._mapping = {'test': {}, 'action': {}} if not self._disabled: self._instanciate()
def _load_client_to_xpl_target(self): """ Request the client conversion info This is an mq req to manager If this function does not call self._parse_xpl_target(), all xpl sensors will not be procesed! """ nb_try = 0 max_try = 5 ok = False while nb_try <= max_try and ok == False: nb_try += 1 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()) ok = True else: self.log.error(\ u"Updating client list failed, no response from manager (try number {0}/{1})".format(nb_try, max_try)) # We fail to load the client list, so we wait to get something time.sleep(5) if ok == True: self.log.info(u"Updating client list success") else: self.log.error(u"Updating client list failed too much time! The xpl sensors will not be processed by this component")
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 __init__(self, log, dbid, name, json, disabled): """ Create the instance @param log : A logger instance @param dbid : The id of this scenario in the db @param json : The json describing the scenario """ self._log = log self._name = name self._json = json self._disabled = disabled self.zmq = zmq.Context() # datatypes self.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: res = res.get_data() if 'datatypes' in res: self.datatypes = res['datatypes'] self._parsed_condition = None self._mapping = { 'test': {}, 'action': {} } if not self._disabled: self._instanciate()
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 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, mactive="clients", active = 'devices' )
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 = {} client_list_per_host_per_type = OrderedDict() for client in client_list: cli_type = client_list[client]['type'] cli_host = client_list[client]['host'] if not client_list_per_host_per_type.has_key(cli_host): client_list_per_host_per_type[cli_host] = {} if not client_list_per_host_per_type[cli_host].has_key(cli_type): client_list_per_host_per_type[cli_host][cli_type] = {} client_list_per_host_per_type[cli_host][cli_type][client] = client_list[client] return render_template('clients.html', mactive="clients", overview_state="collapse", clients=client_list, client_list_per_host_per_type=client_list_per_host_per_type )
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 client_devices_known(client_id): detail = get_client_detail(client_id) if app.datatypes == {}: cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('datatype.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: app.datatypes = res.get_data()['datatypes'] else: app.datatypes = {} # todo : grab from MQ ? with app.db.session_scope(): devices = app.db.list_devices_by_plugin(client_id) # sort clients per device type devices_by_device_type_id = {} for dev in devices: if devices_by_device_type_id.has_key(dev['device_type_id']): devices_by_device_type_id[dev['device_type_id']].append(dev) else: devices_by_device_type_id[dev['device_type_id']] = [dev] return render_template('client_devices.html', datatypes = app.datatypes, devices = devices, devices_by_device_type_id = devices_by_device_type_id, clientid = client_id, mactive="clients", active = 'devices', rest_url = get_rest_url(), client_detail = detail )
def do_action(self): self.log.info(u"Command : Do an action...") # live udate some values self.log.debug(u"Command : Preprocessing on parameters...") self.log.debug(u"Command : Parameters before processing : {0}".format(self._params)) params = {} for key in self._params: self._params[key] = ucode(self._params[key]) self.log.debug(u"Command : Preprocess for param : key={0}, typeofvalue={1}, value={2}".format(key, type(self._params[key]), self._params[key])) params[key] = self._params[key] if key == "color" and params[key].startswith("#"): self.log.debug(u"- Processing : for a color, if the color starts with #, remove it") params[key] = params[key][1:] self.log.debug(u"Command : Parameters after processing : {0}".format(params)) self.log.debug(u"Command : Send action command over MQ...") # do the command cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('cmd.send') msg.add_data('cmdid', self._cmdId) msg.add_data('cmdparams', params) self.log.debug(u"Command : Command id = '{0}', command params = '{1}'".format(self._cmdId, params)) # do the request res = cli.request('xplgw', msg.get(), timeout=10) if res: data = res.get_data() if not data['status']: self.log.error(u"Command : Command sending to XPL gw failed: {0}".format(res)) else: self.log.error(u"Command : XPL gw did not respond") self.log.debug(u"Command : Action done")
def delete_configuration(type, name, host): cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action("config.delete") msg.add_data("type", type) msg.add_data("host", host) msg.add_data("name", name) 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("Timeout while deleting configuration for {0}-{1}.{2}".format(type, name, host))
def delete(self, did): """ @api {del} /device/id Delete a device @apiName delDevice @apiGroup Device @apiParam {Number} id The id of the device to be deleted @apiSuccess {String} none No data is returned @apiSuccessExample Success-Response: HTTTP/1.1 200 OK @apiErrorExample Error-Response: HTTTP/1.1 500 INTERNAL SERVER ERROR """ cli = MQSyncReq(urlHandler.zmq_context) msg = MQMessage() msg.set_action('device.delete') msg.set_data({'did': did}) res = cli.request('dbmgr', msg.get(), timeout=10) if res is not None: data = res.get_data() if data["status"]: return 201, None else: return 500, data["reason"] else: return 500, "DbMgr did not respond on the device.create, check the logs" return 201, None
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 = res['result'] for act, params in res.iteritems(): p = [] jso = "" for par, parv in params['parameters'].iteritems(): 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 __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: # admin config cfg_admin = Loader('admin') config_admin = cfg_admin.load() conf_admin = dict(config_admin[1]) self.interfaces = conf_admin['interfaces'] self.port = conf_admin['port'] # if use_ssl = True, set here path for ssl certificate/key self.use_ssl = conf_admin['use_ssl'] self.key_file = conf_admin['ssl_certificate'] self.cert_file = conf_admin['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.error("Error while reading configuration for section [admin] : using default values instead") 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 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 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 scenario_disable(id): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('scenario.disable') msg.add_data('cid', id) res = cli.request('scenario', msg.get(), timeout=10) flash(gettext(u"Scenario disabled"), u"success") return redirect(u"/scenario") pass
def scenario_del(id): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action("scenario.delete") msg.add_data("cid", id) res = cli.request("scenario", msg.get(), timeout=10) flash(gettext(u"Scenario deleted"), u"success") return redirect(u"/scenario") pass
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 __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 get_clients_list(): 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 client_list
def get_butler_history(): cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('butler.history.get') res = cli.request('butler', msg.get(), timeout=10) if res is not None: history = res.get_data()['history'] else: history = [] return history
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 client_devices_known(client_id): detail = get_client_detail(client_id) if app.datatypes == {}: cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('datatype.get') res = cli.request('manager', msg.get(), timeout=10) if res is not None: app.datatypes = res.get_data()['datatypes'] else: app.datatypes = {} # todo : grab from MQ ? with app.db.session_scope(): try: devices = app.db.list_devices_by_plugin(client_id) error = None except: error = "Error while retrieving the devices list. Error is : {0}".format(traceback.format_exc()) devices = [] # first sort by device name devices = sorted(devices, key=itemgetter("name")) # device_types_list[key] = data["device_types"][key] # group clients per device type devices_by_device_type_id = {} for dev in devices: try: device_type_name = detail['data']['device_types'][dev['device_type_id']]['name'] except KeyError: print(u"Warning : the device type '{0}' does not exist anymore in the installed package release. Device type name set as the existing id in database".format(dev['device_type_id'])) device_type_name = dev['device_type_id'] if device_type_name in devices_by_device_type_id: devices_by_device_type_id[device_type_name].append(dev) else: devices_by_device_type_id[device_type_name] = [dev] # sorting # disabled as this breaks because of storing other objects then normal strings devices_by_device_type_id = json.dumps(devices_by_device_type_id, sort_keys=True) devices_by_device_type_id = json.loads(devices_by_device_type_id, object_pairs_hook=OrderedDict) return render_template('client_devices.html', datatypes = app.datatypes, devices = devices, devices_by_device_type_id = devices_by_device_type_id, clientid = client_id, mactive="clients", active = 'devices', #rest_url = get_rest_url(), rest_url = request.url_root + "rest", client_detail = detail, error = error)
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=10) return True
def get_openzwave_all_products(abort = False): data = {u'data': [], u'error': u''} if not abort : cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('ozwave.openzwave.getallproducts') res = cli.request('plugin-ozwave.{0}'.format(get_sanitized_hostname()), msg.get(), timeout=10) if res is not None: data = res.get_data() else : data['error'] = u'Plugin timeout response.' return data
def get_manager_status(abort = False): data = {u'status': u'dead', u'rfPlayers': [], u'error': u''} if not abort : cli = MQSyncReq(app.zmq_context ) msg = MQMessage() msg.set_action('rfplayer.manager.getstatus') res = cli.request('plugin-rfplayer.{0}'.format(get_sanitized_hostname()), msg.get(), timeout=10) if res is not None: data = res.get_data() else : data['error'] = u'Plugin timeout response.' return data
def get_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 detail
def get_brain_content(client_id): # get data over MQ cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('butler.scripts.get') res = cli.request('butler', msg.get(), timeout=10) if res is not None: data = res.get_data() detail = {} try: detail[client_id] = data[client_id] except KeyError: # this can happen if you install a package and don't do a butler reload! detail[client_id] = None else: detail = {} # sorting detail = json.dumps(detail, sort_keys=True) detail = json.loads(detail, object_pairs_hook=OrderedDict) # do a post processing on content to add html inside for client_id in detail: # we skip the learn file if client_id in ["learn", "not_understood"]: continue if detail[client_id]: for lang in detail[client_id]: idx = 0 for file in detail[client_id][lang]: content = html_escape(detail[client_id][lang][file]) # python objects idx += 1 reg = re.compile(r"> object", re.IGNORECASE) content = reg.sub( "<button class='btn btn-info' onclick=\"$('#python_object_{0}').toggle();\"><span class='glyphicon glyphicon-paperclip' aria-hidden='true'></span> python object</button><div class='python' id='python_object_{0}' style='display: none'>> object" .format(idx), content) reg = re.compile(r"< object", re.IGNORECASE) content = reg.sub("< object</div>", content) # trigger reg = re.compile(r"\+(?P<trigger>.*)", re.IGNORECASE) content = reg.sub("<strong>+\g<trigger></strong>", content) # comments reg = re.compile(r"//(?P<comment>.*)", re.IGNORECASE) content = reg.sub("<em>//\g<comment></em>", content) detail[client_id][lang][file] = content else: detail[client_id] = "" return detail
def trigger_bool_command(dt_type, device, value): try: device_name = device.lower() cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('device.get') str_devices = cli.request('dbmgr', msg.get(), timeout=10).get()[1] devices = json.loads(str_devices)['devices'] found = None for a_device in devices: if a_device['name'].lower() == device_name: cid = 0 for a_command in a_device['commands']: pid = 0 for a_param in a_device['commands'][a_command][ 'parameters']: if a_param['data_type'] == dt_type: found = [a_device, a_command, pid] break pid = pid + 1 else: continue cid = cid + 1 break if found: dev = found[0] cid = found[1] pid = found[2] cli = MQSyncReq(zmq.Context()) msg = MQMessage() msg.set_action('cmd.send') msg.add_data('cmdid', dev['commands'][cid]['id']) msg.add_data( 'cmdparams', {dev['commands'][cid]['parameters'][pid]['key']: value}) return cli.request('xplgw', msg.get(), timeout=10).get() else: return None except: print("ERROR : {0}".format(traceback.format_exc())) return None
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 = "An error occured while adding the device in database. Please check the file dbmgr.log for more informations" 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()) # send the pub message if status and res: self._pub.send_event('device.update', { "device_id": res['id'], "client_id": res['client_id'] })
def get_openzwave_info(abort = False): data = {u'status': u'dead', u'PYOZWLibVers':u'unknown', u'ConfigPath': u'undefined', 'uUserPath': u'not init', u'Options' : {}, u'error': u''} if not abort : cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('ozwave.openzwave.get') res = cli.request('plugin-ozwave.{0}'.format(get_sanitized_hostname()), msg.get(), timeout=10) if res is not None: data = res.get_data() else : data['error'] = u'Plugin timeout response.' return data
def get_controller_nodes(NetworkID, abort = False): data = { u'nodes': [], u'error': u''} if not abort : cli = MQSyncReq(app.zmq_context) msg = MQMessage() msg.set_action('ozwave.ctrl.nodes') msg.add_data('NetworkID', NetworkID) res = cli.request('plugin-ozwave.{0}'.format(get_sanitized_hostname()), msg.get(), timeout=10) if res is not None: data = res.get_data() else : data['error'] = u'Plugin timeout response.' return data
def _load_conversions(self): """ Request the client conversion info This is an mq req to manager """ 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 conversion list failed, no response from manager")