-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhamservice.py
executable file
·132 lines (102 loc) · 5.06 KB
/
whamservice.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
#!/usr/bin/env python
from flask import Flask, redirect, request, Response
from time import sleep
from datetime import datetime, timedelta
import jsonpickle
import threading
import json
from s128p import S128P
app = Flask(__name__)
controller = None
last_active = {}
@app.route('/')
def homepage():
return redirect("/static/index.html")
@app.route('/inputs/')
def get_inputs():
return jsonpickle.encode(controller.inputs, unpicklable=False)
@app.route('/inputs/<int:id>', methods=['GET'])
def get_input(id):
return jsonpickle.encode(controller.inputs[int(id)], unpicklable=False)
#TODO: remove duplication here.
@app.route('/outputs/')
def get_outputs():
return Response(jsonpickle.encode(controller.outputs, unpicklable=False), mimetype="application/json")
@app.route('/outputs/<int:id>', methods=['GET'])
def get_output(id):
return Response(jsonpickle.encode(controller.outputs[int(id)], unpicklable=False), mimetype="application/json")
@app.route('/outputs/<int:id>', methods=['PUT'])
def put_output(id):
print request.form
try:
controller.set_input(id, jsonpickle.decode(request.form['input'])) #handle ints or 'null'
except KeyError:
print "not updating input"
print controller.outputs[id-1]
return ('',200)
def refresh_thread():
global last_active
while True:
sleep(5)
print "Refreshing"
controller.refresh_input_data()
controller.refresh_output_data()
now = datetime.now()
for input in controller.inputs:
if input.active:
last_active[input.id] = now
print "=== last_active:", last_active
for output in controller.outputs:
try:
if not output.enabled:
print output.id, "output disabled - input", output.input
continue
if output.input == None or last_active[output.input] < now - timedelta(seconds=8):
if output.default_input != None and last_active[output.default_input] < now - timedelta(seconds=8):
controller.set_input(output.id, output.default_input)
print "reset " + str(output.id) + " to " + str(output.default_input)
else:
print "Not resetting", output.id, "because its default_input", output.default_input, "is not idle"
else:
print output.id, "last active", last_active[output.input]
except TypeError:
pass
if __name__ == '__main__':
app.debug = False
controller = S128P()
with open('inputs.json') as f: config_inputs = json.loads(f.read())
with open('outputs.json') as f: config_outputs = json.loads(f.read())
#convert them to dicts
config_inputs = dict(zip([x['id'] for x in config_inputs], config_inputs))
config_outputs = dict(zip([x['id'] for x in config_outputs], config_outputs))
for output in controller.outputs:
try:
output.name = config_outputs[output.id]['name']
print "output", output.id, "=", output.name
try:
output.default_input = config_outputs[output.id]['default_input']
except KeyError:
print "No default input for output", output.id
try:
output.default_volume = config_outputs[output.id]['default_volume']
except KeyError:
print "No default volume for output", output.id
except KeyError:
print "No name-config for output", output.id, "- disabling"
output.enabled = False
for input in controller.inputs:
try:
input.name = config_inputs[input.id]['name']
print "input", input.id, "=", input.name
except KeyError:
print "No name-config for input", input.id, "- disabling"
input.enabled = False
print "INPUTS: " + jsonpickle.encode(controller.inputs, unpicklable=False)
print "OUTPUTS: " + jsonpickle.encode(controller.outputs, unpicklable=False)
for input in controller.inputs:
last_active[input.id] = datetime.now()
print(last_active)
print "Starting refresh thread"
thread = threading.Thread(target=refresh_thread)
thread.start()
app.run(host='0.0.0.0')