Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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'
                )
Ejemplo n.º 3
0
 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()
Ejemplo n.º 4
0
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'
            )
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
 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")
Ejemplo n.º 8
0
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')
Ejemplo n.º 9
0
 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")
Ejemplo n.º 10
0
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))
Ejemplo n.º 11
0
 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")
Ejemplo n.º 12
0
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")
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
    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())
Ejemplo n.º 15
0
 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")
Ejemplo n.º 16
0
    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
Ejemplo n.º 17
0
def device_params(client_id, dev_type_id):
    cli = MQSyncReq(urlHandler.zmq_context)
    msg = MQMessage()
    msg.set_action('device.params')
    msg.add_data('device_type', dev_type_id)
    res = cli.request('dbmgr', msg.get(), timeout=10)
    result = ""
    if res:
        res = res.get_data()
        result = res['result'];
        result["client_id"] = client_id
    # return the info
    return 200, result
Ejemplo n.º 18
0
class QueryMQ:
    """
    Query to the mq to find the config
    """

    def __init__(self, xpl, log):
        """
        Init the query system and connect it to xPL network
        @param xpl : Will not be used
        @param log : a Logger instance (usually took from self.log))
        """
        self.log = log
        self.log.debug("Init config query(mq) instance")
        self.cli = MQSyncReq(zmq.Context())

    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 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
Ejemplo n.º 19
0
 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")
Ejemplo n.º 20
0
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
        )
Ejemplo n.º 21
0
def scenario_blocks_tests():
    """
        this.setHelpUrl('');
        this.setColour(160);
        this.appendDummyInput()
        .appendField('Time')
            .appendField(new Blockly.FieldTextInput("<cron like timestamp>"), "cron");
        this.setOutput(true, null);
        this.setTooltip('');
        this.setInputsInline(false);
    """
    js = ""
    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'])
            for test, params in res.iteritems():
                p = []
                jso = ""
                for par, parv in params['parameters'].iteritems():
                    par = parv['expected'].keys()[0]
                    parv = parv['expected'][par]
                    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)
                    p.append(papp)
                add = """Blockly.Blocks['{0}'] = {{
                            init: function() {{
                                this.setColour(160);
                                this.appendDummyInput().appendField("{0}");
                                {1}
                                this.setOutput(true);
                                this.setInputsInline(true);
                                this.setTooltip('{2}'); 
                                this.contextMenu = false;
                            }}
                        }};
                        """.format(test, '\n'.join(p), params['description'], jso)
                js = '{0}\n\r{1}'.format(js, add)
    return Response(js, content_type='text/javascript; charset=utf-8')
Ejemplo n.º 22
0
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,
            mactive="clients",
            active = 'devices'
            )
Ejemplo n.º 23
0
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')
Ejemplo n.º 24
0
 def refreshClientDevice(self,  client):
     """Request a refresh domogik device data for a IRTrans Client."""
     cli = MQSyncReq(zmq.Context())
     msg = MQMessage()
     msg.set_action('device.get')
     msg.add_data('type', 'plugin')
     msg.add_data('name', self._xplPlugin.get_plugin_name())
     msg.add_data('host', get_sanitized_hostname())
     devices = cli.request('dbmgr', msg.get(), timeout=10).get()
     for a_device in devices:
         if a_device['device_type_id'] == client._device['device_type_id']  and a_device['id'] == client._device['id'] :
             if a_device['name'] != client.device['name'] : # rename and change key client id
                 old_id = getIRTransId(client._device)
                 self.irTransClients[getIRTransId(a_device)] = self.irTransClients.pop(old_id)
                 self._xplPlugin.log.info(u"IRTransciever Client {0} is rename {1}".format(old_id,  getIRTransId(a_device)))
             client.updateDevice(a_device)
             break
Ejemplo n.º 25
0
class QueryMQ():
    '''
    Query to the mq to find the config
    '''
    def __init__(self, zmq, log):
        '''
        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._log.debug("Init config query(mq) instance")
        self.cli = MQSyncReq(self._zmq)

    def query(self, name, key = None):
        '''
        Ask the config system for the value. Calling this function will make
        your program wait until it got an answer

        @param name : the plugin 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', 'plugin')
        msg.add_data('name', name)
        msg.add_data('host', get_sanitized_hostname())
        if key != None:
            msg.add_data('key', key)
        else:
            key = "*"
        self._log.info("Request query config for plugin {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 plugin {0} on host {1}, key {2} : no response from dbmgr".format(name, get_sanitized_hostname(), 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
Ejemplo n.º 26
0
    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
Ejemplo n.º 27
0
    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())
Ejemplo n.º 28
0
 def __init__(self, xpl, log):
     """
     Init the query system and connect it to xPL network
     @param xpl : Will not be used
     @param log : a Logger instance (usually took from self.log))
     """
     self.log = log
     self.log.debug("Init config query(mq) instance")
     self.cli = MQSyncReq(zmq.Context())
Ejemplo n.º 29
0
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'
            )
Ejemplo n.º 30
0
def client_devices_known(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 = {}
    with app.db.session_scope():
        devices = app.db.list_devices_by_plugin(client_id)
    return render_template('client_devices.html',
                           datatypes=app.datatypes,
                           devices=devices,
                           clientid=client_id,
                           mactve="clients",
                           active='devices')
Ejemplo n.º 31
0
    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
Ejemplo n.º 32
0
def client_devices_known(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 = {}
    with app.db.session_scope():
        devices = app.db.list_devices_by_plugin(client_id)
    return render_template('client_devices.html',
            datatypes = app.datatypes,
            devices = devices,
            clientid = client_id,
            mactive="clients",
            active = 'devices'
            )
Ejemplo n.º 33
0
 def __init__(self, zmq, log):
     """
     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._log.debug("Init config query(mq) instance")
     self.cli = MQSyncReq(self._zmq)
Ejemplo n.º 34
0
def device_xplstat_params(did):
    cmd = urlHandler.db.get_xpl_stat(device_id)
    if cmd == None:
        # ERROR
        return
    # get the device
    dev = urlHandler.db.get_device(cmd.device_id)
    if dev == None:
        # ERROR
        return
    # get the device_type
    dt = urlHandler.db.get_device_type_by_id(dev.device_type_id)
    if dt == None:
        # ERROR
        return
    # get the json
    cli = MQSyncReq(urlHandler.zmq_context)
    msg = MQMessage()
    msg.set_action('device_types.get')
    msg.add_data('device_type', dev.device_type_id)
    res = cli.request('manager', msg.get(), timeout=10)
    if res is None:
        return "Bad device type"
    pjson = res.get_data()

    if pjson['json_version'] < 2:
        # ERROR
        return
    # get the json device params for this command
    if pjson['xpl_stats'][cmd.name] is None:
        # ERROR
        return
    for p in pjson['xpl_stats'][cmd.name]['parameters']['device']:
        if request.form.get(p['key']) is None:
            # ERROR
            return
        # go and add the param
        urlHandler.db.add_xpl_stat_param(cmd_id=cmd.id,
                                         key=p['key'],
                                         value=request.form.get(['key']))
    urlHandler.reload_stats()
    return 204, ""
Ejemplo n.º 35
0
def api_ncommand(cid):
    cli = MQSyncReq(urlHandler.zmq_context)
    msg = MQMessage()
    msg.set_action('cmd.send')
    msg.add_data('cmdid', cid)
    # build the commandparams
    cmdparams = {}
    for param in request.args:
        cmdparams[param] = request.args.get(param)
    msg.add_data('cmdparams', cmdparams)
    # do the request
    resp = cli.request('xplgw', msg.get(), timeout=10)
    if resp:
        response = resp.get_data()
        if response['status']:
            return 204, None
        else:
            return 400, {'msg': response['reason']}
    else:
        return 400, {'msg': "XPL gateway does not respond"}
Ejemplo n.º 36
0
def api_ncommand(cid):
    cli = MQSyncReq(urlHandler.zmq_context)
    msg = MQMessage()
    msg.set_action('cmd.send')
    msg.add_data('cmdid', cid)
    # build the commandparams
    cmdparams = {}
    for param in request.args:
        cmdparams[param] = request.args.get(param)
    msg.add_data('cmdparams', cmdparams)
    # do the request
    resp = cli.request('xplgw', msg.get(), timeout=10)
    if resp:
        response = resp.get_data()
        if response['status']:
            return 204, None
        else:
            return 400, {'msg': response['reason']}
    else:
        return 400, {'msg': "XPL gateway does not respond"}
Ejemplo n.º 37
0
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))
Ejemplo n.º 38
0
def orphans():
    # get all 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 = {}
    # get all devices
    with app.db.session_scope():
        devs = app.db.list_devices()
    # loop over the devices
    orphan_devs = []
    for dev in devs:
        if dev["client_id"] not in list(client_list.keys()):
            orphan_devs.append(dev)

    return render_template('orphans.html',
                           mactve="orphans",
                           devices=orphan_devs)
Ejemplo n.º 39
0
 def refreshClientDevice(self, client):
     """Request a refresh domogik device data for a IRTrans Client."""
     cli = MQSyncReq(zmq.Context())
     msg = MQMessage()
     msg.set_action('device.get')
     msg.add_data('type', 'plugin')
     msg.add_data('name', self._xplPlugin.get_plugin_name())
     msg.add_data('host', get_sanitized_hostname())
     devices = cli.request('dbmgr', msg.get(), timeout=10).get()
     for a_device in devices:
         if a_device['device_type_id'] == client._device[
                 'device_type_id'] and a_device['id'] == client._device[
                     'id']:
             if a_device['name'] != client.device[
                     'name']:  # rename and change key client id
                 old_id = getIRTransId(client._device)
                 self.irTransClients[getIRTransId(
                     a_device)] = self.irTransClients.pop(old_id)
                 self._xplPlugin.log.info(
                     u"IRTransciever Client {0} is rename {1}".format(
                         old_id, getIRTransId(a_device)))
             client.updateDevice(a_device)
             break
Ejemplo n.º 40
0
 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})
Ejemplo n.º 41
0
def get_device_params(dev_type_id, zmq=None):
    if zmq:
        cli = MQSyncReq(zmq)
    else:
        cli = MQSyncReq(urlHandler.zmq_context)
    msg = MQMessage()
    msg.set_action('device_types.get')
    msg.add_data('device_type', dev_type_id)
    res = cli.request('manager', msg.get(), timeout=10)
    if res is None:
        raise Exception("Bad device type (MQ)")
    pjson = res.get_data()
    if pjson is None:
        raise Exception("Bad device type (json)")
    pjson = pjson[dev_type_id]
    if pjson is None:
        raise Exception("Device type not found")
    # parse the data
    ret = {}
    ret['commands'] = []
    ret['global'] = []
    if 'parameters' in pjson['device_types'][dev_type_id]:
        ret['global'] = pjson['device_types'][dev_type_id]['parameters']
    ret['xpl_stat'] = []
    ret['xpl_cmd'] = []
    # find all features for this device
    for c in pjson['device_types'][dev_type_id]['commands']:
        if not c in pjson['commands']:
            break
        cm = pjson['commands'][c]
        ret['commands'].append(c)
        # we must have an xpl command
        if not 'xpl_command' in cm:
            break
        # we have an xpl_command => find it
        if not cm['xpl_command'] in pjson['xpl_commands']:
            raise "Command references an unexisting xpl_command"
        # find the xpl commands that are neede for this feature
        cmd = pjson['xpl_commands'][cm['xpl_command']].copy()
        cmd['id'] = c
        # finc the xpl_stat message
        cmd = pjson['xpl_commands'][cm['xpl_command']].copy()
        cmd['id'] = c
        # finc the xpl_stat message
        if not 'xplstat_name' in cmd:
            break
        if not cmd['xplstat_name'] in pjson['xpl_stats']:
            raise Exception("XPL command references an unexisting xpl_stat")
        stat = pjson['xpl_stats'][cmd['xplstat_name']].copy()
        stat['id'] = cmd['xplstat_name']
        # remove all parameters
        cmd['parameters'] = cmd['parameters']['device']
        del cmd['parameters']
        ret['xpl_cmd'].append(cmd)
        if stat is not None:
            # remove all parameters
            stat['parameters'] = stat['parameters']['device']
            del stat['parameters']
            ret['xpl_stat'].append(stat)
        del stat
        del cmd
    ret['global'] = [
        x for i, x in enumerate(ret['global'])
        if x not in ret['global'][i + 1:]
    ]
    return ret
Ejemplo n.º 42
0
def client_config(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()
        config = detaila[client_id]['data']['configuration']
    else:
        config = {}
    known_items = []

    # dynamically generate the wtfform
    class F(Form):
        submit = SubmitField("Send")
        pass

    for item in config:
        # keep track of the known fields
        known_items.append(item["key"])
        # handle required
        if item["required"] == "yes":
            arguments = [Required()]
        else:
            arguments = []
        # fill in the field
        if 'value' in item:
            default = item["value"]
        else:
            default = item["default"]
        # build the field
        if item["type"] == "boolean":
            field = BooleanField(item["name"],
                                 arguments,
                                 description=item["description"],
                                 default=default)
        elif item["type"] == "number":
            field = IntegerField(item["name"],
                                 arguments,
                                 description=item["description"],
                                 default=default)
        elif item["type"] == "enum":
            choices = []
            for choice in item["choices"]:
                choices.append((choice, choice))
            field = SelectField(item["name"],
                                arguments,
                                description=item["description"],
                                choices=choices,
                                default=default)
        else:
            field = TextField(item["name"],
                              arguments,
                              description=item["description"],
                              default=default)
        # add the field
        setattr(F, item["key"], field)
    # add the submit button
    field = submit = SubmitField("Send")
    setattr(F, "submit", field)

    form = F()

    if request.method == 'POST' and form.validate():
        # build the requested config set
        data = {}
        for arg, value in list(request.form.items()):
            if arg in known_items:
                data[arg] = value
        # build the message
        msg = MQMessage()
        msg.set_action('config.set')
        tmp = client_id.split('-')
        msg.add_data('type', tmp[0])
        tmp = tmp[1].split('.')
        msg.add_data('host', tmp[1])
        msg.add_data('name', tmp[0])
        msg.add_data('data', data)
        res = cli.request('dbmgr', msg.get(), timeout=10)
        if res is not None:
            data = res.get_data()
            if data["status"]:
                flash(gettext("Config saved successfull"), 'success')
            else:
                flash(gettext("Config saved failed"), 'warning')
                flash(data["reason"], 'danger')
        else:
            flash(
                gettext(
                    "DbMgr did not respond on the config.set, check the logs"),
                'danger')

    return render_template('client_config.html',
                           form=form,
                           clientid=client_id,
                           mactve="clients",
                           active='config')
Ejemplo n.º 43
0
#!/usr/bin/python

import zmq
from zmq.eventloop.ioloop import IOLoop
from domogik.mq.reqrep.client import MQSyncReq
from domogik.mq.message import MQMessage

cli = MQSyncReq(zmq.Context())
print cli.rawrequest('mmi.service', 'xplgw', timeout=10)

cli = MQSyncReq(zmq.Context())
print cli.rawrequest('mmi.services', '', timeout=10)

Ejemplo n.º 44
0
#!/usr/bin/python

import zmq
from zmq.eventloop.ioloop import IOLoop
from domogik.mq.reqrep.client import MQSyncReq
from domogik.mq.message import MQMessage

cli = MQSyncReq(zmq.Context())
msg = MQMessage()
msg.set_action('plugin.stop.do')
msg.add_data('name', 'diskfree')
msg.add_data('host', 'darkstar')
print cli.request('plugin-diskfree.darkstar', msg.get(), timeout=10).get()


Ejemplo n.º 45
0
#!/usr/bin/python

import zmq
from zmq.eventloop.ioloop import IOLoop
from domogik.mq.reqrep.client import MQSyncReq
from domogik.mq.message import MQMessage

cli = MQSyncReq(zmq.Context())
msg = MQMessage()
msg.set_action('helper.help.get')
msg.add_data('command', 'scan')
print cli.request('velbus', msg.get(), timeout=10).get()

Ejemplo n.º 46
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

import zmq
import json
from time import sleep

from domogik.mq.message import MQMessage
from domogik.mq.reqrep.client import MQSyncReq

z = zmq.Context()
m = MQMessage('test.list', {})
m2 = MQMessage('parameter.list', {})
m3 = MQMessage('scenario.list', {})
m4 = MQMessage('action.list', {})
c = MQSyncReq(z)

print "==== List of tests ===="
tests = c.request('scenario', m.get())
print tests
print "==== List of parameters ===="
print c.request('scenario', m2.get())
print "==== List of conditions ===="
print c.request('scenario', m3.get())
print "==== List actions ===="
print c.request('scenario', m4.get())
print "==== Get one test"
tests_data = json.loads(tests._data['payload'])
test_k = tests_data.keys()[0]
test_v = tests_data[tests_data.keys()[0]]
Ejemplo n.º 47
0
#!/usr/bin/python

import zmq
from zmq.eventloop.ioloop import IOLoop
from domogik.mq.reqrep.client import MQSyncReq
from domogik.mq.message import MQMessage

cli = MQSyncReq(zmq.Context())
msg = MQMessage()
msg.set_action('plugin.start.do')
msg.add_data('name', 'diskfree')
msg.add_data('host', 'darkstar')
print cli.request('manager', msg.get(), timeout=10).get()


Ejemplo n.º 48
0
def client_devices_new_wiz(client_id, device_type_id, product):
    # TODO get them
    params = get_device_params(device_type_id, app.zmq_context)

    # dynamically generate the wtfform
    class F(Form):
        name = TextField(
            "Device", [Required()],
            description=gettext("the display name for this device"))
        description = TextField(
            "Description",
            description=gettext("A description for this device"))
        if product:
            default = product
        else:
            default = None
        reference = TextField(
            "Reference",
            description=gettext("A reference for this device"),
            default=default)
        pass

    # add the global params
    for item in params["global"]:
        if item["type"] == "integer":
            field = IntegerField("Global {0}".format(item["key"]),
                                 [Required()],
                                 description=item["description"])
        else:
            field = TextField("Global {0}".format(item["key"]), [Required()],
                              description=item["description"])
        setattr(F, item["key"], field)
    form = F()

    if request.method == 'POST' and form.validate():
        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()
            client_data = detaila[client_id]['data']
        else:
            flash(gettext("Device creation failed"), "warning")
            flash(gettext("Can not find this client id"), "danger")
            return redirect("/client/{0}/devices/known".format(client_id))
        with app.db.session_scope():
            # create the device
            created_device = app.db.add_device_and_commands(
                name=request.form.get('name'),
                device_type=device_type_id,
                client_id=client_id,
                description=request.form.get('description'),
                reference=request.form.get('reference'),
                client_data=client_data)
            # add the global
            for x in app.db.get_xpl_command_by_device_id(created_device["id"]):
                for p in params['global']:
                    if p["xpl"] is True:
                        app.db.add_xpl_command_param(cmd_id=x.id,
                                                     key=p['key'],
                                                     value=request.form.get(
                                                         p['key']))
            for x in app.db.get_xpl_stat_by_device_id(created_device["id"]):
                for p in params['global']:
                    if p["xpl"] is True:
                        app.db.add_xpl_stat_param(statid=x.id,
                                                  key=p['key'],
                                                  value=request.form.get(
                                                      p['key']),
                                                  static=True,
                                                  type=p['type'])
            for p in params['global']:
                if p["xpl"] is not True:
                    app.db.add_device_param(created_device["id"], p["key"],
                                            request.form.get(p['key']),
                                            p['type'])
            # reload stats
            req = MQSyncReq(app.zmq_context)
            msg = MQMessage()
            msg.set_action('reload')
            resp = req.request('xplgw', msg.get(), 100)
            # inform the user
            flash(gettext("device created"), "success")
            return redirect("/client/{0}/devices/known".format(client_id))

    return render_template('client_device_new_wiz.html',
                           form=form,
                           params=params,
                           dtype=device_type_id,
                           clientid=client_id,
                           mactve="clients",
                           active='devices')