VStoreCollection.cs
10.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
using log4net;
using MydataWcfServiceTest.ServiceReference2;
using Newtonsoft.Json;
using OnlineStore.Common;
using RemoteSheardObject;
using RtTower.WebService.ComTowerApp.Contract;
using RtTower.WebService.SharedService;
using System;
using System.Collections.Generic;
using System.IO;
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;
using static RemoteSheardObject.TheLine;
namespace DeviceLibrary
{
public class VStoreCollection
{
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);
//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}");
VStoreList[towerid].log("收到入库完成通知:" + $"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}");
VStoreList[towerid].log("收到出库完成通知:" + $"ReelID:{reelid},TowerID:{towerid}");
if (!VStoreList.ContainsKey(towerid)) {
VLog.Error("收到出库信息,但没有找到该料仓id");
}
else
VStoreList[towerid].OutStoreEnd(reelid, towerid);
//VStoreList[towerid].ReelPutOnOutSide(reelid);
break;
}
}
Thread td;
public void Start() {
td = new Thread(new ThreadStart(Run));
td.Start();
}
bool mstate = false;
DateTime LastQueryCarrierListTime = DateTime.MinValue;
void Run() {
mstate = true;
while (mstate) {
Thread.Sleep(1000);
VStoreList.Values.ToList().ForEach(vs=>
{
try
{
MsgService.MSList["RT-" + vs.CID].clear();
vs.VTowerProcess();
}
catch (Exception ex)
{
VLog.Error("VTowerProcess error:"+ex);
}
finally
{
MsgService.MSList["RT-" + vs.CID].Show();
}
});
if ((DateTime.Now - LastQueryCarrierListTime).TotalHours > 1)
{
LastQueryCarrierListTime = DateTime.Now;
Task.Run(() => { GetCarrierList(); });
}
}
VLog.Info("RT料仓退出主循环");
}
public static Dictionary<string, JobInfo> OutStoreReelInfo = new Dictionary<string,JobInfo>();
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");
}
public void GetCarrierList()
{
try
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = 1073741824;
binding.MaxBufferSize = 1073741824;
binding.MaxBufferPoolSize = 5242880;
binding.AllowCookies = false;
Uri baseAddress = new Uri(Setting_Init.Device_StoreServerURL);
EndpointAddress endpointAddress = new EndpointAddress(baseAddress.ToString());
var towerAppWSClient = new MydataWcfServiceTest.ServiceReference2.TowerAppWSClient(binding, endpointAddress);
VLog.Info("开始查询 CarrierList");
var result = towerAppWSClient.XmlAction(VCmd.GetCarrierList);
VLog.Info("结束查询 CarrierList");
File.WriteAllText("logs\\CarrierList.xml", result.ToString());
towerAppWSClient.Close();
towerAppWSClient = null;
UploadCarrierInformationData();
}
catch (Exception ex)
{
VLog.Error(ex.ToString());
}
}
public void UploadCarrierInformationData()
{
var cril = new List<CarrierInformationData>();
try
{
XmlDocument xml = new XmlDocument();
xml.Load("logs\\CarrierList.xml");
var en = xml.SelectSingleNode("/XmlResult/Errorcode");
if (en.InnerText != "0")
return;
var crl = xml.SelectNodes("/XmlResult/Data/CarrierInformation");
foreach (XmlNode cr in crl)
{
var ci = new CarrierInformationData();
var Depot = cr.SelectSingleNode("Depot");
if (Depot == null)
{
continue;
}
var carrier = cr.SelectSingleNode("Carrier").InnerText;
//var carriers = carrier.Split('#');
//if (carriers.Count()!=4) {
// continue;
//}
ci.carrier = carrier;
//ci.partnumber = cr.SelectSingleNode("Carrier").InnerText;
ci.depot = cr.SelectSingleNode("Depot").InnerText;
ci.diameter = int.Parse(cr.SelectSingleNode("Diameter").InnerText);
ci.height = int.Parse(cr.SelectSingleNode("Height").InnerText);
cril.Add(ci);
}
TheLine.UploadCarrierInformation(cril);
VLog.Info("上传库位信息:" + cril.Count);
}catch (Exception ex)
{
VLog.Error("上传库位信息失败:" + ex);
}
}
}
}