-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_message.py
executable file
·143 lines (97 loc) · 4.5 KB
/
parse_message.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
from server_lib import gpio
import json, os, sys
import traceback
import tailer
import logging
pathname = os.path.dirname(sys.argv[0])
fullpath = os.path.abspath(pathname)
config_file = fullpath + "/config/config.json"
people_file = fullpath + "/data.json"
thermostat_file = fullpath + "/thermostat.json"
with open(config_file) as data_file:
conf_obj = json.load(data_file)
sender = conf_obj['servername']
def onMessage(obj, config_file, send_function):
try:
# obj = json.loads(payload.decode('utf8'))
# print obj
# tested working
if obj['MessageType'] == 'Command':
cmd = obj['Command']
if cmd == "pin_out":
logging.info("Received pin_out Command - pin %s %s" % (obj['pin_number'], obj['value']))
gpio.cmd_pin_out(obj)
elif cmd == "SaveConfig":
config_object = obj['ConfigData']
json_to_file = json.dumps(config_object, indent=4)
logging.info("Received SaveConfig Command \n %s" % json_to_file)
with open(config_file, 'w') as data_file:
data_file.truncate()
data_file.write(json_to_file)
elif cmd == "TempConfig":
with open(thermostat_file) as d_file:
d_file_obj = json.load(d_file)
d_file_obj['target_temp'] = obj['target_temp']
d_file_obj['fan'] = obj['fan']
d_file_obj['system'] = obj['system']
with open(thermostat_file, 'w') as out_file:
out_file.write(json.dumps(d_file_obj, indent=4))
else:
logging.error("Command not recognized (%s)" % cmd)
# tested working
elif obj['MessageType'] == 'Query':
q = obj['Query']
if q == "pin_out":
logging.info("Received pin_out Query - pin %s" % obj['pin_number'])
send_function(json.dumps(gpio.q_pin_out(obj)))
elif q == "Config":
logging.info("Received Config Query")
with open(config_file) as data_file:
config_object = json.load(data_file)
reply_object = {
"Sender": sender,
"MessageType": "QueryReply",
"Query": "Config",
"ConfigData": config_object
}
# self.sendMessage(json.dumps(reply_object))
send_function(json.dumps(reply_object))
elif q == "People":
logging.info("Received People Query")
with open(people_file) as d_file:
data_object = json.load(d_file)
people_array = data_object['People']
reply_object = {
"Sender": sender,
"MessageType": "QueryReply",
"Query": "People",
"People": people_array
}
# self.sendMessage(json.dumps(reply_object))
send_function(json.dumps(reply_object))
elif q == "ThermostatData":
logging.info("Received Thermostat Query")
with open(thermostat_file) as d_file:
data_object = json.load(d_file)
reply_object = {
"Sender": sender,
"MessageType": "QueryReply",
"Query": "ThermostatData",
"Data": data_object
}
send_function(json.dumps(reply_object))
elif q == "Log":
logging.info("Received Log Query")
logarray = tailer.tail(open('nohup.out'), 500)
# logstr = '\n'.join(logarray)
reply_object = {
"Sender": sender,
"MessageType": "QueryReply",
"Query": "Log",
"Data": logarray
}
send_function(json.dumps(reply_object))
else:
logging.error("Query not recognized (%s)" % q)
except Exception as e:
logging.exception('')