收心,好好工作

部署运行你感兴趣的模型镜像

  昨天收到了同学给我寄来的毕业证和学位证,从现在开始才算是一个真正工作的人,在两个月前也就是现在的工作之前,老爱一位的追求那些新技术,学习过一段WCF和实体框架。但是进了这家公司之后,发现公司用的全是.NET2.0的东西,在公司里必须要精通3个工具:VS,SQL Server,Crystal Reports 。由于公司是给银行开发系统的,对那些流行的东西不感冒,所以要下定决心,要在一年内了解系统的核心代码,不再人云亦云的胡乱学习。好好学习,天天向上!!!

您可能感兴趣的与本文相关的镜像

Yolo-v8.3

Yolo-v8.3

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

import asyncio import json import time import websockets from websockets.exceptions import ConnectionClosed from communication_config import * from datetime import datetime up_ws = None # 唯一的上位机连接 async def process_heartbeat(msg: str): ''' 处理下位机心跳报文 更新车辆和相机状态以及记录更新时间 ''' data = json.loads(msg) sn = data.get("sn") camera_list = data.get("cameras", []) if sn not in CAR_STATUS_MAP: print(f"未知车辆: {sn}") return car_info = CAR_STATUS_MAP[sn] car_info["last_heartbeat"] = time.time() for cam in camera_list: cid = cam["camera_id"] status = cam.get("camera_status", "unknown") if cid in car_info["cameras"]: car_info["cameras"][cid]["camera_status"] = status all_ready = all( cam["camera_status"] == "ready" for cam in car_info["cameras"].values() ) car_info["car_status"] = "online" if all_ready else "partial" async def forward_command_to_car(cmd_msg: dict, up_ws): ''' 转发指令(上位机→车辆) ''' sn = cmd_msg.get("sn") car = CAR_STATUS_MAP.get(sn) resp = { "type": "response", "sn": sn, "status": "", "message": "" } if not car or car["ws"] is None: resp["status"] = "error" resp["message"] = f"car {sn} not found or offline" await up_ws.send(json.dumps(resp)) # 未找到车辆异常 响应上位机 return False cmd_msg["type"] = "car_command" # 修改给下位机的topic try: await car["ws"].send(json.dumps(cmd_msg)) # 通过设备的ws异步发送至下位机 resp["status"] = "success" resp["message"] = "command received and executed" await up_ws.send(json.dumps(resp)) # 正常 响应上位机 return True except Exception as e: resp["status"] = "error" resp["message"] = str(e) await up_ws.send(json.dumps(resp)) # 异常 响应上位机 car["ws"] = None return False async def process_command(msg: str, ws): ''' 处理上位机指令 TODO 下发指令 问下位机指定车辆的相机要图 并持续返回图片or直接返回视频流rstp ''' print("-------------------", msg) data = json.loads(msg) if data.get("type") == "command": await forward_command_to_car(data, ws) # pass async def down_handler(ws): ''' -------- 下行:心跳 & 图片 -------- ''' sn = None # 记录这条连接归属哪辆车 try: async for msg in ws: # print("======", msg) data = json.loads(msg) # 第一次收到任何包都先绑定 sn if sn is None: sn = data.get("sn") if not sn or sn not in CAR_STATUS_MAP: await ws.close(code=1002, reason="missing or unknown sn") return CAR_STATUS_MAP[sn]["ws"] = ws print(f"[S] 车辆 {sn} 绑定 websocket") # 按类型分发 if data.get("type") == "heartbeat": # 收心跳 await process_heartbeat(msg) elif data.get("type") == "car_response": # 转发指令后 收到下位机的返回报文 print("====== yesyes =======") elif data.get("type") == "car_data": # 收到图片数据 # 增加和状态的校验 car_sn = data.get("sn") cam_id = data.get("camera_id") # # 先查状态 # car_info = CAR_STATUS_MAP.get(car_sn) # if (car_info and # car_info["cameras"].get(cam_id, {}).get("camera_status") == "ready"): # data["type"] = "data" # if up_ws: # await up_ws.send(json.dumps(data)) # else: # # 可选:静默丢弃 or 打印 # current_time = datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S") # print(f"{current_time} [DROP] {car_sn}-Cam{cam_id} not ready") # 直接转发 if up_ws: data["type"] = "data" await up_ws.send(json.dumps(data)) # await process_image(msg) # 后续对图像做进一步处理 # 其他类型继续扩展... except ConnectionClosed: print(f"[S] 车辆 {sn} 断开连接") pass finally: print("======== Nothing ========") # if sn: # 连接断,清绑定 # CAR_STATUS_MAP[sn]["ws"] = None # print(f"[S] 车辆 {sn} 解绑 websocket") async def up_handler(ws): ''' -------- 上行:指令 -------- ''' global up_ws # 全局变量 up_ws = ws print("[S] 上位机已连接") try: async for msg in ws: data = json.loads(msg) if data["type"] == "command": await forward_command_to_car(data, ws) # 复用你现有函数 finally: up_ws = None print("[S] 上位机断开") async def ws_router(ws): ''' 区分接口 ''' path = ws.request.path if path == DOWN_PATH: await down_handler(ws) elif path == UPPER_PATH: await up_handler(ws) else: await ws.close() async def status_broadcaster(): ''' 向上位机广播车辆状态 ''' while True: await asyncio.sleep(UPPER_STATUS_BROADCAST_INTERVAL) if up_ws is None: continue now = time.time() result = {} for sn, info in CAR_STATUS_MAP.items(): if now - info["last_heartbeat"] > HEARTBEAT_TIMEOUT: info["car_status"] = "offline" for cam in info["cameras"].values(): cam["camera_status"] = "offline" result[sn] = { "car_status": info["car_status"], "cameras": info["cameras"] } msg = json.dumps({ "type": "car_status", "data": result, "timestamp": datetime.fromtimestamp(now).strftime("%Y-%m-%d %H:%M:%S") # 输出可读的时间 }) try: await up_ws.send(msg) except: pass async def main(): print(f"服务运行中 ws://{HOST}:{PORT}") asyncio.create_task(status_broadcaster()) server = await websockets.serve(ws_router, HOST, PORT) await server.wait_closed() if __name__ == "__main__": import sys if sys.platform == "win32": asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) asyncio.run(main()) 上述代码中,接收到下位机发送的心跳数据后,上位机有什么显示吗
11-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值