LineRunMonitor.cs
5.8 KB
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
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace DeviceLibrary
{
public class LineRunMonitor : ISafetyDevice
{
Timer lineTimer = null;
Dictionary<string, DateTime> linrunlist = new Dictionary<string, DateTime>();
string Name;
ushort LineIO;
ushort LineRevIO;
bool isIOon = false;
public LineRunMonitor(string name, ushort io, ushort rev_io=0) {
Name = name;
LineIO = io;
LineRevIO = rev_io;
LineInit();
SafetyDevice.AddDevice(this);
}
//public bool Reversal { get; set; }
void LineInit()
{
if (lineTimer == null)
{
lineTimer = new Timer(100);
lineTimer.Elapsed += LineTimer_Elapsed;
lineTimer.Start();
GC.KeepAlive(lineTimer);
}
}
private void LineTimer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (linrunlist)
{
if (canStopLine(out _) && isIOon)//(DOValue(LineIO).Equals(IO_VALUE.HIGH) || DOValue(LineRevIO).Equals(IO_VALUE.HIGH)))
{
IOSTOP();
LogUtil.info(Name + $" 线体管理器 停止线体.");
}
}
}
private void DOMove(ushort LineIO, IO_VALUE iovalue)
{
IOManager.WriteSingleDO("", 0x00, LineIO, iovalue);
}
private object DOValue(ushort LineIO)
{
return IOManager.GetDOValue("", 0x00, LineIO);
}
DateTime pauseTime= DateTime.MinValue;
public void Pause() {
if (lineTimer.Enabled)
{
lineTimer.Enabled = false;
pauseTime = DateTime.Now;
IOSTOP();
if (linrunlist.Count>0)
LogUtil.info(Name + $" 线体管理器 暂停线体.");
}
}
public void Resume() {
if (pauseTime != DateTime.MinValue)
{
lock (linrunlist)
{
if (linrunlist.Count > 0)
{
foreach (var k in linrunlist.Keys.ToArray())
{
if (linrunlist.ContainsKey(k))
linrunlist[k] += DateTime.Now - pauseTime;
}
IORUN();
}
}
pauseTime = DateTime.MinValue;
lineTimer.Enabled = true;
if (linrunlist.Count > 0)
LogUtil.info(Name + $" 线体管理器 恢复运行线体.");
}
}
void IORUN() {
if (Reversal)
{
DOMove(LineIO, IO_VALUE.LOW);
DOMove(LineRevIO, IO_VALUE.HIGH);
}
else
{
DOMove(LineRevIO, IO_VALUE.LOW);
DOMove(LineIO, IO_VALUE.HIGH);
}
isIOon = true;
}
void IOSTOP()
{
isIOon = false;
DOMove(LineIO, IO_VALUE.LOW);
DOMove(LineRevIO, IO_VALUE.LOW);
}
bool Reversal = false;
/// <summary>
/// 控制线体运转
/// </summary>
/// <param name="id">需求方标识</param>
/// <param name="Reversal">是否反转</param>
/// <param name="seconds">秒数</param>
public void LineRun(string id,bool reversal, int seconds, string parentname = "")
{
LineInit();
lock (linrunlist)
{
this.Reversal = reversal;
IORUN();
if (!string.IsNullOrEmpty(id) && seconds > 0)
{
if (linrunlist.ContainsKey(id))
linrunlist[id] = DateTime.Now.AddSeconds(seconds);
else
{
linrunlist.Add(id, DateTime.Now.AddSeconds(seconds));
}
LogUtil.info(Name + $" 线体管理器 {id},{parentname} 请求链条运行 {seconds}秒.");
}
}
}
public void LineStop(string id, string parentname = "")
{
lock (linrunlist)
{
if (!string.IsNullOrEmpty(id))
{
if (linrunlist.ContainsKey(id))
linrunlist.Remove(id);
LogUtil.info(Name + $" 线体管理器 {id},{parentname} 请求立刻停止线体.");
}
}
if (!canStopLine(out string msg))
LogUtil.info(Name + $" {Name}");
// IOMove(IO_Type.Line_Run, IO_VALUE.LOW);
}
bool canStopLine(out string msg)
{
msg = "";
bool canStop = true;
lock (linrunlist)
{
foreach (var x in linrunlist.ToList())
{
if (x.Value > DateTime.Now)
{
canStop = false;
msg = Name + $" 线体管理器 {x.Key} 不允许停止线体 需求停止时间 {x.Value.ToString()}.";
//LogUtil.info(Name + $" {x.Key} 不允许停止线体 需求停止时间 {x.Value.ToString()}.");
}
else
{
LogUtil.info(Name + $" 线体管理器 {x.Key} 请求时间已过期,删除.");
linrunlist.Remove(x.Key);
}
}
}
return canStop;
}
}
}