-
Notifications
You must be signed in to change notification settings - Fork 0
/
hagwserver.py
237 lines (210 loc) · 8.61 KB
/
hagwserver.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
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])
"""
"""
Home Automation Gateway with RVI integration.
"""
import sys, os, logging, jsonrpclib
import time
from signal import *
from urlparse import urlparse
import __init__, settings
from daemon import Daemon
import coreserver
import pixieserver
import thingcontrolserver
import umsgserver
import vehicleserver
logger = logging.getLogger('hagw.default')
class HAGWServer(Daemon):
"""
Main server daemon
"""
rvi_service_edge = None
servers = {}
core_cb_server = None
pixie_cb_server = None
tc_cb_server = None
umsg_cb_server = None
umsg_cb_server = None
def shutdown(self, *args):
"""
Clean up and exit.
"""
logger.info('HAGW Server: Caught signal: %d. Shutting down...', args[0])
self.cleanup()
sys.exit(0)
def cleanup(self):
"""
Clean up.
"""
for key, value in self.servers.iteritems():
if value is not None:
value.shutdown()
self.rvi_service_edge = None
def startup(self):
"""
Initialization and startup
"""
logger.info('HAGW Server: Starting...')
logger.debug('HAGW Server: General Configuration: ' +
'RVI_SERVICE_EDGE_URL: ' + settings.RVI_SERVICE_EDGE_URL
)
# setup RVI Service Edge
logger.info('HAGW Server: Setting up outbound connection to RVI Service Edge at %s', settings.RVI_SERVICE_EDGE_URL)
self.rvi_service_edge = jsonrpclib.Server(settings.RVI_SERVICE_EDGE_URL)
# Core Server Startup
logger.info('HAGW Server: Starting Core Server on %s with service id %s.', settings.CORE_SERVER_CALLBACK_URL, settings.CORE_SERVER_SERVICE_ID)
while True:
try:
server = coreserver.CoreCallbackServer(logger, self.rvi_service_edge)
server.start()
self.servers['core'] = server
logger.info('HAGW Server: Core Server started.')
break
except Exception as e:
logger.error('HAGW Server: Core Server startup failure: %s', e)
self.cleanup()
time.sleep(settings.MAIN_LOOP_INTERVAL)
# Pixie Server Startup
if settings.PIXIE_SERVER_ENABLE == True:
# start the Pixie Server
try:
logger.info('HAGW Server: Starting Pixie Adjacent Callback Server on %s with service id %s.', settings.PIXIE_SERVER_CALLBACK_URL, settings.PIXIE_SERVER_SERVICE_ID)
server = pixieserver.PixieCallbackServer(logger, self.rvi_service_edge)
server.start()
self.servers['pixie'] = server
logger.info('HAGW Server: Pixie Adjacent Callback Server started.')
except Exception as e:
logger.error('HAGW Server: Cannot start Pixie Adjacent Callback Server: %s', e)
self.cleanup()
return False
# wait for Pixie Adjacent Callback server to come up
time.sleep(0.5)
else:
logger.info('HAGW Server: Pixie Adjacent Server not enabled')
# Thingcontrol Server Startup
if settings.TC_SERVER_ENABLE == True:
# start the Thingcontrol Server
try:
logger.info('HAGW Server: Starting Thingcontrol Callback Server on %s with service id %s.', settings.TC_SERVER_CALLBACK_URL, settings.TC_SERVER_SERVICE_ID)
server = thingcontrolserver.ThingcontrolCallbackServer(logger, self.rvi_service_edge)
server.start()
self.servers['thingcontrol'] = server
logger.info('HAGW Server: Thingcontrol Callback Server started.')
except Exception as e:
logger.error('HAGW Server: Cannot start Thingcontrol Callback Server: %s', e)
self.cleanup()
return False
# wait for Thingcontrol Callback server to come up
time.sleep(0.5)
else:
logger.info('HAGW Server: Thingcontrol Server not enabled')
# Usermessage Server Startup
if settings.UM_SERVER_ENABLE == True:
# start the Usermessage Server
try:
logger.info('HAGW Server: Starting Usermessage Callback Server on %s with service id %s.', settings.UM_SERVER_CALLBACK_URL, settings.UM_SERVER_SERVICE_ID)
server = umsgserver.UsermessageCallbackServer(logger, self.rvi_service_edge)
server.start()
self.servers['usermessage'] = server
logger.info('HAGW Server: Usermessage Callback Server started.')
except Exception as e:
logger.error('HAGW Server: Cannot start Usermessage Callback Server: %s', e)
self.cleanup()
return False
# wait for Usermessge Callback server to come up
time.sleep(0.5)
else:
logger.info('HAGW Server: Usermessage Server not enabled')
# Vehicle Server Startup
if settings.VH_SERVER_ENABLE == True:
# start the Vehicle Server
try:
logger.info('HAGW Server: Starting Vehicle Callback Server on %s with service id %s.', settings.VH_SERVER_CALLBACK_URL, settings.VH_SERVER_SERVICE_ID)
server = vehicleserver.VehicleCallbackServer(logger, self.rvi_service_edge)
server.start()
self.servers['vehicle'] = server
logger.info('HAGW Server: Vehicle Callback Server started.')
except Exception as e:
logger.error('HAGW Server: Cannot start Vehicle Callback Server: %s', e)
self.cleanup()
return False
# wait for Vehicle Callback server to come up
time.sleep(0.5)
else:
logger.info('HAGW Server: Vehicle Server not enabled')
return True
def run(self):
"""
Main execution loop
"""
# catch signals for proper shutdown
for sig in (SIGABRT, SIGTERM, SIGINT):
signal(sig, self.shutdown)
# start servers
self.startup()
# main loop
logger.debug('HAGW Server: Entering Main Loop')
while True:
try:
time.sleep(settings.MAIN_LOOP_INTERVAL)
if self.ping() == False:
# cannot ping myself via RVI -> restart
logger.warning('HAGW Server: ping failed -> restarting')
self.cleanup()
if not self.startup():
logger.warning('HAGW Server: startup failed.')
continue
#logger.debug('HAGW Server: Thingcontrol Status: %s', thingcontrolserver.setIVIHVAC())
except KeyboardInterrupt:
print ('\n')
break
def ping(self):
"""
Ping myself via RVI
"""
try:
self.rvi_service_edge.message(service_name = settings.CORE_SERVER_RVI_DOMAIN + settings.CORE_SERVER_SERVICE_ID + "/ping",
timeout = int(time.time()) + settings.RVI_SEND_TIMEOUT,
parameters = [{"message":"alive"}]
)
return True
except Exception as e:
return False
def usage():
"""
Print usage message
"""
print "HAGW Server: Usage: %s foreground|start|stop|restart" % sys.argv[0]
"""
Main Function
"""
if __name__ == "__main__":
pid_file = '/var/run/' + os.path.splitext(__file__)[0] + '.pid'
hagw_server = None
if len(sys.argv) == 3:
pid_file = sys.argv[2]
hagw_server = HAGWServer(pid_file, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null')
if len(sys.argv) >= 2:
if sys.argv[1] in ('foreground', 'fg'):
# in foreground we also log to the console
logger = logging.getLogger('hagw.console')
hagw_server.run()
elif sys.argv[1] in ('start', 'st'):
hagw_server.start()
elif sys.argv[1] in ('stop', 'sp'):
hagw_server.stop()
elif sys.argv[1] in ('restart', 're'):
hagw_server.restart()
else:
print "HAGW Server: Unknown command."
usage()
sys.exit(2)
else:
usage()
sys.exit(2)