VStoreCollection.cs
7.3 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
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
{
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;
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();
}
});
}
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");
}
}
}