using HslCommunication.WebSocket; using RYB_PTL_API; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_DTO; using WIDESEAWCS_DTO.TaskInfo; namespace WIDESEAWCS_Server.HostedService { public static class WebSocketSetup { private static bool _eventSubscribed = false; public static void AddWebSocketSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); if(AppSettings.Get("WebSocketEnable").ObjToBool()) { int port = AppSettings.Get("WebSocketPort").ObjToInt(); if (port == 0) { port = 9296; } services.AddSingleton(x => { WebSocketServer socketServer = new WebSocketServer(); socketServer.ServerStart(port); return socketServer; }); } } /// ///// 订阅PTL事件 ///// //public static void SubscribeEvent(this IServiceCollection services) //{ // if (!_eventSubscribed) // { // RYB_PTL.UserResultAvailable += new RYB_PTL.UserResultAvailableEventHandler(RYB_PTL_UserResultAvailable); // _eventSubscribed = true; // } //} ///// ///// PTL回调事件处理(只处理手拍事件) ///// //public static void RYB_PTL_UserResultAvailable(RYB_PTL.RtnValueStruct rs) //{ // try // { // if (rs.KeyCode == null) // { // return; // } // // 构建回调数据 // var pTLCallBackDTO = new PTLCallBackDTO // { // sIp = rs.Ip, // sTagID = rs.Tagid, // sValue = rs.Number, // sKeyCode = rs.KeyCode, // sLocator = rs.Locator // }; // // 根据业务需要构建任务信息 // var taskBackLight = new TaskBackLight() // { // TagNo = "B1", // TagCode = pTLCallBackDTO.sLocator, // }; // } // catch (Exception ex) // { // // 记录日志或处理异常 // Console.WriteLine($"处理PTL手拍回调时出错: {ex.Message}"); // } //} } }