def getIdsClient(self, idToCheck):
     """Get UPS client key ids."""
     retval =[]
     findId = ""
     self._xplPlugin.log.debug (u"getIdsClient check for device : {0}".format(idToCheck))
     if isinstance(idToCheck,  UPSClient) :
         for id in self.upsClients.keys() :
             if self.upsClients[id] == idToCheck :
                 retval = [id]
                 break
     else :
         self._xplPlugin.log.debug (u"getIdsClient, no UPSClient instance...")
         if isinstance(idToCheck,  str) :  
             findId = idToCheck
             self._xplPlugin.log.debug (u"str instance...")
         else :
             if isinstance(idToCheck,  dict) :
                 if idToCheck.has_key('device') : findId = idToCheck['device']
                 else :
                     if idToCheck.has_key('name') and idToCheck.has_key('id'): 
                         findId = getUPSId(idToCheck)
         if self.upsClients.has_key(findId) : 
             retval = [findId]
             self._xplPlugin.log.debug (u"key id type find")
         else :
             self._xplPlugin.log.debug (u"No key id type, search {0} in devices {1}".format(findId, self.upsClients.keys()))
             for id in self.upsClients.keys() :
                 self._xplPlugin.log.debug(u"Search in list by device key : {0}".format(self.upsClients[id].domogikDevice))
                 if self.upsClients[id].domogikDevice == findId : 
                     self._xplPlugin.log.debug('find UPS Client :)')
                     retval.append(id)
     self._xplPlugin.log.debug(u"getIdsClient result : {0}".format(retval))
     return retval
 def refreshClientDevice(self,  client):
     """Request a refresh domogik device data for a UPS 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 = getUPSId(client._device)
                 self.upsClients[getUPSId(a_device)] = self.upsClients.pop(old_id)
                 self._xplPlugin.log.info(u"UPS Client {0} is rename {1}".format(old_id,  getUPSId(a_device)))
             client.updateDevice(a_device)
             break
 def addClient(self, device):
     """Add a UPS from domogik device"""
     name = getUPSId(device)
     if self.upsClients.has_key(name) :
         self._xplPlugin.log.debug(u"Manager UPS : UPS Client {0} already exist, not added.".format(name))
         return False
     else:
         if checkIfConfigured(device["device_type_id"],  device ) :
             if device["device_type_id"] == "ups.device" :
                 self.upsClients[name] = UPSClient(self,  device,  self._xplPlugin.log)
             else :
                 self._xplPlugin.log.error(u"Manager UPS : UPS Client type {0} not exist, not added.".format(name))
                 return False                
             self._xplPlugin.log.info(u"Manager UPS : created new client {0}.".format(name))
         else : 
             self._xplPlugin.log.info(u"Manager UPS : device not configured can't add new client {0}.".format(name))
             return False
         return True
    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()