コード例 #1
0
ファイル: blead.py プロジェクト: yoan-m/plugin-blea
def action_handler(message):
    manuf = ''
    if manuf in message['command']:
        manuf = message['command']['manuf']
    name = message['command']['name']
    if 'localname' in globals.KNOWN_DEVICES[message['device']['id']]:
        message['device']['localname'] = globals.KNOWN_DEVICES[
            message['device']['id']]['localname']
    result = {}
    if message['cmd'] == 'helper' or message['cmd'] == 'helperrandom':
        type = 'public'
        if message['cmd'] == 'helperrandom':
            type = 'random'
        try:
            globals.PENDING_ACTION = True
            mac = message['device']['id']
            if mac in globals.KEEPED_CONNECTION:
                logging.debug('ACTION------Already a connection for ' + mac +
                              ' use it')
                conn = globals.KEEPED_CONNECTION[mac]
            else:
                logging.debug('ACTION------Creating a new connection for ' +
                              mac)
                conn = Connector(mac)
                globals.KEEPED_CONNECTION[mac] = conn
                conn.connect(type=type)
            if not conn.isconnected:
                conn.connect(type=type)
                if not conn.isconnected:
                    globals.PENDING_ACTION = False
                    return
            try:
                conn.helper()
            except Exception, e:
                logging.debug("ACTION------Helper failed : %s" % str(e))
                globals.PENDING_ACTION = False
            conn.disconnect()
            globals.PENDING_ACTION = False
            return
        except Exception, e:
            logging.debug("ACTION------Helper failed : %s" % str(e))
            globals.PENDING_ACTION = False
コード例 #2
0
ファイル: blead.py プロジェクト: seb821/plugin-blea
def action_handler(message):
    manuf = ''
    if manuf in message['command']:
        manuf = message['command']['manuf']
    name = message['command']['name']
    if 'localname' in globals.KNOWN_DEVICES[message['device']['id']]:
        message['device']['localname'] = globals.KNOWN_DEVICES[
            message['device']['id']]['localname']
    result = {}
    if message['cmd'] == 'helper' or message['cmd'] == 'helperrandom':
        type = 'public'
        if message['cmd'] == 'helperrandom':
            type = 'random'
        try:
            mac = message['device']['id']
            if mac in globals.KEEPED_CONNECTION:
                logging.debug('ACTION------Already a connection for ' + mac +
                              ' use it')
                conn = globals.KEEPED_CONNECTION[mac]
            else:
                logging.debug('ACTION------Creating a new connection for ' +
                              mac)
                conn = Connector(mac)
                globals.KEEPED_CONNECTION[mac] = conn
                conn.connect(type=type)
            if not conn.isconnected:
                conn.connect(type=type)
                if not conn.isconnected:
                    return
            try:
                conn.helper()
            except Exception as e:
                logging.debug("ACTION------Helper failed : %s" % str(e))
            conn.disconnect()
            return
        except Exception as e:
            logging.debug("ACTION------Helper failed : %s" % str(e))
    elif message['cmd'] == 'refresh':
        for compatible in globals.COMPATIBILITY:
            classname = message['command']['device']['name']
            if compatible().name.lower() == classname.lower():
                logging.debug('ACTION------Attempt to refresh values')
                try:
                    result = compatible().read(message['device']['id'])
                except Exception as e:
                    logging.debug("ACTION------Refresh failed : %s" % str(e))
                break
        if result and len(result) >= 2:
            if message['device'][
                    'id'] in globals.LAST_STATE and result == globals.LAST_STATE[
                        message['device']['id']]:
                return
            else:
                globals.LAST_STATE[message['device']['id']] = result
                result['source'] = globals.daemonname
                globals.JEEDOM_COM.add_changes(
                    'devices::' + message['device']['id'], result)
                return
    else:
        for device in globals.COMPATIBILITY:
            if device().isvalid(name, manuf):
                try:
                    result = device().action(message)
                except Exception as e:
                    logging.debug("ACTION------Action failed :" + str(e))
                if result:
                    if message['device'][
                            'id'] in globals.LAST_STATE and result == globals.LAST_STATE[
                                message['device']['id']]:
                        return
                    else:
                        globals.LAST_STATE[message['device']['id']] = result
                        result['source'] = globals.daemonname
                        globals.JEEDOM_COM.add_changes(
                            'devices::' + message['device']['id'], result)
                        return
                return
    return