-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflaskServer.py
More file actions
executable file
·99 lines (76 loc) · 2.79 KB
/
flaskServer.py
File metadata and controls
executable file
·99 lines (76 loc) · 2.79 KB
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
#!/usr/bin/env python
from flask import Flask, request, jsonify, Response
import json
import gevent
from gevent.wsgi import WSGIServer
from gevent.queue import Queue
import ProgramModules.sharedObjects as app
from SculptureController import SculptureController
flaskApp = Flask(__name__, static_folder='jsGui', static_url_path='/jsGui')
sculpture = SculptureController()
serverSentEventStreams = []
class ServerSentEvent(object):
def __init__(self, data):
self.data = data
self.event = None
self.id = None
self.desc_map = {
self.data : "data",
self.event : "event",
self.id : "id"
}
def encode(self):
if not self.data:
return ""
lines = ["%s: %s" % (v, k)
for k, v in self.desc_map.iteritems() if k]
return "%s\n\n" % "\n".join(lines)
def sendNewOutputState(whatChanged):
data = {whatChanged : app.messenger.getMessages(whatChanged)}
for sub in serverSentEventStreams[:]:
sub.put(json.dumps(data), False)
@flaskApp.route('/')
def jsGui():
return flaskApp.send_static_file('index.htm')
@flaskApp.route('/getData', methods =['GET', 'POST'])
def getData():
return jsonify(sculpture.getCurrentStateData())
@flaskApp.route('/doCommand', methods =['POST'])
def doCommand():
requestData = json.loads(request.data)
command = requestData[0]
result = sculpture.doCommand(requestData)
if command == 'loadSculpture':
app.messenger.addBinding('outputChanged', sendNewOutputState, ('outputChanged',))
app.messenger.addBinding('log', sendNewOutputState, ('log',))
return jsonify({'command' : command, 'result' : result})
@flaskApp.route("/dataStream")
def subscribe():
def gen():
q = Queue()
serverSentEventStreams.append(q)
try:
while True:
result = q.get()
ev = ServerSentEvent(str(result))
yield ev.encode()
except GeneratorExit:
serverSentEventStreams.remove(q)
return Response(gen(), mimetype="text/event-stream")
# Testing commands
# sculpture.loadSculpture('tympani')
# app.messenger.addBinding('outputChanged', sendNewOutputState, ('outputChanged',))
# app.messenger.addBinding('log', sendNewOutputState, ('log',))
# sculpture.doCommand(['addPattern', 'poofers', 'AllPoof'])
# sculpture.doCommand(['addPattern', 'poofers', 'Chase'])
# sculpture.doCommand(['addGlobalInput', {'type' : 'pulse', 'subType' : 'audio'}])
# sculpture.doCommand(['addGlobalInput', {'type' : 'multi', 'subType' : 'randomPulse'}])
# sculpture.doCommand(['addGlobalInput', {'type' : 'multi', 'subType' : 'basic', 'number' : 5, 'min' : [0, 0, 1, 0, 1], 'max' : [1, 2, 3, 4, 5], 'description' : ['foo', 'bar', 'baz', 'bam', 'boo']}])
# sculpture.doCommand(["changePatternInputBinding","poofers","poofersPattern0","poofButton",2,0])
if __name__ == '__main__':
flaskApp.debug = True
server = WSGIServer(("", 80), flaskApp)
try:
server.serve_forever()
except:
sculpture.doReset()