using APT.BaseData.Domain.ApiModel.EX; using APT.BaseData.Domain.ApiModel.Platform; using APT.BaseData.Domain.Entities; using APT.BaseData.Domain.Enums.PF; using APT.BaseData.Domain.IServices.FM; using APT.Infrastructure.Api; using APT.Infrastructure.Api.Redis; using APT.Infrastructure.Core; using APT.Utility; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using static APT.BaseData.Domain.ApiModel.SocketInfo; namespace APT.BaseData.Services.Services.EX { public static class WebSocketService { //private IFMNotificatoinService _notificatoinService; //public WebSocketService(IFMNotificatoinService notificatoinService) //{ // _notificatoinService = notificatoinService; //} /// /// webscoket 打开 /// /// /// public static WebSocketResponseInfo OnOpenWebSocket(WebSocketClientInfo clientInfo) { Console.WriteLine($"Socket连接:{clientInfo?.UserId}"); //clientInfo.WebSocketConnection.Close(); //var tenant = APT.Infrastructure.Api.ServiceLocator.Instance.GetService(); //clientInfo.Tenant = tenant?.Name; //BaseFilter baseFilter = new BaseFilter(); //baseFilter.IgnoreOrgRule = true; //baseFilter.IsMultipleDb = true; //var commonService = ServiceLocator.Instance.GetService(); //var consumeService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService(); //var cueerntInfo = commonService.GetEntity(x=>x.CURRENT_CLIENT_ID == clientInfo.UserId, baseFilter);//当前占用充电桩 //if (cueerntInfo!=null && cueerntInfo.CURRENT_CLIENT_ID!=null) //{ // var user = commonService.GetEntity(x => x.CLIENT_ID == clientInfo.UserId, baseFilter); // baseFilter.Sort = "CREATE_TIME"; // baseFilter.Order = DbOrder.DESC; // var order = commonService.GetEntity(x => x.OPEN_ID == user.OPEN_ID && x.CHARGING_PILE_ID == cueerntInfo.ID, baseFilter, "Nav_ChargingPile"); // if (order!=null && order.ORDER_STATUS == (int)FCOrderStatus.充电中)//充电中 // { // SocketData socketData = new SocketData() // { // status = (int)MQChargingStatusEnum.充电进行 // }; // OrderReallyData orderReallyData = new OrderReallyData() // { // code = order.CODE, // chargeMode = order.CHARGE_TYPE, // }; // ChargeInfo charge = new ChargeInfo() // { // stakeId = order.CHARGING_PILE_ID.ToString(), // stakeCode = order.Nav_ChargingPile.CODE, // stakeName = order.Nav_ChargingPile.NAME // }; // if (order.CHARGE_TYPE == 1)//固定金额 // { // orderReallyData.chargeMoney = order.CHARGE_VALUE; // } // else if (order.CHARGE_TYPE == 2)//固定时长 // { // orderReallyData.chargeTime = order.CHARGE_VALUE; // } // orderReallyData.stake = charge; // #region 计算金额/充电时长 // var model = consumeService.ConsumeOrder(order.CODE); // var time = Convert.ToInt32((order.END_TIME - order.START_TIME)?.TotalMinutes); // orderReallyData.time = time; // orderReallyData.used = model.Order.ENERGY_USED; // orderReallyData.amount = model.Order.AMOUNT; // #endregion // socketData.data = orderReallyData; // var notificatoinService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService(); // notificatoinService.SendNotification(user.CLIENT_ID, socketData); // } //} return null; } /// /// websocket 关闭 /// /// public static void OnCloseWebSocket(Guid userId, WebSocketClientInfo clientInfo) { Console.WriteLine($"Socket连接断开:{userId}"); } /// /// websocket 信息 /// /// /// /// public static WebSocketResponseInfo OnMessageWebSocket(WebSocketClientInfo clientInfo, WebSocketRequestInfo requestInfo) { if (requestInfo != null) { switch (requestInfo.TypeCode) { case "M0002": { var service = APT.Infrastructure.Api.ServiceLocator.Instance.GetService(); var clientInfoByManu = JsonConvert.DeserializeObject(requestInfo.Data); List ingoreClientInfos = new List(); ingoreClientInfos.Add(clientInfo); service.SendNotification(clientInfoByManu, ingoreClientInfos); } break; case "M0003": { var service = APT.Infrastructure.Api.ServiceLocator.Instance.GetService(); var clientInfoByManu = JsonConvert.DeserializeObject(requestInfo.Data); List ingoreClientInfos = new List(); ingoreClientInfos.Add(clientInfo); service.SendAlarmNotification(clientInfoByManu, ingoreClientInfos); } break; case "P0001": { BaseFilter baseFilter = new BaseFilter(); baseFilter.IgnoreOrgRule = true; baseFilter.IsMultipleDb = true; var commonService = ServiceLocator.Instance.GetService(); //var consumeService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService(); //var cueerntInfo = commonService.GetEntity(x => x.CURRENT_CLIENT_ID == clientInfo.UserId, baseFilter);//当前占用充电桩 //if (cueerntInfo != null && cueerntInfo.CURRENT_CLIENT_ID != null) //{ // //var user = commonService.GetEntity(x => x.CLIENT_ID == clientInfo.UserId, baseFilter); // baseFilter.Sort = "CREATE_TIME"; // baseFilter.Order = DbOrder.DESC; // var order = commonService.GetEntity(x => x.CLIENT_ID == clientInfo.UserId.ToString() && x.CHARGING_PILE_ID == cueerntInfo.ID, baseFilter, "Nav_ChargingPile"); // if (order != null && order.ORDER_STATUS == (int)FCOrderStatus.充电中)//充电中 // { // SocketData socketData = new SocketData() // { // status = (int)MQChargingStatusEnum.充电进行 // }; // OrderReallyData orderReallyData = new OrderReallyData() // { // code = order.CODE, // tenantCode = order.CODE.Substring(2, 4), // chargeMode = order.CHARGE_TYPE, // orderStatus = order.ORDER_STATUS // }; // Domain.ApiModel.SocketInfo.ChargeInfo charge = new Domain.ApiModel.SocketInfo.ChargeInfo() // { // stakeId = order.CHARGING_PILE_ID.ToString(), // stakeCode = order.Nav_ChargingPile.CODE, // stakeName = order.Nav_ChargingPile.NAME // }; // if (order.CHARGE_TYPE == 1)//固定金额 // { // orderReallyData.chargeMoney = order.CHARGE_VALUE; // } // else if (order.CHARGE_TYPE == 2)//固定时长 // { // orderReallyData.chargeTime = order.CHARGE_VALUE; // } // orderReallyData.stake = charge; // #region 计算金额/充电时长 // var model = consumeService.ConsumeOrder(order.CODE); // var time = Convert.ToInt32((order.END_TIME - order.START_TIME)?.TotalMinutes); // orderReallyData.time = time; // orderReallyData.used = model.Order.ENERGY_USED; // orderReallyData.amount = model.Order.AMOUNT; // #endregion // socketData.data = orderReallyData; // var notificatoinService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService(); // notificatoinService.SendNotification((Guid)clientInfo.UserId, socketData); // } //} } break; case "MQ02": { BaseFilter filter = new BaseFilter(); filter.Sort = "CREATE_TIME"; filter.Order = DbOrder.DESC; filter.IgnoreOrgRule = true; filter.IsMultipleDb = true; var commonService = ServiceLocator.Instance.GetService(); //var endInfo = JsonConvert.DeserializeObject(requestInfo.Data); //var order = commonService.GetEntity(x => x.CODE == endInfo.code, filter, "Nav_ChargingPile"); //var orgId = (Guid)order.ORG_ID; //#region 发送指令 //MqttMesage mqttMesage = new MqttMesage() //{ // cmd = ((int)PFThemeTypeEnum.P_结束充电).ToString().PadLeft(3, '0'), // title = order.Nav_ChargingPile.CODE, // charge_no = order.CODE, // //terminal_sn = order.Nav_ChargingPile.SEQUENCE, // //terminal_no = order.Nav_ChargingPile.CODE, // timestamp = DataHelper.GetTimeSpanFrom1970Ticks(DateTime.Now) //}; //filter.OrgId = order.ORG_ID; //var mqConfs = commonService.GetEntitiesByRedis(null, filter, "", "Nav_MqttConfigTheme"); //if (mqConfs == null || !mqConfs.Any()) // throw new Exception("主题配置不存在"); //var mqConf = mqConfs.FirstOrDefault(); //BaseFilter basefilter = new BaseFilter(); //basefilter.OrgId = mqConf.ORG_ID; //basefilter.IsSpecifyDb = true; //basefilter.SpecifyDbConn = order.DbConn; //var org = commonService.GetEntity(null, basefilter); //var mqName = org.TENANT_CODE + "_" + mqConf.NAME; //CsRedisManager.GetClient().RPush(mqName.GetPubRedisKey(), mqttMesage); //#endregion } break; } } return null; } } }