Compare commits

...

3 commits

Author SHA1 Message Date
mia
3a14a55ede added basic webserver 2025-05-21 11:48:26 +02:00
mia
f42da30372 Merge branch 'main' of https://git.miaig.dev/mia/abschlussarbeit 2025-05-21 11:47:39 +02:00
mia
7112d06e88 added basic webserver 2025-05-21 11:47:27 +02:00
3 changed files with 88 additions and 0 deletions

BIN
htmlHelp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 701 KiB

35
webserver/data.json Normal file
View file

@ -0,0 +1,35 @@
{
"location": "Building A - Lab 3",
"sensors": [
{
"id": "sensor_001",
"type": "temperature",
"unit": "C",
"readings": [
{ "ts": 1747814400, "value": 22.5 },
{ "ts": 1747818000, "value": 23.0 },
{ "ts": 1747821600, "value": 23.7 }
]
},
{
"id": "sensor_002",
"type": "humidity",
"unit": "%",
"readings": [
{ "ts": 1747814400, "value": 45.2 },
{ "ts": 1747818000, "value": 47.1 },
{ "ts": 1747821600, "value": 46.8 }
]
},
{
"id": "sensor_003",
"type": "pressure",
"unit": "hPa",
"readings": [
{ "ts": 1747814400, "value": 1012.4 },
{ "ts": 1747818000, "value": 1012.8 },
{ "ts": 1747821600, "value": 1013.0 }
]
}
]
}

53
webserver/server.py Normal file
View file

@ -0,0 +1,53 @@
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
# Opening JSON file
f = open("data.json")
# returns JSON object as a dictionary
data = json.load(f)
sensors = dict()
sensorid = None
sensortype = None
sensorunit = None
sensorreadings = None
readings = None
# Iterating through the json list
for i in data["sensors"]:
sensorid = i["id"]
sensors[sensorid] = [i["type"], i["unit"], i["readings"]]
# if i["type"] == "temperature":
# readings = i["readings"]
# Closing file
f.close()
html = ""
for i in sensors:
type = sensors[i][0]
unit = sensors[i][1]
readings = f"{sensors[i][2][0]["ts"]}: {sensors[i][2][0]["value"]} {unit}"
html += f"""
<p>Id: {i}</p>
<p>Type: {type}</p>
<p>Readings: {readings}</p>
<p>---------------------------------------------</p>
"""
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(html.encode("utf-8"))
if __name__ == "__main__":
server_address = ("", 8000)
httpd = HTTPServer(server_address, MyHandler)
print("Serving on http://localhost:8000")
httpd.serve_forever()