示例#1
0
    def __init__(self, params):
        self._logger = logging.getLogger('HermesLedControl')
        self._logger.info('Initializing HermesLedControl')

        self._mqttClient = None
        self._hardwareReference = None
        self._ledsController = None
        self._params = params

        self._mqttServer = 'localhost'
        self._me = 'default'
        self._mqttPort = 1883
        self._mqttUsername = ''
        self._mqttPassword = ''
        self._tlsFile = ''

        self._hotwordRegex = re.compile(
            self._SUB_ON_HOTWORD.replace('+', '(.*)'))

        if params.engine == 'projectalice':
            from models.engines.ProjectAlice import ProjectAlice
            engine = ProjectAlice()
        elif params.engine == 'rhasspy':
            from models.engines.Rhasspy import Rhasspy
            engine = Rhasspy()
        elif params.engine == 'snips':
            from models.engines.Snips import Snips
            engine = Snips()
        else:
            self._logger.error('Unsupported assistant engine "{}"'.format(
                params.engine))
            self.onStop()
            return

        self._configs = engine.loadConfig(params)
        if not self._configs:
            self.onStop()

        with open('hardware.json') as f:
            self._hardwareReference = json.load(f)
            self._logger.info('Loaded {} hardware references'.format(
                len(self._hardwareReference)))

        if params.hardware not in self._hardwareReference:
            self._logger.fatal('Trying to use an unsupported hardware')
            self.onStop()
        else:
            self._hardware = self._hardwareReference[self._params.hardware]

        self._mqttServer = params.mqttServer or self._configs['mqttServer']
        self._mqttPort = int(params.mqttPort or self._configs['mqttPort'])
        self._mqttUsername = params.mqttUsername or self._configs[
            'mqttUsername']
        self._mqttPassword = params.mqttPassword or self._configs[
            'mqttPassword']
        self._tlsFile = self._configs['mqttTLSCAFile']
        self._me = params.clientId or self._configs['deviceName']

        self._SUB_ON_PLAY_FINISHED = self._SUB_ON_PLAY_FINISHED.format(
            self._me)

        self._logger.info('- Mqtt server set to {}'.format(self._mqttServer))
        self._logger.info('- Mqtt port set to {}'.format(self._mqttPort))

        if self._mqttUsername:
            self._logger.info('- Mqtt username set to {}'.format(
                self._mqttUsername))
        if self._mqttPassword:
            self._logger.info('- Mqtt password set to "hidden"')

        self._logger.info('- Client id set to {}'.format(self._me))
        self._logger.info('- Hardware set to {}'.format(
            self._hardware['name']))

        if params.leds is not None:
            self._hardware['numberOfLeds'] = params.leds
        self._logger.info('- Using {} as pattern with {} leds'.format(
            params.pattern, self._hardware['numberOfLeds']))

        if 'gpioPin' in self._hardware:
            if params.gpioPin is not None:
                self._hardware['gpioPin'] = params.gpioPin
            self._logger.info('Using pin #{}'.format(
                self._hardware['gpioPin']))

        if 'vid' in self._hardware and params.vid is not None:
            self._hardware['vid'] = params.vid

        if 'gpios' in self._hardware and len(params.pureGpioPinout) > 0:
            self.hardware['gpios'] = params.pureGpioPinout

        if 'activeHigh' in self._hardware:
            self._hardware['activeHigh'] = params.activeHigh

        if 'endFrame' in self._hardware and params.endFrame is not None:
            self._hardware['endFrame'] = params.endFrame

        self._ledsController = LedsController(self)
        self._mqttClient = self.connectMqtt()
    def __init__(self, params):
        self._logger = logging.getLogger('SnipsLedControl')
        self._logger.info('Initializing SnipsLedControl')

        self._mqttClient = None
        self._hardwareReference = None
        self._ledsController = None
        self._params = params

        self._snipsConfigs = self.loadConfigs()

        self._mqttServer = 'localhost'
        self._me = 'default'
        self._mqttPort = 1883
        self._mqttUsername = ''
        self._mqttPassword = ''
        self._tlsFile = ''

        self._hotwordRegex = re.compile(
            self._SUB_ON_HOTWORD.replace('+', '(.*)'))

        with open('hardware.json') as f:
            self._hardwareReference = json.load(f)
            self._logger.info('Loaded {} hardware references'.format(
                len(self._hardwareReference)))

        if params.hardware not in self._hardwareReference:
            self._logger.fatal('Trying to use an unsupported hardware')
            self.onStop()
        else:
            self._hardware = self._hardwareReference[self._params.hardware]

        if params.mqttServer is None:
            try:
                if 'snips-common' in self._snipsConfigs and 'mqtt' in self._snipsConfigs[
                        'snips-common']:
                    self._mqttServer = self._snipsConfigs['snips-common'][
                        'mqtt'].replace(':1883', '')

                    if 'mqtt_username' in self._snipsConfigs[
                            'snips-common'] and params.mqttUsername is None:
                        self._mqttUsername = self._snipsConfigs[
                            'snips-common']['mqtt_username']

                    if 'mqtt_password' in self._snipsConfigs[
                            'snips-common'] and params.mqttPassword is None:
                        self._mqttPassword = self._snipsConfigs[
                            'snips-common']['mqtt_password']

                    if 'mqtt_tls_cafile' in self._snipsConfigs['snips-common']:
                        self._tlsFile = self._snipsConfigs['snips-common'][
                            'mqtt_tls_cafile']

            except:
                self._logger.info(
                    '- Falling back to default config for mqtt server')
        else:
            self._mqttServer = params.mqttServer

        if params.clientId is None:
            try:
                if 'snips-audio-server' in self._snipsConfigs and 'bind' in self._snipsConfigs[
                        'snips-audio-server']:
                    self._me = self._snipsConfigs['snips-audio-server'][
                        'bind'].replace('@mqtt', '')
            except:
                self._logger.info(
                    '- Falling back to default config for client id')
        else:
            self._me = params.clientId

        self._SUB_ON_PLAY_FINISHED = self._SUB_ON_PLAY_FINISHED.format(
            self._me)

        if params.mqttPort is None:
            try:
                if 'snips-common' in self._snipsConfigs and 'mqtt' in self._snipsConfigs[
                        'snips-common']:
                    self._mqttPort = self._snipsConfigs['snips-common'][
                        'mqtt'].split(':')[1]
            except:
                self._logger.info(
                    '- Falling back to default config for mqtt port')
        else:
            self._mqttPort = params.mqttPort

        self._logger.info('- Mqtt server set to {}'.format(self._mqttServer))
        self._logger.info('- Mqtt port set to {}'.format(self._mqttPort))

        if self._mqttUsername:
            self._logger.info('- Mqtt username set to {}'.format(
                self._mqttUsername))
        if self._mqttPassword:
            self._logger.info('- Mqtt password set to "hidden"')

        self._logger.info('- Client id set to {}'.format(self._me))
        self._logger.info('- Hardware set to {}'.format(
            self._hardware['name']))

        string = '- Using {} as pattern with {} leds'
        if params.leds is not None:
            self._logger.info(string.format(params.pattern, params.leds))
            self._hardware['numberOfLeds'] = params.leds
        else:
            self._logger.info(
                string.format(params.pattern, self._hardware['numberOfLeds']))

        if 'gpioPin' in self._hardware:
            string = 'Using pin #{}'
            if params.gpioPin is not None:
                self._logger.info(string.format(params.gpioPin))
                self._hardware['gpioPin'] = params.gpioPin
            else:
                self._logger.info(string.format(self._hardware['gpioPin']))

        if 'vid' in self._hardware and params.vid is not None:
            self._hardware['vid'] = params.vid

        if 'gpios' in self._hardware and len(params.pureGpioPinout) > 0:
            self.hardware['gpios'] = params.pureGpioPinout

        if 'activeHigh' in self._hardware and params.activeHigh != self._hardware[
                'activeHigh']:
            self._hardware['activeHigh'] = params.activeHigh

        if 'endFrame' in self._hardware and params.endFrame is not None:
            self._hardware['endFrame'] = params.endFrame

        self._ledsController = LedsController(self)
        self._mqttClient = self.connectMqtt()