AgvClient.cs
7.9 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
using Agv;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class AgvClient
{
private static string ServerIp = ConfigAppSettings.GetValue(Setting_Init.AgvServerIp);
private static int ServerPort = ConfigAppSettings.GetIntValue(Setting_Init.AgvServerPort);
private static Agv.AgvClient agvClient;
public static Dictionary<string, Agv.ClientAction> actionMap = new Dictionary<string, Agv.ClientAction>();
public static Dictionary<string, Agv.ClientShelf> shelfMap = new Dictionary<string, Agv.ClientShelf>();
public static List<string> NodeList = new List<string>();
private static bool isInit = false;
public static void Init()
{
try
{
if (!isInit)
{
isInit = true;
agvClient = new Agv.AgvClient();
agvClient.Received += AgvClient_Received;
agvClient.Connected += AgvClient_Connected;
//NodeList.Add(StoreManager.Config.AgvInName);
//NodeList.Add(StoreManager.Config.AgvOutName);
}
actionMap = new Dictionary<string, Agv.ClientAction>();
shelfMap = new Dictionary<string, Agv.ClientShelf>();
foreach (string key in NodeList)
{
actionMap.Add(key, Agv.ClientAction.None);
shelfMap.Add(key, Agv.ClientShelf.None);
}
agvClient.Connect(ServerIp, ServerPort);
foreach (string id in NodeList)
{
//SetStatus(id, "");
agvClient.SetStatus(id);
LogUtil.info("agv init ,SetStatus[" + id + "]=none ");
}
SetCancelState(false);
}
catch (Exception ex)
{
LogUtil.error("初始化agvClient " + ServerIp + " 出错:", ex);
}
}
private static void AgvClient_Connected(bool status)
{
//LogUtil.info($"AgvClient_Connected:{status}");
}
private static void AgvClient_Received(Node node)
{
/*var nodename = node.Name;
if (nodename == StoreManager.Config.AgvInName) {
SetStatus(nodename, "", ClientAction.MayEnter);
_ = StoreManager.Store.AgvSendShelfIn();
}
else if (nodename == StoreManager.Config.AgvOutName) {
SetStatus(nodename, "", ClientAction.MayLeave);
StoreManager.Store.OneShelfOutProcess();
}*/
AgvClient_Ready(node.Name, node.RFID);
}
public static void SetCancelState(bool cancel)
{
agvClient.CancelState = cancel;
}
public static void SetStatus(string id, string shelfId, ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low, ClientShelf clientShelf=ClientShelf.Empty)
{
if (agvClient == null)
return;
if (actionMap.ContainsKey(id))
{
Agv.ClientAction currA = actionMap[id]; //相同状态就设置一次
if (currA.Equals(action))
{
return;
}
}
agvClient.SetStatus(id, "", shelfId, action, level, clientShelf);
UpdateAction(id, action);
UpdateShelf(id, clientShelf);
LogUtil.info("AgvClient SetStatus id:" + id + ",shelfId:" + shelfId + ",action:" + action.ToString() + ",level:" + level.ToString() + ",clientShelf:" + clientShelf.ToString());
}
private static void AgvClient_Ready(string id, string rfid)
{
try
{
LogUtil.info("收到 AgvClient_Ready [" + id + "] [" + rfid + "] " + GetAction(id).ToString());
if (GetAction(id) == ClientAction.NeedEnter || id == StoreManager.Config.AgvInName)
{
SetStatus(id, "", ClientAction.MayEnter);
_=StoreManager.Store.AgvSendShelfIn();
}
else if (GetAction(id) == ClientAction.NeedLeave || id == StoreManager.Config.AgvOutName)
{
SetStatus(id, "", ClientAction.MayLeave);
StoreManager.Store.OneShelfOutProcess();
}
else
{
LogUtil.error("收到 AgvClient_Arrive [" + id + "] [" + rfid + "] 未找到对应的设备 ,暂不处理");
}
}
catch (Exception ex)
{
LogUtil.error("AgvClient_Ready [" + id + "] [" + rfid + "] 处理出错:" + ex.ToString());
}
}
internal static bool ISConnected()
{
if (agvClient == null)
{
return false;
}
return agvClient.IsConn;
}
public static bool SetToNone(string id, string shelfId = "")
{
Agv.ClientAction currA = GetAction(id);
//if (currA.Equals(Agv.ClientAction.None) || currA.Equals(Agv.ClientAction.NeedLeave) || currA.Equals(Agv.ClientAction.NeedEnter))
//{
SetStatus(id, shelfId, Agv.ClientAction.None);
return true;
//}
// return false;
}
public static bool NeedEnter(string id, string shelfId, ClientShelf clientShelf)
{
Agv.ClientAction currA = GetAction(id);
if (currA.Equals(Agv.ClientAction.None) || currA.Equals(Agv.ClientAction.NeedEnter))
{
SetStatus(id, shelfId, Agv.ClientAction.NeedEnter,ClientLevel.High, clientShelf);
return true;
}
return false;
}
public static bool NeedLeave(string id, string shelfId,ClientShelf clientShelf)
{
Agv.ClientAction currA = GetAction(id);
if (currA.Equals(Agv.ClientAction.None) || currA.Equals(Agv.ClientAction.NeedLeave) || currA.Equals(Agv.ClientAction.NeedEnter))
{
SetStatus(id, shelfId, Agv.ClientAction.NeedLeave,ClientLevel.High, clientShelf);
return true;
}
return false;
}
public static Agv.ClientAction GetAction(string NodeName)
{
if (actionMap.ContainsKey(NodeName))
{
return actionMap[NodeName];
}
return Agv.ClientAction.None;
}
public static Agv.ClientShelf GetShelf(string NodeName)
{
if (shelfMap.ContainsKey(NodeName))
{
return shelfMap[NodeName];
}
return Agv.ClientShelf.None;
}
public static void UpdateAction(string name, Agv.ClientAction action)
{
if (actionMap.ContainsKey(name))
{
actionMap[name] = action;
}
else
{
actionMap.Add(name, action);
}
}
public static void UpdateShelf(string name, Agv.ClientShelf action)
{
if (shelfMap.ContainsKey(name))
{
shelfMap[name] = action;
}
else
{
shelfMap.Add(name, action);
}
}
public static void Dispose()
{
try
{
if (agvClient != null)
{
SetCancelState(true);
agvClient.Close();
}
}
catch (Exception ex)
{
LogUtil.error("agvClient.Close " + ServerIp + " 出错:", ex);
}
}
}
}