def __init__(self): """ Init plugin """ XplPlugin.__init__(self, name='script') # check if the plugin is configured. If not, this will stop the plugin and log an error if not self.check_configured(): return # ### get the devices list # for this plugin, if no devices are created we won't be able to use devices. self.devices = self.get_device_list(quit_if_no_device=True) # print(self.devices) # List devices créés dans plugin ? # Init script functions self.script = Script(self.log) # ### For each device for a_device in self.devices: # self.log.info(u"a_device: %s" % format(a_device)) device_name = a_device["name"] # Ex.: "Conso Elec Jour" device_typeid = a_device["device_type_id"] # Ex.: "script.info_number | script.info_binary | script.info_string | script.action" device_statname = device_typeid.replace('.', '_') # Ex.: "script_info_number | script_info_binary | script_info_string | script_action" command_script = self.get_parameter_for_feature(a_device, "xpl_stats", "stat_" + device_statname, "command") # Ex.: "/home/user/getElec.sh -jour" if device_typeid != "script.action": # Shedule only script_info_* scripts command_interval = self.get_parameter(a_device, "interval") # Ex.: "60" in secondes self.log.info(u"==> Device '{0}' ({1}) to call = '{2}' with interval = {3}s".format(device_name, device_typeid, command_script, command_interval)) if command_interval > 0: thr_name = "dev_{0}-{1}".format(a_device['id'], "script_info") self.log.info(u"==> Launch script thread '%s' for '%s' device !" % (thr_name, device_name)) threads = {} threads[thr_name] = threading.Thread(None, self.script.runScheduledCmd, thr_name, (self.log, device_name, device_statname, command_script, command_interval, self.send_xpl, self.get_stop() ), {}) threads[thr_name].start() self.register_thread(threads[thr_name]) self.log.info(u"==> Wait some time before running the next scheduled script ...") time.sleep(5) # Wait some time to not start the script with the same interval et the same time. else: self.log.info(u"==> Script thread '%s' for '%s' device is DISABLED (interval < 0) !" % (thr_name, device_name)) else: self.log.info(u"==> Device '{0}' ({1}) to call = '{2}'".format(device_name, device_typeid, command_script)) # Create listeners self.log.info(u"==> Creating listener for Script") Listener(self.scriptCmnd_cb, self.myxpl, {'xpltype': 'xpl-cmnd', 'schema': 'exec.basic'}) self.ready()
class XplScriptManager(XplPlugin): """ """ def __init__(self): """ Init plugin """ XplPlugin.__init__(self, name='script') # check if the plugin is configured. If not, this will stop the plugin and log an error if not self.check_configured(): return # ### get the devices list # for this plugin, if no devices are created we won't be able to use devices. self.devices = self.get_device_list(quit_if_no_device=True) # print(self.devices) # List devices créés dans plugin ? # Init script functions self.script = Script(self.log) # ### For each device for a_device in self.devices: # self.log.info(u"a_device: %s" % format(a_device)) device_name = a_device["name"] # Ex.: "Conso Elec Jour" device_typeid = a_device["device_type_id"] # Ex.: "script.info_number | script.info_binary | script.info_string | script.action" device_statname = device_typeid.replace('.', '_') # Ex.: "script_info_number | script_info_binary | script_info_string | script_action" command_script = self.get_parameter_for_feature(a_device, "xpl_stats", "stat_" + device_statname, "command") # Ex.: "/home/user/getElec.sh -jour" if device_typeid != "script.action": # Shedule only script_info_* scripts command_interval = self.get_parameter(a_device, "interval") # Ex.: "60" in secondes self.log.info(u"==> Device '{0}' ({1}) to call = '{2}' with interval = {3}s".format(device_name, device_typeid, command_script, command_interval)) if command_interval > 0: thr_name = "dev_{0}-{1}".format(a_device['id'], "script_info") self.log.info(u"==> Launch script thread '%s' for '%s' device !" % (thr_name, device_name)) threads = {} threads[thr_name] = threading.Thread(None, self.script.runScheduledCmd, thr_name, (self.log, device_name, device_statname, command_script, command_interval, self.send_xpl, self.get_stop() ), {}) threads[thr_name].start() self.register_thread(threads[thr_name]) self.log.info(u"==> Wait some time before running the next scheduled script ...") time.sleep(5) # Wait some time to not start the script with the same interval et the same time. else: self.log.info(u"==> Script thread '%s' for '%s' device is DISABLED (interval < 0) !" % (thr_name, device_name)) else: self.log.info(u"==> Device '{0}' ({1}) to call = '{2}'".format(device_name, device_typeid, command_script)) # Create listeners self.log.info(u"==> Creating listener for Script") Listener(self.scriptCmnd_cb, self.myxpl, {'xpltype': 'xpl-cmnd', 'schema': 'exec.basic'}) self.ready() def scriptCmnd_cb(self, message): """ Call script lib for run command @param message : xpl message type : Command type, must be set to "script.info_number | script.info_binary | script.action" command : Executable filename, including path and extension status : 'start' for running command """ self.log.debug(u"==> Call scriptCmnd_cb") scripttype = message.data['type'] if (scripttype != "script_info_number") and (scripttype != "script_info_binary") and (scripttype != "script_action"): self.log.error(u"### This command type %s' is not for Domogik Script plugin" % message.data['type']) return if message.data['status'] != "start": self.log.error(u"### This command with status '%s' is not for Domogik Script plugin" % message.data['status']) return command = message.data['command'].strip() # Execute command self.log.info(u"==> Execute requested script '%s' type '%s'" % (command, scripttype)) # call command resultcmd = self.script.runCmd(command, scripttype) # resultcmd = "executed|value|failed" # Send ACK xpl-trig message to xpl-cmnd command. self.log.debug(u"==> Send xpl-trig msg for script '%s' with return '%s'" % (command, resultcmd)) self.send_xpl(u"xpl-trig", {"command": command, "type": scripttype, "status": resultcmd}) def send_xpl(self, type, data): """ Send data on xPL network @param data : data to send (dict) """ msg = XplMessage() msg.set_type(type) msg.set_schema(u"exec.basic") for element in data: msg.add_data({element: data[element]}) self.log.debug(u"==> Send xpl message...") self.log.debug(msg) self.myxpl.send(msg)