From 761683a046fc94c2a55de6dac7c5554110dd1dc3 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 21 Nov 2019 20:39:47 -0500 Subject: [PATCH] animation controls functioning --- events.py | 53 +++++++++++++++++++++++++++++++------------- models.py | 27 ++++++++++++++++++++-- static/juice.js | 38 +++++++++++++++++++++++++++++++ templates/index.html | 17 ++++++++++---- 4 files changed, 113 insertions(+), 22 deletions(-) diff --git a/events.py b/events.py index 503fbb0..7695dba 100644 --- a/events.py +++ b/events.py @@ -195,25 +195,46 @@ async def neopixel(request, ws, data): await ws.send_json(data) return - # TODO: form validation - mqtt_msg = [device.mqtt_root] - mqtt_msg.append(data.get('change_mode')) - mqtt_msg.append(data.get('type')) - if data.get('type') == 'solid': - mqtt_msg.append(data.get('amount')) - if data.get('amount') == 'single': - mqtt_msg.append(data.get('sub_device_id').replace('led', '')) + if data.get('change_mode') == 'state': + mqtt_msg.append(data.get('change_mode')) + if data.get('type') == 'solid': + mqtt_msg.append(data.get('type')) + if data.get('amount') == 'single': + mqtt_msg.append(data.get('amount')) + mqtt_msg.append(data.get('sub_device_id').replace('led', '')) + elif data.get('amount') == 'all': + mqtt_msg.append(data.get('amount')) + else: + error = "invalid amount" + payload = tools.from_html_color(data.get('color')) + payload = str(payload)[1:-1].replace(' ', '') + elif data.get('type') == 'rainbow': + mqtt_msg.append(data.get('type')) + payload = ','.join(data.get('rainbow_params')) + elif data.get('type') == 'america': + mqtt_msg.append(data.get('type')) + payload = ','.join(data.get('america_params')) + else: + error = "invalid state type" + elif data.get('change_mode') == 'animation': + mqtt_msg.append(data.get('change_mode')) + if data.get('property_type') == 'mode': + mqtt_msg.append(data.get('property_type')) + if data.get('type') == 'static': + mqtt_msg.append(data.get('type')) + payload = '' + elif data.get('type') == 'rotate_left': + mqtt_msg.append(data.get('type')) + payload = data.get('rotate_count') + elif data.get('type') == 'rotate_right': + mqtt_msg.append(data.get('type')) + payload = data.get('rotate_count') + elif data.get('property_type') == 'delay': + mqtt_msg.append(data.get('property_type')) + payload = data.get('delay') mqtt_msg = '/'.join(mqtt_msg) - if data.get('type') == 'solid': - payload = tools.from_html_color(data.get('color')) - payload = str(payload)[1:-1].replace(' ', '') - elif data.get('type') == 'rainbow': - payload = ','.join(data.get('rainbow_params')) - elif data.get('type') == 'america': - payload = ','.join(data.get('america_params')) - request.app['mqtt'].publish(mqtt_msg, payload) # websocket response is handled under LightStrip.mqtt_callback diff --git a/models.py b/models.py index a9bc0d1..eca220b 100644 --- a/models.py +++ b/models.py @@ -173,29 +173,52 @@ class LightStrip(Device): data['change_mode'] = topic[1] if topic[1] == 'state': - data['type'] = topic[2] if topic[2] == 'solid': + data['type'] = topic[2] payload = payload.split(',') payload = [int(num) for num in payload] - data['amount'] = topic[3] if topic[3] == 'single': + data['amount'] = topic[3] data['sub_device_id'] = 'led' + topic[4] sub_device = self.find('led' + topic[4]) sub_device.color = payload elif topic[3] == 'all': + data['amount'] = topic[3] for sub_device in self.sub_devices: sub_device.color = payload data['color'] = tools.to_html_color(payload) elif topic[2] == 'rainbow': + data['type'] = topic[2] payload = payload.split(',') self.rainbow_params = [float(p) for p in payload[:3]] self.rainbow_params += [int(p) for p in payload[3:]] data['rainbow_params'] = self.rainbow_params elif topic[2] == 'america': + data['type'] = topic[2] payload = payload.split(',') self.america_params = [int(n) for n in payload] data['america_params'] = self.america_params + elif topic[1] == 'animation': + if topic[2] == 'mode': + data['property_type'] = topic[2] + if topic[3] == 'static': + data['type'] = topic[3] + self.animation = topic[3] + elif topic[3] == 'rotate_left': + data['type'] = topic[3] + data['rotate_count'] = payload + self.animation = topic[3] + self.animation_rotate_count = int(payload) + elif topic[3] == 'rotate_right': + data['type'] = topic[3] + data['rotate_count'] = payload + self.animation = topic[3] + self.animation_rotate_count = int(payload) + elif topic[2] == 'delay': + data['property_type'] = topic[2] + data['delay'] = payload + self.animation_delay = int(payload) elif topic[1] == 'strip': data['amount'] = topic[2] if topic[2] == 'full': diff --git a/static/juice.js b/static/juice.js index 275310d..596ab7a 100644 --- a/static/juice.js +++ b/static/juice.js @@ -305,6 +305,32 @@ function neopixel_state(device) { socket.send('neopixel', data); } +function neopixel_animation(device) { + let property_type; + if (device.nodeName === 'INPUT') { + property_type = 'delay'; + device = device.closest('.device'); + } else { + property_type = 'mode'; + } + let data = { + device_id: device.id, + change_mode: 'animation', + property_type: property_type + } + if (property_type === 'mode') { + let animation_mode = device.querySelector('.animation_select').value; + data['type'] = animation_mode; + if (animation_mode === 'static') { + } else if (animation_mode === 'rotate_left' || animation_mode === 'rotate_right') { + data['rotate_count'] = device.querySelector('#animation_rotate_count_' + device.id).value; + } + } else if (property_type === 'delay') { + data['delay'] = device.querySelector('#animation_delay_' + device.id).value; + } + socket.send('neopixel', data); +} + /* DOM */ function edit_field(field) { let value = field.firstElementChild.innerText; @@ -360,6 +386,18 @@ function state_select(select) { neopixel_state(device); } +function animation_select(select) { + let device = select.closest('.device'); + if (select.value === 'static') { + select.parentElement.querySelector('.animation_static').style.display = 'block'; + select.parentElement.querySelector('.animation_rotate').style.display = 'none'; + } else if (select.value === 'rotate_left' || select.value === 'rotate_right') { + select.parentElement.querySelector('.animation_static').style.display = 'none'; + select.parentElement.querySelector('.animation_rotate').style.display = 'block'; + } + neopixel_animation(device); +} + /* Misc */ function get_object_from_svg(svg) { let all_objects = document.getElementsByTagName("object"); diff --git a/templates/index.html b/templates/index.html index 826b9ff..c29f99d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -115,11 +115,20 @@
Animation
- + Static + Rotate Left + Rotate Right +
+
+
+ + +
+ Delay + +
{% endif %}