MainMachine _LedProcess.cs
8.6 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using OnlineStore;
using CodeLibrary;
using Newtonsoft.Json;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceLibrary
{
partial class MainMachine
{
Led AlarmLed;
Led StandbyLed;
Led RunningLed;
public Dictionary<MachineLedStateE, Dictionary<LedColor, LedState>> MachineLedState = new Dictionary<MachineLedStateE, Dictionary<LedColor, LedState>>();
public Dictionary<MachineLedStateE, string> MachineLedStateName = new Dictionary<MachineLedStateE, string>();
System.Threading.Timer ledtimer;
void LedProcessInit()
{
DefaultLedCfg();
LoadLedCfg();
ledtimer = new System.Threading.Timer(new TimerCallback(LedProcess), null, 0, 1000);
GC.KeepAlive(ledtimer);
}
public void LoadLedCfg()
{
if (File.Exists("Config\\LedConfig.json"))
{
var s = File.ReadAllText("Config\\LedConfig.json");
try
{
var ledcfgs = JsonConvert.DeserializeObject<Dictionary<MachineLedStateE, Dictionary<LedColor, LedState>>>(s);
if (ledcfgs != null)
foreach (var key in ledcfgs.Keys)
MachineLedState[key] = ledcfgs[key];
}
catch (Exception e)
{
LogUtil.info("灯塔亮灯配置加载失败:" + e.ToString());
}
}
}
public bool SaveLedCfg()
{
try
{
var cfgtxt = JsonConvert.SerializeObject(MachineLedState);
File.WriteAllText("Config\\LedConfig.json", cfgtxt);
return true;
}
catch (Exception e)
{
LogUtil.info("灯塔亮灯配置保存失败:" + e.ToString());
return false;
}
}
public void DefaultLedCfg()
{
MachineLedState.Clear();
//系统报警并停止,红亮
MachineLedStateName[MachineLedStateE.AlarmStop] = crc.GetString("ledstate_AlarmStop","停机报警");
MachineLedState.Add(MachineLedStateE.AlarmStop, nls(LedState.on, LedState.off, LedState.off));
//系统运行时报警, 绿亮,红闪
MachineLedStateName[MachineLedStateE.Alarm] = crc.GetString("ledstate_Alarm","报警");
MachineLedState.Add(MachineLedStateE.Alarm, nls(LedState.blink, LedState.off, LedState.on));
//系统复位中 绿闪
MachineLedStateName[MachineLedStateE.HomeReset] = crc.GetString("ledstate_HomeReset","复位");
MachineLedState.Add(MachineLedStateE.HomeReset, nls(LedState.off, LedState.off, LedState.blink));
//系统正常运行
MachineLedStateName[MachineLedStateE.Running] = crc.GetString("ledstate_Running","运行");
MachineLedState.Add(MachineLedStateE.Running, nls(LedState.none, LedState.none, LedState.on));
//系统暂停, 绿闪,红闪
MachineLedStateName[MachineLedStateE.SystemPause] = crc.GetString("ledstate_SystemPause","暂停");
MachineLedState.Add(MachineLedStateE.SystemPause, nls(LedState.blink, LedState.off, LedState.blink));
//温湿度超限30分钟. 红闪,黄闪
MachineLedStateName[MachineLedStateE.THoutRangeOver30m] = crc.GetString("Res0031.cbc9a6e9","温湿度超限30分钟");
MachineLedState.Add(MachineLedStateE.THoutRangeOver30m, nls(LedState.blink, LedState.blink, LedState.none));
//温湿度超限 绿闪黄闪
//MachineLedStateName[MachineLedStateE.THoutRange] = "温湿度超限";
//MachineLedState.Add(MachineLedStateE.THoutRange, nls(LedState.none, LedState.blink, LedState.blink));
//进出库, 绿亮,黄闪
MachineLedStateName[MachineLedStateE.InOut] = crc.GetString("ledstate_InOut","出入库中");
MachineLedState.Add(MachineLedStateE.InOut, nls(LedState.none, LedState.blink, LedState.on));
}
Dictionary<LedColor, LedState> nls(LedState AlarmLedstate, LedState StandbyLedstate, LedState RunningLedstate) {
var a = new Dictionary<LedColor, LedState>();
a.Add(LedColor.red, AlarmLedstate);
a.Add(LedColor.yellow, StandbyLedstate);
a.Add(LedColor.green, RunningLedstate);
return a;
}
void ProcessLefCfg(MachineLedStateE machineLedStateE) {
if (!MachineLedState.ContainsKey(machineLedStateE))
{
LogUtil.info("灯塔配置未包含:" + machineLedStateE.ToString());
return;
}
var ledcfg = MachineLedState[machineLedStateE];
foreach (var ledcolor in ledcfg.Keys)
{
if (ledcfg[ledcolor] == LedState.none)
continue;
Led.LedColors[ledcolor].LedState = ledcfg[ledcolor];
}
}
void LedProcess(object o)
{
StandbyLed.LedState = LedState.off;
AlarmLed.LedState = LedState.off;
RunningLed.LedState = LedState.off;
//回原 绿闪
if (RobotManage.Stores.Sum(x => x.runStatus == RunStatus.HomeReset ? 1 : 0) > 0)
{
ProcessLefCfg(MachineLedStateE.HomeReset);
}
//正常 绿亮
else if (RobotManage.Stores.Sum(x => x.runStatus == RunStatus.Running ? 1 : 0) > 0)
{
ProcessLefCfg(MachineLedStateE.Running);
//出入库 绿闪 黄闪
if (RobotManage.Stores.Sum(x=>x.isBusy?1:0)>0){
ProcessLefCfg(MachineLedStateE.InOut);
}
//系统暂停,说明书未定义, 绿闪, 红闪
if (!canRunning || UserPause)
{
ProcessLefCfg(MachineLedStateE.SystemPause);
}
if (!Setting_Init.Device_Disable_Humidity_Alarm && (DateTime.Now - LastHumiCheckTime).TotalSeconds > 30)
{
ProcessLefCfg(MachineLedStateE.THoutRangeOver30m);
}
}
else if (RobotManage.Stores.Sum(x => x.runStatus == RunStatus.Stop ? 1 : 0) > 0)
{
//系统停止时有报警, 红亮
if (RobotManage.Stores.Sum(x => x.isAlarm ? 1 : 0) > 0)
// if (hasAlarm)
{
ProcessLefCfg(MachineLedStateE.AlarmStop);
}
}
//系统运行时报警, 绿亮,红闪
if (runStatus != RunStatus.Stop && isAlarm)
{
ProcessLefCfg(MachineLedStateE.Alarm);
AlarmBuzzer.ON();
}
Led.LedGroup.ForEach((x) => { x.run(); });
}
}
public class Led {
public static List<Led> LedGroup = new List<Led>();
public static Dictionary<LedColor,Led> LedColors = new Dictionary<LedColor, Led>();
public LedState LedState = LedState.off;
public LedColor Color = LedColor.green;
ushort ledio;
public Led(ushort io,LedColor ledColor) {
Color = ledColor;
ledio = io;
LedGroup.Add(this);
LedColors.Add(ledColor, this);
}
IO_VALUE iovalue;
IO_VALUE lastiovalue;
public void run() {
if (this.LedState == LedState.on) {
iovalue = IO_VALUE.HIGH;
}
if (this.LedState == LedState.off)
{
iovalue = IO_VALUE.LOW;
}
if (this.LedState == LedState.blink)
{
if (iovalue == IO_VALUE.LOW)
{
iovalue = IO_VALUE.HIGH;
}
else {
iovalue = IO_VALUE.LOW;
}
}
if (iovalue != lastiovalue) {
lastiovalue = iovalue;
IOManager.WriteSingleDO("", 0x00, ledio, iovalue);
}
}
}
public enum LedState {
none,
off,
on,
blink
}
public enum LedColor
{
red,
yellow,
green
}
public enum MachineLedStateE
{
AlarmStop,
Alarm,
HomeReset,
Running,
SystemPause,
THoutRangeOver30m,
THoutRange,
InOut,
}
}