yanjinhui
2025-06-06 69c9387f07b48948f7bc3c1b89bf82948293a2fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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);
        }
    }
}