Services.cs
5.1 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
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System;
using System.IO;
using System.Runtime.Serialization;
using Common;
using DeviceLibrary.Models.Service.Request;
namespace DeviceLibrary.Service
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
internal class WebService : IService
{
static log4net.ILog Log = log4net.LogManager.GetLogger("Service");
public Result Leave(Stream stream)
{
Result result = new Result();
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
DoorInfo doorInfo = JsonHelper.DeserializeJsonToObject<DoorInfo>(s);
if(doorInfo==null)
{
result.code = -1;
result.msg = $"解析异常:{s}";
Log.Error($"Leave 解析异常:{s}");
}
else
{
Log.Info($"AGVLeave接口被调用:{doorInfo}");
Context.LiftContext.AGVLeave(doorInfo,out string msg);
result.msg = msg;
}
return result;
}
public Result Call(Stream stream)
{
Result result = new Result();
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
DoorInfo doorInfo = JsonHelper.DeserializeJsonToObject<DoorInfo>(s);
if (doorInfo == null)
{
result.code = -1;
result.msg = $"解析异常:{s}";
Log.Error($"Call 解析异常:{s}");
}
else
{
Log.Info($"AGVCall接口被调用:{doorInfo}");
Context.LiftContext.AGVCall(doorInfo, out string msg);
result.msg = msg;
}
return result;
}
public Result sendIn(Stream stream)
{
Result result = new Result();
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
SendInInfo sendInInfo = JsonHelper.DeserializeJsonToObject<SendInInfo>(s);
if(sendInInfo ==null)
{
result.code = -1;
result.msg = $"解析异常:{s}";
Log.Error($"sendIn 解析异常:{s}");
}
else
{
Log.Info($"请求接口被调用:{sendInInfo}");
bool idle= Context.LiftContext.RequestSendIn(sendInInfo,out string msg);
result.msg = msg;
}
return result;
}
//public Result status(Stream stream)
//{
// Result result = new Result();
// StreamReader sr = new StreamReader(stream);
// string s = sr.ReadToEnd();
// result.data = Common.JsonHelper.SerializeObject(new Models.Service.Response.Status());
// return result;
//}
public StatusResult status(Stream stream)
{
StatusResult result = new StatusResult();
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
ClientStatus clientStatus = JsonHelper.DeserializeJsonToObject<ClientStatus>(s);
if(clientStatus==null)
{
result.code = -1;
result.msg = $"解析异常:{s}";
Log.Error($"status 解析异常:{s}");
}
else
{
string msg = "";
Context.LiftContext.UpdateClient(clientStatus,out msg);
result.data = Context.LiftContext.GetLiftStatus(clientStatus,out msg);
result.msg = msg;
}
return result;
}
}
public class LiftService
{
private WebServiceHost serviceHost;
public bool State = false;
public void Open(string baseUrl)
{
try
{
if (State)
{
LogUtil.error("Web Service has been opened");
return;
}
WebService service = new WebService();
serviceHost = new WebServiceHost(service, new Uri(baseUrl));//service, new Uri(url)
serviceHost.Open();
State = true;
LogUtil.info("Web Service Opened.");
}
catch (Exception ex)
{
State = false;
LogUtil.error("Web Service Open", ex);
}
}
public void Close()
{
try
{
if (serviceHost != null)//判断服务是否关闭
serviceHost.Close();//关闭服务
State = false;
LogUtil.info("Web Service Closed.");
}
catch (Exception ex)
{
LogUtil.error("Web Service Close", ex);
}
}
}
}