-
Notifications
You must be signed in to change notification settings - Fork 0
/
WEBPandaZHCDSacdaService.cs
187 lines (172 loc) · 6.64 KB
/
WEBPandaZHCDSacdaService.cs
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
using CityIoTCommand;
using CityLogService;
using CityPublicClassLib;
using CityUtils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Threading;
namespace CityWEBDataService
{
public class WEBPandaZHCDSacdaService : ISonService, IServiceWorker
{
// WEB-综合监测点-SCADA 数据采集任务
private System.Timers.Timer timer;
private PandaParam param;
private CommandConsumer commandCustomer;
public void ReceiveCommand(RequestCommand command)
{
// 已经在入口验证过命令对象
if (!IsRuning || this.commandCustomer == null || !this.commandCustomer.IsRuning)
{
CommandManager.MakeFail("Scada-WEB-综合监测点命令消费器运行异常", ref command);
CommandManager.CompleteCommand(command);
TraceManagerForCommand.AppendErrMsg("Scada-WEB-综合监测点命令消费器运行异常");
return;
}
this.commandCustomer.Append(command);
}
public WEBPandaZHCDSacdaService(string configFilePath)
{
Config.configFilePath = configFilePath;
}
public void Start(out string errMsg)
{
errMsg = "";
if (IsRuning)
return;
// 环境检查
if (!EnvChecker.CheckPandaCeDianWEB(out errMsg))
return;
TraceManagerForWeb.AppendDebug("Scada-WEB-综合监测点环境检查通过");
this.param = Config.pandaCeDianParam;
WebPandaZHCDScadaCommand.CreateInitSensorRealData(param).Execute(); //初始化实时表
timer = new System.Timers.Timer();
timer.Interval = this.param.collectInterval * 60 * 1000;
timer.Elapsed += (o, e) =>
{
try
{
Excute();
}
catch(Exception ee)
{
TraceManagerForWeb.AppendErrMsg("Scada-WEB-综合监测点定时任务执行失败:" + ee.Message);
}
};
timer.Enabled = true;
// 控制器服务
if (commandCustomer != null)
commandCustomer.Stop();
commandCustomer = new CommandConsumer(ConsumerCommand);
commandCustomer.Start();
if (commandCustomer.IsRuning)
TraceManagerForWeb.AppendDebug("Scada-WEB-综合监测点控制器服务已经打开");
else
{
TraceManagerForWeb.AppendErrMsg("Scada-WEB-综合监测点控制器服务打开失败");
Stop();
return;
}
IsRuning = true;
// 开始异步执行一次-防止启动卡死
Action action = Excute;
action.BeginInvoke(null, null);
}
public bool IsRuning { get; set; }
private bool ExcuteDoing { get; set; } = false;
public void Stop()
{
if (!IsRuning)
return;
try
{
// 控制器服务
if (commandCustomer != null)
{
commandCustomer.Stop();
if (!commandCustomer.IsRuning)
{
TraceManagerForWeb.AppendDebug("Scada-WEB-综合监测点控制器服务已停止");
this.commandCustomer = null;
}
else
TraceManagerForWeb.AppendErrMsg("Scada-WEB-综合监测点控制器服务停止失败");
}
}
catch { }
// 关闭定时器
if (timer != null)
{
timer.Enabled = false;
timer.Close();
timer = null;
}
IsRuning = false;
}
private void Excute()
{
lock (this)
{
if (ExcuteDoing)
return;
ExcuteDoing = true;
ExcuteHandle();
ExcuteDoing = false;
}
}
private void ExcuteHandle()
{
// 报警维护
WebPandaZHCDScadaCommand.CreateCollectAndSaveScadaSensors(param).Execute();
}
// 执行调度命令
private void ConsumerCommand(RequestCommand command)
{
try
{
ExcuteCommand(command);
}
catch (Exception e)
{
CommandManager.MakeFail("Scada-WEB-综合监测点 定时任务执行失败:" + e.Message, ref command);
CommandManager.CompleteCommand(command);
TraceManagerForCommand.AppendErrMsg(command.message);
}
}
private void ExcuteCommand(RequestCommand command)
{
if (command.sonServerType == CommandServerType.ZHCD_WEB && command.operType == CommandOperType.ReLoadData)
{
if (ExcuteDoing) // 正在采集,等这次采集结束,在采集一次
{
DateTime time1 = DateTime.Now;
while (true)
{
Thread.Sleep(1);
if (DateTime.Now - time1 > TimeSpan.FromSeconds(command.timeoutSeconds)) // 超时
{
CommandManager.MakeTimeout("Scada-WEB-综合监测点 数据更新超时", ref command);
CommandManager.CompleteCommand(command);
TraceManagerForCommand.AppendInfo(command.message);
return;
}
if (!ExcuteDoing)
break;
}
}
// 调取之前先重新加载一次缓存
Excute();
CommandManager.MakeSuccess("Scada-WEB-综合监测点 数据已更新", ref command);
CommandManager.CompleteCommand(command);
TraceManagerForCommand.AppendInfo("Scada-WEB-综合监测点 数据已更新");
return;
}
CommandManager.MakeFail("错误的请求服务类型", ref command);
CommandManager.CompleteCommand(command);
TraceManagerForCommand.AppendErrMsg(command.message);
return;
}
}
}