using HslCommunication.WebSocket;
|
using Newtonsoft.Json;
|
using SqlSugar;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEAWCS_Core;
|
using WIDESEAWCS_Core.BaseRepository;
|
using WIDESEAWCS_Core.BaseServices;
|
using WIDESEAWCS_Core.Helper;
|
using WIDESEAWCS_DTO.Telescopic;
|
using WIDESEAWCS_ITaskInfoService;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob;
|
|
namespace WIDESEAWCS_TelescopicService
|
{
|
public class AlarmResetHsyServer : ServiceBase<Dt_AlarmResetHsy, IRepository<Dt_AlarmResetHsy>>, IAlarmResetHsyServer
|
{
|
public IRepository<Dt_AlarmResetHsy> Repository => BaseDal;
|
private readonly WebSocketServer _webSocketServer;
|
public AlarmResetHsyServer(IRepository<Dt_AlarmResetHsy> BaseDal, WebSocketServer webSocketServer) : base(BaseDal)
|
{
|
_webSocketServer = webSocketServer;
|
}
|
|
|
/// <summary>
|
/// 添加报错数据
|
/// </summary>
|
/// <param name="entity"></param>
|
/// <returns></returns>
|
public WebResponseContent AddAlarmHsy(int deptid, string alarmContent, bool resetStatus)
|
{
|
var alarm = new Dt_AlarmResetHsy
|
{
|
Deptid = deptid,
|
AlarmContent= alarmContent,
|
ResetStatus= resetStatus,
|
AlarmTime=DateTime.Now,
|
|
};
|
BaseDal.AddData(alarm);
|
|
return new WebResponseContent { Status=true, Data = alarm };
|
}
|
|
|
public WebResponseContent GetWebSocketInfo()
|
{
|
try
|
{
|
// 查询并排序,确保 ResetStatus 为 true 的记录排在前面,并按 CreateDate 降序排列
|
// var query = Db.Queryable<Dt_AlarmResetHsy>()
|
// .OrderBy(x => new { ResetStatus = SqlFunc.IsNull(x.ResetStatus, false), x.CreateDate }, OrderByType.Desc); // 按 ResetStatus 为 true 排在前,并按 CreateDate 降序
|
|
var query = BaseDal.QueryData().OrderByDescending(x => x.ResetStatus). ThenByDescending(x => x.CreateDate); // 再按 CreateDate 降序;
|
|
// 如果需要根据其他字段进行过滤或模糊查询,可以在此添加
|
|
|
return new WebResponseContent
|
{
|
Status = true,
|
Message = "查询成功",
|
Data = query
|
};
|
}
|
catch (Exception ex)
|
{
|
// 异常捕获和错误返回
|
return new WebResponseContent { Status = false, Message = "错误:" + ex.Message };
|
}
|
}
|
|
|
/// <summary>
|
/// 将表中的状态全部为true的改为false
|
/// </summary>
|
/// <param name="status"></param>
|
/// <returns></returns>
|
public WebResponseContent BecomeTrue()
|
{
|
var query = BaseDal.QueryData(x => x.ResetStatus == true).ToList();
|
// 如果没有找到符合条件的数据
|
if (query.Count == 0)
|
{
|
return new WebResponseContent { Status = false, Message = "没有要复位数据" };
|
}
|
// 获取所有不同的 DeptId(每个代表一个PLC)
|
var deptIds = query.Select(x => x.Deptid).Distinct();
|
|
foreach (var deptId in deptIds)
|
{
|
try
|
{
|
var devices = GetDevicesByDeptId(deptId);
|
|
try
|
{
|
devices.Value.left.Communicator.Write<bool>("M105", true);
|
}
|
catch (Exception)
|
{
|
|
|
}
|
try
|
{
|
devices.Value.right.Communicator.Write<bool>("M105", true);
|
}
|
catch (Exception)
|
{
|
|
|
}
|
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"向 DeptId={deptId} 的PLC写入失败:" + ex.Message);
|
// 可以选择继续或中断
|
}
|
}
|
foreach (var item in query)
|
{
|
item.ResetStatus = false;
|
item.ResetTime = DateTime.Now;
|
}
|
|
// 执行批量更新操作
|
var updateResult = BaseDal.UpdateData(query);
|
|
// 检查更新是否成功
|
if (updateResult)
|
{
|
return new WebResponseContent { Status = true, Message = "数据已成功复位", Data = query };
|
}
|
else
|
{
|
return new WebResponseContent { Status = false, Message = "复位操作失败" };
|
}
|
|
|
}
|
|
public WebResponseContent UpstreamInspectionRoad(UpstreamIDTO upstreamIDTO)
|
{
|
try
|
{
|
// 将 upstreamIDTO 转成 JSON 字符串
|
var json = JsonConvert.SerializeObject(upstreamIDTO);
|
|
// 推送到所有 WebSocket 客户端
|
_webSocketServer.PublishAllClientPayload(json);
|
|
return new WebResponseContent { Status = true, Message = "推送成功" };
|
}
|
catch (Exception ex)
|
{
|
// 出现异常,返回失败
|
return new WebResponseContent { Status = false, Message = "推送失败:" + ex.Message };
|
}
|
}
|
public WebResponseContent DeleteAllinform()
|
{
|
try
|
{
|
|
|
//找到该轨道站的全部信息
|
var alarm = BaseDal.QueryData().ToList();
|
if (alarm.Count < 0)
|
{
|
return new WebResponseContent { Status = false, Message = $"没有要删除的报警信息" };
|
}
|
//找到后全部删除
|
var reslut = BaseDal.DeleteData(alarm);
|
return new WebResponseContent { Status = true, Message = "删除成功", Data = reslut };
|
}
|
catch (Exception ex)
|
{
|
|
return new WebResponseContent { Status = false, Message = ex.Message };
|
}
|
}
|
public (OtherDevice left, OtherDevice right)? GetDevicesByDeptId(int deptId)
|
{
|
// 左设备号:1 -> 001,2 -> 003,3 -> 005...
|
int baseCode = 1 + (deptId - 1) * 2;
|
string leftCode = $"SSG{baseCode.ToString("D3")}";
|
string rightCode = $"SSG{(baseCode + 1).ToString("D3")}";
|
|
var left = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == leftCode);
|
var right = (OtherDevice)Storage.Devices.Find(x => x.DeviceCode == rightCode);
|
|
|
//if (left == null || right == null)
|
// return null;
|
|
return (left, right);
|
}
|
}
|
}
|