MesUtil.cs
7.6 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
using TSA_V.Common;
namespace TSA_V.DeviceLibrary
{
[ServiceContract(Name = "Services")]
public interface IWebService
{
#region Post x-www-form-urlencoded
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "status", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
DeviceStatus Post_status(Stream info);
#endregion
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WebService : IWebService
{
public WebService()
{
}
public DeviceStatus Post_status(Stream info)
{
DeviceStatus res = MesUtil.GetCurrStatus();
string str = JsonHelper.SerializeObject(res);
LogUtil.info(" WebService Post_status ,return [" + str + "]");
return res;
}
}
public static class MesUtil
{
private static System.ServiceModel.Web.WebServiceHost _serviceHost;
/// <summary>
/// 打开服务
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool Open()
{
//获取状态:http://localhost:4090/rest/api/v1/station/status
string url = ConfigAppSettings.GetValue(Setting_Init.WebServiceAddr);
if (String.IsNullOrEmpty(serverAddress))
{
return false ;
}
LogUtil.info("MesUtil 本地地址【" + url + "】上传数据地址【" + serverAddress + "】");
try
{
WebService service = new WebService();
_serviceHost = new System.ServiceModel.Web.WebServiceHost(service, new Uri(url));
_serviceHost.Open();
LogUtil.info("MesUtil WebService Open: " + url);
return true;
}
catch (Exception ex)
{
LogUtil.error("MesUtil WebService Open [" + url + "] error : " + ex.ToString());
return false;
}
}
/// <summary>
/// 关闭服务
/// </summary>
public static void Close()
{
if (_serviceHost != null)
_serviceHost.Close();
LogUtil.info("MesUtil WebService Close");
}
#region 上传工作结果
public static string serverAddress = ConfigAppSettings.GetValue(Setting_Init.DCSServerAddr);
public static string SendStatus(DeviceStatus status)
{
if (String.IsNullOrEmpty(serverAddress))
{
return "";
}
string result = HttpHelper.PostJson(serverAddress, new JsonParam(status));
LogUtil.info("MesUtil :SendStatus【" + serverAddress + "】 返回值【" + result + "】");
return result;
}
private static string GetParamStr(Dictionary<string, object> map)
{
string str = "";
foreach (string key in map.Keys)
{
if (str.Equals(""))
{
str = key + "=" + map[key];
}
else
{
str += "&" + key + "=" + map[key];
}
}
return str;
}
#endregion
#region 数据获取和保存
public static OpInfo GetWorkInfo()
{
OpInfo op = new OpInfo();
if (TSAVBean.Work.IsWorking)
{
op = OpInfo.GetOpInfo(TSAVBean.Work.currBoard, TSAVBean.Work.currIndex);
}
else if (TSAVBean.LineStep.moveType.Equals(1))
{
op = OpInfo.GetOpInfo(TSAVBean.Work.currBoard);
}
if (CodeISOk)
{
op.BarCode = CurrPCBCode;
op.AoiResult = CurrAOIResult;
}
return op;
}
public static DeviceStatus GetCurrStatus()
{
DeviceStatus deviceStatus = new DeviceStatus();
deviceStatus.CurrStatus = 0;
OpInfo op = new OpInfo();
if (TSAVBean.Work.IsWorking)
{
deviceStatus.CurrStatus = 3;
op = OpInfo.GetOpInfo(TSAVBean.Work.currBoard, TSAVBean.Work.currIndex);
}
else if (TSAVBean.LineStep.moveType.Equals(1))
{
deviceStatus.CurrStatus = 3;
op = OpInfo.GetOpInfo(TSAVBean.Work.currBoard);
}
else if (TSAVBean.Status.Equals(TSAVStatus.Reset))
{
deviceStatus.CurrStatus = 1;
}
else if (TSAVBean.Status >= TSAVStatus.Runing)
{
deviceStatus.CurrStatus = 2;
}
if (CodeISOk)
{
op.BarCode = CurrPCBCode;
op.AoiResult = CurrAOIResult;
}
deviceStatus.workInfo = op;
deviceStatus.WarnMsg = TSAVBean.WarnMsg;
string msg = JsonHelper.SerializeObject(deviceStatus);
return deviceStatus;
}
public static DeviceStatus GetTestStatus(BoardInfo board, string pcbcode = "")
{
DeviceStatus deviceStatus = new DeviceStatus();
OpInfo op = OpInfo.GetOpInfo(board,board.GetSmtList().Count);
deviceStatus.CurrStatus = 3;
//op.ProName = board.boardName;
//op.ProType = board.boardCode;
//op.BarCode = pcbcode;
//op.BoardWidth = board.boardWidth;
//op.BoardLength = board.boardLength;
//op.pointList = new List<OpPointInfo>();
//foreach (SMTPointInfo sm in board.smtList)
//{
// op.pointList.Add(new OpPointInfo(sm.PartNum, sm.pointName));
//}
deviceStatus.workInfo = op;
deviceStatus.WarnMsg = TSAVBean.WarnMsg;
string msg = JsonHelper.SerializeObject(deviceStatus);
return deviceStatus;
}
public static bool CodeISOk = false;
public static string CurrPCBCode = "";
public static string CurrAOIResult = "";
public static void SetCode(string pCBCode, List<string> codeList)
{
CurrPCBCode = pCBCode;
CodeISOk = true;
CurrAOIResult = "";
LogUtil.info("MesUtil: SetCode [" + CurrPCBCode + "][" + CurrAOIResult + "]");
}
public static void ClearCode()
{
CodeISOk = false;
CurrPCBCode = "";
CurrAOIResult = "";
}
public static void SetAOIResult(int result)
{
if (result.Equals(0))
{
CurrAOIResult = "";
}
else if (result.Equals(1))
{
CurrAOIResult = "OK";
}
else if (result.Equals(2))
{
CurrAOIResult = "NG";
}
LogUtil.info("MesUtil: SetAOIResult [" + CurrPCBCode + "][" + CurrAOIResult + "]");
}
#endregion
}
}