-
Notifications
You must be signed in to change notification settings - Fork 1
/
nad-rs232-rest.py
229 lines (200 loc) · 6.47 KB
/
nad-rs232-rest.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
#!/usr/bin/python
# This file is part of nad-rs232-rest.
#
# nad-rs232-rest 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, either version 3 of the License, or
# any later version.
#
# nad-rs232-rest 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
import sys
import logging
import serial
import thread
import Queue
import re
import paho.mqtt.client as mqtt
from flask import Flask
from flask import jsonify
config = {
"serialPort": "/dev/ttyUSB0",
"serialSpeed": 115200,
"mqttBroker": "192.168.128.2",
"mqttPort": 1883,
"restBindIp": "0.0.0.0",
"restBindPort": "3333",
"deviceType": "C368",
"deviceId": "LivingRoom"
}
validMainCommands = [
"autosense",
"autostandby",
"balance",
"bass",
"brightness",
"btworkmode",
"controlstandby",
"display",
"filters",
"listeningmode",
"model",
"mute",
"polarity",
"power",
"preoutsub",
"source",
"sources",
"speakera",
"speakerb",
"tonedefeat",
"treble",
"version",
"volume",
"volumedisplaymode",
]
currentValues = {}
requestQueue = Queue.Queue()
answerQueue = Queue.Queue()
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
##########
# Helper #
##########
def stripCommand(cmd):
m = re.search("([a-zA-Z0-9]+\.[a-zA-Z0-9\.]+)[?=].*", cmd)
if m:
return m.group(1)
else:
return ""
def stripValue(cmd):
m = re.search("[a-zA-Z0-9]+\.[a-zA-Z0-9\.]+[=](.*)", cmd)
if m:
return m.group(1)
else:
return ""
##########
# MQTT #
##########
def mqtt_on_connect(client, userdata, flags, rc):
global config
logging.debug("[MQTT] Connected to broker (result code " + str(rc) + ")")
client.subscribe("NAD/" + config["deviceType"] + "/" + config["deviceId"] + "/Commands")
def mqtt_on_message(client, userdata, msg):
global requestQueue
logging.debug("[MQTT] Message on '" + str(msg.topic) + "': " + str(msg.payload))
requestQueue.put(msg.payload)
##########
# SERIAL #
##########
def handleSerial(config, mqttClient, requestQueue, answerQueue, currentValues):
try:
ser = serial.Serial(port=config["serialPort"], baudrate=config["serialSpeed"], xonxoff=False, rtscts=False, dsrdtr=False, timeout=0.5)
except:
logging.critical( "[SERIAL] Could not open " + config["serialPort"])
sys.exit(1)
logging.info("[SERIAL] Connected to " + config["serialPort"] + " with " + str(config["serialSpeed"]) + " Baud")
buffer = ""
while 1 :
cmd = None
try:
cmd = requestQueue.get_nowait()
except:
pass
if(cmd):
logging.debug("[SERIAL] Sending request '" + cmd + "'")
ser.write('\n' + cmd + '\n')
cmd = None
readByte = ser.read(1)
if(readByte):
if(ord(readByte) == 13):
if(buffer):
logging.debug("[SERIAL] Found a message on the wire: " + buffer)
command = stripCommand(buffer.lower())
if(command):
currentValues[command] = stripValue(buffer.lower())
answerQueue.put(buffer)
mqttClient.publish("NAD/" + config["deviceType"] + "/" + config["deviceId"] + "/Messages", buffer)
buffer = ""
else:
buffer = buffer + readByte
############
# REST-API #
############
app = Flask(__name__)
@app.route('/nad/c368/v1.0/Main/<command>', methods=['GET'])
def getMainCommand(command):
command = command.lower()
if command not in validMainCommands:
answerStruct = { "error": 2, "command": "main." + command, "value": None, "errorMsg": "Command invalid" }
return jsonify(answerStruct)
nadCmd = "main." + command
if nadCmd in currentValues:
logging.info("Serving request from cache")
answerStruct = { "error": 0, "command": "main." + command, "value": currentValues[nadCmd] }
return jsonify(answerStruct)
# this is rather ugly, but...
# since we are expecting an answer to our query, let's make sure there
# is nothing else stuck in the answerQueue
# TODO: optimize handleSerial() to only put answers to outstanding requests into answerQueue
with answerQueue.mutex:
answerQueue.queue.clear()
requestQueue.put("main." + command + "?")
try:
answer = answerQueue.get(True,4).lower()
if stripCommand(answer) == "main." + command:
answerStruct = { "error": 0, "command": "main." + command, "value": stripValue(answer) }
return jsonify(answerStruct)
except:
pass
answerStruct = { "error": 1, "command": "main." + command, "value": None, "errorMsg": "Did not receive a valid answer withing 4 seconds" }
return jsonify(answerStruct)
@app.route('/nad/c368/v1.0/Main/<command>/<value>', methods=['PUT'])
def putMainCommand(command, value):
command = command.lower()
if command not in validMainCommands:
answerStruct = { "error": 2, "command": "main." + command, "value": value, "errorMsg": "Command invalid" }
return jsonify(answerStruct)
# this is rather ugly, but...
# since we are expecting an answer to our query, let's make sure there
# is nothing else stuck in the answerQueue
# TODO: optimize handleSerial() to only put answers to outstanding requests into answerQueue
with answerQueue.mutex:
answerQueue.queue.clear()
requestQueue.put("main." + command + "=" + value)
try:
answer = answerQueue.get(True,4).lower()
if stripCommand(answer) == "main." + command:
answerStruct = { "error": 0, "command": "main." + command, "value": stripValue(answer) }
return jsonify(answerStruct)
except:
pass
answerStruct = { "error": 1, "command": "main." + command, "value": None, "errorMsg": "Did not receive a valid answer withing 4 seconds" }
return jsonify(answerStruct)
###############
# MAIN Thread #
###############
def main(args):
logging.info("Starting mqtt thread")
try:
client = mqtt.Client()
client.on_connect = mqtt_on_connect
client.on_message = mqtt_on_message
client.connect(config["mqttBroker"], config["mqttPort"], 60)
client.loop_start()
except:
logging.critical("Could not setup mqtt session. Bye")
sys.exit(1)
logging.info("Starting serial port thread")
try:
thread.start_new_thread(handleSerial, (config, client, requestQueue, answerQueue, currentValues))
except Exception as e:
logging.critical("Error spawning serial port thread: " + str(e))
sys.exit(1)
app.run(host=config["restBindIp"], port=config["restBindPort"])
if __name__ == '__main__':
main(sys.argv[1:])