begin strip light

This commit is contained in:
iou1name 2019-07-02 13:09:31 -04:00
parent 2a628908bb
commit 921163f454
4 changed files with 70 additions and 1 deletions

View File

@ -102,6 +102,35 @@ class RelayOutlet:
return self return self
class LightStrip:
"""
Represents an RGB LED light strip controller.
"""
def __init__(self):
self.id = ""
self.type = "LightStrip"
self.description = ""
self.location = ""
self.ip_address = ""
self.locked = False
def from_dict(self, data):
"""
Initializes self with data from JSON dict.
"""
for key, value in data.items():
setattr(self, key, value)
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:
typ = sub_dev.get('type')
raise ValueError(f"Unknown sub_device type {typ}")
return self
class DeviceEncoder(json.JSONEncoder): class DeviceEncoder(json.JSONEncoder):
""" """
A custom json encoder for dumping devices to file. A custom json encoder for dumping devices to file.
@ -123,7 +152,8 @@ def init_network(filepath="devices.json"):
device = RelayDevice().from_dict(device_dict) device = RelayDevice().from_dict(device_dict)
network.append(device) network.append(device)
elif device_dict.get('type') == 'LightStrip': elif device_dict.get('type') == 'LightStrip':
continue device = LightStrip().from_dict(device_dict)
network.append(device)
else: else:
raise ValueError(f"Unknown device type {device_dict.get('type')}") raise ValueError(f"Unknown device type {device_dict.get('type')}")
return network return network

View File

@ -105,6 +105,29 @@ nav span:hover {
height: 5em; height: 5em;
} }
.LightStrip .sub_devices {
flex-wrap: wrap;
}
.led {
padding: 0.25em;
margin: 0.125em;
}
.led_color {
border-radius: 0.25em;
width: 1em;
height: 1em;
}
.led_color_input {
opacity: 0;
display: block;
width: 1em;
height: 1em;
border: 0;
}
@font-face { @font-face {
font-family: FontAwesome; font-family: FontAwesome;
src: url("/static/fontawesome-webfont.woff2"); src: url("/static/fontawesome-webfont.woff2");

View File

@ -10,6 +10,12 @@ function load() {
} }
}); });
}); });
document.querySelectorAll('input[type=color]').forEach((led_input) => {
led_input.onchange = function(event) {
event.target.parentElement.style.backgroundColor = event.target.value;
}
});
} }
function get_object_from_svg(svg) { function get_object_from_svg(svg) {

View File

@ -36,6 +36,7 @@
<div class="location editable"><span class="field_value">{{ device.location }}</span>{% if not device.locked %}<span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span>{% endif %}</div> <div class="location editable"><span class="field_value">{{ device.location }}</span>{% if not device.locked %}<span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span>{% endif %}</div>
<div class="ip_address editable"><span class="field_value">{{ device.ip_address }}</span>{% if not device.locked %}<span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span>{% endif %}</div> <div class="ip_address editable"><span class="field_value">{{ device.ip_address }}</span>{% if not device.locked %}<span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span>{% endif %}</div>
<div class="sub_devices"> <div class="sub_devices">
{% if device.type == 'RelayDevice' %}
{% for sub_dev in device.sub_devices %} {% for sub_dev in device.sub_devices %}
<div class="sub_device {{ sub_dev.type }} {{ sub_dev.id }}"> <div class="sub_device {{ sub_dev.type }} {{ sub_dev.id }}">
<div class="id">{{ sub_dev.id }}</div> <div class="id">{{ sub_dev.id }}</div>
@ -43,6 +44,15 @@
<div class="description editable"><span class="field_value">{{ sub_dev.description }}</span>{% if not device.locked %}<span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span>{% endif %}</div> <div class="description editable"><span class="field_value">{{ sub_dev.description }}</span>{% if not device.locked %}<span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span>{% endif %}</div>
</div> </div>
{% endfor %} {% endfor %}
{% elif device.type == 'LightStrip' %}
{% for n in range(48) %}
<div class="sub_device led led{{n}}">
<label class="led_color">
<input class="led_color_input" type="color">
</label>
</div>
{% endfor %}
{% endif %}
</div> </div>
</div> </div>
{% endfor %} {% endfor %}