muizenval

Observe mouse traps remotely
Log | Files | Refs

test-server.py (2523B)


      1 from flask import json, jsonify
      2 from werkzeug.exceptions import HTTPException
      3 from flask import Flask, request
      4 
      5 
      6 class Status:
      7     trap = False
      8     latitude = 0.0
      9     longitude = 0.0
     10     accuracy = 0.0
     11     satellites = 0
     12     battery = 0
     13     temperature = 0
     14     charging = False
     15 
     16     def update(self, req: dict):
     17         self.trap = req['trap']
     18         self.battery = req['battery']
     19         self.temperature = req['temperature']
     20         self.charging = req['charging']
     21         self.latitude = req['latitude']
     22         self.longitude = req['longitude']
     23         self.accuracy = req['accuracy']
     24         self.satellites = req['satellites']
     25 
     26 
     27 app = Flask(__name__)
     28 
     29 status = Status()
     30 
     31 
     32 @app.post("/api/update")
     33 def update():
     34     global status
     35 
     36     req = request.get_json(True)
     37     if req:
     38         status.update(req)
     39 
     40     return jsonify({})
     41 
     42 
     43 @app.post('/api/hello')
     44 def hello():
     45 
     46     return jsonify({})
     47 
     48 
     49 @app.get("/")
     50 def index():
     51     return f'''
     52 	<link 
     53 		rel="stylesheet" 
     54 		href="https://unpkg.com/[email protected]/dist/leaflet.css" 
     55 		integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ==" 
     56 		crossorigin="" />
     57 	<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
     58 		integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
     59 		crossorigin=""></script>
     60 
     61 	<h1>Status update</h1>
     62 	<p>trap: <code>{'yes' if status.trap else 'no'}</code></p>
     63 	<p>latitude: <code>{status.latitude:.10f}</code></p>
     64 	<p>longitude: <code>{status.longitude:.10f}</code></p>
     65 	<p>accuracy: <code>{status.accuracy:.1f}%</code></p>
     66 	<p>satellites: <code>{status.satellites}</code></p>
     67 	<p>battery: <code>{status.battery}V</code></p>
     68 	<p>temperature: <code>{status.temperature}&deg;c</code></p>
     69 	<p>charging: <code>{'yes' if status.charging else 'no'}</code></p>
     70 
     71 	<div id="map" style='height: 50%;'></div>
     72 
     73 	<script type="text/javascript">
     74 	var map = L.map('map').setView([52.283333, 5.666667], 7);
     75 	L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
     76 		maxZoom: 19,
     77 		attribution: '© OpenStreetMap'
     78 	}}).addTo(map);
     79 	var marker = L.marker([{status.latitude}, {status.longitude}]).addTo(map);
     80 	</script>
     81 	'''
     82 
     83 
     84 @app.errorhandler(HTTPException)
     85 def handle_exception(e):
     86     response = e.get_response()
     87     response.data = json.dumps({
     88         "code": e.code,
     89         "name": e.name,
     90         "description": e.description,
     91     })
     92     response.content_type = "application/json"
     93     return response
     94 
     95 
     96 app.run('0.0.0.0', 5000)