-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumsgserver.py
83 lines (68 loc) · 3.15 KB
/
umsgserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Copyright (C) 2014, Jaguar Land Rover
This program is licensed under the terms and conditions of the
Mozilla Public License, version 2.0. The full text of the
Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
Maintainer: Rudolf Streif ([email protected])
"""
"""
User Message Server.
"""
import os, threading, base64
import time, httplib, json, math
from urlparse import urlparse
from rvijsonrpc import RVIJSONRPCServer
import settings
logger = None
service_edge = None
transaction_id = 0
# Usermessage Callback Server
class UsermessageCallbackServer(threading.Thread):
"""
RPC server thread responding to user message callbacks from the RVI framework
"""
def __init__(self, _logger, _service_edge):
global logger
global service_edge
logger = _logger
service_edge = _service_edge
threading.Thread.__init__(self)
self.init_callback_server()
self.register_services()
def init_callback_server(self):
# initialize RPC server and register callback functions
url = urlparse(settings.UM_SERVER_CALLBACK_URL)
self.localServer = RVIJSONRPCServer(addr=((url.hostname, url.port)), logRequests=False)
self.localServer.register_function(showUserMessage, settings.UM_SERVER_SERVICE_ID + "/showusermessage")
self.localServer.register_function(cancelUserMessage, settings.UM_SERVER_SERVICE_ID + "/canelusermessage")
def register_services(self):
# register services with RVI framework
result = service_edge.register_service(service = settings.UM_SERVER_SERVICE_ID + '/showusermessage',
network_address = settings.UM_SERVER_CALLBACK_URL)
logger.info('Usermessage Service Registration: showusermessage service name: %s', result['service'])
result = service_edge.register_service(service = settings.UM_SERVER_SERVICE_ID + '/cancelusermessage',
network_address = settings.UM_SERVER_CALLBACK_URL)
logger.info('Usermessage Service Registration: cancelusermessage service name: %s', result['service'])
def run(self):
self.localServer.serve_forever()
def shutdown(self):
self.localServer.shutdown()
self.localServer.server_close()
# Callback functions
def showUserMessage(messageid, displays, messagetext):
"""
Show a message to users on displays in the house.
:param: messageid: unique id of the message
:param: displays: list of displays to show the message on
:param: messagetext: text of the message
"""
logger.info('Usermessage Callback Server: showUserMessage: messageid: %s, displays: %s, message: %s.', messageid, displays, messagetext)
return {u'status': 0}
def cancelUserMessage(messageid, displays):
"""
Cancel a previously shown message on one or more displays
:param: messageid: unique id of the message
:param: displays: list of displays to remove the message from
"""
logger.info('Usermessage Callback Server: cancelUserMessage: messageid: %s, displays: %s, message: %s.', messageid, displays, messagetext)
return {u'status': 0}