muizenval

Observe mouse traps remotely
Log | Files | Refs

commit fa8912be0dff48b4f857121f97ee7c288bc8ad2e
parent 21cd760209ad2f34a7423bdf962a36cedc1afb0d
Author: Friedel Schon <[email protected]>
Date:   Fri,  3 Jun 2022 10:45:39 +0200

simplified connect-process

Diffstat:
M.vscode/c_cpp_properties.json | 24++++++++++++------------
Mserver/forms.py | 2+-
Mserver/models.py | 1+
Mserver/routes.py | 16++++++++++++----
Mserver/site.db | 0
Mtest-client.py | 4++--
6 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json @@ -3,7 +3,7 @@ "configurations": [ { "name": "Arduino", - "compilerPath": "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-g++", + "compilerPath": "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/bin/arm-none-eabi-g++", "compilerArgs": [ "-mcpu=cortex-m0plus", "-mthumb", @@ -19,19 +19,19 @@ ], "intelliSenseMode": "gcc-x64", "includePath": [ - "/Users/friedel/Library/Arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/", - "/Users/friedel/Library/Arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL/", - "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino", - "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/variants/sodaq_sara", - "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1", - "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/arm-none-eabi", - "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/backward", - "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include", - "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include-fixed", - "/Users/friedel/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include" + "/home/friedel/.arduino15/packages/arduino/tools/CMSIS/4.5.0/CMSIS/Include/", + "/home/friedel/.arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL/", + "/home/friedel/.arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino", + "/home/friedel/.arduino15/packages/SODAQ/hardware/samd/1.8.9/variants/sodaq_sara", + "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1", + "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/arm-none-eabi", + "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/c++/7.2.1/backward", + "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include", + "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/lib/gcc/arm-none-eabi/7.2.1/include-fixed", + "/home/friedel/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include" ], "forcedInclude": [ - "/Users/friedel/Library/Arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino/Arduino.h" + "/home/friedel/.arduino15/packages/SODAQ/hardware/samd/1.8.9/cores/arduino/Arduino.h" ], "cStandard": "c11", "cppStandard": "c++11", diff --git a/server/forms.py b/server/forms.py @@ -74,7 +74,7 @@ class UpdateTrapForm(FlaskForm): submit = SubmitField('Bewerken') class ConnectTrapForm(FlaskForm): - mac = StringField('MAC', validators=[ Length(min=16, max=16) ]) + code = StringField('Koppel-Code', validators=[ Length(min=16, max=16) ]) submit = SubmitField('Verbinden') diff --git a/server/models.py b/server/models.py @@ -37,6 +37,7 @@ class Trap(db.Model): caught = db.Column(db.Boolean, nullable=False, default=False) owner = db.Column(db.Integer, db.ForeignKey('user.id')) connect_expired = db.Column(db.DateTime) + connect_code = db.Column(db.String(5)) location_lat = db.Column(db.Float) location_lon = db.Column(db.Float) diff --git a/server/routes.py b/server/routes.py @@ -1,4 +1,4 @@ -import json +import random import os import secrets from datetime import datetime, timedelta @@ -49,14 +49,21 @@ def search_connect(): return jsonify({ "error": "invalid-mac" }) mac = request.json['mac'].lower() - + trap = Trap.query.filter_by(mac=mac).first() if not trap: trap = Trap(mac=mac) db.session.add(trap) + code = "" + while True: + code = ''.join([ random.choice('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') for _ in range(5) ]) + if not Trap.query.filter_by(connect_code=code).first(): + break + trap.owner = None trap.connect_expired = datetime.utcnow() + timedelta(minutes=5) + trap.connect_code = code db.session.commit() @@ -183,14 +190,15 @@ def traps(): @login_required def trap_connect(): form = ConnectTrapForm() - if form.validate_on_submit() and form.mac.data: - trap = Trap.query.filter_by(mac=form.mac.data.replace(':', '').replace(' ', '').lower()).filter(Trap.connect_expired > datetime.utcnow()).first() + if form.validate_on_submit() and form.code.data: + trap = Trap.query.filter_by(mac=form.code.data.replace(':', '').replace(' ', '').lower()).filter(Trap.connect_expired > datetime.utcnow()).first() if not trap: flash('Muizenval niet gevonden', 'danger') return redirect(url_for('trap_connect')) trap.owner = current_user.id trap.connect_expired = None + trap.connect_code = None db.session.commit() flash('Muizenval toegevoegd!', 'success') return redirect(url_for('traps')) diff --git a/server/site.db b/server/site.db Binary files differ. diff --git a/test-client.py b/test-client.py @@ -28,8 +28,8 @@ if opt.connect: res = requests.post(f'http://{host}:{port}/api/search_connect', json={ 'mac': mac }) print('->', res.json()['error']) elif opt.status == 'idle': - res = requests.post('http://{host}:{port}/api/update_status', json={ 'mac': mac, 'status': False }) + res = requests.post(f'http://{host}:{port}/api/update_status', json={ 'mac': mac, 'status': False }) print('->', res.json()['error']) else: - res = requests.post('http://{host}:{port}/api/update_status', json={ 'mac': mac, 'status': True }) + res = requests.post(f'http://{host}:{port}/api/update_status', json={ 'mac': mac, 'status': True }) print('->', res.json()['error'])