LineWork.cs
6.0 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
using System;
using System.Collections.Generic;
using System.Text;
using Model;
namespace BLL
{
public class LineWork
{
private Advantech.IO_Module[] module;
public delegate void IOChanged();
public event IOChanged DI_Changed;
public delegate void Connected(bool con);
public event Connected Connect_Event;
public LineWork()
{
Load();
module = new Advantech.IO_Module[Common.lineInfos.Count];
for (int i = 0; i < module.Length; i++)
{
module[i] = new Advantech.IO_Module(0, 4, 4, 4, "IO_Line") { IP = Common.lineInfos[i].IP };
module[i].DI_Changed_Event += Module_DI_Changed_Event;
}
}
public void Open()
{
for (int i = 0; i < module.Length; i++)
module[i].Open();
}
public void Close()
{
for (int i = 0; i < module.Length; i++)
module[i].Close();
}
public IJob GetJob()
{
if (Common.FirstFloorCurr < Common.FirstFloorCount)
{
int index = Common.lineInfos.FindIndex(s => s.LineCall && s.ShelfExist);
if (index > -1)
{
Common.log.Info(Common.lineInfos[index].Name + "呼叫送满架");
return new FullShelfJob();
}
else
{
Common.log.Debug("没有找到任何产线呼叫送满架");
return null;
}
}
else
{
Common.log.Debug("一楼货架已满" + Common.FirstFloorCount + "个");
return null;
}
}
public void TakeAway(string key)
{
int index = Common.lineInfos.FindIndex(s => s.Key == key);
if (index == -1) return;
Common.lineInfos[index].LineCall = false;
Common.lineInfos[index].ShelfExist = false;
Common.lineInfos[index].FinalDate = DateTime.Now;
Save();
DI_Changed?.Invoke();
}
public void SendTo(string key)
{
int index = Common.lineInfos.FindIndex(s => s.Key == key);
if (index == -1) return;
Common.lineInfos[index].LineCall = false;
Common.lineInfos[index].ShelfExist = true;
Save();
DI_Changed?.Invoke();
}
private void Load()
{
if (System.IO.File.Exists(Common.PATH_LINE_WORK))
{
string[] lines = System.IO.File.ReadAllLines(Common.PATH_LINE_WORK, Encoding.UTF8);
for (int i = 0; i < lines.Length; i++)
{
//if (lines[i].StartsWith("#")) continue;
if (i == 0) continue; //第一行是标题
string[] str = lines[i].Split(',');
if (str.Length != 4) continue;
int index = Common.lineInfos.FindIndex(s => s.Key == str[0]);
if (index == -1) continue;
Common.lineInfos[index].LineCall = Convert.ToBoolean(str[1]);
Common.lineInfos[index].ShelfExist = Convert.ToBoolean(str[2]);
Common.lineInfos[index].FinalDate = Convert.ToDateTime(str[3]);
}
Common.log.Info("加载缓存文件 " + Common.PATH_LINE_WORK);
}
else
{
Save();
}
}
public void Save()
{
string[] content = new string[Common.lineInfos.Count + 1];
content[0] = "产线名,呼叫,架子,时间";
for (int i = 0; i < Common.lineInfos.Count; i++)
content[i + 1] = string.Format("{0},{1},{2},{3:yyyy-MM-dd HH:mm:ss}", Common.lineInfos[i].Key, Common.lineInfos[i].LineCall, Common.lineInfos[i].ShelfExist, Common.lineInfos[i].FinalDate);
System.IO.File.WriteAllLines(Common.PATH_LINE_WORK, content, Encoding.UTF8);
Common.log.Info("保存缓存文件 " + Common.PATH_LINE_WORK);
}
private void Module_Connect_Event(Advantech.IO_Module box)
{
Connect_Event?.Invoke(box.IsConn);
}
private void Module_DO_Changed_Event(Advantech.IO_Module box, Advantech.IO_State[] sta)
{
}
private void Module_DI_Changed_Event(Advantech.IO_Module box, Advantech.IO_State[] sta)
{
int lineIdx = Common.lineInfos.FindIndex(s => s.IP == box.IP);
if (lineIdx == -1) return;
int add = Common.lineInfos[lineIdx].Address;
if (add >= sta.Length) return;
bool change = false;
if (sta[add] == Advantech.IO_State.On)
{
if (!Common.lineInfos[lineIdx].LineCall) //已经呼叫了就不在更新时间了
{
Common.lineInfos[lineIdx].LineCall = true;
Common.lineInfos[lineIdx].FinalDate = DateTime.Now;
change = true;
}
}
if (change) Save();
DI_Changed?.Invoke();
//List<bool> work = new List<bool>();
//List<string> content = new List<string>();
//for (int i = 0; i < Common.lineInfos.Count; i++)
//{
// if (Common.lineInfos[i].Type == "DI")
// {
// int idx = Common.lineInfos[i].Address;
// if (idx < sta.Length)
// {
// if (sta[idx] == Advantech.IO_State.On)
// Common.lineInfos[i].State = true;
// }
// work.Add(Common.lineInfos[i].State);
// content.Add(Common.lineInfos[i].ToText());
// }
//}
//System.IO.File.WriteAllLines(Common.PATH_LINE_WORK, content, Encoding.UTF8);
//DI_Changed?.Invoke(work.ToArray());
}
}
}