Compare commits
No commits in common. "ded3593a2fc475a702c2b53b53a86dfa3b336518" and "a95b90002e766a2b596ac3036a9f569225f90a42" have entirely different histories.
ded3593a2f
...
a95b90002e
40
juice.py
40
juice.py
|
@ -28,14 +28,6 @@ class RelayDevice:
|
||||||
self.location = ""
|
self.location = ""
|
||||||
self.ip_address = ""
|
self.ip_address = ""
|
||||||
self.sub_devices = []
|
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()
|
self.update()
|
||||||
|
|
||||||
|
@ -44,8 +36,6 @@ class RelayDevice:
|
||||||
Queries the physical device and updates the internal model as
|
Queries the physical device and updates the internal model as
|
||||||
appropriate.
|
appropriate.
|
||||||
"""
|
"""
|
||||||
if not self.ip_address:
|
|
||||||
return
|
|
||||||
for sub_dev in self.sub_devices:
|
for sub_dev in self.sub_devices:
|
||||||
res = requests.get(self.ip_address)
|
res = requests.get(self.ip_address)
|
||||||
gpio0 = re.search(r"GPIO0: (\bLow|\bHigh)", res.text).groups()[0]
|
gpio0 = re.search(r"GPIO0: (\bLow|\bHigh)", res.text).groups()[0]
|
||||||
|
@ -221,36 +211,6 @@ def edit():
|
||||||
return json.dumps(data)
|
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):
|
def make_error(code, message):
|
||||||
"""
|
"""
|
||||||
Returns a JSON error.
|
Returns a JSON error.
|
||||||
|
|
Binary file not shown.
|
@ -4,33 +4,9 @@ body {
|
||||||
margin: 0;
|
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 {
|
#devices {
|
||||||
padding: 5%;
|
padding: 5%;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-wrap: wrap;
|
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,6 +53,7 @@ nav span:hover {
|
||||||
}
|
}
|
||||||
|
|
||||||
.edit, .save {
|
.edit, .save {
|
||||||
|
font-family: FontAwesome;
|
||||||
font-size: 0.8em;
|
font-size: 0.8em;
|
||||||
color: dimgrey;
|
color: dimgrey;
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ function edit_field(field) {
|
||||||
|
|
||||||
let save = document.createElement('span');
|
let save = document.createElement('span');
|
||||||
save.innerHTML = '';
|
save.innerHTML = '';
|
||||||
save.className = 'save font-awesome';
|
save.className = 'save';
|
||||||
save.setAttribute('onclick', 'save_field(this.parentElement)');
|
save.setAttribute('onclick', 'save_field(this.parentElement)');
|
||||||
field.children[1].replaceWith(save);
|
field.children[1].replaceWith(save);
|
||||||
}
|
}
|
||||||
|
@ -93,25 +93,8 @@ function save_field(field) {
|
||||||
|
|
||||||
let edit = document.createElement('span');
|
let edit = document.createElement('span');
|
||||||
edit.innerHTML = '';
|
edit.innerHTML = '';
|
||||||
edit.className = 'edit font-awesome';
|
edit.className = 'edit';
|
||||||
edit.setAttribute('onclick', 'edit_field(this.parentElement)');
|
edit.setAttribute('onclick', 'edit_field(this.parentElement)');
|
||||||
field.children[1].replaceWith(edit);
|
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
|
@ -8,26 +8,20 @@
|
||||||
<script>window.onload = load;</script>
|
<script>window.onload = load;</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<header>
|
|
||||||
<nav>
|
|
||||||
<span onclick="new_device()" title="Add new device"><span class="font-awesome"></span></span>
|
|
||||||
<span title="Register a new authenticator">Register</span>
|
|
||||||
</nav>
|
|
||||||
</header>
|
|
||||||
<main>
|
<main>
|
||||||
<div id="devices">
|
<div id="devices">
|
||||||
{% for device in network %}
|
{% for device in network %}
|
||||||
<div class="device {{ device.type }}" id="{{ device.id }}">
|
<div class="device {{ device.type }}" id="{{ device.id }}">
|
||||||
<div class="id">{{ device.id }}</div>
|
<div class="id">{{ device.id }}</div>
|
||||||
<div class="description"><span class="field_value">{{ device.description }}</span> <span class="edit font-awesome" onclick="edit_field(this.parentElement)"></span></div>
|
<div class="description"><span class="field_value">{{ device.description }}</span> <span class="edit" onclick="edit_field(this.parentElement)"></span></div>
|
||||||
<div class="location"><span class="field_value">{{ device.location }}</span> <span class="edit font-awesome" onclick="edit_field(this.parentElement)"></span></div>
|
<div class="location"><span class="field_value">{{ device.location }}</span> <span class="edit" onclick="edit_field(this.parentElement)"></span></div>
|
||||||
<div class="ip_address"><i>{{ device.ip_address }}</i></div>
|
<div class="ip_address"><i>{{ device.ip_address }}</i></div>
|
||||||
<div class="sub_devices">
|
<div class="sub_devices">
|
||||||
{% for sub_dev in device.sub_devices %}
|
{% for sub_dev in device.sub_devices %}
|
||||||
<div class="sub_device {{ sub_dev.type }}" id="{{ sub_dev.id }}">
|
<div class="sub_device {{ sub_dev.type }}" id="{{ sub_dev.id }}">
|
||||||
<div class="id">{{ sub_dev.id }}</div>
|
<div class="id">{{ sub_dev.id }}</div>
|
||||||
<object class="outlet_image" data="/static/outlet.svg"></object>
|
<object class="outlet_image" data="/static/outlet.svg"></object>
|
||||||
<div class="description"><span class="field_value">{{ sub_dev.description }}</span> <span class="edit font-awesome" onclick="edit_field(this.parentElement)"></span></div>
|
<div class="description"><span class="field_value">{{ sub_dev.description }}</span> <span class="edit" onclick="edit_field(this.parentElement)"></span></div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user