Added device toggle

This commit is contained in:
Filip Znachor 2022-12-20 17:51:27 +01:00
parent 2f982dd29a
commit 3fce82e971
5 changed files with 31 additions and 6 deletions

View file

@ -79,6 +79,15 @@ class API:
d.set_stop(request.forms.get('stop_id'))
return {'success': True}
@route('/admin/devices/toggle', method='POST')
def admin_devices_toggle():
if main.config["admin"]["secret"] != request.forms.get('secret'):
return {'error': "unauthorized"}
for i, d in enumerate(main.controller.devices):
if str(i) == request.forms.get('index'):
d.toggle(request.forms.get('state') == "true")
return {'success': True}
@error(404)
def error404(err):
return ''

View file

@ -34,21 +34,22 @@ class LoraController:
data = requests.post(url, verify=False, headers=headers, data=json.dumps(data)).json()
self.token = data["token"]
def new(self, id: int, stop_id: str):
self.devices.append(LoraDevice(self, id, stop_id))
def new(self, id: int, stop_id: str, enabled: bool):
self.devices.append(LoraDevice(self, id, stop_id, enabled))
def json(self):
resp = []
for d in self.devices:
resp.append({'id': f"{d.id:0>16x}", 'stop_id': d.stop_id})
resp.append({'id': f"{d.id:0>16x}", 'stop_id': d.stop_id, 'enabled': d.enabled})
return resp
class LoraDevice:
def __init__(self, controller: LoraController, deveui: int, stop_id: str):
def __init__(self, controller: LoraController, deveui: int, stop_id: str, enabled: bool):
self.controller = controller
self.id = deveui
self.stop_id = stop_id
self.enabled = enabled
self.message_pool = []
self.thread = None
self.port = 1
@ -59,6 +60,8 @@ class LoraDevice:
self.stop_id = stop_id
def get_updated_departures(self):
if not self.enabled:
return
self.send_time()
for d in Departure.get(self.stop_id):
if self.id not in d.updated or d.updated[self.id] > 0:
@ -67,6 +70,11 @@ class LoraDevice:
def clear(self):
self.send(lambda id: "CLEAR")
def toggle(self, state: bool):
self.enabled = state
if not state:
self.message_pool = []
def send_time(self):
self.send(lambda id: f"TIME|{(datetime.now() - datetime(1970, 1, 1)).total_seconds():.0f}")

View file

@ -26,7 +26,9 @@ class Main:
lora_controller.generate_token()
for d in self.config["devices"]:
if "stop_id" in d:
lora_controller.new(d["id"], str(d["stop_id"]))
if "enabled" not in d:
d["enabled"] = True
lora_controller.new(d["id"], str(d["stop_id"]), d["enabled"])
self.thread = threading.Thread(target=self.update_loop)
self.thread.start()

View file

@ -20,8 +20,10 @@
<div class="settings" :class="{ visible: d.visible }">
<div class="actions">
<button @click="device_clear(i)">Vymazat</button>
<button @click="device_resend(i)">Přeposlat</button>
<button @click="device_resend(i)" v-if="d.enabled">Přeposlat</button>
<button @click="device_change_stop(i)">Změnit zastávku</button>
<button @click="device_toggle(i, false)" v-if="d.enabled">Deaktivovat</button>
<button @click="device_toggle(i, true)" v-else>Aktivovat</button>
</div>
</div>
</div>

View file

@ -29,6 +29,10 @@ let app = createApp({
async device_resend(index) {
await api("/admin/devices/resend", {index, secret: this.secret});
},
async device_toggle(index, state) {
this.devices[index].enabled = state;
await api("/admin/devices/toggle", {index, state, secret: this.secret});
},
async device_change_stop(index) {
let stop_id = prompt("Zadejte ID zastávky:");
if(!stop_id || stop_id.trim() == "") return;