コード例 #1
0
ファイル: plugin.py プロジェクト: thefrip/domogik
    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
コード例 #2
0
 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)"
         )
コード例 #3
0
 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")
コード例 #4
0
ファイル: testcommand.py プロジェクト: thefrip/domogik
    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
コード例 #5
0
def configure(type, name, host, key, value):
    cli = MQSyncReq(zmq.Context())
    msg = MQMessage()
    msg.set_action('config.set')
    msg.add_data('type', type)
    msg.add_data('host', host)
    msg.add_data('name', name)
    msg.add_data('data', {key: value})
    result = cli.request('dbmgr', msg.get(), timeout=10)
    if result:
        data = result.get_data()
        if 'status' in data:
            if not data['status']:
                print(result.get())
                raise RuntimeError(
                    "DbMgr did not return status true on a config.set for {0}-{1}.{2} : {3} = {4}"
                    .format(type, name, host, key, value))
            else:
                return True
        else:
            print(result.get())
            raise RuntimeError(
                "DbMgr did not return a status on a config.set for {0}-{1}.{2} : {3} = {4}"
                .format(type, name, host, key, value))
    else:
        raise RuntimeError(
            "Error while setting configuration for {0}-{1}.{2} : {3} = {4}".
            format(type, name, host, key, value))
コード例 #6
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}/devices/known".format(client_id))
        else:
            return render_template('client_device_edit.html',
                                   form=form,
                                   clientid=client_id,
                                   mactve="clients",
                                   active='devices')
コード例 #7
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')
コード例 #8
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")
コード例 #9
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))
コード例 #10
0
ファイル: queryconfig.py プロジェクト: wookiesh/domogik
 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())
コード例 #11
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
        """

        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())
コード例 #12
0
ファイル: queryconfig.py プロジェクト: thefrip/domogik
 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)
コード例 #13
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")
コード例 #14
0
ファイル: xplgw.py プロジェクト: thefrip/domogik
 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"
         )
コード例 #15
0
ファイル: xplgw.py プロジェクト: thefrip/domogik
 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"
         )
コード例 #16
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)
コード例 #17
0
ファイル: command.py プロジェクト: thefrip/domogik
 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")
コード例 #18
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,
                           mactve="clients",
                           active='devices')
コード例 #19
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')
コード例 #20
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})
コード例 #21
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
コード例 #22
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')
コード例 #23
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, ""
コード例 #24
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"}
コード例 #25
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))
コード例 #26
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)
コード例 #27
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
コード例 #28
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()

コード例 #29
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')
コード例 #30
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')