d_sms_service/APT.BaseData.Services/Services/EX/WebSocketService.cs

226 lines
13 KiB
C#
Raw Normal View History

2024-10-28 13:45:58 +08:00
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;
//}
/// <summary>
/// webscoket 打开
/// </summary>
/// <param name="clientInfo"></param>
/// <returns></returns>
public static WebSocketResponseInfo OnOpenWebSocket(WebSocketClientInfo clientInfo)
{
Console.WriteLine($"Socket连接:{clientInfo?.UserId}");
//clientInfo.WebSocketConnection.Close();
//var tenant = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<TenantInfo>();
//clientInfo.Tenant = tenant?.Name;
//BaseFilter baseFilter = new BaseFilter();
//baseFilter.IgnoreOrgRule = true;
//baseFilter.IsMultipleDb = true;
//var commonService = ServiceLocator.Instance.GetService<ICommonService>();
//var consumeService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<ISOConsumeChargingOrderService>();
//var cueerntInfo = commonService.GetEntity<T_CP_CHARGING_PILE>(x=>x.CURRENT_CLIENT_ID == clientInfo.UserId, baseFilter);//当前占用充电桩
//if (cueerntInfo!=null && cueerntInfo.CURRENT_CLIENT_ID!=null)
//{
// var user = commonService.GetEntity<T_OP_CLINET_OPENID>(x => x.CLIENT_ID == clientInfo.UserId, baseFilter);
// baseFilter.Sort = "CREATE_TIME";
// baseFilter.Order = DbOrder.DESC;
// var order = commonService.GetEntity<T_SO_CHARGING_ORDER>(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<ISONotificatoinService>();
// notificatoinService.SendNotification(user.CLIENT_ID, socketData);
// }
//}
return null;
}
/// <summary>
/// websocket 关闭
/// </summary>
/// <param name="userId"></param>
public static void OnCloseWebSocket(Guid userId, WebSocketClientInfo clientInfo)
{
Console.WriteLine($"Socket连接断开:{userId}");
}
/// <summary>
/// websocket 信息
/// </summary>
/// <param name="clientInfo"></param>
/// <param name="requestInfo"></param>
/// <returns></returns>
public static WebSocketResponseInfo OnMessageWebSocket(WebSocketClientInfo clientInfo,
WebSocketRequestInfo requestInfo)
{
if (requestInfo != null)
{
switch (requestInfo.TypeCode)
{
case "M0002":
{
var service = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IFMNotificatoinService>();
var clientInfoByManu = JsonConvert.DeserializeObject<SendNotificationModel>(requestInfo.Data);
List<WebSocketClientInfo> ingoreClientInfos = new List<WebSocketClientInfo>();
ingoreClientInfos.Add(clientInfo);
service.SendNotification(clientInfoByManu, ingoreClientInfos);
}
break;
case "M0003":
{
var service = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IFMNotificatoinService>();
var clientInfoByManu = JsonConvert.DeserializeObject<SendNotificationModel>(requestInfo.Data);
List<WebSocketClientInfo> ingoreClientInfos = new List<WebSocketClientInfo>();
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<ICommonService>();
//var consumeService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<ISOConsumeChargingOrderService>();
//var cueerntInfo = commonService.GetEntity<T_CP_CHARGING_PILE>(x => x.CURRENT_CLIENT_ID == clientInfo.UserId, baseFilter);//当前占用充电桩
//if (cueerntInfo != null && cueerntInfo.CURRENT_CLIENT_ID != null)
//{
// //var user = commonService.GetEntity<T_OP_CLINET_OPENID>(x => x.CLIENT_ID == clientInfo.UserId, baseFilter);
// baseFilter.Sort = "CREATE_TIME";
// baseFilter.Order = DbOrder.DESC;
// var order = commonService.GetEntity<T_SO_CHARGING_ORDER>(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<IFMNotificatoinService>();
// 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<ICommonService>();
//var endInfo = JsonConvert.DeserializeObject<EndStake>(requestInfo.Data);
//var order = commonService.GetEntity<T_SO_CHARGING_ORDER>(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<T_PF_MQTT_CONFIG>(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<T_FM_ORGANIZATION>(null, basefilter);
//var mqName = org.TENANT_CODE + "_" + mqConf.NAME;
//CsRedisManager.GetClient().RPush<MqttMesage>(mqName.GetPubRedisKey(), mqttMesage);
//#endregion
}
break;
}
}
return null;
}
}
}