VStoreCollection.cs
11.5 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using log4net;
using Newtonsoft.Json;
using OnlineStore.Common;
using RtTower.WebService.ComTowerApp.Contract;
using RtTower.WebService.SharedService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace DeviceLibrary
{
public class VStoreCollection
{
MydataWcfServiceTest.ServiceReference2.TowerAppWSClient towerAppWSClient;
ServiceHost Host = null;
public VLog VLog = new VLog();
public string StoreServerURL;
public static Dictionary<string, VStore> VStoreList = new Dictionary<string, VStore>();
public VStoreCollection() {
StoreServerURL = Setting_Init.Device_StoreServerURL;
service.OnCommand += Service_OnCommand;
StartService();
if(!string.IsNullOrEmpty(Setting_Init.Runtime_OutStoreReelInfo))
OutStoreReelInfo = JsonConvert.DeserializeObject<Dictionary<string, JobInfo>>(Setting_Init.Runtime_OutStoreReelInfo);
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = 1073741824;
binding.MaxBufferSize = 1073741824;
binding.MaxBufferPoolSize = 5242880;
binding.AllowCookies = false;
Uri baseAddress = new Uri(StoreServerURL);
EndpointAddress endpointAddress = new EndpointAddress(baseAddress.ToString());
towerAppWSClient = new MydataWcfServiceTest.ServiceReference2.TowerAppWSClient(binding, endpointAddress);
//TowerList.List.Add("123132", new TowerInfo() { TowerID = "13123", TowerName = "asdasd" });
//TowerList.SaveTowerInfo();
TowerList.List.Values.ToList().ForEach(s =>
{
VLog.Info("加载RT料仓:" + s.TowerID);
VStore vStore = new VStore(s,this);
VStoreList[s.TowerID] = vStore;
});
}
private void Service_OnCommand(object sender, string xmlString)
{
var cmd= XmlCommandSerializer.DeserializeCommand(xmlString);
switch (cmd.Command) {
case "NotifyCarrierLoad":
// put in your code here
var r = XElement.Parse(cmd.Parameter);
var reelid = r.Descendants("Barcode").FirstOrDefault().Value;
var towerid = r.Descendants("TowerId").FirstOrDefault().Value;
VLog.Info("收到入库完成通知:" + $"ReelID:{reelid},TowerID:{towerid}");
if (!VStoreList.ContainsKey(towerid))
{
VLog.Error("收到入库信息,但没有找到该料仓id");
}else
VStoreList[towerid].InStoreEnd(reelid);
break;
case "NotifyCarrierUnload":
// put in your code here
r = XElement.Parse(cmd.Parameter);
reelid = r.Descendants("Barcode").FirstOrDefault().Value;
towerid = r.Descendants("TowerId").FirstOrDefault().Value;
VLog.Info("收到出库完成通知:" + $"ReelID:{reelid},TowerID:{towerid}");
if (!VStoreList.ContainsKey(towerid)) {
VLog.Error("收到出库信息,但没有找到该料仓id");
}
else
VStoreList[towerid].OutStoreEnd(reelid);
//VStoreList[towerid].ReelPutOnOutSide(reelid);
break;
}
}
Thread td;
public void Start() {
td = new Thread(new ThreadStart(Run));
td.Start();
}
bool mstate = false;
void Run() {
mstate = true;
while (mstate) {
Thread.Sleep(1000);
VStoreList.Values.ToList().ForEach(vs=>
{
try
{
MsgService.MSList["RT-" + vs.CID].clear();
vs.VTowerProcess();
}
finally {
MsgService.MSList["RT-" + vs.CID].Show();
}
});
}
VLog.Info("RT料仓退出主循环");
}
public bool SendCommand(string command,bool needlog, out string msg,params string[] arg) {
string errmsg;
try
{
XmlDocument xd = new XmlDocument();
command = string.Format(command, arg);
xd.LoadXml(command);
}
catch (Exception ex)
{
errmsg = "Command format error:\r\n" + ex.ToString();
msg = errmsg;
return false;
}
try
{
if (needlog)
VLog.Info("send: " + command);
//string command = textBox_sendcommand.Text;
try
{
var re = towerAppWSClient.XmlAction(command);
msg = re.ToString();
return true;
//errmsg = "Request sent successfully.";
}
catch (Exception ex)
{
errmsg = "Failed to send request."+ ex;
msg = errmsg;
return false;
}
}
catch (Exception ex)
{
errmsg = "Error: " + ex.ToString();
msg = errmsg;
}
return false;
}
/*
10001 UnknownTower
10002 no Towers to list
10003 no ProductionSite to list
10010 no Articles to list
10011 Article not found
10012 Article aready exists
10013 Article not deleted
10020 no Carriers to list
10021 Carrier not found
10022 Carrier alredy exists
10023 Carrier already in Tower
10041 JobList not found
10042 JobList creation Error
10043 Joblist creation Warning
*/
public TowerDetailInformation GetTowerDetailInformation(string CID) {
if (!SendCommand(VCmd.GetTowerInformation,false, out string data, CID))
{
VLog.Error(data);
return null;
}
var result = XmlCommandSerializer.DeserializeResult(data);
if (result.Errorcode != 0) {
VLog.Error(data);
return null;
}
return XmlCommandSerializer.DeserializeTowerDetailInformation(result.Data);
}
public static Dictionary<string, JobInfo> OutStoreReelInfo = new Dictionary<string,JobInfo>();
public bool BeginOutStore(JobInfo jobInfo, VLog vLog)
{
if (!SendCommand(VCmd.GetCarrierInformation, true, out string data, jobInfo.WareNum))
{
vLog.Error(data);
return false;
}
var result = XmlCommandSerializer.DeserializeResult(data);
if (result.Errorcode != 0)
{
vLog.Error(data);
return false;
}
var reelinfo = XmlCommandSerializer.DeserializeCarrierInformation(result.Data);
lock (OutStoreReelInfo) {
OutStoreReelInfo[jobInfo.WareNum] = new JobInfo(jobInfo.WareNum,"", reelinfo.Diameter, reelinfo.Height);
Setting_Init.Runtime_OutStoreReelInfo = JsonConvert.SerializeObject(OutStoreReelInfo);
}
vLog.Info("出库时获取到料盘数据:"+ OutStoreReelInfo[jobInfo.WareNum].ToStr());
if (!SendCommand(VCmd.UnloadCarrier,true, out data, jobInfo.WareNum))
{
vLog.Error(data);
return false;
}
result = XmlCommandSerializer.DeserializeResult(data);
if (result.Errorcode != 0)
{
vLog.Error(data);
return false;
}
return true;
}
public bool BeginInStore(string towerid,JobInfo jobInfo,VLog vLog)
{
if (!SendCommand(VCmd.SimulateStartButton, true,out string data, towerid, jobInfo.WareNum))
{
vLog.Error(data);
return false;
}
var result = XmlCommandSerializer.DeserializeResult(data);
if (result.Errorcode != 0)
{
vLog.Error(data);
return false;
}
return true;
}
RtTowerClientService.Service service = new RtTowerClientService.Service();
void StartService()
{
try
{
VLog.Info("start service");
var uri = new Uri("http://localhost:" + 8686 + "/RtTowerClientService");
VLog.Info(uri.ToString());
Host = new ServiceHost(service, uri);
Host.UnknownMessageReceived += Host_UnknownMessageReceived;
//绑定
System.ServiceModel.Channels.Binding httpBinding = new BasicHttpBinding();
//终结点
Host.AddServiceEndpoint(typeof(RtTowerClientService.IRtTowerClientService), httpBinding, uri);
//Host.Open();
if (Host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>() == null)
{
//行为
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
////元数据地址
//behavior.HttpGetUrl = new Uri("http://localhost:18002/Service");
Host.Description.Behaviors.Add(behavior);
}
var serviceDebugBehavior = Host.Description.Behaviors.Find<ServiceDebugBehavior>();
serviceDebugBehavior.HttpHelpPageEnabled = true;
serviceDebugBehavior.IncludeExceptionDetailInFaults = true;
//启动
Host.Open();
//linkLabel1.Text = uri.ToString();
VLog.Info("start service success");
//btn_start.Enabled = false;
//btn_end.Enabled = true;
//label_service_state.Text = Host.State.ToString();
}
catch (Exception ex)
{
VLog.Error(ex.ToString());
//MessageBox.Show(ex.ToString());
}
}
internal void Stop()
{
mstate = false;
}
internal void ShutDown()
{
StopService();
}
private void Host_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
{
try
{
VLog.Info("Host_UnknownMessageReceived:");
VLog.Info(e.Message.ToString());
VLog.Info(e.Message.GetReaderAtBodyContents().ToString());
}
catch (Exception ex)
{
VLog.Error(ex.ToString());
//MessageBox.Show(ex.ToString());
}
}
void StopService()
{
if (Host != null && Host.State == CommunicationState.Opened)
{
Host.Close();
//label_service_state.Text = Host.State.ToString();
Host = null;
}
VLog.Info("stop service success");
}
}
}