commit 2d584d248a5a1da55b407fcf157232ca1b2f2dcb
parent bfea7d6d3067524fcff70b967a661af492426d36
Author: Friedel Schön <[email protected]>
Date: Fri, 13 May 2022 01:34:37 +0200
DEMO-ready
Diffstat:
11 files changed, 136 insertions(+), 108 deletions(-)
diff --git a/client/config.py b/client/config.py
@@ -3,4 +3,6 @@ WIFI_PASSWORD = "password"
SERVER = "192.168.137.1"
PORT = "5000"
ENDPOINTSTATUS = "/api/update_status"
-ENDPOINTCONNECT = "/api/search_connect"
-\ No newline at end of file
+ENDPOINTCONNECT = "/api/search_connect"
+MAC_ADDRESS = "0000000000000000"
+SLEEP_TIME = 1 # secound
diff --git a/client/main.py b/client/main.py
@@ -1,5 +1,4 @@
-import esp32
-from machine import Pin, deepsleep
+from machine import Pin
from time import sleep
import config
import urequests as requests
@@ -10,8 +9,8 @@ urlconnect = f"http://{config.SERVER}:{config.PORT}{config.ENDPOINTCONNECT}"
led = Pin(23, Pin.OUT)
trap = Pin(15, Pin.IN, Pin.PULL_DOWN)
connectbutton = Pin(4, Pin.IN, Pin.PULL_DOWN)
+
state = False
-mac = "0000000000000000"
#esp32.wake_on_ext1(pins = (button, button2), level = esp32.WAKEUP_ANY_HIGH)
while connection.isconnected():
@@ -19,15 +18,18 @@ while connection.isconnected():
print(trap.value())
new_state = trap.value()
led.value(new_state)
+
if state != new_state:
- response = requests.post(urlstatus, json=new_state)
+ response = requests.post(urlstatus, json={ "mac": config.MAC_ADDRESS, "state": new_state })
answer = response.json()
state = new_state
print(answer)
+
if connectbutton.value():
- response = requests.post(urlconnect, json=mac)
+ response = requests.post(urlconnect, json={ "mac": config.MAC_ADDRESS })
answer = response.json()
print(answer)
- sleep(1)
+
+ sleep(config.SLEEP_TIME)
#deepsleep()
diff --git a/server/routes.py b/server/routes.py
@@ -1,4 +1,3 @@
-from operator import or_
import os
import secrets
@@ -11,34 +10,26 @@ from .forms import LoginForm, RegistrationForm, UpdateAccountForm, UpdateTrapFor
from .models import Trap, User, UserType
@app.route("/api/update_status", methods=['POST', 'GET'])
-def my_function():
- data = request.json
- status = False
- if data:
- if data[0] == "0":
- status = False
- else:
- status = True
- mac = data[1:]
- val = Trap.query.filter_by(mac=mac).first()
- if val:
- val.caught = status
- db.session.commit()
- reaction = "congrats"
- return jsonify(reaction)
+def update_status():
+ if not request.json:
+ return jsonify({ "error": "invalid-json" })
+ val = Trap.query.filter_by(mac=request.json['mac']).first()
+ if not val:
+ return jsonify({ "error": "not-found" })
+ val.caught = request.json['status']
+ db.session.commit()
+ return jsonify({ "error": "ok" })
@app.route("/api/search_connect", methods=['POST', 'GET'])
-def my_function2():
- data = request.json # temperature reading
- if data is None:
- status = "Error"
- elif data:
- if not Trap.query.filter_by(mac=data).first():
- trap = Trap(mac=data, caught=False)
- db.session.add(trap)
- db.session.commit()
- reaction = data
- return jsonify(reaction)
+def search_connect():
+ if not request.json:
+ return jsonify({ "error": "invalid-json" })
+
+ if not Trap.query.filter_by(mac=request.json['mac']).first():
+ trap = Trap(mac=request.json['mac'], caught=False)
+ db.session.add(trap)
+ db.session.commit()
+ return jsonify({ "error": "ok" })
""" index.html (home-page) route """
@app.route("/")
@@ -140,7 +131,7 @@ def account():
image_file = url_for('static', filename='profile_pics/' + current_user.image_file)
return render_template('account.html', title='Profiel', image_file=image_file, form=form)
[email protected]('/dashboard')
+"""@app.route('/dashboard')
@login_required
def dashboard():
query = [ current_user ]
@@ -150,39 +141,40 @@ def dashboard():
traps = [ trap for user in query for trap in Trap.query.filter_by(owner=user.id) ]
return render_template('dashboard.html', title='Dashboard', traps=traps)
+"""
[email protected]('/trap')
[email protected]('/traps')
@login_required
-def trap():
+def traps():
traps = Trap.query.all()
- return render_template('trap.html', traps = traps)
+ return render_template('trap.html', traps=traps)
[email protected]('/trap/<trap_id>', methods=['POST', 'GET'])
[email protected]('/trap/<trap_id>/update', methods=['POST', 'GET'])
@login_required
-def trapform(trap_id):
+def trap_update(trap_id):
form = UpdateTrapForm()
val = Trap.query.filter_by(mac=trap_id).first()
if form.validate_on_submit():
val.name = form.name.data
- email = form.email.data
- if email:
- user = User.query.filter_by(email = email).first()
+ if form.email.data:
+ user = User.query.filter_by(email=form.email.data).first()
val.owner = user.id
db.session.commit()
- return redirect('/trap')
+ return redirect(url_for('traps'))
elif request.method == 'GET':
form.mac.data = val.mac
form.name.data = val.name
#form.email = val.owner
return render_template('updatetrap.html', form=form)
[email protected]('/trap/delete/<trap_id>')
[email protected]('/trap/<trap_id>/delete')
@login_required
-def delete_trap(trap_id):
+def trap_delete(trap_id):
trap = Trap.query.filter_by(mac=trap_id).first()
db.session.delete(trap)
db.session.commit()
- return redirect(url_for('trap'))
+
+ return redirect(url_for('traps'))
""" 404 not found handler """
@app.errorhandler(404)
diff --git a/server/site.db b/server/site.db
Binary files differ.
diff --git a/server/templates/dashboard.html b/server/templates/dashboard.html
@@ -8,7 +8,7 @@
</p>
</div>
</article>
-{% for home, trap in traps %}
+{% for trap in traps %}
<article class="media content-section">
<div class="media-body">
<h3><a class="article-title" href="#">
diff --git a/server/templates/index.html b/server/templates/index.html
@@ -3,7 +3,7 @@
<article class="media content-section">
<div class="media-body">
<h2>Welkom!</h2>
- <a class="ml-2" href="{{ url_for('trap') }}">Bekijk de vallen</a>
+ {#} <a class="ml-2" href="{{ url_for('traps') }}">Bekijk de vallen</a> {#}
</div>
</article>
diff --git a/server/templates/layout.html b/server/templates/layout.html
@@ -80,11 +80,11 @@
<li class="list-group-item list-group-item-light">
<a href="{{ url_for('account') }}">Profielinstellingen</a>
</li>
- {% if current_user.type in [ 'client', 'catcher' ] %}
+ {#} {% if current_user.type in [ 'client', 'catcher' ] %}{#}
<li class="list-group-item list-group-item-light">
- <a href="{{ url_for('dashboard') }}">Uw muizenvallen</a>
+ <a href="{{ url_for('traps') }}">Uw muizenvallen</a>
</li>
- {% endif %}
+ {#}{% endif %}{#}
{% if current_user.type == 'catcher' %}
<li class="list-group-item list-group-item-light">
<a href="#">Uw cliënten</a>
@@ -102,10 +102,10 @@
</p>
{% endif %}
</div>
+ {% with contact = current_user.contact_class() %}
+ {% if contact %}
<div class="content-section">
<h3>Contact</h3>
- {% if current_user.type.name == 'CLIENT' %}
- {% with contact = current_user.contact_class() %}
<h5>
<b>{{ contact.name }}</b>
</h5>
@@ -118,9 +118,9 @@
{{ contact.address | replace('\n', '<br>') }}<br>
{% endautoescape %}
</p>
- {% endwith %}
- {% endif %}
</div>
+ {% endif %}
+ {% endwith %}
</div>
</div>
</main>
diff --git a/server/templates/producten.html b/server/templates/producten.html
@@ -3,67 +3,69 @@
{% block content %}
<article class="media content-section">
-<div class="media-body">
-<h2> Muizenvallen</h2>
+ <div class="media-body">
+ <h2> Muizenvallen</h2>
-<p>Kies hier uit de meerdere artikelen die we ter beschikking hebben.</p>
+ <p>Kies hier uit de meerdere artikelen die we ter beschikking hebben.</p>
-</div>
+ </div>
</article>
<!DOCTYPE html>
<html>
+
<head>
- <style>
- div.gallery {
- margin: 5px;
- border: 1px solid #ccc;
- float: left;
- width: 220px;
- }
-
- div.gallery:hover {
- border: 1.5px solid #777;
- }
-
- div.gallery img {
- width: 100%;
- height: auto;
- }
-
- div.title {
- padding: 5px;
- text-align: center;
- font-size: 20px;
-
- font-weight: bold;
- }
-
- div.desc {
- padding: 7px;
- text-align: center;
- font-size: 12px;
-
- }
- </style>
+ <style>
+ div.gallery {
+ margin: 5px;
+ border: 1px solid #ccc;
+ float: left;
+ width: 220px;
+ }
+
+ div.gallery:hover {
+ border: 1.5px solid #777;
+ }
+
+ div.gallery img {
+ width: 100%;
+ height: auto;
+ }
+
+ div.title {
+ padding: 5px;
+ text-align: center;
+ font-size: 20px;
+
+ font-weight: bold;
+ }
+
+ div.desc {
+ padding: 7px;
+ text-align: center;
+ font-size: 12px;
+
+ }
+ </style>
</head>
<body>
-<div class="gallery">
- <!---- <a target="_blank" href=""> -->
- <img src="../static/product_pics/muizenval1.jpg" alt="Cinque Terre" width="800" height="600">
+ <div class="gallery">
+ <!---- <a target="_blank" href=""> -->
+ <img src="../static/product_pics/muizenval1.jpg" alt="Cinque Terre" width="800" height="600">
</a>
<div class="title">slimme muizenval 3000</div>
<div class="desc">De gekste muizenval ter wereld!! (source: trust me bro)</div>
- </div>
+ </div>
+
-
</body>
- </html>
+
+</html>
{% endblock content %}
\ No newline at end of file
diff --git a/server/templates/trap.html b/server/templates/trap.html
@@ -3,16 +3,41 @@
{% for trap in traps %}
<article class="media content-section">
<div class="media-body">
- <h2><a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{url_for('trapform', trap_id=trap.mac)}}">bewerken</a></h2>
- <h2><a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{url_for('delete_trap', trap_id=trap.mac)}}">verwijderen</a></h2>
- <h3>Naam: {{ trap.name }}</h3>
- <p> Mac adres: {{ trap.mac }} </p>
- {% if trap.caught %}
- <p> Status: Gevangen! </p>
+ <p>
+ <a class="btn btn-secondary btn-sm mt-1 mb-1"
+ href="{{url_for('trap_update', trap_id=trap.mac)}}">Bewerken</a>
+ <a class="btn btn-secondary btn-sm mt-1 mb-1"
+ href="{{url_for('trap_delete', trap_id=trap.mac)}}">Verwijderen</a>
+ </p>
+ <h3><a class="article-title" href="#">
+ <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="{{ trap.status_color() }}"
+ class="bi bi-circle-fill" viewBox="0 0 20 20">
+ <circle cx="10" cy="10" r="10" />
+ </svg>
+ -
+ {% if trap.name %}
+ {{ trap.name }}
{% else %}
- <p>Status: Leeg!</p>
- {% endif %}
+ <code>[{{ trap.pretty_mac() }}]</code>
+ {% endif %}
+ </a>
+ </h3>
+ {% if trap.name %}
+ <p>
+ <code>[{{ trap.pretty_mac() }}]</code>
+ </p>
+ {% endif %}
</div>
+
+ {#} <div class="media-body">
+ <h3>Naam: {{ trap.name }}</h3>
+ <p> Mac adres: {{ trap.mac }} </p>
+ {% if trap.caught %}
+ <p> Status: Gevangen! </p>
+ {% else %}
+ <p>Status: Leeg!</p>
+ {% endif %}
+ </div>{#}
</article>
{% endfor %}
{% endblock content %}
\ No newline at end of file
diff --git a/server/templates/updatetrap.html b/server/templates/updatetrap.html
@@ -4,7 +4,9 @@
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
- <legend class="border-bottom mb-4"><h1>{{ legend }}</h1></legend>
+ <legend class="border-bottom mb-4">
+ <h1>{{ legend }}</h1>
+ </legend>
<div class="form-group">
{{ form.mac.label(class="form-control-label") }}
{% if form.mac.errors %}
diff --git a/test-client.py b/test-client.py
@@ -0,0 +1,4 @@
+import requests
+
+res = requests.post('http://0.0.0.0:5000/api/search_connect', json={ 'mac': '0000000000000000' })
+print(res.content)