def __init__(self, lower_threshold, upper_threshold, interval, strings): self.__notification_types = ("dialog-information", "dialog-warn", "dialog-error") self.__style = self.__notification_types[0] self.__body = Notify.get_app_name() self.__summary = Notify.get_app_name() self.__notification = Notify.Notification.new(self.__body, self.__summary, self.__style) self.__lower_threshold = lower_threshold self.__upper_threshold = upper_threshold self.__interval = interval self.__strings = strings self.__battery = None self.__notified = True self.__scheduler = scheduler(time.time, time.sleep) self.find_battey()
def process_event(event): if event.type == EventType.ON_CONVERSATION_TURN_STARTED: print() print(event) if (event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args and event.args['text']): note = Notify.Notification.new(Notify.get_app_name(),'聞き取りました:{}'.format(event.args['text']),'mic-volume-high') note.set_urgency(Notify.Urgency.NORMAL) note.show() subprocess.Popen(['python3','{}/textinput.py'.format(os.path.dirname(os.path.abspath(__file__))),'--device-id',device_id_global,'--device-model-id',device_model_id_global,'--query',event.args['text']]) if (event.type == EventType.ON_CONVERSATION_TURN_STARTED): note = Notify.Notification.new(Notify.get_app_name(),'聞き取り中です…','mic-volume-high') note.set_urgency(Notify.Urgency.NORMAL) note.show() if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and event.args and not event.args['with_follow_on_turn']): print() if event.type == EventType.ON_DEVICE_ACTION: for command, params in event.actions: print('Do command', command, 'with params', str(params))
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('--device-model-id', '--device_model_id', type=str, metavar='DEVICE_MODEL_ID', required=False, help='the device model ID registered with Google') parser.add_argument('--project-id', '--project_id', type=str, metavar='PROJECT_ID', required=False, help='the project ID used to register this device') parser.add_argument('--nickname', type=str, metavar='NICKNAME', required=False, help='the nickname used to register this device') parser.add_argument('--device-config', type=str, metavar='DEVICE_CONFIG_FILE', default=os.path.join( os.path.expanduser('~/.config'), 'googlesamples-assistant', 'device_config_library.json' ), help='path to store and read device configuration') parser.add_argument('--credentials', type=existing_file, metavar='OAUTH2_CREDENTIALS_FILE', default=os.path.join( os.path.expanduser('~/.config'), 'google-oauthlib-tool', 'credentials.json' ), help='path to store and read OAuth2 credentials') parser.add_argument('--query', type=str, metavar='QUERY', help='query to send as soon as the Assistant starts') parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + Assistant.__version_str__()) args = parser.parse_args() with open(args.credentials, 'r') as f: credentials = google.oauth2.credentials.Credentials(token=None, **json.load(f)) device_model_id = None last_device_id = None try: with open(args.device_config) as f: device_config = json.load(f) device_model_id = device_config['model_id'] last_device_id = device_config.get('last_device_id', None) except FileNotFoundError: pass if not args.device_model_id and not device_model_id: raise Exception('Missing --device-model-id option') should_register = ( args.device_model_id and args.device_model_id != device_model_id) device_model_id = args.device_model_id or device_model_id note = Notify.Notification.new(Notify.get_app_name(),'Micno with Assistant は動作中です','mic-volume-high') note.set_urgency(Notify.Urgency.NORMAL) note.show() with Assistant(credentials, device_model_id) as assistant: global device_id_global, device_model_id_global events = assistant.start() device_id = assistant.device_id print('device_model_id:', device_model_id) print('device_id:', device_id + '\n') device_id_global = device_id device_model_id_global = device_model_id if should_register or (device_id != last_device_id): if args.project_id: register_device(args.project_id, credentials, device_model_id, device_id, args.nickname) pathlib.Path(os.path.dirname(args.device_config)).mkdir( exist_ok=True) with open(args.device_config, 'w') as f: json.dump({ 'last_device_id': device_id, 'model_id': device_model_id, }, f) else: print(WARNING_NOT_REGISTERED) for event in events: if event.type == EventType.ON_START_FINISHED and args.query: assistant.send_text_query(args.query) process_event(event)