FixtureService.cs
11.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
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
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System;
using System.IO;
using System.Runtime.Serialization;
using Common;
using DeviceLibrary.manager;
using DeviceLibrary.manager;
namespace DeviceLibrary
{
[ServiceContract(Name = "FixtureServices")]
internal interface IWebService
{
[OperationContract]
[WebInvoke(UriTemplate = "warehouse", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
string WarehouseRequestByPost(Stream stream);
[OperationContract]
[WebInvoke(UriTemplate = "warehouse?name={name}&targetline={targetline}&type={type}", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
string WarehouseRequestByGet(string name, string targetline,int type);
[OperationContract]
[WebInvoke(UriTemplate = "line", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
string LineRequestByPost(Stream stream);
[OperationContract]
[WebInvoke(UriTemplate = "line?name={name}&targetwarehouse={targetwarehouse}&type={type}", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
string LineRequestByGet(string name, string targetwarehouse, int type);
}
[DataContract]
internal class Result
{
/// <summary>
/// 呼叫成功;false:呼叫失败
/// </summary>
[DataMember]
public bool Succeed { get; set; }
/// <summary>
/// 返回对应接口呼叫的参数信息;
/// </summary>
[DataMember]
public string Data { get; set; }
/// <summary>
/// 呼叫成功,为空。呼叫失败会返回原因
/// </summary>
[DataMember]
public string Msg { get; set; }
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
internal class ClsWebService : IWebService
{
static log4net.ILog Log = log4net.LogManager.GetLogger("FixtureServices");
public string LineRequestByGet(string name, string targetwarehouse, int type)
{
Result res;
name = name.ToUpper();
targetwarehouse = SettingString .Warehouse;
manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetwarehouse, manager.MissionType.Line);
if (!NodeManager.HasNode(name))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{name}不存在" };
Log.Error("产线呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());
}
else if(!NodeManager.HasNode(targetwarehouse))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{targetwarehouse}不存在" };
Log.Error("产线呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());
}
else
{
if (!FixMissionManager.Contains(missionInfo))
{
FixMissionManager.Add(missionInfo);
res = new Result() { Succeed = true, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "" };
Log.Info("产线呼叫任务成功[GET]:" + missionInfo.ToString());
}
else
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "该任务已存在" };
Log.Error("产线呼叫任务失败[GET](已存在相同任务):" + missionInfo.ToString());
}
}
return JsonHelper.SerializeObject(res);
}
public string LineRequestByPost(Stream stream)
{
Result res;
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(s);
string name = nvc["name"].ToUpper();
string targetwarehouse = SettingString.Warehouse;//nvc["targetwarehouse"].ToUpper();
int type = int.Parse(nvc["type"]);
manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetwarehouse, manager.MissionType.Line);
if (!NodeManager.HasNode(name))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{name}不存在" };
Log.Error("产线呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());
}
else if (!NodeManager.HasNode(targetwarehouse))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = $"节点名{targetwarehouse}不存在" };
Log.Error("产线呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());
}
else
{
if (!FixMissionManager.Contains(missionInfo))
{
FixMissionManager.Add(missionInfo);
res = new Result() { Succeed = true, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "" };
Log.Info("产线呼叫任务成功[POST]:" + missionInfo.ToString());
}
else
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetwarehouse},type:{type}", Msg = "该任务已存在" };
Log.Error("产线呼叫任务失败[POST](已存在相同任务):" + missionInfo.ToString());
}
}
return JsonHelper.SerializeObject(res);
}
public string WarehouseRequestByGet(string name, string targetline, int type)
{
Result res;
name = SettingString.Warehouse;//name.ToUpper();
targetline = targetline.ToUpper();
manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetline, manager.MissionType.Warehouse);
if (!NodeManager.HasNode(name))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{name}不存在" };
Log.Error("立库呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());
}
else if (!NodeManager.HasNode(targetline))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{targetline}不存在" };
Log.Error("立库呼叫任务失败[GET](节点不存在):" + missionInfo.ToString());
}
else
{
if (!FixMissionManager.Contains(missionInfo))
{
FixMissionManager.Add(missionInfo);
res = new Result() { Succeed = true, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "" };
Log.Info("立库呼叫任务成功[GET]:" + missionInfo.ToString());
}
else
{
res = new Result() { Succeed = false, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "该任务已存在" };
Log.Error("立库呼叫任务失败[GET](已存在相同任务):" + missionInfo.ToString());
}
}
return JsonHelper.SerializeObject(res);
}
public string WarehouseRequestByPost(Stream stream)
{
Result res;
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
System.Collections.Specialized.NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(s);
string name = SettingString.Warehouse;//nvc["name"].ToUpper();
string targetline = nvc["targetline"].ToUpper();
int type = int.Parse(nvc["type"]);
manager.MissionInfo missionInfo = new manager.MissionInfo(name, (manager.FixType)type, targetline, manager.MissionType.Warehouse);
if (!NodeManager.HasNode(name))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{name}不存在" };
Log.Error("立库呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());
}
else if (!NodeManager.HasNode(targetline))
{
res = new Result() { Succeed = false, Data = $"name:{name},targetwarehouse:{targetline},type:{type}", Msg = $"节点名{targetline}不存在" };
Log.Error("立库呼叫任务失败[POST](节点不存在):" + missionInfo.ToString());
}
else
{
if (!FixMissionManager.Contains(missionInfo))
{
FixMissionManager.Add(missionInfo);
res = new Result() { Succeed = true, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "" };
Log.Info("立库呼叫任务成功[POST]:" + missionInfo.ToString());
}
else
{
res = new Result() { Succeed = false, Data = $"name:{name},targetline:{targetline},type:{type}", Msg = "该任务已存在" };
Log.Error("立库呼叫任务失败[POST](已存在相同任务):" + missionInfo.ToString());
}
}
return JsonHelper.SerializeObject(res);
}
}
public class WebService
{
private WebServiceHost _serviceHost;
public bool State = false;
public void Open(string url)
{
try
{
ClsWebService service = new ClsWebService();
_serviceHost = new WebServiceHost(service, new Uri(url));//service, new Uri(url)
_serviceHost.Open();
State = true;
LogUtil.info("Web服务已开启");
}
catch (Exception ex)
{
State = false;
LogUtil.error("Web服务开启", ex);
}
}
public void Close()
{
try
{
if (_serviceHost != null)//判断服务是否关闭
_serviceHost.Close();//关闭服务
State = false;
LogUtil.info("Web服务已关闭");
}
catch (Exception ex)
{
LogUtil.error("Web服务关闭", ex);
}
}
}
}