Exemplo n.º 1
0
    def __init__(self):
        """ Create listener for Foscam Relay
        """
        XplPlugin.__init__(self, name='foscam')

        self._config = Query(self.myxpl, self.log)
        # Configuration : list of cameras
        self.cameras = {}
        num = 1
        loop = True
        while loop == True:
            #Get each camera settings
            name = self._config.query('foscam', 'name-%s' % str(num))
            ip = self._config.query('foscam', 'ip-%s' % str(num))
            port = self._config.query('foscam', 'port-%s' % str(num))
            user = self._config.query('foscam', 'user-%s' % str(num))
            password = self._config.query('foscam', 'password-%s' % str(num))
            delay = self._config.query('foscam', 'delay-%s' % str(num))
            if port == None:
                port = 80
            if user == None:
                user = ""
            if password == None:
                password = ""
            if delay == None:
                delay = 0
            if name != None:
                self.log.info(
                    "Configuration : name=%s, ip=%s, port=%s, user=%s, password=No_Log, delay=%s"
                    % (name, ip, port, user, delay))
                self.cameras[name] = {
                    "ip": ip,
                    "port": port,
                    "user": user,
                    "password": password,
                    "delay": delay
                }
            else:
                loop = False
            num += 1

        ### Create FOSCAM object
        self._foscammanager = RELAY(self.log)
        # Create listeners
        Listener(self.foscam_command, self.myxpl, {
            'schema': 'control.basic',
            'xpltype': 'xpl-cmnd',
            'type': 'output'
        })
        self.log.info("Listener for Foscam relay created")
        #print ("Listener for Foscam relay created")
        self.enable_hbeat()
Exemplo n.º 2
0
class foscam(XplPlugin):
    """ Implements a listener for Foscam relay messages on xPL network
    """
    def __init__(self):
        """ Create listener for Foscam Relay
        """
        XplPlugin.__init__(self, name='foscam')

        self._config = Query(self.myxpl, self.log)
        # Configuration : list of cameras
        self.cameras = {}
        num = 1
        loop = True
        while loop == True:
            #Get each camera settings
            name = self._config.query('foscam', 'name-%s' % str(num))
            ip = self._config.query('foscam', 'ip-%s' % str(num))
            port = self._config.query('foscam', 'port-%s' % str(num))
            user = self._config.query('foscam', 'user-%s' % str(num))
            password = self._config.query('foscam', 'password-%s' % str(num))
            delay = self._config.query('foscam', 'delay-%s' % str(num))
            if port == None:
                port = 80
            if user == None:
                user = ""
            if password == None:
                password = ""
            if delay == None:
                delay = 0
            if name != None:
                self.log.info(
                    "Configuration : name=%s, ip=%s, port=%s, user=%s, password=No_Log, delay=%s"
                    % (name, ip, port, user, delay))
                self.cameras[name] = {
                    "ip": ip,
                    "port": port,
                    "user": user,
                    "password": password,
                    "delay": delay
                }
            else:
                loop = False
            num += 1

        ### Create FOSCAM object
        self._foscammanager = RELAY(self.log)
        # Create listeners
        Listener(self.foscam_command, self.myxpl, {
            'schema': 'control.basic',
            'xpltype': 'xpl-cmnd',
            'type': 'output'
        })
        self.log.info("Listener for Foscam relay created")
        #print ("Listener for Foscam relay created")
        self.enable_hbeat()

    def foscam_command(self, message):
        """ Call Foscamn lib
            @param message : xPL message detected by listener
        """
        if 'device' in message.data:
            device = message.data['device']
        if 'current' in message.data:
            msg_current = message.data['current'].upper()

        if 'type' in message.data:
            msg_type = message.data['type'].lower()
        try:
            ip = self.cameras[device]["ip"]
            port = int(self.cameras[device]["port"])
            user = self.cameras[device]["user"]
            password = self.cameras[device]["password"]
            delay = int(self.cameras[device]["delay"])
        except KeyError:
            self.log.warning("Camera named '%s' is not defined" % device)
            return
        if msg_type == 'output' and msg_current.lower() in ['high']:
            print("high recu")
            self.log.debug("high command receive on relay '%s'" % device)
            # Our listener catch a Message with low output command
            status = self._foscammanager.close_relay(ip, port, user, password,
                                                     device)
            # Send xpl-trig to say plugin whell receive high command
            if status == True:
                #print ("high ACKed")
                self.log.debug("high command Ack on relay '%s'" % device)
                mess = XplMessage()
                mess.set_type('xpl-trig')
                mess.set_schema('sensor.basic')
                mess.add_data({'device': device})
                mess.add_data({'type': 'output'})
                mess.add_data({'current': 'high'})
                self.myxpl.send(mess)
        if msg_type == 'output' and msg_current.lower() in ['low']:
            print("low recu")
            self.log.debug("low command receive on relay '%s'" % device)
            # Our listener catch a Message with low output command
            status = self._foscammanager.open_relay(ip, port, user, password,
                                                    device)
            # Send xpl-trig to say plugin whell receive low command
            if status == True:
                #print ("low ACKed")
                self.log.debug("low command Ack on relay '%s'" % device)
                mess = XplMessage()
                mess.set_type('xpl-trig')
                mess.set_schema('sensor.basic')
                mess.add_data({'device': device})
                mess.add_data({'type': 'output'})
                mess.add_data({'current': 'low'})
                self.myxpl.send(mess)

        if msg_type == 'output' and msg_current.lower() in ['pulse']:
            print("pulse recu")
            self.log.debug("pulse command receive on relay '%s'" % device)
            # Our listener catch a Message with output pulse output command
            status = self._foscammanager.pulse_relay(ip, port, user, password,
                                                     delay, device)
            # Send xpl-trig to say plugin whell receive pulse command
            if status == True:
                print("pulse ACKed")
                self.log.debug("pulse command Ack on relay '%s'" % device)
                mess = XplMessage()
                mess.set_type('xpl-trig')
                mess.set_schema('sensor.basic')
                mess.add_data({'device': device})
                mess.add_data({'type': 'output'})
                mess.add_data({'current': 'pulse'})
                self.myxpl.send(mess)