Compare commits

...

2 Commits

Author SHA1 Message Date
ded3593a2f added new_devices() 2019-06-20 07:49:29 -04:00
1f0078446d added nav bar 2019-06-20 07:48:11 -04:00
5 changed files with 92 additions and 6 deletions

View File

@ -28,6 +28,14 @@ class RelayDevice:
self.location = ""
self.ip_address = ""
self.sub_devices = []
self.sub_devices.append(RelayOutlet())
self.sub_devices.append(RelayOutlet())
self.sub_devices[0].id = 'GPIO0'
self.sub_devices[0].type = 'RelayOutlet'
self.sub_devices[0].gpio = '0'
self.sub_devices[1].id = 'GPIO2'
self.sub_devices[1].type = 'RelayOutlet'
self.sub_devices[1].gpio = '2'
self.update()
@ -36,6 +44,8 @@ class RelayDevice:
Queries the physical device and updates the internal model as
appropriate.
"""
if not self.ip_address:
return
for sub_dev in self.sub_devices:
res = requests.get(self.ip_address)
gpio0 = re.search(r"GPIO0: (\bLow|\bHigh)", res.text).groups()[0]
@ -211,6 +221,36 @@ def edit():
return json.dumps(data)
@app_views.route('/new_device')
def new_device():
"""
Allows adding a new device. Accepts device_type parameter, returns
the device_id.
"""
device_type = request.args.get('device_type')
if device_type == 'RelayDevice':
device = RelayDevice()
else:
return make_error(400, "Unknown device type")
devices = [dev for dev in network if dev.type == device_type]
devices.sort(key=lambda dev: dev.type)
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
network.append(device)
save_network()
data = {'device_id': device.id}
return json.dumps(data)
def make_error(code, message):
"""
Returns a JSON error.

Binary file not shown.

View File

@ -4,9 +4,33 @@ body {
margin: 0;
}
.font-awesome {
font-family: FontAwesome;
}
nav {
background-color: whitesmoke;
border-bottom: 1px solid darkgray;
display: flex;
}
nav span {
color: dimgrey;
cursor: pointer;
transition: 0.1s;
padding: 0.5em;
margin-left: 0.5em;
margin-right: 0.5em;
}
nav span:hover {
background-color: lightgray;
}
#devices {
padding: 5%;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
@ -53,7 +77,6 @@ body {
}
.edit, .save {
font-family: FontAwesome;
font-size: 0.8em;
color: dimgrey;
}

View File

@ -55,7 +55,7 @@ function edit_field(field) {
let save = document.createElement('span');
save.innerHTML = '';
save.className = 'save';
save.className = 'save font-awesome';
save.setAttribute('onclick', 'save_field(this.parentElement)');
field.children[1].replaceWith(save);
}
@ -93,8 +93,25 @@ function save_field(field) {
let edit = document.createElement('span');
edit.innerHTML = '';
edit.className = 'edit';
edit.className = 'edit font-awesome';
edit.setAttribute('onclick', 'edit_field(this.parentElement)');
field.children[1].replaceWith(edit);
});
}
function new_device() {
let params = {
device_type: 'RelayDevice',
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
fetch(window.location.href + 'new_device?' + query)
.then(function(response) {
return response.json();
})
.then(function(json) {
console.log(json);
});
}

View File

@ -8,20 +8,26 @@
<script>window.onload = load;</script>
</head>
<body>
<header>
<nav>
<span onclick="new_device()" title="Add new device"><span class="font-awesome">&#xe802;</span></span>
<span title="Register a new authenticator">Register</span>
</nav>
</header>
<main>
<div id="devices">
{% for device in network %}
<div class="device {{ device.type }}" id="{{ device.id }}">
<div class="id">{{ device.id }}</div>
<div class="description"><span class="field_value">{{ device.description }}</span> <span class="edit" onclick="edit_field(this.parentElement)">&#xe800;</span></div>
<div class="location"><span class="field_value">{{ device.location }}</span> <span class="edit" onclick="edit_field(this.parentElement)">&#xe800;</span></div>
<div class="description"><span class="field_value">{{ device.description }}</span> <span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span></div>
<div class="location"><span class="field_value">{{ device.location }}</span> <span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span></div>
<div class="ip_address"><i>{{ device.ip_address }}</i></div>
<div class="sub_devices">
{% for sub_dev in device.sub_devices %}
<div class="sub_device {{ sub_dev.type }}" id="{{ sub_dev.id }}">
<div class="id">{{ sub_dev.id }}</div>
<object class="outlet_image" data="/static/outlet.svg"></object>
<div class="description"><span class="field_value">{{ sub_dev.description }}</span> <span class="edit" onclick="edit_field(this.parentElement)">&#xe800;</span></div>
<div class="description"><span class="field_value">{{ sub_dev.description }}</span> <span class="edit font-awesome" onclick="edit_field(this.parentElement)">&#xe800;</span></div>
</div>
{% endfor %}
</div>