def __init__(self):
        """ Init plugin
        """
        XplPlugin.__init__(self, name='nutserve')

        # check if the plugin is configured. If not, this will stop the plugin and log an error
        if not self.check_configured():
            return
        host = self.get_config("host")
        port = self.get_config("port")
        login =  self.get_config("login")
        pwd =  self.get_config("pwd")
        # get the devices list
        self.devices = self.get_device_list(quit_if_no_device = False)
        # get the config values
        self.managerClients = UpsManager(self,  host,  port, login if login else None,  pwd if login else None, self.send_xplTrig)
        for a_device in self.devices :
            try :
                if a_device['device_type_id'] != 'ups.device' :
                    self.log.error(u"No a device type reconized : {0}".format(a_device['device_type_id']))
                    break
                else :
                    if self.managerClients.addClient(a_device) :
                        self.log.info("Ready to work with device {0}".format(getUPSId(a_device)))
                    else : self.log.info("Device parameters not configured, can't create UPS Client : {0}".format(getUPSId(a_device)))
            except:
                self.log.error(traceback.format_exc())
                # we don't quit plugin if an error occured
                #self.force_leave()
                #return
        # Create the xpl listeners
        Listener(self.handle_xpl_cmd, self.myxpl,{'schema': 'ups.basic',
                                                                        'xpltype': 'xpl-cmnd'})
        self.add_stop_cb(self.managerClients.stop)
        print "Plugin ready :)"
        self.log.info("Plugin ready :)")
        self.ready()
class NUTManager(XplPlugin):
    """ Envois et recois des codes xPL UPS
    """

    def __init__(self):
        """ Init plugin
        """
        XplPlugin.__init__(self, name='nutserve')

        # check if the plugin is configured. If not, this will stop the plugin and log an error
        if not self.check_configured():
            return
        host = self.get_config("host")
        port = self.get_config("port")
        login =  self.get_config("login")
        pwd =  self.get_config("pwd")
        # get the devices list
        self.devices = self.get_device_list(quit_if_no_device = False)
        # get the config values
        self.managerClients = UpsManager(self,  host,  port, login if login else None,  pwd if login else None, self.send_xplTrig)
        for a_device in self.devices :
            try :
                if a_device['device_type_id'] != 'ups.device' :
                    self.log.error(u"No a device type reconized : {0}".format(a_device['device_type_id']))
                    break
                else :
                    if self.managerClients.addClient(a_device) :
                        self.log.info("Ready to work with device {0}".format(getUPSId(a_device)))
                    else : self.log.info("Device parameters not configured, can't create UPS Client : {0}".format(getUPSId(a_device)))
            except:
                self.log.error(traceback.format_exc())
                # we don't quit plugin if an error occured
                #self.force_leave()
                #return
        # Create the xpl listeners
        Listener(self.handle_xpl_cmd, self.myxpl,{'schema': 'ups.basic',
                                                                        'xpltype': 'xpl-cmnd'})
        self.add_stop_cb(self.managerClients.stop)
        print "Plugin ready :)"
        self.log.info("Plugin ready :)")
        self.ready()
        
    def __del__(self):
        """Close managerClients"""
        print "Try __del__ self.managerClients."
        self.managerClients = None

    def send_xplStat(self, data):
        """ Send xPL cmd message on network
        """
        msg = XplMessage()
        msg.set_type("xpl-stat")
        msg.set_schema("sensor.basic")
        msg.add_data(data)
        self.myxpl.send(msg)

    def send_xplTrig(self, schema,  data):
        """ Send xPL message on network
        """
        self.log.debug("Xpl Trig for {0}".format(data))
        msg = XplMessage()
        msg.set_type("xpl-trig")
        msg.set_schema(schema)
        msg.add_data(data)
        self.myxpl.send(msg)
        
    def handle_xpl_trig(self, message):
        self.log.debug("xpl-trig listener received message:{0}".format(message))
        print message
    
    def handle_xpl_cmd(self,  message):
        """ Process xpl schema ups.basic
        """
        self.log.debug("xpl-cmds listener received message:{0}".format(message))
        device_name = message.data['device']
        self.log.debug("device :" + device_name)
        idsClient = self.managerClients.getIdsClient(device_name)
        find = False
        if idsClient != [] :
            for id in idsClient :       
                client = self.managerClients.getClient(id)
                if client :
                    self.log.debug("Handle xpl-cmds for UPS :{0}".format(message.data['device']))
                    find = True
                    client.handle_xpl_cmd(message.data)
        if not find : self.log.debug("xpl-cmds received for unknowns UPS :{0}".format(message.data['device']))