Files
shgx_tz_mes_backend_sync/ZR.Admin.WebApi/Hubs/MessageHub.cs

199 lines
8.1 KiB
C#
Raw Normal View History

2022-05-16 11:30:32 +08:00
using Infrastructure;
using Infrastructure.Constant;
2022-02-27 21:11:46 +08:00
using Infrastructure.Model;
2022-06-08 09:39:37 +08:00
using IPTools.Core;
2022-02-27 21:11:46 +08:00
using Microsoft.AspNetCore.SignalR;
2023-12-12 22:26:20 +08:00
using System.ComponentModel;
using System.Diagnostics.Metrics;
2023-04-19 19:55:35 +08:00
using UAParser;
2022-05-15 09:23:55 +08:00
using ZR.Admin.WebApi.Extensions;
2023-12-12 22:26:20 +08:00
using ZR.Service.mes.qc;
using ZR.Service.mes.qc.IService;
using ZR.Service.System.IService;
2022-02-27 21:11:46 +08:00
namespace ZR.Admin.WebApi.Hubs
{
2023-04-19 19:55:35 +08:00
/// <summary>
2023-10-12 09:36:03 +08:00
/// msghub OperationHub
2023-04-19 19:55:35 +08:00
/// </summary>
2022-02-27 21:11:46 +08:00
public class MessageHub : Hub
{
//创建用户集合,用于存储所有链接的用户数据
2023-05-12 15:08:24 +08:00
public static readonly List<OnlineUsers> clientUsers = new();
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
2022-06-08 09:39:37 +08:00
private readonly ISysNoticeService SysNoticeService;
2023-12-12 22:26:20 +08:00
private readonly IFirstFQCService firstFQCService;
2023-12-12 22:26:20 +08:00
public MessageHub(ISysNoticeService noticeService, IFirstFQCService firstFQCService)
{
SysNoticeService = noticeService;
2023-12-12 22:26:20 +08:00
this.firstFQCService = firstFQCService;
}
private ApiResult SendNotice()
{
var result = SysNoticeService.GetSysNotices();
return new ApiResult(200, "success", result);
}
2022-02-27 21:11:46 +08:00
#region
/// <summary>
/// 客户端连接的时候调用
/// </summary>
/// <returns></returns>
public override Task OnConnectedAsync()
{
2023-05-12 15:08:24 +08:00
var name = HttpContextExtension.GetName(App.HttpContext);
2022-05-22 19:34:57 +08:00
var ip = HttpContextExtension.GetClientUserIp(App.HttpContext);
2022-06-08 09:39:37 +08:00
var ip_info = IpTool.Search(ip);
2023-04-19 19:55:35 +08:00
ClientInfo clientInfo = HttpContextExtension.GetClientInfo(App.HttpContext);
string device = clientInfo.ToString();
2023-05-12 15:08:24 +08:00
2023-03-18 07:58:39 +08:00
var userid = HttpContextExtension.GetUId(App.HttpContext);
2023-04-19 19:55:35 +08:00
string uuid = device + userid + ip;
2022-02-27 21:11:46 +08:00
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
2023-04-19 19:55:35 +08:00
var user2 = clientUsers.Any(u => u.Uuid == uuid);
2023-05-12 15:08:24 +08:00
//判断用户是否存在,否则添加集合!user2 && !user &&
2023-05-15 08:44:08 +08:00
if (!user2 && !user && Context.User.Identity.IsAuthenticated)
2022-02-27 21:11:46 +08:00
{
2023-05-12 15:08:24 +08:00
OnlineUsers users = new(Context.ConnectionId, name, userid, ip, device)
2022-06-08 09:39:37 +08:00
{
2023-04-19 19:55:35 +08:00
Location = ip_info.City,
Uuid = uuid
2022-06-08 09:39:37 +08:00
};
clientUsers.Add(users);
2022-02-28 18:37:23 +08:00
Console.WriteLine($"{DateTime.Now}{name},{Context.ConnectionId}连接服务端success当前已连接{clientUsers.Count}个");
2023-12-01 15:39:07 +08:00
Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
2022-02-27 21:11:46 +08:00
}
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
2022-02-27 21:11:46 +08:00
return base.OnConnectedAsync();
}
/// <summary>
/// 连接终止时调用。
/// </summary>
/// <returns></returns>
2022-11-28 17:08:48 +08:00
public override Task OnDisconnectedAsync(Exception? exception)
2022-02-27 21:11:46 +08:00
{
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
//判断用户是否存在,否则添加集合
if (user != null)
{
clientUsers.Remove(user);
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
2023-05-12 15:08:24 +08:00
2022-06-08 09:39:37 +08:00
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
2022-02-27 21:11:46 +08:00
}
return base.OnDisconnectedAsync(exception);
}
#endregion
/// <summary>
2023-12-01 15:39:07 +08:00
/// 注册信息 todo vue向服务器端发送请求 注意参数一致和集线器转发的客户端
/// </summary>
/// <param name="connectId"></param>
/// <param name="userName"></param>
/// <param name="message"></param>
/// <returns></returns>
[HubMethodName("SendMessage")]
public async Task SendMessage(string connectId, string userName, string message)
{
Console.WriteLine($"{connectId},message={message}");
await Clients.Client(connectId).SendAsync("receiveChat", new { userName, message });
}
2023-12-12 22:26:20 +08:00
/// <summary>
2023-12-18 16:01:17 +08:00
/// 保存油漆缺陷采集累加信息 首检
2023-12-12 22:26:20 +08:00
/// </summary>
/// <param name="workorderid">工单id</param>
/// <param name="checkid">检测项</param>
/// <returns></returns>
2023-12-18 16:01:17 +08:00
public async Task SaveCacheInformation_v1(string workorderid, string Moudle, string checkid)
2023-12-12 22:26:20 +08:00
{
2024-01-29 16:55:45 +08:00
//缓存key
string checkid_Key =checkid + "_" + workorderid + "_v1";
2023-12-12 22:26:20 +08:00
2024-01-29 16:55:45 +08:00
if (CacheHelper.Exists(checkid_Key))
2023-12-12 22:26:20 +08:00
{
2024-01-29 16:55:45 +08:00
int sum = Convert.ToInt32(CacheHelper.GetCache(checkid_Key)) + 1;
logger.Info($"当前保存工单号{workorderid},检测项{checkid_Key},累加的数为{sum}");
CacheHelper.SetCache(checkid_Key, sum);
await firstFQCService.SaveinspectItem_v1(workorderid,Moudle, checkid_Key, sum);
await Clients.All.SendAsync("GetCache_v1", Moudle, checkid_Key, sum);
2023-12-12 22:26:20 +08:00
}
else
{
2024-01-29 16:55:45 +08:00
CacheHelper.SetCache(checkid_Key, 1);
logger.Info($"当前保存工单号{workorderid},检测项{checkid_Key}累加的数为1");
await firstFQCService.SaveinspectItem_v1(workorderid, Moudle, checkid_Key, 1);
await Clients.All.SendAsync("GetCache_v1", Moudle, checkid_Key, 1);
2023-12-18 16:01:17 +08:00
}
}
/// <summary>
/// 保存油漆缺陷采集累加信息 二检
/// </summary>
/// <param name="workorderid">工单id</param>
/// <param name="checkid">检测项</param>
/// <returns></returns>
public async Task SaveCacheInformation_v2(string workorderid, string Moudle, string checkid)
{
checkid = checkid + "_v2";
if (CacheHelper.Exists(checkid))
{
int sum = Convert.ToInt32(CacheHelper.GetCache(checkid)) + 1;
logger.Info($"当前保存工单号{workorderid},检测项{checkid},累加的数为{sum}");
CacheHelper.SetCache(checkid, sum);
//SaveinspectItem
await firstFQCService.SaveinspectItem_v2(workorderid, Moudle, checkid, sum);
await Clients.All.SendAsync("GetCache_v2", Moudle, checkid, sum);
}
else
{
CacheHelper.SetCache(checkid, 1);
logger.Info($"当前保存工单号{workorderid},检测项{checkid}累加的数为1");
await firstFQCService.SaveinspectItem_v2(workorderid, Moudle, checkid, 1);
await Clients.All.SendAsync("GetCache_v2", Moudle, checkid, 1);
}
}
/// <summary>
/// 保存油漆缺陷采集累加信息 三检
/// </summary>
/// <param name="workorderid">工单id</param>
/// <param name="checkid">检测项</param>
/// <returns></returns>
public async Task SaveCacheInformation_v3(string workorderid, string Moudle, string checkid)
{
checkid = checkid + "_v3";
if (CacheHelper.Exists(checkid))
{
int sum = Convert.ToInt32(CacheHelper.GetCache(checkid)) + 1;
logger.Info($"当前保存工单号{workorderid},检测项{checkid},累加的数为{sum}");
CacheHelper.SetCache(checkid, sum);
//SaveinspectItem
await firstFQCService.SaveinspectItem_v3(workorderid, Moudle, checkid, sum);
await Clients.All.SendAsync("GetCache_v3", Moudle, checkid, sum);
}
else
{
CacheHelper.SetCache(checkid, 1);
logger.Info($"当前保存工单号{workorderid},检测项{checkid}累加的数为1");
await firstFQCService.SaveinspectItem_v3(workorderid, Moudle, checkid, 1);
await Clients.All.SendAsync("GetCache_v3", Moudle, checkid, 1);
2023-12-12 22:26:20 +08:00
}
}
2022-02-27 21:11:46 +08:00
}
}