From 33f071accdc2f7d81b70613ff160492239cba269 Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 18 Jun 2019 13:44:35 -0400 Subject: [PATCH] refactored network topology --- juice.py | 44 ++++++++++++++++++++++---------------------- templates/index.html | 12 ++++-------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/juice.py b/juice.py index ef6b0f7..24f6709 100644 --- a/juice.py +++ b/juice.py @@ -23,10 +23,11 @@ class RelayDevice: """ def __init__(self): self.id = "" + self.type = "" self.description = "" self.location = "" self.ip_address = "" - self.sub_devices = {} + self.sub_devices = [] self.update() @@ -35,16 +36,16 @@ class RelayDevice: Queries the physical device and updates the internal model as appropriate. """ - for name, device in self.sub_devices.items(): + for sub_dev in self.sub_devices: res = requests.get(self.ip_address) gpio0 = re.search(r"GPIO0: (\bLow|\bHigh)", res.text).groups()[0] - device.state = gpio0 == 'High' + sub_dev.state = gpio0 == 'High' def toggle(self, sub_dev_id): """ Toggles the state of a sub device. """ - for sub_dev in sum(self.sub_devices.values(), []): + for sub_dev in self.sub_devices: if sub_dev.id == sub_dev_id: break else: @@ -68,14 +69,14 @@ class RelayDevice: """ for key, value in data.items(): setattr(self, key, value) - for sub_dev_type, sub_devs in data['sub_devices'].items(): - if sub_dev_type == 'RelayOutlet': - self.sub_devices[sub_dev_type] = [] - for sub_dev in sub_devs: - sub_dev = RelayOutlet().from_dict(sub_dev) - self.sub_devices[sub_dev_type].append(sub_dev) + self.sub_devices = [] + for sub_dev_dict in data.get('sub_devices'): + if sub_dev_dict.get('type') == 'RelayOutlet': + sub_dev = RelayOutlet().from_dict(sub_dev_dict) + self.sub_devices.append(sub_dev) else: - raise ValueError(f"Unknown sub_device type {dev_name}") + typ = sub_dev.get('type') + raise ValueError(f"Unknown sub_device type {typ}") return self @@ -85,6 +86,7 @@ class RelayOutlet: """ def __init__(self): self.id = "" + self.type = "" self.description = "" self.gpio = "" self.state = False @@ -113,15 +115,13 @@ def init_network(filepath="devices.json"): with open(filepath, 'r') as file: data = file.read() data = json.loads(data) - network = {} - for dev_type, devices in data.items(): - if dev_type == 'RelayDevice': - network[dev_type] = [] - for device in devices: - device = RelayDevice().from_dict(device) - network[dev_type].append(device) + network = [] + for device_dict in data: + if device_dict.get('type') == 'RelayDevice': + device = RelayDevice().from_dict(device_dict) + network.append(device) else: - raise ValueError(f"Unknown device type {dev_name}") + raise ValueError(f"Unknown device type {device.get('type')}") return network @@ -152,7 +152,7 @@ def toggle(): device_id = request.args.get('device_id') sub_dev_id = request.args.get('sub_dev_id') - for device in sum(network.values(), []): + for device in network: if device.id == device_id: break else: @@ -174,14 +174,14 @@ def edit(): field = request.args.get('field') value = request.args.get('value') - for device in sum(network.values(), []): + for device in network: if device.id == device_id: break else: return make_error(404, "device_id not found") if sub_dev_id: - for sub_dev in sum(device.sub_devices.values(), []): + for sub_dev in device.sub_devices: if sub_dev.id == sub_dev_id: break else: diff --git a/templates/index.html b/templates/index.html index 0b1efae..912069b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -8,27 +8,23 @@
- {% for dev_type, devices in network.items() %} - {% for device in devices %} -
+ {% for device in network %} +
{{ device.id }}
{{ device.description }}
{{ device.location }}
{{ device.ip_address }}
- {% for sub_dev_type, sub_devs in device.sub_devices.items() %} - {% for sub_dev in sub_devs %} -
+ {% for sub_dev in device.sub_devices %} +
{{ sub_dev.id }}
{{ sub_dev.description }}
{% endfor %} - {% endfor %}
{% endfor %} - {% endfor %}