hugvey/hugvey/panopticon.py

378 lines
15 KiB
Python
Raw Normal View History

"""
The panopticon provides a way to observe (& control) all running Hugveys trough a web interface
"""
import logging
import tornado
2019-01-23 22:38:27 +01:00
import string
import random
2019-01-18 19:39:35 +01:00
import tornado.websocket
import tornado.web
import tornado.ioloop
import os
from pytz.reference import Central
import asyncio
import json
2019-01-23 15:26:44 +01:00
from urllib.parse import urlparse
2019-01-23 22:38:27 +01:00
from hugvey import central_command
2019-02-18 20:38:54 +01:00
from hugvey.voice import VoiceStorage
from multiprocessing import Queue
import threading
mainLogger = logging.getLogger("hugvey")
logger = mainLogger.getChild("panopticon")
web_dir = os.path.join(os.path.split(__file__)[0], '..', 'www')
2019-01-18 19:39:35 +01:00
def getWebSocketHandler(central_command):
class WebSocketHandler(tornado.websocket.WebSocketHandler):
2020-02-15 14:14:25 +01:00
CORS_ORIGINS = ['localhost', 'hugveycmd.local', 'r3.local','192.168.1.155']
connections = set()
2019-05-17 16:39:53 +02:00
selections = {}
2019-01-23 15:26:44 +01:00
def check_origin(self, origin):
parsed_origin = urlparse(origin)
# parsed_origin.netloc.lower() gives localhost:3333
valid = parsed_origin.hostname in self.CORS_ORIGINS
return valid
2019-01-18 19:39:35 +01:00
# the client connected
def open(self, p = None):
WebSocketHandler.connections.add(self)
logger.info("New client connected")
self.write_message("hello!")
2019-01-18 19:39:35 +01:00
# the client sent the message
def on_message(self, message):
2019-01-23 15:26:44 +01:00
logger.debug(f"recieve: {message}")
try:
msg = json.loads(message)
if msg['action'] == 'init':
self.msgInit()
2019-05-17 16:39:53 +02:00
elif msg['action'] == 'selection':
self.selectHugvey(msg['selected_id'])
# elif msg['action'] == 'get_status':
# self.msgStatus(msg['selected_id'])
elif msg['action'] == 'block':
self.msgBlock(msg['hugvey'])
elif msg['action'] == 'unblock':
self.msgUnblock(msg['hugvey'])
2019-01-25 11:59:03 +01:00
elif msg['action'] == 'resume':
self.msgResume(msg['hugvey'])
2019-01-25 11:59:03 +01:00
elif msg['action'] == 'pause':
self.msgPause(msg['hugvey'])
2019-01-25 11:59:03 +01:00
elif msg['action'] == 'restart':
self.msgRestart(msg['hugvey'])
elif msg['action'] == 'finish':
self.msgFinish(msg['hugvey'])
2019-01-25 11:59:03 +01:00
elif msg['action'] == 'change_language':
self.msgChangeLanguage(msg['hugvey'], msg['lang_code'])
2019-11-11 12:45:08 +01:00
elif msg['action'] == 'change_language_for_available':
self.msgChangeLanguageForAllAvailableHugveys(msg['lang_code'])
elif msg['action'] == 'change_light':
self.msgChangeLightId(msg['hugvey'], int(msg['light_id']))
elif msg['action'] == 'change_light_status':
self.msgChangeLightStatus(msg['hugvey'], bool(msg['light_status']))
2019-01-25 15:45:46 +01:00
elif msg['action'] == 'play_msg':
self.msgPlayMsg(msg['hugvey'], msg['msg_id'], msg['reloadStory'])
elif msg['action'] == 'loop_time':
self.msgSetLoopTime(msg['time'])
2019-01-25 11:59:03 +01:00
else:
# self.send({'alert': 'Unknown request: {}'.format(message)})
2019-01-25 11:59:03 +01:00
logger.warn('Unknown request: {}'.format(message))
except Exception as e:
# self.send({'alert': 'Invalid request: {}'.format(e)})
logger.exception(e)
def send(self, message):
# Possible useless method: use self.write_message()
j = json.dumps(message)
self.write_message(j)
# client disconnected
def on_close(self):
self.__class__.rmConnection(self)
logger.info("Client disconnected")
@classmethod
2019-05-17 16:39:53 +02:00
def getStatusMsg(cls):
msg = central_command.getStatusSummary(cls.selections.values())
msg['action'] = 'status'
return msg
@classmethod
2019-05-17 16:39:53 +02:00
def broadcastStatus(cls):
cls.write_to_clients(cls.getStatusMsg())
def msgInit(self):
msg = self.getStatusMsg()
self.send(msg)
2019-05-17 16:39:53 +02:00
def selectHugvey(self, hv_id):
if hv_id is None:
self.__class__.selections.pop(self, None)
else:
self.__class__.selections[self] = int(hv_id)
def msgBlock(self, hv_id):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'block'})
def msgUnblock(self, hv_id):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'unblock'})
def msgResume(self, hv_id):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'resume'})
def msgPause(self, hv_id):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'pause'})
def msgRestart(self, hv_id):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'restart'})
def msgFinish(self, hv_id):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'finish'})
2019-01-25 11:59:03 +01:00
def msgChangeLanguage(self, hv_id, lang_code):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'change_language', 'lang_code': lang_code})
2019-11-11 12:45:08 +01:00
def msgChangeLanguageForAllAvailableHugveys(self, lang_code):
"""
Set language for all future hugvey runs (so after a 'finish')
"""
central_command.setFutureLanguage(lang_code)
2019-11-11 12:45:08 +01:00
def msgSetLoopTime(self, loop_time):
parts = loop_time.split(":")
if len(parts) == 2:
h, m = parts
s = 0
elif len(parts) == 3:
h, m, s = parts
else:
logger.critical(f"Invalid time to set: {loop_time}")
return
seconds = int(h)*3600+int(m)*60+int(s)
central_command.setLoopTime(seconds)
def msgChangeLightId(self, hv_id, lightId):
central_command.setLightForHugvey(hv_id, lightId)
# if central_command.hugveys[hv_id].eventQueue:
# central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'change_light', 'light_id': lightId})
def msgChangeLightStatus(self, hv_id, status):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'change_light_status', 'status': status})
def msgPlayMsg(self, hv_id, msg_id, reloadStory):
if central_command.hugveys[hv_id].eventQueue:
central_command.hugveys[hv_id].eventQueue.put_nowait({'event': 'play_msg', 'msg_id': msg_id, 'reloadStory':bool(reloadStory)})
2019-05-17 16:39:53 +02:00
@classmethod
def rmConnection(cls, client):
if client not in cls.connections:
return
if client in cls.selections:
cls.selections.pop(client, None)
cls.connections.remove(client)
@classmethod
def write_to_clients(wsHandlerClass, msg):
if msg is None:
logger.critical("Tried to send 'none' to Panopticon")
return
toRemove = []
for client in wsHandlerClass.connections:
try:
client.write_message(msg)
except tornado.websocket.WebSocketClosedError as e:
logger.warning(f"Not properly closed websocket connection")
toRemove.append(client) # If we remove it here from the set we get an exception about changing set size during iteration
for client in toRemove:
wsHandlerClass.rmConnection(client)
return WebSocketHandler
2019-01-18 19:39:35 +01:00
2019-01-24 14:27:04 +01:00
class NonCachingStaticFileHandler(tornado.web.StaticFileHandler):
def set_extra_headers(self, path):
# Disable cache
self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
2019-01-23 22:38:27 +01:00
def getUploadHandler(central_command):
class UploadHandler(tornado.web.RequestHandler):
2019-01-24 15:01:01 +01:00
def set_default_headers(self):
# headers for CORS
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
def options(self):
# OPTIONS request for CORS
self.set_status(204)
self.finish()
2019-01-23 22:38:27 +01:00
def post(self):
2019-05-06 08:13:13 +02:00
logger.info('upload')
2019-01-23 22:38:27 +01:00
langCode = self.get_argument("language")
langFile = os.path.join(central_command.config['web']['files_dir'] , central_command.languageFiles[langCode])
2019-01-23 22:38:27 +01:00
storyData = json.loads(self.request.files['json'][0]['body'])
2019-01-24 14:27:04 +01:00
# print(json.dumps(storyData))
# self.finish()
# return
2019-01-23 22:38:27 +01:00
if 'audio' in self.request.files:
msgId = self.get_argument("message_id")
audioFile = self.request.files['audio'][0]
original_fname = audioFile['filename']
fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(10))
ext = os.path.splitext(original_fname)[1]
2019-01-24 15:01:01 +01:00
audioFilename = os.path.join(central_command.config['web']['files_dir'], langCode, fname + ext)
2019-01-23 22:38:27 +01:00
for i, data in enumerate(storyData):
if data['@id'] != msgId:
continue
2019-05-06 08:13:13 +02:00
if 'audio' in storyData[i] and storyData[i]['audio'] is not None and os.path.exists(storyData[i]['audio']['file']):
2019-01-24 15:01:01 +01:00
logger.info(f"Remove previous file {storyData[i]['audio']['file']} ({storyData[i]['audio']['original_name']})")
os.unlink(storyData[i]['audio']['file'])
2019-01-23 22:38:27 +01:00
storyData[i]['audio'] = {
'file': audioFilename,
'original_name': original_fname
}
2019-01-24 15:01:01 +01:00
with open(audioFilename, 'wb') as fp:
2019-01-23 22:38:27 +01:00
logger.info(f'Save {original_fname} to {audioFilename}')
2019-01-24 15:01:01 +01:00
fp.write(audioFile['body'])
2019-01-23 22:38:27 +01:00
break
2019-05-06 08:13:13 +02:00
# logger.info(os.path.abspath(langFile))
langFile = os.path.abspath(langFile)
2019-01-24 14:27:04 +01:00
with open(langFile, 'w') as json_fp:
logger.info(f'Save story to {langFile} {json_fp}')
json.dump(storyData, json_fp, indent=2)
# Reload language files for new instances
central_command.loadLanguages()
2019-01-23 22:38:27 +01:00
self.finish()
2019-01-23 22:38:27 +01:00
return UploadHandler
2019-01-18 19:39:35 +01:00
2019-02-18 20:38:54 +01:00
def getVoiceHandler(voiceStorage):
class VoiceHandler(tornado.web.RequestHandler):
async def get(self):
# TODO: we should be using ZMQ here...
text = self.get_argument('text')
2019-04-09 09:40:50 +02:00
lang_code = self.get_argument('lang')
2019-02-18 20:38:54 +01:00
isVariable = True if int(self.get_argument('variable')) >0 else False
# TODO: make zmq socket request/reply pattern:
2019-04-09 09:40:50 +02:00
fn = await voiceStorage.requestFile(lang_code, text, isVariable)
2019-02-18 20:38:54 +01:00
if not fn:
raise Exception(f"No Filename for text: {text}")
2019-02-18 20:38:54 +01:00
if int(self.get_argument('filename')) == 1:
self.set_header("Content-Type","text/plain")
self.write(fn)
else:
self.set_header("Content-Type","audio/wave")
with open(fn, 'rb') as fp:
self.write(fp.read())
self.finish()
return VoiceHandler
class InfoHandler(tornado.web.RequestHandler):
def initialize(self, command):
self.command = command
async def get(self):
self.write(json.dumps(self.command.hugvey_ids))
class StaticFileWithHeaderHandler(tornado.web.StaticFileHandler):
def set_extra_headers(self, path):
"""For subclass to add extra headers to the response"""
if path[-5:] == '.html':
self.set_header("Access-Control-Allow-Origin", "*")
2019-02-18 20:38:54 +01:00
class Panopticon(object):
def __init__(self, central_command, config, voiceStorage):
2019-01-18 19:39:35 +01:00
self.command = central_command
self.config = config
self.voiceStorage = voiceStorage
self.wsHandler = getWebSocketHandler(self.command)
2019-01-18 19:39:35 +01:00
self.application = tornado.web.Application([
(r"/ws(.*)", self.wsHandler),
2019-01-24 14:27:04 +01:00
(r"/local/(.*)", NonCachingStaticFileHandler,
2019-01-23 22:38:27 +01:00
{"path": config['web']['files_dir']}),
(r"/upload", getUploadHandler(self.command)),
2019-02-18 20:38:54 +01:00
(r"/voice", getVoiceHandler(self.voiceStorage)),
(r"/count", InfoHandler, {'command': self.command}),
(r"/(.*)", StaticFileWithHeaderHandler,
2019-01-23 22:38:27 +01:00
{"path": web_dir, "default_filename": 'index.html'}),
], debug=True)
2019-01-18 19:39:35 +01:00
# self.loop.configure(evt_loop)
def start(self):
2019-01-18 19:39:35 +01:00
evt_loop = asyncio.new_event_loop()
asyncio.set_event_loop(evt_loop)
2019-01-18 19:39:35 +01:00
self.loop = tornado.ioloop.IOLoop.current()
self.application.listen(self.config['web']['port'])
thread = threading.Thread(
target=self.broadcastLoggingQueueToWs, kwargs={'wsHandler': self.wsHandler, 'q': self.command.logQueue}, name=f"panopticon/logws")
thread.start()
task = evt_loop.create_task(
self.statusSender(self.wsHandler))
2019-01-23 15:26:44 +01:00
logger.info(f"Start Panopticon on http://localhost:{self.config['web']['port']}")
2019-01-18 19:39:35 +01:00
self.loop.start()
2019-01-18 19:39:35 +01:00
def stop(self):
self.loop.stop()
async def statusSender(self, wsHandler):
while True:
try:
2019-05-17 16:39:53 +02:00
self.wsHandler.broadcastStatus()
await asyncio.sleep(3)
except Exception as e:
logger.exception(e)
def broadcastLoggingQueueToWs(self, wsHandler, q: Queue):
while True:
record = q.get()
# record: logging.LogRecord
assert isinstance(record, logging.LogRecord)
hugvey_id = record.name.split('.')[1]
items = record.msg.split(':', 2)
2019-04-27 16:39:20 +02:00
msg = {'action': 'log', 'id':hugvey_id, 'type': items[0], 'lvl':record.levelno, 'lvlname':record.levelname}
if len(items) > 1:
msg['info'] = items[1]
if len(items) > 2:
msg['args'] = items[2]
j = json.dumps(msg)
logger.debug(j)
self.loop.add_callback(wsHandler.write_to_clients, j)