forked from jbruce12000/kiln-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiln-controller.py
executable file
·354 lines (299 loc) · 10.7 KB
/
kiln-controller.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
import time
import os
import sys
import logging
import json
import bottle
import gevent
import geventwebsocket
#from bottle import post, get
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler
from geventwebsocket import WebSocketError
# try/except removed here on purpose so folks can see why things break
import config
logging.basicConfig(level=config.log_level, format=config.log_format)
log = logging.getLogger("kiln-controller")
log.info("Starting kiln controller")
script_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, script_dir + '/lib/')
profile_path = config.kiln_profiles_directory
from oven import SimulatedOven, RealOven, Profile
from ovenWatcher import OvenWatcher
app = bottle.Bottle()
if config.simulate == True:
log.info("this is a simulation")
oven = SimulatedOven()
else:
log.info("this is a real kiln")
oven = RealOven()
ovenWatcher = OvenWatcher(oven)
# this ovenwatcher is used in the oven class for restarts
oven.set_ovenwatcher(ovenWatcher)
@app.route('/')
def index():
return bottle.redirect('/picoreflow/index.html')
@app.route('/state')
def state():
return bottle.redirect('/picoreflow/state.html')
@app.get('/api/stats')
def handle_api():
log.info("/api/stats command received")
if hasattr(oven,'pid'):
if hasattr(oven.pid,'pidstats'):
return json.dumps(oven.pid.pidstats)
@app.post('/api')
def handle_api():
log.info("/api is alive")
# run a kiln schedule
if bottle.request.json['cmd'] == 'run':
wanted = bottle.request.json['profile']
log.info('api requested run of profile = %s' % wanted)
# start at a specific minute in the schedule
# for restarting and skipping over early parts of a schedule
startat = 0;
if 'startat' in bottle.request.json:
startat = bottle.request.json['startat']
#Shut off seek if start time has been set
allow_seek = True
if startat > 0:
allow_seek = False
# get the wanted profile/kiln schedule
profile = find_profile(wanted)
if profile is None:
return { "success" : False, "error" : "profile %s not found" % wanted }
# FIXME juggling of json should happen in the Profile class
profile_json = json.dumps(profile)
profile = Profile(profile_json)
oven.run_profile(profile, startat=startat, allow_seek=allow_seek)
ovenWatcher.record(profile)
if bottle.request.json['cmd'] == 'pause':
log.info("api pause command received")
oven.state = 'PAUSED'
if bottle.request.json['cmd'] == 'resume':
log.info("api resume command received")
oven.state = 'RUNNING'
if bottle.request.json['cmd'] == 'stop':
log.info("api stop command received")
oven.abort_run()
if bottle.request.json['cmd'] == 'memo':
log.info("api memo command received")
memo = bottle.request.json['memo']
log.info("memo=%s" % (memo))
# get stats during a run
if bottle.request.json['cmd'] == 'stats':
log.info("api stats command received")
if hasattr(oven,'pid'):
if hasattr(oven.pid,'pidstats'):
return json.dumps(oven.pid.pidstats)
return { "success" : True }
def find_profile(wanted):
'''
given a wanted profile name, find it and return the parsed
json profile object or None.
'''
#load all profiles from disk
profiles = get_profiles()
json_profiles = json.loads(profiles)
# find the wanted profile
for profile in json_profiles:
if profile['name'] == wanted:
return profile
return None
@app.route('/picoreflow/:filename#.*#')
def send_static(filename):
log.debug("serving %s" % filename)
return bottle.static_file(filename, root=os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "public"))
def get_websocket_from_request():
env = bottle.request.environ
wsock = env.get('wsgi.websocket')
if not wsock:
abort(400, 'Expected WebSocket request.')
return wsock
@app.route('/control')
def handle_control():
wsock = get_websocket_from_request()
log.info("websocket (control) opened")
while True:
try:
message = wsock.receive()
if message:
log.info("Received (control): %s" % message)
msgdict = json.loads(message)
if msgdict.get("cmd") == "RUN":
log.info("RUN command received")
profile_obj = msgdict.get('profile')
if profile_obj:
profile_json = json.dumps(profile_obj)
profile = Profile(profile_json)
oven.run_profile(profile)
ovenWatcher.record(profile)
elif msgdict.get("cmd") == "SIMULATE":
log.info("SIMULATE command received")
#profile_obj = msgdict.get('profile')
#if profile_obj:
# profile_json = json.dumps(profile_obj)
# profile = Profile(profile_json)
#simulated_oven = Oven(simulate=True, time_step=0.05)
#simulation_watcher = OvenWatcher(simulated_oven)
#simulation_watcher.add_observer(wsock)
#simulated_oven.run_profile(profile)
#simulation_watcher.record(profile)
elif msgdict.get("cmd") == "STOP":
log.info("Stop command received")
oven.abort_run()
time.sleep(1)
except WebSocketError as e:
log.error(e)
break
log.info("websocket (control) closed")
@app.route('/storage')
def handle_storage():
wsock = get_websocket_from_request()
log.info("websocket (storage) opened")
while True:
try:
message = wsock.receive()
if not message:
break
log.debug("websocket (storage) received: %s" % message)
try:
msgdict = json.loads(message)
except:
msgdict = {}
if message == "GET":
log.info("GET command received")
wsock.send(get_profiles())
elif msgdict.get("cmd") == "DELETE":
log.info("DELETE command received")
profile_obj = msgdict.get('profile')
if delete_profile(profile_obj):
msgdict["resp"] = "OK"
wsock.send(json.dumps(msgdict))
#wsock.send(get_profiles())
elif msgdict.get("cmd") == "PUT":
log.info("PUT command received")
profile_obj = msgdict.get('profile')
#force = msgdict.get('force', False)
force = True
if profile_obj:
#del msgdict["cmd"]
if save_profile(profile_obj, force):
msgdict["resp"] = "OK"
else:
msgdict["resp"] = "FAIL"
log.debug("websocket (storage) sent: %s" % message)
wsock.send(json.dumps(msgdict))
wsock.send(get_profiles())
time.sleep(1)
except WebSocketError:
break
log.info("websocket (storage) closed")
@app.route('/config')
def handle_config():
wsock = get_websocket_from_request()
log.info("websocket (config) opened")
while True:
try:
message = wsock.receive()
wsock.send(get_config())
except WebSocketError:
break
time.sleep(1)
log.info("websocket (config) closed")
@app.route('/status')
def handle_status():
wsock = get_websocket_from_request()
ovenWatcher.add_observer(wsock)
log.info("websocket (status) opened")
while True:
try:
message = wsock.receive()
wsock.send("Your message was: %r" % message)
except WebSocketError:
break
time.sleep(1)
log.info("websocket (status) closed")
def get_profiles():
try:
profile_files = os.listdir(profile_path)
except:
profile_files = []
profiles = []
for filename in profile_files:
with open(os.path.join(profile_path, filename), 'r') as f:
profiles.append(json.load(f))
profiles = normalize_temp_units(profiles)
return json.dumps(profiles)
def save_profile(profile, force=False):
profile=add_temp_units(profile)
profile_json = json.dumps(profile)
filename = profile['name']+".json"
filepath = os.path.join(profile_path, filename)
if not force and os.path.exists(filepath):
log.error("Could not write, %s already exists" % filepath)
return False
with open(filepath, 'w+') as f:
f.write(profile_json)
f.close()
log.info("Wrote %s" % filepath)
return True
def add_temp_units(profile):
"""
always store the temperature in degrees c
this way folks can share profiles
"""
if "temp_units" in profile:
return profile
profile['temp_units']="c"
if config.temp_scale=="c":
return profile
if config.temp_scale=="f":
profile=convert_to_c(profile);
return profile
def convert_to_c(profile):
newdata=[]
for (secs,temp) in profile["data"]:
temp = (5/9)*(temp-32)
newdata.append((secs,temp))
profile["data"]=newdata
return profile
def convert_to_f(profile):
newdata=[]
for (secs,temp) in profile["data"]:
temp = ((9/5)*temp)+32
newdata.append((secs,temp))
profile["data"]=newdata
return profile
def normalize_temp_units(profiles):
normalized = []
for profile in profiles:
if "temp_units" in profile:
if config.temp_scale == "f" and profile["temp_units"] == "c":
profile = convert_to_f(profile)
profile["temp_units"] = "f"
normalized.append(profile)
return normalized
def delete_profile(profile):
profile_json = json.dumps(profile)
filename = profile['name']+".json"
filepath = os.path.join(profile_path, filename)
os.remove(filepath)
log.info("Deleted %s" % filepath)
return True
def get_config():
return json.dumps({"temp_scale": config.temp_scale,
"time_scale_slope": config.time_scale_slope,
"time_scale_profile": config.time_scale_profile,
"kwh_rate": config.kwh_rate,
"currency_type": config.currency_type})
def main():
ip = "0.0.0.0"
port = config.listening_port
log.info("listening on %s:%d" % (ip, port))
server = WSGIServer((ip, port), app,
handler_class=WebSocketHandler)
server.serve_forever()
if __name__ == "__main__":
main()