-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·61 lines (48 loc) · 1.68 KB
/
Copy pathapp.py
File metadata and controls
executable file
·61 lines (48 loc) · 1.68 KB
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
#!/usr/bin/python3
import logging
import sys
import getopt
import json
import copy
import server as Server
import target as Target
def main(argv):
try:
opts,args = getopt.getopt(argv, 'c:')
except getopt.GetoptError as err:
print('app.py -c <configfile> : ' + err)
sys.exit(2)
config = None
for opt,arg in opts:
if opt == '-c':
json_data=open(arg).read()
config = json.loads(json_data)
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
if config is None:
logging.error('Configuration not defined!')
sys.exit(2)
server_list = {}
for server_config in config['servers']:
server_list[server_config.get('type')] = Server.create(server_config)
gateway_list = []
for gateway_config in config['gateways']:
if gateway_config.get('server') is None:
print('Invalid config')
sys.exit(2)
try:
server = server_list[gateway_config.get('server')]
gateway_config['server'] = server.to_dictionary()
gateway = Target.create_gateway(config['application'], gateway_config)
if gateway is not None:
gateway_list.append(gateway)
server.attach(gateway)
gateway.attach(server)
else:
logging.error('Gateway creation failed :', gateway)
except Exception as error:
logging.error('Unknown exception : %s'%error)
for gateway in gateway_list:
gateway.start()
if __name__ == "__main__":
sys.path.append('/home/xtra/Projects/thingplus-gateway-for-python/')
main(sys.argv[1:])