收心了.

2004-10-31

收心了.

   演出结束,谢幕.关灯. 迎接明天!

                       -----------那边工作OVER了.

考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
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、付费专栏及课程。

余额充值