-
Notifications
You must be signed in to change notification settings - Fork 10
/
api.py
executable file
·72 lines (66 loc) · 2 KB
/
api.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
import re
import json
import time
import stratasys as st
from pprint import pprint
import flask as fk
from flask import jsonify,Response
import config as cf
app = fk.Flask(__name__)
def return_unavail():
rd = { 'status' : "offline"}
resp = jsonify(rd)
resp.status_code=503
return resp
def refreshCond():
global od, lastref
if(lastref < time.monotonic()):
od=st.output_postproc(st.stratasys_out_proc(st.printer_get_data(cf.printer_ip)))
print("Perfomed Refresh")
lastref = (time.monotonic() + 10.0)
#dict_keys(['machineStatus(cassette)', 'machineStatus(queue)', 'machineStatus(previousJob)', 'machineStatus(general)', 'Transferred', 'machineStatus(mariner)', 'machineStatus(currentJob)'])
@app.route("/v1/uprint/queue")
def queue():
global od
refreshCond()
if not od:
return return_unavail()
rs = json.dumps(od['machineStatus(queue)'])
return Response(rs, status=200, mimetype = 'application/json')
@app.route("/v1/uprint/status")
def stat():
global od
refreshCond()
if not od:
return return_unavail()
return jsonify(sanitize(od['machineStatus(general)']))
@app.route("/v1/uprint/job")
def job():
global od
refreshCond()
if not od:
return return_unavail()
return jsonify(sanitize(od['machineStatus(currentJob)']))
@app.route("/v1/uprint/extstatus")
def es():
global od
refreshCond()
if not od:
return return_unavail()
return jsonify(sanitize(od['machineStatus(extended)']))
@app.route("/v1/uprint/material")
def cass():
global od
refreshCond()
if not od:
return return_unavail()
return jsonify(sanitize(od['machineStatus(cassette)']))
@app.route("/")
def root():
return app.send_static_file('index.html')
def sanitize(ind):
r = re.compile("version|serial|mfglot|mfgdate|usagedate")
return {k: v for k, v in ind.items() if r.search(k.lower()) == None}
if( __name__ == "__main__"):
lastref = 0
app.run(host='0.0.0.0',port=cf.app_port,debug=False)