-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebclient.py
73 lines (54 loc) · 2.25 KB
/
webclient.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
70
71
72
73
import websockets
import asyncio
import json
import logging
import sys
from datetime import datetime
from typing import Iterator
from fastapi import FastAPI
from fastapi.requests import Request
from fastapi.responses import HTMLResponse, StreamingResponse
from fastapi.templating import Jinja2Templates
from starlette.responses import Response
from time import time
import uvicorn
from write_to_scv import write_to_scv
recording_period = 0.1 # период записи данных в .csv
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger(__name__)
application = FastAPI()
templates = Jinja2Templates(directory="templates")
@application.get("/", response_class=HTMLResponse)
async def index(request: Request) -> Response:
return templates.TemplateResponse("index.html", {"request": request})
async def generate_random_data(request: Request) -> Iterator[str]:
client_ip = request.client.host
logger.info("Client %s connected", client_ip)
fixed_time = time()
async with websockets.connect("ws://192.168.1.10", ping_interval=None) as websocket:
while True:
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
value = await websocket.recv()
shock_sensor = value[0]
voice_sensor = value[1:]
json_data = json.dumps(
{
"time": date,
"shock_sensor": shock_sensor,
"voice_sensor": voice_sensor,
}
)
yield f"data:{json_data}\n\n"
current_time = time()
if current_time - fixed_time > recording_period:
#write_to_scv(date, voice_sensor)
fixed_time = time()
await asyncio.sleep(0.0005)
@application.get("/chart-data")
async def chart_data(request: Request) -> StreamingResponse:
response = StreamingResponse(generate_random_data(request), media_type="text/event-stream")
response.headers["Cache-Control"] = "no-cache"
response.headers["X-Accel-Buffering"] = "no"
return response
if __name__=="__main__":
uvicorn.run("webclient:application", port=80)