Rewrite to reconnect handler v2
This commit is contained in:
25
main.py
25
main.py
@@ -1,9 +1,6 @@
|
||||
from fastapi import FastAPI #, HTTPException
|
||||
from fastapi import FastAPI
|
||||
from routers import websocket
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
#from starlette import status
|
||||
#from fastapi.staticfiles import StaticFiles
|
||||
#from models import SemiAutoControl, SemiAutoBatteryStatus
|
||||
|
||||
import asyncio
|
||||
import msgpack
|
||||
@@ -11,7 +8,7 @@ import msgpack
|
||||
#from globalData import node_storage, clients
|
||||
import globalData as gd
|
||||
import globalConfigs as gc
|
||||
from opcuaHandler import initOPCUAConnection, connection_watcher
|
||||
from opcuaHandler import initOPCUAConnection, connection_watcher2
|
||||
|
||||
update_buffer = {}
|
||||
|
||||
@@ -23,6 +20,7 @@ async def data_consumer(queue):
|
||||
update_buffer[nodeid] = val
|
||||
queue.task_done()
|
||||
|
||||
|
||||
async def event_consumer(queue):
|
||||
while True:
|
||||
node_id, event_data = await queue.get()
|
||||
@@ -40,6 +38,7 @@ async def event_consumer(queue):
|
||||
await ws.send_bytes(msgpack.packb({"e": {node_id: event_data}}))
|
||||
queue.task_done()
|
||||
|
||||
|
||||
async def broadcast_deltas(changes: dict):
|
||||
payload = msgpack.packb({"u": changes})
|
||||
for ws, subscribed_tags in list(gd.clients.items()):
|
||||
@@ -47,6 +46,7 @@ async def broadcast_deltas(changes: dict):
|
||||
if relevant:
|
||||
await ws.send_bytes(msgpack.packb({"u": relevant}))
|
||||
|
||||
|
||||
async def broadcast_loop():
|
||||
global update_buffer
|
||||
while True:
|
||||
@@ -68,15 +68,15 @@ async def lifespan(app: FastAPI):
|
||||
"""
|
||||
FastAPI lifespan context — creates the client and starts the watcher.loading="warning"
|
||||
"""
|
||||
await initOPCUAConnection()
|
||||
#await initOPCUAConnection()
|
||||
|
||||
# Launch the background watcher task for the opc ua connection
|
||||
watcher_task = asyncio.create_task(connection_watcher())
|
||||
watcher_task = asyncio.create_task(connection_watcher2())
|
||||
|
||||
# Lunch data consumer task for node value changed events
|
||||
consumer_task = asyncio.create_task(data_consumer(gd.queue))
|
||||
event_consumer_task = asyncio.create_task(event_consumer(gd.event_queue))
|
||||
broadcast_task = asyncio.create_task(broadcast_loop())
|
||||
#consumer_task = asyncio.create_task(data_consumer(gd.queue))
|
||||
#event_consumer_task = asyncio.create_task(event_consumer(gd.event_queue))
|
||||
#broadcast_task = asyncio.create_task(broadcast_loop())
|
||||
|
||||
|
||||
yield # Application runs while this yields
|
||||
@@ -91,6 +91,7 @@ async def lifespan(app: FastAPI):
|
||||
await consumer_task
|
||||
await broadcast_task
|
||||
await event_consumer_task
|
||||
gd.opc_ua_client.disconnect()
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
@@ -99,7 +100,6 @@ app = FastAPI(lifespan=lifespan)
|
||||
origins = [
|
||||
"http://localhost:3000",
|
||||
"http://127.0.0.1:3000",
|
||||
# Fügen Sie hier weitere erforderlich Ursprünge hinzu
|
||||
]
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
@@ -109,7 +109,6 @@ app.add_middleware(
|
||||
allow_headers=["*"], # Erlaubt alle Header
|
||||
)
|
||||
app.include_router(websocket.router)
|
||||
#app.include_router(semiAuto.router)
|
||||
|
||||
# Serve the built frontend
|
||||
#app.mount("/assets", StaticFiles(directory="static/assets"), name="assets")
|
||||
#app.mount("/assets", StaticFiles(directory="static/assets"), name="assets")
|
||||
Reference in New Issue
Block a user