openlp/openlp/core/api/websockets.py

144 lines
5.5 KiB
Python
Raw Normal View History

2016-06-05 05:49:27 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2017-03-04 19:17:59 +00:00
# Copyright (c) 2008-2017 OpenLP Developers #
2016-06-05 05:49:27 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:`http` module contains the API web server. This is a lightweight web server used by remotes to interact
with OpenLP. It uses JSON to communicate with the remotes.
"""
import asyncio
2016-09-30 19:50:48 +00:00
import json
2016-06-05 05:49:27 +00:00
import logging
import time
2017-12-28 08:27:44 +00:00
import websockets
2016-06-05 05:49:27 +00:00
from PyQt5 import QtCore
2017-10-23 22:09:57 +00:00
from openlp.core.common.mixins import LogMixin, RegistryProperties
from openlp.core.common.registry import Registry
2017-10-07 07:05:07 +00:00
from openlp.core.common.settings import Settings
2016-06-05 05:49:27 +00:00
log = logging.getLogger(__name__)
2016-06-14 20:56:50 +00:00
class WebSocketWorker(QtCore.QObject):
2016-06-05 05:49:27 +00:00
"""
2016-06-13 19:51:46 +00:00
A special Qt thread class to allow the WebSockets server to run at the same time as the UI.
2016-06-05 05:49:27 +00:00
"""
def __init__(self, server):
"""
Constructor for the thread class.
:param server: The http server class.
"""
2016-06-06 19:56:38 +00:00
self.ws_server = server
2016-06-16 20:50:01 +00:00
super(WebSocketWorker, self).__init__()
2016-06-05 05:49:27 +00:00
2016-06-16 20:50:01 +00:00
def run(self):
2016-06-05 05:49:27 +00:00
"""
Run the thread.
"""
2016-06-06 19:56:38 +00:00
self.ws_server.start_server()
2016-06-05 05:49:27 +00:00
def stop(self):
2016-06-06 19:56:38 +00:00
self.ws_server.stop = True
2016-06-05 05:49:27 +00:00
2017-10-23 22:09:57 +00:00
class WebSocketServer(RegistryProperties, LogMixin):
2016-06-05 05:49:27 +00:00
"""
Wrapper round a server instance
"""
2016-06-06 19:56:38 +00:00
def __init__(self):
2016-06-05 05:49:27 +00:00
"""
2016-06-16 20:50:01 +00:00
Initialise and start the WebSockets server
2016-06-05 05:49:27 +00:00
"""
2016-06-14 20:56:50 +00:00
super(WebSocketServer, self).__init__()
2017-12-02 09:11:22 +00:00
if Registry().get_flag('no_web_server'):
self.settings_section = 'api'
self.worker = WebSocketWorker(self)
self.thread = QtCore.QThread()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
self.thread.start()
2016-06-05 05:49:27 +00:00
def start_server(self):
"""
Start the correct server and save the handler
"""
address = Settings().value(self.settings_section + '/ip address')
2016-07-09 11:21:25 +00:00
port = Settings().value(self.settings_section + '/websocket port')
2016-06-05 05:49:27 +00:00
self.start_websocket_instance(address, port)
# If web socket server start listening
if hasattr(self, 'ws_server') and self.ws_server:
event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(event_loop)
event_loop.run_until_complete(self.ws_server)
event_loop.run_forever()
else:
log.debug('Failed to start ws server on port {port}'.format(port=port))
def start_websocket_instance(self, address, port):
"""
Start the server
:param address: The server address
:param port: The run port
"""
loop = 1
while loop < 4:
try:
2016-06-05 16:20:39 +00:00
self.ws_server = websockets.serve(self.handle_websocket, address, port)
2016-06-05 05:49:27 +00:00
log.debug("Web Socket Server started for class {address} {port}".format(address=address, port=port))
break
except Exception as e:
log.error('Failed to start ws server {why}'.format(why=e))
loop += 1
time.sleep(0.1)
@staticmethod
2017-06-08 19:56:19 +00:00
async def handle_websocket(request, path):
"""
Handle web socket requests and return the poll information.
2016-06-13 19:51:46 +00:00
Check ever 0.2 seconds to get the latest position and send if changed.
Only gets triggered when 1st client attaches
2016-06-13 19:51:46 +00:00
:param request: request from client
2016-06-13 19:51:46 +00:00
:param path: determines the endpoints supported
:return:
"""
log.debug("web socket handler registered with client")
previous_poll = None
previous_main_poll = None
2016-06-19 06:16:01 +00:00
poller = Registry().get('poller')
2016-07-10 06:25:25 +00:00
if path == '/state':
while True:
2016-06-14 22:03:08 +00:00
current_poll = poller.poll()
if current_poll != previous_poll:
2017-06-08 19:56:19 +00:00
await request.send(json.dumps(current_poll).encode())
previous_poll = current_poll
2017-06-08 19:56:19 +00:00
await asyncio.sleep(0.2)
2016-07-10 06:25:25 +00:00
elif path == '/live_changed':
while True:
2016-06-14 22:03:08 +00:00
main_poll = poller.main_poll()
if main_poll != previous_main_poll:
2017-06-08 19:56:19 +00:00
await request.send(main_poll)
previous_main_poll = main_poll
2017-06-08 19:56:19 +00:00
await asyncio.sleep(0.2)