This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWS-web_livedataProvider.py
69 lines (60 loc) · 1.63 KB
/
WS-web_livedataProvider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python3
#
# written by @author ZyzonixDev
# published by ZyzonixDevelopments
# -
# date | 20/06/2021
# python-v | 3.7.3
# -
# file | WS-web_livedataProvider.py
# project | rpi-weatherstation
# project-v | 0.9.0
#
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from datetime import datetime, date
import station.air_stats as ais
# creates webserverinstance
app = FastAPI()
# webserverconfig
app.add_middleware(
CORSMiddleware,
allow_origins="*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# timeservice for consoleoutput
def getTime():
curTime = "[" + str(datetime.now().strftime("%H:%M:%S")) + "]"
return curTime
# subpage/fastapi register for data request
@app.get("/")
async def datarange():
print(getTime(), "answering data request")
status = 1000
try:
temperature = ais.getTemperature()
except Exception as e:
print(e)
status += 1
try:
humidity = ais.getHumidity()
except Exception as e:
print(e)
status += 10
try:
pressure = ais.getPressure()
except Exception as e:
status += 100
timestamp = str(date.today()) + "|" + str(datetime.now().strftime("%H:%M:%S"))
toJSON = [{"status": 1000, "data":[timestamp, temperature, humidity, pressure]}]
return toJSON
# initialize webserver --> port 8000 --> runnning on localhost --> eth0/wlan0 ipv4
def serverInit():
uvicorn.run(app, port=8001, host='0.0.0.0')
# initialize script
if __name__ == "__main__":
print(getTime(), "started coreServer-App")
serverInit()