-
Notifications
You must be signed in to change notification settings - Fork 0
/
poly_app.py
286 lines (255 loc) · 10.4 KB
/
poly_app.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
################################################################################
# STANDARD LIB IMPORTS
import sys, os, time, gc
try:
import sys
from sys import print_exception #micropython specific
except ImportError:
import traceback
print_exception = lambda exc, file_: traceback.print_exc(file=file_)
try:
from time import monotonic as time_monotonic
except ImportError:
time_monotonic = lambda: time.ticks_us()/1e6
try:
from collections import OrderedDict
except ImportError:
from ucollections import OrderedDict #micropython specific
try:
import json
except ImportError:
import ujson as json #micropython specific
try:
from micropython import schedule_exc
# New feature not in official release! Allows soft-IRQs for timer to throw
# exceptions in main program. We use this to timeout user supplied loop.py
# see this micropython branch: https://github.com/dpgeorge/micropython/tree/scheduler
except ImportError:
schedule_exc = lambda arg: None
#-------------------------------------------------------------------------------
# PAWPAW PACKAGE IMPORTS
from pawpaw import WebApp, Router, route, Template, LazyTemplate, AutoTreeFormat
from pawpaw import auto_tree_format
#-------------------------------------------------------------------------------
# LOCAL IMPORTS
################################################################################
# PLATFORM SPECIFIC SETUP
#-------------------------------------------------------------------------------
import platform_setup
SERVER_ADDR = platform_setup.SERVER_ADDR
SERVER_PORT = platform_setup.SERVER_PORT
DEBUG = platform_setup.DEBUG
DEFAULT_LOOP_PY_TIMEOUT_MS = 1000
if DEBUG:
print("INSIDE MODULE name='%s' " % ('poly_app',))
try:
from micropython import mem_info #micropython specific
mem_info()
except ImportError:
pass
################################################################################
# FILE SYSTEM SETUP
#-------------------------------------------------------------------------------
LOGDIR = "logs" #nested directories are not supported
if not LOGDIR in os.listdir():
print("Running os.mkdir('%s')" % logs)
os.mkdir(LOGDIR)
################################################################################
# HARDWARE INTERFACES
#-------------------------------------------------------------------------------
PINS = OrderedDict()
#try:
# import board, nativeio #CircuitPython specific
# PINS[0] = board.GPIO0
# PINS[2] = board.GPIO2
# #PINS[4] = board.GPIO4 #FIXME using this pin gives occasional
# #"ValueError: Pin GPIO4 in use"
# PINS[5] = board.GPIO5
# PINS[12] = board.GPIO12
# PINS[13] = board.GPIO13
# PINS[14] = board.GPIO14
# PINS[15] = board.GPIO15
# #set all pins to input, safest state
# for pin_num, board_pin in PINS.items():
# with nativeio.DigitalInOut(board_pin) as d_in:
# d_in.switch_to_input()
# with nativeio.DigitalInOut(PINS[2]) as d_out:
# d_out.switch_to_output()
# d_out.value =
#except ImportError:
try:
import machine #micropython specific
except ImportError:
import mock_machine as machine #a substitute for PC testing
pin_numbers = (0, 2, 4, 5, 12, 13, 14, 15)
PINS = OrderedDict((i,machine.Pin(i, machine.Pin.IN)) for i in pin_numbers)
#configure humidity/temperature sensor interface
try:
from am2315 import AM2315
except ImportError:
from mock_am2315 import AM2315
try:
from micropython import mem_info
except ImportError:
mem_info = lambda: None
ht_sensor = AM2315()
ht_sensor.init()
class TimeoutException(Exception):
pass
################################################################################
# APPLICATION CODE
#-------------------------------------------------------------------------------
@Router
class PolyServer(WebApp):
@route("/", methods=['GET'])
def index(self, context):
context.send_file("html/index.html")
@route("/test", methods=['GET'])
def test(self, context):
global DEBUG
if DEBUG:
print("INSIDE ROUTE HANDLER name='%s' " % ('test'))
try:
from micropython import mem_info
mem_info()
except ImportError:
pass
context.send_file("html/test.html")
@route("/config", methods=['GET','POST'])
def config(self, context):
config_filename = "SECRET_CONFIG.json"
comment = ""
if context.request.method == 'POST':
body = context.request.body
form = auto_tree_format.parse_form_url(body)
comment = "'%s' has been updated" % config_filename
with open(config_filename,'w') as f:
f.write(json.dumps(form))
gc.collect()
ATF = AutoTreeFormat.from_json_file(config_filename)
gen_form = ATF.gen_html_form()
tmp = LazyTemplate.from_file("html/config_form.html", endline = "", rstrip_lines = False)
tmp.format(form_content = gen_form, comment = comment)
context.render_template(tmp)
@route("/logs/PolyServer.yaml", methods=['GET','DELETE'])
def logs(self, context):
if context.request.method == 'GET':
context.send_file("logs/PolyServer.yaml")
elif context.request.method == 'DELETE':
os.remove("logs/PolyServer.yaml")
open("logs/PolyServer.yaml",'w').close()
context.send_json({})
@route("/exc", methods=['PUT'])
def exc(self, context):
global DEBUG
if DEBUG:
print("INSIDE ROUTE HANDLER name='%s' " % ('exc'))
try:
from micropython import mem_info
mem_info()
except ImportError:
pass
context.send_json({})
raise Exception("this is a test")
@route("/pins", methods=['GET','PUT'])
def pins(self, context):
pin_num = int(context.request.args['pin_num'][0])
pin_type = context.request.args['pin_type'][0]
pin_value = None
pin = PINS[pin_num]
if context.request.method == 'PUT':
pin_value = json.loads(context.request.args['pin_value'][0]) #converts to int
pin_value = bool(pin_value)
if pin_type == "digital_out":
if DEBUG:
print("SETTING pin_type = 'digital_out' to pin_value = %s" % pin_value)
try:
if DEBUG:
print("USING HARDWARE INTERFACE machine.Pin")
PINS[pin_num] = pin = machine.Pin(pin_num,machine.Pin.OUT)
pin.value(pin_value)
except ImportError:
#works in micropython and PC
if DEBUG:
print("USING HARDWARE INTERFACE machine.Pin")
PINS[pin_num] = pin = machine.Pin(pin_num,machine.Pin.OUT)
pin.value(pin_value)
elif context.request.method == 'GET':
if DEBUG:
print("GETTING pin_type = '%s'" % pin_type)
print("USING HARDWARE INTERFACE machine.Pin")
pin = PINS[pin_num]
pin_value = pin.value()
pin_value = 1 if pin_value else 0
resp = {'pin_num': pin_num, 'pin_value': pin_value}
context.send_json(resp)
@route("/am2315", methods=['GET'])
def am2315(self, context):
d = {}
#acquire a humidity and temperature sample
ht_sensor.get_data(d) #adds fields 'humid', 'temp'
context.send_json(d)
def __init__(self, *args,**kwargs):
kwargs['log_dir'] = LOGDIR
super().__init__(*args, **kwargs) #IMPORTANT call parent constructor
#self._loop_timer = machine.Timer(0) #FIXME no machine.Timer on ESP32
def run(self, control_loop_period = 10.0):
import loop
t0 = time_monotonic()
t_last_ctrl_loop = time_monotonic()
while True:
#handle any waiting requests
server_has_timedout = self.serve_once()
t_now = time_monotonic()
if (t_now - t_last_ctrl_loop) > control_loop_period:
t_last_ctrl_loop = t_now
try:
print("RUNNING CONTROL LOOP")
self.init_loop_timeout()
loop.run(t_now)
except Exception as exc:
msg = "Exception caught in 'PolyServer.run' while running loop.py"
#print out message and exception/traceback
print("*"*40,file=sys.stderr)
print("* EXCEPTION", file=sys.stderr)
print("-"*40,file=sys.stderr)
print(msg, file = sys.stderr)
print_exception(exc, sys.stderr)
print("*"*40,file=sys.stderr)
#log it as well
logger = self.get_logger()
with logger as entry:
entry.write(msg)
entry.write_exception(exc)
finally:
self.deinit_loop_timeout()
def init_loop_timeout(self, period = DEFAULT_LOOP_PY_TIMEOUT_MS):
pass #FIXME no machine.Timer on ESP32
# def timeout_callback(t):
# schedule_exc(TimeoutException("Timeout while running loop.py"))
#
# self._loop_timer.init(period = period,
# mode = machine.Timer.ONE_SHOT,
# callback = timeout_callback)
def deinit_loop_timeout(self):
pass #FIXME no machine.Timer on ESP32
#self._loop_timer.deinit()
################################################################################
# MAIN
#-------------------------------------------------------------------------------
#if __name__ == "__main__":
#---------------------------------------------------------------------------
# Create application instance binding to localhost on port 9999
time.sleep(2.0)
gc.collect()
app = PolyServer(server_addr = SERVER_ADDR,
server_port = SERVER_PORT,
socket_timeout = 1.0,
)
#if DEBUG:
# print("APP HANDLER REGISTRY: %s" % app.handler_registry)
#h = app.handler_registry['GET /']
#h({})
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
app.run()