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 _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 __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 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, 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 _mdp_reply_client_stop(self, data): """ Stop the client @param data : MQ req message First, send the MQ Rep to 'ack' the request Then, change the client status to STATUS_STOP_REQUEST Then, quit the client by calling force_leave(). This should make the client 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 client """ # 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) msg.add_data('name', self._name) msg.add_data('host', self.get_sanitized_hostname()) self.log.info("Send reply for the stop request : {0}".format(msg)) self.reply(msg.get()) ### Change the client status self._set_status(STATUS_STOP_REQUEST) ### Try to stop the client # if it fails, the manager should try to kill the client self.force_leave()
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 on_mdp_request(self, msg): """ Called when a MQ req/rep message is received """ Plugin.on_mdp_request(self, msg) #self.log.debug(u"==> Received 0MQ messages: %s" % format(msg)) if msg.get_action() == "client.cmd": data = msg.get_data() self.log.debug(u"==> Received 0MQ messages data: %s" % format(data)) # ==> Received 0MQ messages data: {u'command_id': 35, u'value': u'1', u'device_id': 112} # ==> Received 0MQ messages data: {u'command_id': 36, u'value': u'128', u'device_id': 113} # ==> Received 0MQ messages data: {u'command_id': 37, u'value': u'Bonjour', u'device_id': 114} sensor_id = self.get_related_sensor_id(data['device_id'], data['command_id']) self.log.debug(u"Storing data for sensor_id = {0} : '{1}'".format( sensor_id, data["value"])) status, reason = self.send_data(sensor_id, data["value"]) self.log.info("Reply to command 0MQ") reply_msg = MQMessage() reply_msg.set_action('client.cmd.result') reply_msg.add_data('status', status) reply_msg.add_data('reason', reason) self.reply(reply_msg.get())
def on_mdp_request(self, msg): """ Called when a MQ req/rep message is received """ Plugin.on_mdp_request(self, msg) self.log.info(u"Received 0MQ messages: {0}".format(msg)) action = msg.get_action().split(".") if action[0] == "client" and action[1] == "cmd" : # action on dmg device data = msg.get_data() reply_msg = MQMessage() reply_msg.set_action('client.cmd.result') idsClient = self.managerClients.getIdsClient(data) find = False if idsClient != [] : for id in idsClient : client = self.managerClients.getClient(id) if client : self.log.debug(u"Handle requested action for Notify client {0} : {1}".format(id, data)) commands = client.getDmgCommands() for cmd in commands : if commands[cmd]['id'] == data['command_id'] : find = True client.handle_cmd(data) reply_msg.add_data('status', True) reply_msg.add_data('reason', None) break if not find : self.log.warning(u"Requested action received for unknown Notify client : {0}".format(data)) reply_msg.add_data('status', False) reply_msg.add_data('reason', u"Requested action received for unknown Notify client : {0}".format(data)) self.log.debug(u"Reply to MQ: {0}".format(reply_msg.get())) self.reply(reply_msg.get())
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 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 _mdp_reply_butler_history(self, message): """ Butler history """ msg = MQMessage() msg.set_action('butler.history.result') msg.add_data(u"history", self.history) self.reply(msg.get())
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 _mdp_reply_butler_features(self, message): """ Butler features """ msg = MQMessage() msg.set_action('butler.features.result') msg.add_data(u"features", self.butler_features) self.reply(msg.get())
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 on_mdp_request(self, msg): """ Called when a MQ req/rep message is received """ Plugin.on_mdp_request(self, msg) if msg.get_action() == "client.cmd": data = msg.get_data() self.log.info(u"==> Received 0MQ messages data: %s" % format(data)) # ==> Received 0MQ messages data: {u'command_id': 35, u'value': u'1', u'device_id': 112} # ==> Received 0MQ messages data: {u'command_id': 36, u'value': u'128', u'device_id': 113} # ==> Received 0MQ messages data: {u'command_id': 37, u'value': u'Bonjour', u'device_id': 114} # search for related device for a_device in self.devices: for a_cmd in a_device['commands']: if data['command_id'] == a_device['commands'][a_cmd]['id']: # As we will just execute a shell script, we can't really known if the command will be ok and how long it will take... # so we respond first on MQ to say we got the request self.log.info("Reply to command 0MQ") reply_msg = MQMessage() reply_msg.set_action('client.cmd.result') reply_msg.add_data('status', True) reply_msg.add_data('reason', '') self.reply(reply_msg.get()) # Now, launch the speak action ! ip = self.get_parameter(a_device, "ip") lang = 'fr-FR' thr_speak = threading.Thread(None, self.yi.speak, "speak", (ip, lang, data['text'], self.get_data_files_directory), {}) thr_speak.start() self.register_thread(thr_speak)
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 _mdp_reply_devices_delete_result(self, data): status = True reason = False try: did = data.get_data()['did'] if did: res = self._db.del_device(did) if not res: status = False else: status = True else: status = False reason = "Device delete failed" # delete done self.reload_stats() except DbHelperException as d: status = False reason = "Error while deleting device: {0}".format(d.value) except: status = False reason = "Error while deleting device: {0}".format(traceback.format_exc()) # send the result msg = MQMessage() msg.set_action('device.delete.result') msg.add_data('status', status) if reason: msg.add_data('reason', reason) self.log.debug(msg.get()) self.reply(msg.get())
def _mdp_reply_sensor_history(self, data): """ Reply to sensor_history.get MQ req @param data : MQ req message If no other param than the sensor id, return the last value """ msg = MQMessage() msg.set_action('sensor_history.result') status = True reason = "" msg_data = data.get_data() try: sensor_id = msg_data['sensor_id'] history = self._db.list_sensor_history(sensor_id, 1) if len(history) == 0: last_value = None else: last_value = self._db.list_sensor_history(sensor_id, 1)[0].value_str except: self.log.error("ERROR when getting sensor history for id = {0} : {1}".format(sensor_id, traceback.format_exc())) reason = "ERROR : {0}".format(traceback.format_exc()) status = False msg.add_data('status', status) msg.add_data('reason', reason) msg.add_data('sensor_id', sensor_id) msg.add_data('values', [last_value]) 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 = 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 _mdp_reply_config_set(self, data): """ Reply to config.set MQ req @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 set : missing 'type' field : {0}".format(data) if msg_data['type'] not in ["plugin", "brain", "interface"]: status = False reason = "You are not able to configure items 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(type, name, host, "configured", True) for key in msg_data['data']: self._db.set_plugin_config(type, 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_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 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_devices_update_result(self, data): status = True reason = False self.log.debug(u"Updating device : {0}".format(data)) try: data = data.get_data() if 'did' in data: did = data['did'] if 'name' not in data: name = None else: name = data['name'] if 'reference' not in data: ref = None else: ref = data['reference'] if 'description' not in data: desc = None else: desc = data['description'] # do the update res = self._db.update_device(did, \ d_name=name, \ d_description=desc, \ d_reference=ref) if not res: status = False else: status = True else: status = False reason = "There is no such device" self.log.debug(reason) # delete done except DbHelperException as d: status = False reason = "Error while updating device: {0}".format(d.value) self.log.error(reason) except: status = False reason = "Error while updating device: {0}".format( traceback.format_exc()) self.log.error(reason) # send the result msg = MQMessage() msg.set_action('device.update.result') msg.add_data('status', status) if reason: msg.add_data('reason', reason) 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 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 send_rep_ack(self, status, reason, cmd_id): """ Send ACQ to a command via MQ """ #self.log.info(u"==> Reply ACK to command id '%s' for device '%s'" % (cmd_id, dev_name)) reply_msg = MQMessage() reply_msg.set_action('client.cmd.result') reply_msg.add_data('status', status) reply_msg.add_data('reason', reason) self.reply(reply_msg.get())
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() # request for all devices if 'type' not in msg_data and \ 'name' not in msg_data and \ 'host' not in msg_data: reason = "" status = True dev_list = self._db.list_devices() dev_json = dev_list msg.add_data('status', status) msg.add_data('reason', reason) msg.add_data('devices', dev_json) # request for all devices of one client else: 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'] 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 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 _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")