Files
shgx_tz_mes_backend_sync/ZR.Admin.WebApi/Hubs/MessageHub.cs
2024-01-29 16:55:45 +08:00

199 lines
8.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Infrastructure;
using Infrastructure.Constant;
using Infrastructure.Model;
using IPTools.Core;
using Microsoft.AspNetCore.SignalR;
using System.ComponentModel;
using System.Diagnostics.Metrics;
using UAParser;
using ZR.Admin.WebApi.Extensions;
using ZR.Service.mes.qc;
using ZR.Service.mes.qc.IService;
using ZR.Service.System.IService;
namespace ZR.Admin.WebApi.Hubs
{
/// <summary>
/// msghub OperationHub
/// </summary>
public class MessageHub : Hub
{
//创建用户集合,用于存储所有链接的用户数据
public static readonly List<OnlineUsers> clientUsers = new();
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly ISysNoticeService SysNoticeService;
private readonly IFirstFQCService firstFQCService;
public MessageHub(ISysNoticeService noticeService, IFirstFQCService firstFQCService)
{
SysNoticeService = noticeService;
this.firstFQCService = firstFQCService;
}
private ApiResult SendNotice()
{
var result = SysNoticeService.GetSysNotices();
return new ApiResult(200, "success", result);
}
#region
/// <summary>
/// 客户端连接的时候调用
/// </summary>
/// <returns></returns>
public override Task OnConnectedAsync()
{
var name = HttpContextExtension.GetName(App.HttpContext);
var ip = HttpContextExtension.GetClientUserIp(App.HttpContext);
var ip_info = IpTool.Search(ip);
ClientInfo clientInfo = HttpContextExtension.GetClientInfo(App.HttpContext);
string device = clientInfo.ToString();
var userid = HttpContextExtension.GetUId(App.HttpContext);
string uuid = device + userid + ip;
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
var user2 = clientUsers.Any(u => u.Uuid == uuid);
//判断用户是否存在,否则添加集合!user2 && !user &&
if (!user2 && !user && Context.User.Identity.IsAuthenticated)
{
OnlineUsers users = new(Context.ConnectionId, name, userid, ip, device)
{
Location = ip_info.City,
Uuid = uuid
};
clientUsers.Add(users);
Console.WriteLine($"{DateTime.Now}{name},{Context.ConnectionId}连接服务端success当前已连接{clientUsers.Count}个");
Clients.All.SendAsync("welcome", $"欢迎您:{name},当前时间:{DateTime.Now}");
Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
}
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
return base.OnConnectedAsync();
}
/// <summary>
/// 连接终止时调用。
/// </summary>
/// <returns></returns>
public override Task OnDisconnectedAsync(Exception? exception)
{
var user = clientUsers.Where(p => p.ConnnectionId == Context.ConnectionId).FirstOrDefault();
//判断用户是否存在,否则添加集合
if (user != null)
{
clientUsers.Remove(user);
Clients.All.SendAsync(HubsConstant.OnlineNum, clientUsers.Count);
Console.WriteLine($"用户{user?.Name}离开了,当前已连接{clientUsers.Count}个");
}
return base.OnDisconnectedAsync(exception);
}
#endregion
/// <summary>
/// 注册信息 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 });
}
/// <summary>
/// 保存油漆缺陷采集累加信息 首检
/// </summary>
/// <param name="workorderid">工单id</param>
/// <param name="checkid">检测项</param>
/// <returns></returns>
public async Task SaveCacheInformation_v1(string workorderid, string Moudle, string checkid)
{
//缓存key
string checkid_Key =checkid + "_" + workorderid + "_v1";
if (CacheHelper.Exists(checkid_Key))
{
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);
}
else
{
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);
}
}
/// <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);
}
}
}
}