Passed
Push — web-timed ( 1d9e3e...80b983 )
by Matt
01:14
created

PyDMXControl.web._routes.run_timed_event()   A

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 9
rs 9.95
c 0
b 0
f 0
cc 3
nop 1
1
"""
2
 *  PyDMXControl: A Python 3 module to control DMX via Python. Featuring fixture profiles and working with uDMX.
3
 *  <https://github.com/MattIPv4/PyDMXControl/>
4
 *  Copyright (C) 2018 Matt Cowley (MattIPv4) ([email protected])
5
"""
6
7
from re import compile as re_compile  # Regex
8
from typing import List, Union, Tuple, Dict, Callable  # Typing
9
10
from flask import Blueprint, render_template, current_app, redirect, url_for, jsonify  # Flask
11
12
from .. import Colors  # Colors
13
from ..profiles.defaults import Fixture, Vdim  # Fixtures
14
15
routes = Blueprint('', __name__, url_prefix='/')
16
17
18
def fixture_channels(this_fixture: Fixture) -> List[Tuple[str, int]]:
19
    chans = [(f['name'], fixture_channel_value(this_fixture, f['name'])) for f in this_fixture.channels.values()]
20
    if issubclass(type(this_fixture), Vdim):
21
        chans.append(("dimmer", fixture_channel_value(this_fixture, "dimmer")))
22
    return chans
23
24
25
def fixture_channel_value(this_fixture: Fixture, this_channel: Union[str, int]) -> int:
26
    if issubclass(type(this_fixture), Vdim):
27
        return this_fixture.get_channel_value(this_channel, False)[0]
28
    return this_fixture.get_channel_value(this_channel)[0]
29
30
31
helpers = ["on", "off", "locate"]
32
33
34
def fixture_helpers(this_fixture: Fixture) -> Dict[str, Callable]:
35
    return {f: this_fixture.__getattribute__(f) for f in helpers if hasattr(this_fixture, f)}
36
37
38
# Home
39
@routes.route('', methods=['GET'])
40
def home():
41
    return render_template("index.jinja2", helpers=helpers)
42
43
44
# Global Intensity
45
@routes.route('intensity/<int:val>', methods=['GET'])
46
def global_intensity(val: int):
47
    if val < 0 or val > 255:
48
        return jsonify({"error": "Value {} is invalid".format(val)}), 400
49
    current_app.parent.controller.all_dim(val)
50
    return jsonify({"message": "All dimmers updated to {}".format(val)}), 200
51
52
53
# Fixture Home
54
@routes.route('fixture/<int:fid>', methods=['GET'])
55
def fixture(fid: int):
56
    this_fixture = current_app.parent.controller.get_fixture(fid)
57
    if not this_fixture:
58
        return redirect(url_for('.home'))
59
    return render_template("fixture.jinja2", fixture=this_fixture, fixture_channels=fixture_channels,
60
                           colors=Colors, helpers=helpers)
61
62
63
# Fixture Channel
64
@routes.route('fixture/<int:fid>/channel/<int:cid>', methods=['GET'])
65
def channel(fid: int, cid: int):
66
    this_fixture = current_app.parent.controller.get_fixture(fid)
67
    if not this_fixture:
68
        return redirect(url_for('.home'))
69
    chan = this_fixture.get_channel_id(cid)
70
    if chan == -1:
71
        return redirect(url_for('.fixture', fid=this_fixture.id))
72
    this_channel = fixture_channels(this_fixture)[chan]
73
    return render_template("channel.jinja2", fixture=this_fixture, channel=this_channel, cid=chan)
74
75
76
# Fixture Channel Set
77
@routes.route('fixture/<int:fid>/channel/<int:cid>/<int:val>', methods=['GET'])
78
def channel_val(fid: int, cid: int, val: int):
79
    this_fixture = current_app.parent.controller.get_fixture(fid)
80
    if not this_fixture:
81
        return jsonify({"error": "Fixture {} not found".format(fid)}), 404
82
    chan = this_fixture.get_channel_id(cid)
83
    if chan == -1:
84
        return jsonify({"error": "Channel {} not found".format(cid)}), 404
85
86
    if val < 0 or val > 255:
87
        return jsonify({"error": "Value {} is invalid".format(val)}), 400
88
89
    this_fixture.set_channel(chan, val)
90
    val = fixture_channel_value(this_fixture, chan)
91
    data = {"message": "Channel {} {} updated to {}".format(chan + 1, this_fixture.channels[chan + 1]["name"], val),
92
            "elements": {
93
                "channel-{}-value".format(chan): val,
94
                "value": val,
95
                "slider_value": val
96
            }}
97
    if chan == this_fixture.get_channel_id("dimmer"):
98
        data["elements"]["intensity_value"] = val
99
    return jsonify(data), 200
100
101
102
# Fixture Color
103
@routes.route('fixture/<int:fid>/color/<string:val>', methods=['GET'])
104
def color(fid: int, val: str):
105
    this_fixture = current_app.parent.controller.get_fixture(fid)
106
    if not this_fixture:
107
        return jsonify({"error": "Fixture {} not found".format(fid)}), 404
108
    pattern = re_compile(r"^\s*(\d{1,3})\s*[, ]\s*(\d{1,3})\s*[, ]\s*(\d{1,3})\s*(?:[, ]\s*(\d{1,3})\s*)*$")
109
    match = pattern.match(val)
110
    if not match:
111
        return jsonify({"error": "Invalid color {} supplied".format(val)}), 400
112
    this_color = [int(f) for f in match.groups() if f]
113
    this_fixture.color(this_color)
114
    return jsonify({"message": "Color updated to {}".format(this_color),
115
                    "elements": dict({"value": Colors.to_hex(this_fixture.get_color())},
116
                                     **{"channel-{}-value".format(i): f[1] for i, f in
117
                                        enumerate(fixture_channels(this_fixture))})}), 200
118
119
120
# Fixture Intensity
121
@routes.route('fixture/<int:fid>/intensity/<int:val>', methods=['GET'])
122
def intensity(fid: int, val: int):
123
    this_fixture = current_app.parent.controller.get_fixture(fid)
124
    if not this_fixture:
125
        return jsonify({"error": "Fixture {} not found".format(fid)}), 404
126
    chan = this_fixture.get_channel_id("dimmer")
127
    if chan == -1:
128
        return jsonify({"error": "Dimmer channel not found"}), 404
129
130
    if val < 0 or val > 255:
131
        return jsonify({"error": "Value {} is invalid".format(val)}), 400
132
133
    this_fixture.set_channel(chan, val)
134
    val = fixture_channel_value(this_fixture, chan)
135
    return jsonify({"message": "Dimmer updated to {}".format(val), "elements": {
136
        "channel-{}-value".format(chan): val,
137
        "intensity_value": val
138
    }}), 200
139
140
141
# Fixture Helpers
142
@routes.route('fixture/<int:fid>/helper/<string:val>', methods=['GET'])
143
def helper(fid: int, val: str):
144
    this_fixture = current_app.parent.controller.get_fixture(fid)
145
    if not this_fixture:
146
        return jsonify({"error": "Fixture {} not found".format(fid)}), 404
147
148
    val = val.lower()
149
    this_helpers = fixture_helpers(this_fixture)
150
    if val not in this_helpers.keys():
151
        return jsonify({"error": "Helper {} not found".format(val)}), 404
152
153
    try:
154
        this_helpers[val]()
155
    except Exception:
156
        return jsonify({"error": "Helper {} failed to execute".format(val)}), 500
157
    return jsonify({"message": "Helper {} executed".format(val), "elements": dict(
158
        {"value": Colors.to_hex(this_fixture.get_color()),
159
         "intensity_value": this_fixture.get_channel_value(this_fixture.get_channel_id("dimmer"))[0]},
160
        **{"channel-{}-value".format(i): f[1] for i, f in enumerate(fixture_channels(this_fixture))})}), 200
161
162
163
# Callbacks
164
@routes.route('callback/<string:cb>', methods=['GET'])
165
def callback(cb: str):
166
    if cb not in current_app.parent.callbacks.keys():
167
        return jsonify({"error": "Callback {} not found".format(cb)}), 404
168
    try:
169
        current_app.parent.callbacks[cb]()
170
    except Exception:
171
        return jsonify({"error": "Callback {} failed to execute".format(cb)}), 500
172
    return jsonify({"message": "Callback {} executed".format(cb)}), 200
173
174
175
# Timed Events
176
@routes.route('timed_event/<string:te>', methods=['GET'])
177
def timed_event(te: str):
178
    if te not in current_app.parent.timed_events.keys():
179
        return redirect(url_for('.home'))
180
    return render_template("timed_event.jinja2", te=te)
181
182
183
# Timed Events Data
184
@routes.route('timed_event/<string:te>/data', methods=['GET'])
185
def timed_event_data(te: str):
186
    if te not in current_app.parent.timed_events.keys():
187
        return jsonify({"error": "Timed Event {} not found".format(te)}), 404
188
    return jsonify({"data": current_app.parent.timed_events[te].data}), 200
189
190
191
# Timed Events Run
192
@routes.route('timed_event/<string:te>/run', methods=['GET'])
193
def run_timed_event(te: str):
194
    if te not in current_app.parent.timed_events.keys():
195
        return jsonify({"error": "Timed Event {} not found".format(te)}), 404
196
    try:
197
        current_app.parent.timed_events[te].run()
198
    except Exception:
199
        return jsonify({"error": "Timed Event {} failed to fire".format(te)}), 500
200
    return jsonify({"message": "Timed Event {} fired".format(te)}), 200
201
202
203
# Timed Events Stop
204
@routes.route('timed_event/<string:te>/stop', methods=['GET'])
205
def stop_timed_event(te: str):
206
    if te not in current_app.parent.timed_events.keys():
207
        return jsonify({"error": "Timed Event {} not found".format(te)}), 404
208
    try:
209
        current_app.parent.timed_events[te].stop()
210
    except Exception:
211
        return jsonify({"error": "Timed Event {} failed to stop".format(te)}), 500
212
    return jsonify({"message": "Timed Event {} stopped".format(te)}), 200
213