Ejemplo n.º 1
0
    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())
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
 def on_message(self, msg, content=None):
     #print(ws_list.list())
     """ This function is quite tricky
         It is called by both WebSocketHandler and MQASyncSub...
     """
     try:
         ### websocket message (from the web)
         # there are the mesages send by the administration javascript part thanks to the ws.send(...) function
         if not content:
             jsons = json.loads(msg)
             # req/rep
             if 'mq_request' in jsons and 'data' in jsons:
                 cli = MQSyncReq(zmq.Context())
                 msg = MQMessage()
                 msg.set_action(str(jsons['mq_request']))
                 msg.set_data(jsons['data'])
                 if 'dst' in jsons:
                     cli.request(str(jsons['dst']), msg.get(), timeout=10).get()
                 else:
                     cli.request('manager', msg.get(), timeout=10).get()
             # pub
             elif 'mq_publish' in jsons and 'data' in jsons:
                 print("Publish : {0}".format(jsons['data']))
                 self.pub.send_event(jsons['mq_publish'],
                                     jsons['data'])
         ### MQ message (from domogik)
         # these are the published messages which are received here
         else:
             try:
                 self.write_message({"msgid": msg, "content": content})
             except WebSocketClosedError:
                 self.close()
     except:
         print("Error : {0}".format(traceback.format_exc()))
Ejemplo n.º 4
0
 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))
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
    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())
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
    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())
Ejemplo n.º 9
0
    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
            })
Ejemplo n.º 10
0
    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})
Ejemplo n.º 11
0
    def _mdp_reply_config_set(self, data):
        """ Reply to config.set MQ req
            @param data : MQ req message
        """
        print "#################"
        msg = MQMessage()
        msg.set_action('config.result')
        status = True
        msg_data = data.get_data()
        if 'type' not in msg_data:
            status = False
            reason = "Config set : missing 'type' field : {0}".format(data)

        if msg_data['type'] != "plugin":
            status = False
            reason = "Config set not available for type={0}".format(msg_data['type'])

        if 'name' not in msg_data:
            status = False
            reason = "Config set : missing 'name' field : {0}".format(data)

        if 'host' not in msg_data:
            status = False
            reason = "Config set : missing 'host' field : {0}".format(data)

        if 'data' not in msg_data:
            status = False
            reason = "Config set : missing 'data' field : {0}".format(data)

        if status == False:
            self.log.error(reason)
        else:
            reason = ""
            type = msg_data['type']
            name = msg_data['name']
            host = msg_data['host']
            data = msg_data['data']
            msg.add_data('type', type)
            msg.add_data('name', name)
            msg.add_data('host', host)
            try: 
                # we add a configured key set to true to tell the UIs and plugins that there are some configuration elements
                self._db.set_plugin_config(name, host, "configured", True)
                for key in msg_data['data']:
                    self._db.set_plugin_config(name, host, key, data[key])
                self.publish_config_updated(type, name, host)
            except:
                reason = "Error while setting configuration for '{0} {1} on {2}' : {3}".format(type, name, host, traceback.format_exc())
                status = False
                self.log.error(reason)

        msg.add_data('status', status)
        msg.add_data('reason', reason)

        self.log.debug(msg.get())
        self.reply(msg.get())
Ejemplo n.º 12
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 = "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']
            })
Ejemplo n.º 13
0
    def _mdp_reply_config_delete(self, data):
        """ Reply to config.delete MQ req
            Delete all the config items for the given type, name and host
            @param data : MQ req message
        """
        msg = MQMessage()
        msg.set_action('config.result')
        status = True
        msg_data = data.get_data()
        if 'type' not in msg_data:
            status = False
            reason = "Config request : missing 'type' field : {0}".format(data)

        if msg_data['type'] not in ["plugin", "brain", "interface"]:
            status = False
            reason = "Configuration deletion not available for type={0}".format(
                msg_data['type'])

        if 'name' not in msg_data:
            status = False
            reason = "Config request : missing 'name' field : {0}".format(data)

        if 'host' not in msg_data:
            status = False
            reason = "Config request : missing 'host' field : {0}".format(data)

        if status == False:
            self.log.error(reason)
        else:
            reason = ""
            type = msg_data['type']
            name = msg_data['name']
            host = msg_data['host']
            msg.add_data('type', type)
            msg.add_data('name', name)
            msg.add_data('host', host)
            try:
                self._db.del_plugin_config(type, name, host)
                self.log.info(u"Delete config for {0} {1}".format(type, name))
                self.publish_config_updated(type, name, host)
            except:
                status = False
                reason = "Error while deleting configuration for '{0} {1} on {2} : {3}".format(
                    type, name, host, traceback.format_exc())
                self.log.error(reason)

        msg.add_data('reason', reason)
        msg.add_data('status', status)

        self.log.debug(msg.get())
        self.reply(msg.get())
Ejemplo n.º 14
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 = "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']})
Ejemplo n.º 15
0
    def _mdp_reply_config_delete(self, data):
        """ Reply to config.delete MQ req
            Delete all the config items for the given type, name and host
            @param data : MQ req message
        """
        msg = MQMessage()
        msg.set_action('config.result')
        status = True
        msg_data = data.get_data()
        if 'type' not in msg_data:
            status = False
            reason = "Config request : missing 'type' field : {0}".format(data)

        if msg_data['type'] not in ["plugin", "brain", "interface"]:
            status = False
            reason = "Configuration deletion not available for type={0}".format(msg_data['type'])

        if 'name' not in msg_data:
            status = False
            reason = "Config request : missing 'name' field : {0}".format(data)

        if 'host' not in msg_data:
            status = False
            reason = "Config request : missing 'host' field : {0}".format(data)

        if status == False:
            self.log.error(reason)
        else:
            reason = ""
            type = msg_data['type']
            name = msg_data['name']
            host = msg_data['host']
            msg.add_data('type', type)
            msg.add_data('name', name)
            msg.add_data('host', host)
            try:
                self._db.del_plugin_config(type, name, host)
                self.log.info(u"Delete config for {0} {1}".format(type, name))
                self.publish_config_updated(type, name, host)
            except:
                status = False
                reason = "Error while deleting configuration for '{0} {1} on {2} : {3}".format(type, name, host, traceback.format_exc())
                self.log.error(reason)

        msg.add_data('reason', reason)
        msg.add_data('status', status)

        self.log.debug(msg.get())
        self.reply(msg.get())
Ejemplo n.º 16
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,
                           mactive="clients",
                           active='devices')
Ejemplo n.º 17
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.º 18
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.º 19
0
    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)
Ejemplo n.º 20
0
    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)
Ejemplo n.º 21
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.º 22
0
    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()
Ejemplo n.º 23
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.º 24
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")
Ejemplo n.º 25
0
    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())
Ejemplo n.º 26
0
    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()
Ejemplo n.º 27
0
    def send_to_butler(self, command_line, widget_proxy, live):
        you = u"You > {0}".format(command_line)
        self.parent.value.set_values(self.parent.value.get() + [you])
        self.parent.wMain.values = self.parent.value.get()
        self.parent.wMain.display()

        # handle special commands
        if command_line.lower() == "quit":
            IOLoop.instance().stop()
            sys.exit(0)  # why is this needed ?

        elif command_line.lower() == "reload":
            try:
                cli = MQSyncReq(zmq.Context())
                msg = MQMessage()
                msg.set_action('butler.reload.do')
                result = cli.request('butler', msg.get(), timeout=10).get()
                if result:
                    msg = "*** action reload : {0}".format(result)
                    self.parent.value.set_values(self.parent.value.get() + [msg])
            except:
                msg = u"*** action reload : error (is butler component ok ?)"
                self.parent.value.set_values(self.parent.value.get() + [msg])

        # handle butler
        else:
            self.parent.butler_cb(command_line, identity = "cli user", media = "chat", location = None, mood = None)

        self.parent.wMain.values = self.parent.value.get()
        self.parent.wMain.display()
Ejemplo n.º 28
0
    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())
Ejemplo n.º 29
0
 def _mdp_reply_butler_history(self, message):
     """ Butler history
     """
     msg = MQMessage()
     msg.set_action('butler.history.result')
     msg.add_data("history", self.history)
     self.reply(msg.get())
Ejemplo n.º 30
0
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))
Ejemplo n.º 31
0
 def _mdp_reply_butler_features(self, message):
     """ Butler features
     """
     msg = MQMessage()
     msg.set_action('butler.features.result')
     msg.add_data("features", self.butler_features)
     self.reply(msg.get())
Ejemplo n.º 32
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)"
         )
Ejemplo n.º 33
0
 def _load_client_to_xpl_target(self):
     """ Request the client conversion info
         This is an mq req to manager
         If this function does not call self._parse_xpl_target(), all xpl sensors will not be procesed!
     """
     nb_try = 0
     max_try = 5
     ok = False
     while nb_try <= max_try and ok == False:
         nb_try += 1
         cli = MQSyncReq(self.zmq)
         msg = MQMessage()
         msg.set_action('client.list.get')
         response = cli.request('manager', msg.get(), timeout=10)
         if response:
             self._parse_xpl_target(response.get_data())
             ok = True
         else:
             self.log.error(\
                 u"Updating client list failed, no response from manager (try number {0}/{1})".format(nb_try, max_try))
             # We fail to load the client list, so we wait to get something
             time.sleep(5)
     if ok == True:
         self.log.info(u"Updating client list success") 
     else:
         self.log.error(u"Updating client list failed too much time! The xpl sensors will not be processed by this component")
Ejemplo n.º 34
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.º 35
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,
            mactive="clients",
            active = 'devices'
            )
Ejemplo n.º 36
0
 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())
Ejemplo n.º 37
0
    def do_action(self):
        self.log.info(u"Command : Do an action...")

        # live udate some values
        self.log.debug(u"Command : Preprocessing on parameters...")
        self.log.debug(u"Command : Parameters before processing : {0}".format(self._params))
        params = {}
        for key in self._params:
            self._params[key] = ucode(self._params[key])
            self.log.debug(u"Command : Preprocess for param : key={0}, typeofvalue={1}, value={2}".format(key, type(self._params[key]), self._params[key]))
            params[key] = self._params[key]
            if key == "color" and params[key].startswith("#"):
                self.log.debug(u"- Processing : for a color, if the color starts with #, remove it")
                params[key] = params[key][1:]

        self.log.debug(u"Command : Parameters after processing : {0}".format(params))
        self.log.debug(u"Command : Send action command over MQ...")

        # do the command
        cli = MQSyncReq(zmq.Context())
        msg = MQMessage()
        msg.set_action('cmd.send')
        msg.add_data('cmdid', self._cmdId)
        msg.add_data('cmdparams', params)

        self.log.debug(u"Command : Command id = '{0}', command params = '{1}'".format(self._cmdId, params)) 
        # do the request
        res = cli.request('xplgw', msg.get(), timeout=10)
        if res:
            data = res.get_data()
            if not data['status']:
                self.log.error(u"Command : Command sending to XPL gw failed: {0}".format(res))
        else:
            self.log.error(u"Command : XPL gw did not respond")
        self.log.debug(u"Command : Action done")
Ejemplo n.º 38
0
    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()
Ejemplo n.º 39
0
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))
Ejemplo n.º 40
0
    def _mdp_reply_plugin_stop(self, data):
        """ Stop the plugin
            @param data : MQ req message

            First, send the MQ Rep to 'ack' the request
            Then, change the plugin status to STATUS_STOP_REQUEST
            Then, quit the plugin by calling force_leave(). This should make the plugin send a STATUS_STOPPED if all is ok

            Notice that no check is done on the MQ req content : we need nothing in it as it is directly addressed to a plugin
        """
        # check if the message is for us
        content = data.get_data()
        if content['name'] != self._name or content['host'] != self.get_sanitized_hostname():
            return

        ### Send the ack over MQ Rep
        msg = MQMessage()
        msg.set_action('plugin.stop.result')
        status = True
        reason = ""
        msg.add_data('status', status)
        msg.add_data('reason', reason)
        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 plugin status
        self._set_status(STATUS_STOP_REQUEST)

        ### Try to stop the plugin
        # if it fails, the manager should try to kill the plugin
        self.force_leave()
Ejemplo n.º 41
0
    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
Ejemplo n.º 42
0
 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())
Ejemplo n.º 43
0
    def on_mdp_request(self, msg):
        """ Called when a MQ req/rep message is received
        """
        XplPlugin.on_mdp_request(self, msg)
        if msg.get_action() == "client.cmd":
            print(msg)
            reason = None
            status = True
            data = msg.get_data()
            if 'blacklist' in data:
                bl = data['blacklist']
            else:
                reason = u"Invalid command : no blacklist key in message"
                status = False

            if status == True:
                try:
                    with open(self.blacklist_file, 'ab') as fp_blacklist:
                        fp_blacklist.write("\n{0};{1}".format("manual blacklisting", bl))
                except:
                    reason = u"Error while completing blacklist file : {0}. Error is : {1}".format(self.blacklist_file, traceback.format_exc())
                    self.log.error(reason)
                    status = False
                self.load_blacklist()


            self.log.info("Reply to command")
            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())

            if status == True:
                thread.start_new_thread(self.open_modems, ())
Ejemplo n.º 44
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.º 45
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.º 46
0
def client_devices_known(client_id):
    detail = get_client_detail(client_id)

    if app.datatypes == {}:
        cli = MQSyncReq(app.zmq_context)
        msg = MQMessage()
        msg.set_action('datatype.get')
        res = cli.request('manager', msg.get(), timeout=10)
        if res is not None:
            app.datatypes = res.get_data()['datatypes']
        else:
            app.datatypes = {}

    # todo : grab from MQ ?
    with app.db.session_scope():
        devices = app.db.list_devices_by_plugin(client_id)

    # sort clients per device type
    devices_by_device_type_id = {}
    for dev in devices:
        if devices_by_device_type_id.has_key(dev['device_type_id']):
            devices_by_device_type_id[dev['device_type_id']].append(dev)
        else:
            devices_by_device_type_id[dev['device_type_id']] = [dev]

    return render_template('client_devices.html',
            datatypes = app.datatypes,
            devices = devices,
            devices_by_device_type_id = devices_by_device_type_id,
            clientid = client_id,
            mactive="clients",
            active = 'devices',
            rest_url = get_rest_url(),
            client_detail = detail
            )
Ejemplo n.º 47
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 = {}

    client_list_per_host_per_type = OrderedDict()
    for client in client_list:
        cli_type = client_list[client]['type']
        cli_host = client_list[client]['host']

        if not client_list_per_host_per_type.has_key(cli_host):
            client_list_per_host_per_type[cli_host] = {}

        if not client_list_per_host_per_type[cli_host].has_key(cli_type):
            client_list_per_host_per_type[cli_host][cli_type] = {}

        client_list_per_host_per_type[cli_host][cli_type][client] = client_list[client]

    return render_template('clients.html',
        mactive="clients",
        overview_state="collapse",
        clients=client_list,
        client_list_per_host_per_type=client_list_per_host_per_type
        )
Ejemplo n.º 48
0
    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)
Ejemplo n.º 49
0
    def loadDevices(cls, develop):
        logger.info(u"MQ: Loading Devices info")
        Device.clean()
        msg = MQMessage()
        msg.set_action('client.list.get')
        res = cli.request('manager', msg.get(), timeout=10)
        if res is not None:
            _datac = res.get_data()
        else:
            _datac = {}
        session = Session()
        for client in _datac.itervalues(): 
            # for each plugin client, we request the list of devices
            if client["type"] == "plugin":
                msg = MQMessage()
                msg.set_action('device.get')
                msg.add_data('type', 'plugin')
                msg.add_data('name', client["name"])
                msg.add_data('host', client["host"])
                logger.info(u"MQ: Get devices list for client {0}-{1}.{2}".format("plugin", client["name"], client["host"]))
                res = cli.request('dbmgr', msg.get(), timeout=10)
                if res is not None:
                    _datad = res.get_data()
                else:
                    _datad = {}
                if 'devices' in _datad:
                    for device in _datad["devices"]:
                        logger.info(u"- {0}".format(device["name"]))
                        d = Device(id=device["id"], name=device["name"], type=device["device_type_id"], reference=device["reference"])
                        session.add(d)
                        if "commands" in device:
                            for ref, command  in device["commands"].iteritems():
                                c = Command(id=command["id"], name=command["name"], device_id=device["id"], reference=ref, return_confirmation=command["return_confirmation"])
                                session.add(c)
                                c.datatypes = ""
                                for param in command["parameters"]:
                                    p = CommandParam(command_id=c.id, key=param["key"], datatype_id=param["data_type"])
                                    session.add(p)
                                    c.datatypes += param["data_type"]
                                session.add(c)
                        if "sensors" in device:
                            for ref, sensor in device["sensors"].iteritems():
                                s = Sensor(id=sensor["id"], name=sensor["name"], device_id=device["id"], reference=ref, datatype_id=sensor["data_type"], last_value=sensor["last_value"], last_received=sensor["last_received"], timeout=sensor["timeout"])
                                session.add(s)

        session.commit()
        session.flush()
Ejemplo n.º 50
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 = 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')
Ejemplo n.º 51
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.º 52
0
 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())
Ejemplo n.º 53
0
    def _mdp_reply_deviceparam_update_result(self, data):
        status = True
        reason = False

        self.log.debug(u"Updating device param : {0}".format(data))
        try:
            data = data.get_data()
            if 'dpid' in data:
                dpid = data['dpid']
                val = data['value']
                # do the update
                res = self._db.udpate_device_param(dpid, value=val)
                if not res:
                    status = False
                else:
                    status = True
            else:
                status = False
                reason = "There is no such device param"
                self.log.debug(reason)
            # delete done
        except DbHelperException as d:
            status = False
            reason = "Error while updating device param: {0}".format(d.value)
            self.log.error(reason)
        except:
            status = False
            reason = "Error while updating device param: {0}".format(
                traceback.format_exc())
            self.log.error(reason)
        # send the result
        msg = MQMessage()
        msg.set_action('deviceparam.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:
            dev = self._db.get_device(res.device_id)
            self._pub.send_event('device.update', {
                "device_id": res.device_id,
                "client_id": dev['client_id']
            })
Ejemplo n.º 54
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.º 55
0
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