From e7fafea8aec98b6039f8bc17c89f00d5324beeb8 Mon Sep 17 00:00:00 2001 From: iou1name Date: Fri, 11 Dec 2020 14:03:44 -0500 Subject: [PATCH] overhaul new_device() function --- events.py | 54 ++++++++++++++++++++++++++++---------------- models.py | 10 ++++---- static/juice.css | 17 ++++++++++++++ static/juice.js | 42 ++++++++++++++++++++++++++-------- templates/index.html | 31 +++++++++++++++++++++---- 5 files changed, 114 insertions(+), 40 deletions(-) diff --git a/events.py b/events.py index 3a780a4..3dc64d8 100644 --- a/events.py +++ b/events.py @@ -98,42 +98,56 @@ async def new_device(request, ws, data): Allows adding a new device. Accepts device_type parameter, returns the device_id. """ + device_id = data.get('device_id', '') device_type = data.get('device_type') + num_sub_devices = data.get('num_sub_devices') + + try: + num_sub_devices = int(num_sub_devices) + except (TypeError, ValueError): + data['ok'] = False + data['error'] = "num_sub_devices value error" + await ws.send_json(data) + return data = {} - data['device_type'] = device_type - - if device_type == 'RelayDevice': + if device_type == 'relay_device': device = models.RelayDevice() - device.sub_devices.append(models.RelayOutlet()) - device.sub_devices.append(models.RelayOutlet()) - device.sub_devices[0].id = 'OUT0' - device.sub_devices[0].gpio = '0' - device.sub_devices[1].id = 'OUT2' - device.sub_devices[1].gpio = '2' + for n in [0,2]: + sub_device = models.RelayOutlet() + sub_device.id = 'OUT' + str(n) + sub_device.gpio = str(n) + device.sub_devices.append(sub_device) device.update() + elif device_type == 'light_strip': + device = models.LightStrip() + for n in range(num_sub_devices): + sub_device = models.NeoPixel() + sub_device.id = 'LED' + str(n) + device.sub_devices.append(sub_device) + elif device_type == 'lixie_clock': + device = models.LixieClock() + for n in range(num_sub_devices): + sub_device = models.LixieDisplay() + sub_device.id = 'LixieDisplay' + str(n) + device.sub_devices.append(sub_device) else: data['ok'] = False data['error'] = "unknown device type" await ws.send_json(data) return - devices = [dev for dev in network if dev.type == device_type] - devices.sort(key=lambda dev: dev.id) - if not devices: - device.id = device_type + '01' - else: - num = re.search(r'(\d*)$', devices[-1].id).groups() - if not num: - device.id = device_type + '01' - else: - num = str(int(num[0]) + 1).zfill(2) - device.id = device_type + num + while network.find(device_id): + device_id += '0' + device.id = device_id + network.append(device) models.save_network(network) data['ok'] = True data['device_id'] = device.id + data['device_type'] = device_type + data['sub_device_ids'] = [sub_dev.id for sub_dev in device.sub_devices] res = {'event': 'new_device', 'data': data} await request.app.send_json_all(res) diff --git a/models.py b/models.py index ba7d6e8..03f1853 100644 --- a/models.py +++ b/models.py @@ -159,11 +159,11 @@ class LightStrip(Device): super().__init__() self.type = "LightStrip" self.state = "solid" - self.animation = "static", - rainbow_params = [0.3, 0.3, 0.3, 0, 2, 4, 127, 128], - america_params = [8, 128], - animation_rotate_count = 1, - animation_delay = 200, + self.animation = "static" + self.rainbow_params = [0.3, 0.3, 0.3, 0, 2, 4, 127, 128] + self.america_params = [8, 128] + self.animation_rotate_count = 1 + self.animation_delay = 200 def mqtt_callback(self, app, msg): topic = msg.topic.split('/') diff --git a/static/juice.css b/static/juice.css index 2a7e4f1..6ff0e8d 100644 --- a/static/juice.css +++ b/static/juice.css @@ -182,3 +182,20 @@ nav span:hover { color: red; cursor: pointer; } + +#new_device_dialog { + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + border: 2px solid darkgray; + border-radius: 0.5em; + padding: 3em; + margin: 3em; + background-color: whitesmoke; +} + +#new_device_settings { + display: flex; + column-gap: 2em; +} diff --git a/static/juice.js b/static/juice.js index 3dfbd05..e14b7a2 100644 --- a/static/juice.js +++ b/static/juice.js @@ -109,14 +109,30 @@ function edit_field_recv(data) { } function new_device_recv(data) { - if (data.device_type == 'RelayDevice') { - var template = document.querySelector('#RelayDevice_template'); - } else { - return; + let device_type, sub_device_template; + if (data.device_type == 'relay_device') { + device_template = document.querySelector('#RelayDevice_template'); + sub_device_template = document.querySelector('#RelayOutlet_template'); + } else if (data.device_type == 'light_strip') { + device_template = document.querySelector('#LightStrip_template'); + sub_device_template = document.querySelector('#NeoPixel_template'); + } else if (data.device_type == 'lixie_clock') { + device_template = document.querySelector('#LixieClock_template'); + sub_device_template = document.querySelector('#LixieDisplay_template'); } - let node = document.importNode(template.content, true); - node.querySelector('.id').querySelector('.field_value').textContent = data.device_id; - document.querySelector('#devices').appendChild(node); + + let device = document.importNode(device_template.content, true); + device.querySelector('.id').querySelector('.field_value').textContent = data.device_id; + for (let sub_device_id of data.sub_device_ids) { + let sub_device = document.importNode(sub_device_template.content, true); + sub_device.firstElementChild.classList.add(sub_device_id); + if (sub_device.querySelector('.id')) { + sub_device.querySelector('.id').textContent = sub_device_id; + } + device.querySelector('.sub_devices').appendChild(sub_device); + } + + document.querySelector('#devices').appendChild(device); let children = document.querySelector('#devices').children; children[children.length - 1].id = data.device_id; } @@ -277,10 +293,16 @@ function save_field(field) { socket.send('edit_field', data); } -function new_device() { +function new_device(dialog) { + let device_id = dialog.querySelector('#new_device_id').value; + let device_type = dialog.querySelector('input[name=new_device_type]:checked').value; + let num_sub_devices = dialog.querySelector('#new_device_num_sub_devices').value; + let data = { - device_type: 'RelayDevice', - }; + 'device_id': device_id, + 'device_type': device_type, + 'num_sub_devices': num_sub_devices, + } socket.send('new_device', data); } diff --git a/templates/index.html b/templates/index.html index eeca41b..e8c10ae 100644 --- a/templates/index.html +++ b/templates/index.html @@ -25,7 +25,7 @@
@@ -78,7 +78,7 @@
- +