-
Notifications
You must be signed in to change notification settings - Fork 6
/
webrop.py
131 lines (95 loc) · 3.43 KB
/
webrop.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
from __future__ import print_function
import config
import logging
import os.path
import traceback
from random import seed
from time import time
from flask import Flask, Response, jsonify
from importer import ImportPlugins
from opc.matrix import OPCMatrix
logging.basicConfig(filename='web.log', level=logging.INFO)
# This code remains experimental. Configurtions are contained within the file.
M_WIDTH = 64
M_HEIGHT = 64
DFLT_FLIPTIME_SECS = 30
app = Flask(__name__)
app.config.from_object(__name__)
class Feed(object):
def __init__(self):
matrix = OPCMatrix(M_WIDTH, M_HEIGHT, "echo", fliplr=True)
arts = ImportPlugins("art", ["template.py"], [], None, matrix,
config.config)
if len(arts) == 0:
matrix.terminate()
print("Couldn't find any art to execute")
exit(1)
self.generator = self._frameGenerator(arts, matrix)
self.packet = None
def _frameGenerator(self, arts, matrix):
while True:
seed(time())
for name, art in arts.items():
matrix.hq(False)
matrix.clear()
try:
art.start(matrix)
except Exception as e:
logging.info("start bork: " + str(e))
logging.info("start bork: " + traceback.format_exc())
continue
start_time = time()
while time() - start_time < DFLT_FLIPTIME_SECS:
cycle_time = time()
try:
art.refresh(matrix)
except Exception as e:
logging.info("refresh bork: " + str(e))
logging.info("refresh bork: " + traceback.format_exc())
break
elapsed = time() - cycle_time
remaining = art.interval() / 1000.0 - elapsed
yield {
"interval": remaining,
"expires": time() + remaining,
"data": matrix.show(),
}
def _webHex(self, pix):
return '{0:06x}'.format(((int(pix[0]) * 0x100) + int(pix[1])) * 0x100 +
int(pix[2]))
def produce(self):
if self.packet is None or time() > self.packet["expires"]:
frame = next(self.generator)
data = [
[self._webHex(pix) for pix in row] for row in frame["data"]
]
self.packet = {
"interval": frame["interval"],
"expires": frame["expires"],
"data": data,
}
return self.packet
def root_dir(): # pragma: no cover
return os.path.abspath(os.path.dirname(__file__))
def get_file(filename): # pragma: no cover
try:
src = os.path.join(root_dir(), filename)
return open(src).read()
except IOError as exc:
return str(exc)
@app.route('/', methods=['GET'])
def docroot():
content = get_file('index.html')
return Response(content, mimetype="text/html")
@app.route("/initialize.json")
def json_initialize():
packet = {"xrange": M_WIDTH, "yrange": M_HEIGHT, }
return jsonify(packet)
@app.route("/refresh.json")
def json_refresh():
global feed
return jsonify(feed.produce())
if __name__ == "__main__":
global feed
feed = Feed()
app.run(threaded=True, debug=True)