-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.py
60 lines (54 loc) · 1.59 KB
/
serializer.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
import serial
import web
import json
# Define the URLs the Web server will respond to, and the classes that will run when that URL is accessed
urls = (
'/api/getdata.json', 'getData',
'/api/openWindow', 'openWindow',
'/api/closeWindow', 'closeWindow',
'/api/setOverride', 'setOverride',
'/api/rmOverride', 'rmOverride'
)
# Set up our serial connection and close it due to weirdness with leaving it open.
# On Windows the device will be COM1, on Linux it is /dev/ttyUSB0
# Set our timeout to 3
serial = serial.Serial("/dev/ttyUSB0", 9600, timeout=3)
serial.close()
class getData:
def GET(self):
web.header('Content-Type', 'application/json') # Return the code as JSON so the browser can use it
return json.dumps(serializeFromSerial()) # Parse the raw json
class openWindow:
def GET(self):
serial.open()
serial.write("a")
serial.close()
return
class closeWindow:
def GET(self):
serial.open()
serial.write("b")
serial.close()
return
class setOverride:
def GET(self):
serial.open()
serial.write("c")
serial.close()
return
class rmOverride:
def GET(self):
serial.open()
serial.write("d")
serial.close()
return
def serializeFromSerial():
serial.open()
data = serial.readline() # Read the next line from serial
serial.close()
print(data) # Print the data to terminal for debugging purposes
j = json.loads(data) # Load the raw string as JSON
return j # Return the JSON
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()