Repository URL to install this package:
|
Version:
0.1.1 ▾
|
#!python
#
# SUSI.AI tray icon
# on start:
# - start susi server
# depending on config (to be done, where?)
# - start susi etherpad
# - start susi voice
#
# menu items:
# - toggle susi voice recognition
# - toggle susi etherpad
# - configure
# - about
# - exit
#
# TODO:
# - start susi linux, but enable a pause/resume mode for faster startup?
# maybe not necessary
# - configure: after changes, make sure that a running susi linux is restarted
# - app window: allow opening an app window, but reuse the susi linux when it is running
# currently a new susi linux process is restarted
#
# OVERALL
# separate out susi_loop and related stuff into susi-voice repository
# keep susi-linux as susi-linux-frontend
import pystray
import sys
import os
import logging
import argparse
import colorlog
from pystray import Menu as menu, MenuItem as item
from PIL import Image
from susi.config import SusiConfig
from susi.ui import ConfigurationWindow
from susi.voice import SusiLoop, startup_sound
parser = argparse.ArgumentParser(prog='susi-assistant',
description='SUSI.AI Assistant')
cfg = SusiConfig()
susi_voice_enabled = False
susi_pad_enabled = False
susi_loop = SusiLoop()
def get_colorlog_handler(short=False):
# Short log format is for use under systemd.
# Here we exclude some info, because they will be added by journalctl.
if short:
log_format = '%(log_color)s%(levelname)s:%(reset)s %(message)s'
else:
log_format = '%(log_color)s%(asctime)s %(levelname)s:%(name)s:%(reset)s %(message)s'
handler = colorlog.StreamHandler()
handler.setFormatter(
colorlog.TTYColoredFormatter(
log_format,
stream=sys.stderr,
datefmt='%Y-%m-%d %H:%M:%S'))
return handler
def on_susi_voice_clicked(icon, item):
global susi_voice_enabled
susi_voice_enabled = not item.checked
if susi_voice_enabled:
if susi_loop.is_queue_running():
pass
else:
susi_loop.start_queue(background=True)
if susi_loop.is_hotword_running():
pass
else:
startup_sound()
susi_loop.start_hotword()
else:
if susi_loop.is_hotword_running():
susi_loop.stop_hotword()
else:
pass
if susi_loop.is_queue_running():
susi_loop.stop_queue()
else:
pass
def on_susi_pad_clicked(icon, item):
global susi_pad_enabled
susi_pad_enabled = not item.checked
if susi_pad_enabled:
os.system("susi-etherpad start")
else:
os.system("susi-etherpad stop")
def on_configure_clicked(icon, item):
window = ConfigurationWindow()
window.show_window()
def on_about_clicked(icon, item):
print("TODO need to display about dialog")
def on_exit_clicked(icon, item):
os.system("susi-etherpad stop")
os.system("susi-server stop")
sys.exit(0)
def create_image():
logo = os.path.join(cfg.get('path.base'), 'data/img/susi-ai.png')
image = Image.open(logo)
return image
parser.add_argument('-v', '--verbose', action='count', default=0,
help='Show log. Repeat to get more detailed one.')
'''
Sometimes, when we enable -v in systemd service command, and read the log via
journalctl, we will see duplication of timestamp and process. These info are
provided by both journalctl and our app. Enable --short-log to stop our app
from including those info in log.
'''
parser.add_argument('--short-log', action='store_true',
help='Produce log w/o timestamp and process name.')
args = parser.parse_args()
# Configure logger
if args.verbose:
levels = (logging.WARNING, logging.INFO, logging.DEBUG)
handler = get_colorlog_handler(args.short_log)
lindex = min(args.verbose, len(levels) - 1)
level = levels[lindex]
# logging.root.propagate = True
logging.root.setLevel(level)
logging.root.handlers = []
logging.root.addHandler(handler)
icon = pystray.Icon('SUSI.AI', create_image(), menu=menu(
item('SUSI.AI', action = lambda item: item),
item(
'Voice Assistant',
on_susi_voice_clicked,
checked = lambda item: susi_voice_enabled),
item(
'Skill Pad',
on_susi_pad_clicked,
checked = lambda item: susi_pad_enabled),
item(
'Configure',
on_configure_clicked),
item(
'About',
on_about_clicked),
item(
'Exit',
on_exit_clicked),
))
os.system("susi-server start")
icon.run()