修改simenshelp的string读写
整理apiclient
This commit is contained in:
parent
18dfe4f807
commit
6c7278ed45
@ -24,13 +24,88 @@ namespace WindowsFormsApp1.server
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly Dictionary<string, string> apiUrlDict;
|
||||
private readonly string _line;
|
||||
private readonly string _stationID;
|
||||
private readonly string _machineID;
|
||||
private readonly string _opID;
|
||||
|
||||
public ApiClient()
|
||||
{
|
||||
_httpClient = new HttpClient();
|
||||
// 设置超时时间
|
||||
_httpClient.Timeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
SQLiteHelper<t_interface> helper = new db.SQLiteHelper<t_interface>();
|
||||
var apiList = helper.QueryAll().Data.ToList();
|
||||
apiUrlDict = apiList.ToDictionary(x => x.code, x => x.url);
|
||||
|
||||
// 从配置中读取通用参数
|
||||
_line = "Testline";
|
||||
_stationID = "TestStation";
|
||||
_machineID = "TestMachine";
|
||||
_opID = "OPID";
|
||||
}
|
||||
|
||||
// 通用API调用方法,减少代码重复
|
||||
private ApiFormResult CallApi(string url, object request, string eventName, plcdata PlcData, int statusCode = 0)
|
||||
{
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"{eventName} 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (res.RESULT != null && !string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
if (statusCode > 0)
|
||||
PlcData.Write_Status = (short)statusCode;
|
||||
|
||||
return res.RESULT.Value == "PASS"
|
||||
? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
else if (res.IsSuccess != null) // 打印 mes不返回result
|
||||
{
|
||||
// 回写对应PLC
|
||||
if (statusCode > 0)
|
||||
PlcData.Write_Status = (short)statusCode;
|
||||
return res.IsSuccess.Value == true
|
||||
? ApiFormResult.Sucess(mesresult.PASS, true, res.MSG?.Value ?? "", PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG?.Value ?? "", PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogExecute.logNet.WriteError($"{eventName} 接口请求失败: {ex.Message}");
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
}
|
||||
|
||||
public ApiFormResult SN_CheckIN(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -39,61 +114,18 @@ namespace WindowsFormsApp1.server
|
||||
var request = new
|
||||
{
|
||||
EventID = "SN_CheckIN",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
Token = "",
|
||||
FixSN = "",
|
||||
SN = PlcData.Read_MaterialCode0,
|
||||
CarrierID = PlcData.Read_MaterialCode1,
|
||||
SendTime = DateTime.Now
|
||||
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"SN_CheckIN 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.SN_CheckIN;
|
||||
return res.RESULT.Value == "PASS" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "SN_CheckIN", PlcData, PLC_Status.SN_CheckIN);
|
||||
}
|
||||
|
||||
public ApiFormResult Carrier_Check(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -102,60 +134,17 @@ namespace WindowsFormsApp1.server
|
||||
var request = new
|
||||
{
|
||||
EventID = "Carrier_Check",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
Token = "",
|
||||
FixSN = "",
|
||||
CarrierID = PlcData.Read_MaterialCode1,
|
||||
SendTime = DateTime.Now
|
||||
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"Carrier_Check 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.Carrier_Check;
|
||||
return res.RESULT.Value == "PASS" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "Carrier_Check", PlcData, PLC_Status.Carrier_Check);
|
||||
}
|
||||
|
||||
public ApiFormResult SN_FeedingCheck(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -164,64 +153,36 @@ namespace WindowsFormsApp1.server
|
||||
var request = new
|
||||
{
|
||||
EventID = "SN_FeedingCheck",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
Token = "",
|
||||
FixSN = "",
|
||||
SN = OtherData.JS_SN,//扫码
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"SN_FeedingCheck 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.SN_FeedingCheck;
|
||||
return res.RESULT.Value == "PASS" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "SN_FeedingCheck", PlcData, PLC_Status.SN_FeedingCheck);
|
||||
}
|
||||
|
||||
public ApiFormResult Link_Comp(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Link_Comp",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
Token = "",
|
||||
FixSN = "",
|
||||
SN = OtherData.JS_SN,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
return CallApi(url, request, "Link_Comp", PlcData, PLC_Status.Link_Comp);
|
||||
}
|
||||
|
||||
public ApiFormResult SN_Release(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -230,48 +191,39 @@ namespace WindowsFormsApp1.server
|
||||
var request = new
|
||||
{
|
||||
EventID = "SN_Release",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
Token = "",
|
||||
FixSN = "",
|
||||
SN = OtherData.JS_SN,
|
||||
CarrierID = "",
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
// 特殊处理:需要设置Write_stSN
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"SN_Release 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.SN_Release;
|
||||
PlcData.Write_stSN = res.MSG.Value;
|
||||
return res.RESULT.Value == "PASS" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
PlcData.Write_stSN = res.MSG.Value; // 特殊处理
|
||||
return res.RESULT.Value == "PASS"
|
||||
? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -282,6 +234,7 @@ namespace WindowsFormsApp1.server
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogExecute.logNet.WriteError($"SN_Release 接口请求失败: {ex.Message}");
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
@ -293,63 +246,34 @@ namespace WindowsFormsApp1.server
|
||||
var request = new
|
||||
{
|
||||
EventID = "Glue_CheckOut",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = "",//判断站,胶水不用传
|
||||
Token = "",//判断站,胶水不用传
|
||||
FixSN = "",//判断站,胶水不用传
|
||||
SN = PlcData.Read_MaterialCode0,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"Glue_CheckOut 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.Glue_CheckOut;
|
||||
return res.RESULT.Value == "PASS" ? ApiFormResult.Sucess(mesresult.PASS, true, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "Glue_CheckOut", PlcData, PLC_Status.Glue_CheckOut);
|
||||
}
|
||||
|
||||
public ApiFormResult Data_Collection(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Data_Collection",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SN = OtherData.JS_SN,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
return CallApi(url, request, "Data_Collection", PlcData, PLC_Status.Data_Collection);
|
||||
}
|
||||
|
||||
public ApiFormResult Status(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -358,57 +282,16 @@ namespace WindowsFormsApp1.server
|
||||
var request = new
|
||||
{
|
||||
EventID = "Status",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
STATUS = OtherData.Plc_Status,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"Status 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.Status;
|
||||
return res.RESULT.Value == "PASS" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
// 注意:这个API不需要设置PlcData.Write_Status
|
||||
return CallApi(url, request, "Status", PlcData, 0);
|
||||
}
|
||||
|
||||
public ApiFormResult Alarm(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -417,58 +300,16 @@ namespace WindowsFormsApp1.server
|
||||
var request = new
|
||||
{
|
||||
EventID = "Alarm",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
AlarmID = OtherData.Plc_ErrorMsgs[0],
|
||||
SendTime = DateTime.Now,
|
||||
ResetTime = ""
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"Alarm 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.Alarm;
|
||||
return res.RESULT.Value == "PASS" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "Alarm", PlcData, PLC_Status.Alarm);
|
||||
}
|
||||
|
||||
public ApiFormResult SN_FileUpload(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -476,10 +317,10 @@ namespace WindowsFormsApp1.server
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SNList = new List<dynamic>()
|
||||
{
|
||||
new
|
||||
@ -487,7 +328,8 @@ namespace WindowsFormsApp1.server
|
||||
SN = OtherData.JS_SN,
|
||||
FileInfo = new List<dynamic>()
|
||||
{
|
||||
new{
|
||||
new
|
||||
{
|
||||
FileName = OtherData.FileUploadAddress,
|
||||
FilePath = OtherData.FileUploadAddress
|
||||
}
|
||||
@ -496,50 +338,8 @@ namespace WindowsFormsApp1.server
|
||||
},
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"SN_FileUpload 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.ErrorMessage.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.SN_FileUpload;
|
||||
return res.IsSuccess.Value == true ? ApiFormResult.Sucess(mesresult.PASS, true, res.ErrorMessage.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.ErrorMessage.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "SN_FileUpload", PlcData, PLC_Status.SN_FileUpload);
|
||||
}
|
||||
|
||||
public ApiFormResult CodeSoftPrint(string url, plcdata PlcData, otherdata OtherData)
|
||||
@ -547,60 +347,33 @@ namespace WindowsFormsApp1.server
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
Line = "CodeSoftPrint",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
Line = "CodeSoftPrint", // 特殊值,不使用_line
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
SN = PlcData.Read_MaterialCode0
|
||||
};
|
||||
// 使用 Newtonsoft.Json 进行序列化
|
||||
string jsonPayload = JsonConvert.SerializeObject(request);
|
||||
|
||||
// 创建 StringContent 对象,将 JSON 字符串作为请求体,指定编码和媒体类型
|
||||
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
|
||||
|
||||
// 调用接口
|
||||
try
|
||||
{
|
||||
var response = _httpClient.PostAsync(url, httpContent).Result;
|
||||
// 检查响应状态码
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
// 读取响应内容
|
||||
string responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
|
||||
// 使用 Newtonsoft.Json 将响应内容反序列化为动态对象
|
||||
dynamic res = JsonConvert.DeserializeObject(responseContent);
|
||||
|
||||
// 访问动态对象的属性
|
||||
if (res != null)
|
||||
{
|
||||
LogExecute.logNet.WriteDebug($"SN_FileUpload 接口请求成功\n请求:{jsonPayload}\n 返回: {res}");
|
||||
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.ErrorMessage.Value))
|
||||
{
|
||||
// 回写对应PLC
|
||||
PlcData.Write_Status = PLC_Status.CodeSoftPrint;
|
||||
return res.IsSuccess.Value == true ? ApiFormResult.Sucess(mesresult.PASS, true, res.ErrorMessage.Value, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.ErrorMessage.Value, PlcData, DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"请求失败,状态码:{response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "CodeSoftPrint", PlcData, PLC_Status.CodeSoftPrint);
|
||||
}
|
||||
|
||||
public ApiFormResult SN_Checkout(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
// 根据不同的lineCode构建不同的请求
|
||||
var lineCode = Init.t_Line.linecode;
|
||||
|
||||
// 基础请求对象
|
||||
var request = new
|
||||
{
|
||||
EventID = "SN_Checkout",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SN = OtherData.JS_SN,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
// 根据不同的lineCode可以在这里扩展request对象
|
||||
switch (lineCode)
|
||||
{
|
||||
case "OP1060": // 追溯码+载具,默认(todo)
|
||||
@ -642,42 +415,92 @@ namespace WindowsFormsApp1.server
|
||||
case "OP1360"://产品码
|
||||
break;
|
||||
|
||||
// case "OP1180"://助焊剂批次码
|
||||
// break;
|
||||
case "OP1230"://AOI检测结果,三防漆涂胶胶宽
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
return CallApi(url, request, "SN_Checkout", PlcData, PLC_Status.SN_Checkout);
|
||||
}
|
||||
|
||||
public ApiFormResult Initialization(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
PlcData.Write_Status = 3;
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Initialization",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
// 直接设置状态码
|
||||
PlcData.Write_Status = PLC_Status.Initialization;
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
}
|
||||
|
||||
#region 暂时不用
|
||||
public ApiFormResult Test(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
var request = new
|
||||
{
|
||||
EventID = "Test",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
return CallApi(url, request, "Test", PlcData, PLC_Status.Test);
|
||||
}
|
||||
|
||||
public ApiFormResult OEEDataCollection(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
var request = new
|
||||
{
|
||||
EventID = "OEEDataCollection",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
return CallApi(url, request, "OEEDataCollection", PlcData, PLC_Status.OEEDataCollection);
|
||||
}
|
||||
|
||||
public ApiFormResult Get_Para(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
var request = new
|
||||
{
|
||||
EventID = "Get_Para",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
return CallApi(url, request, "Get_Para", PlcData, PLC_Status.Get_Para);
|
||||
}
|
||||
|
||||
public ApiFormResult DispatchPara(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData, DateTime.Now);
|
||||
var request = new
|
||||
{
|
||||
EventID = "DispatchPara",
|
||||
Line = _line,
|
||||
StationID = _stationID,
|
||||
MachineID = _machineID,
|
||||
OPID = _opID,
|
||||
SendTime = DateTime.Now
|
||||
};
|
||||
|
||||
return CallApi(url, request, "DispatchPara", PlcData, PLC_Status.DispatchPara);
|
||||
}
|
||||
#endregion 暂时不用
|
||||
|
||||
@ -706,7 +529,7 @@ namespace WindowsFormsApp1.server
|
||||
// 定义超时策略
|
||||
var timeoutPolicy = Policy
|
||||
.Timeout(5, // 超时时间3秒
|
||||
TimeoutStrategy.Pessimistic); // 使用悲观超时策略
|
||||
TimeoutStrategy.Pessimistic); // 使用 pessimistic 超时策略
|
||||
|
||||
// 获取当前类的类型
|
||||
Type type = this.GetType();
|
||||
@ -714,48 +537,42 @@ namespace WindowsFormsApp1.server
|
||||
// 使用反射调用指定名称的方法
|
||||
MethodInfo methodInfo = type.GetMethod(functionName);
|
||||
|
||||
if (methodInfo == null)
|
||||
{
|
||||
LogExecute.logNet.WriteError($"未找到对应的函数 {functionName}");
|
||||
return ApiFormResult.Error(mesresult.FAIL, false, $"未找到对应的函数 {functionName}", null, DateTime.Now);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 打印调用开始日志
|
||||
LogExecute.logNet.WriteError("开始调用API...");
|
||||
LogExecute.logNet.WriteDebug($"开始调用API: {functionName}...");
|
||||
|
||||
// 检查API URL是否存在
|
||||
if (!apiUrlDict.ContainsKey(functionName))
|
||||
{
|
||||
LogExecute.logNet.WriteError($"未找到API URL配置: {functionName}");
|
||||
return ApiFormResult.Error(mesresult.FAIL, false, $"未找到API URL配置: {functionName}", null, DateTime.Now);
|
||||
}
|
||||
|
||||
// 使用同步方法调用API,并结合重试策略
|
||||
var result = retryPolicy.Execute(() =>
|
||||
{
|
||||
LogExecute.logNet.WriteError("正在尝试调用API...");
|
||||
LogExecute.logNet.WriteDebug($"正在尝试调用API: {functionName}...");
|
||||
// 调用该方法
|
||||
return (ApiFormResult)methodInfo.Invoke(this, new object[] { apiUrlDict[functionName], plcdata, otherdata });
|
||||
});
|
||||
|
||||
// 打印调用结束日志
|
||||
LogExecute.logNet.WriteError("API调用成功。");
|
||||
LogExecute.logNet.WriteDebug($"API调用成功: {functionName}");
|
||||
return result;
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果所有调用都失败,打印失败日志
|
||||
LogExecute.logNet.WriteError("所有调用都失败了!");
|
||||
return ApiFormResult.Error(mesresult.FAIL, false, "", null, DateTime.Now);
|
||||
LogExecute.logNet.WriteError($"API调用失败: {functionName}, 错误: {ex.Message}");
|
||||
return ApiFormResult.Error(mesresult.FAIL, false, ex.Message, null, DateTime.Now);
|
||||
}
|
||||
|
||||
/*
|
||||
// 获取当前类的类型
|
||||
Type type = this.GetType();
|
||||
|
||||
// 使用反射调用指定名称的方法
|
||||
MethodInfo methodInfo = type.GetMethod(functionName);
|
||||
|
||||
if (methodInfo != null)
|
||||
{
|
||||
// 调用该方法
|
||||
return (ApiFormResult)methodInfo.Invoke(this, new object[] { plcdata, otherdata });
|
||||
}
|
||||
else
|
||||
{
|
||||
LogExecute.logNet.WriteError($"未找到对应的函数{functionName}");
|
||||
return ApiFormResult.Error(mesresult.NONE, false, $"未找到对应的函数{functionName}", null,DateTime.Now);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public sealed class PLC_Status
|
||||
@ -781,8 +598,6 @@ namespace WindowsFormsApp1.server
|
||||
public const int Initialization = 3;
|
||||
|
||||
public const int Fail = 1001;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -266,7 +266,18 @@ namespace WindowsFormsApp1.server
|
||||
break;
|
||||
|
||||
case "string":
|
||||
address[i].value = Encoding.ASCII.GetString(read.Content, index, address[i].addresstypelegth);
|
||||
// 读取字符串的最大长度和实际长度
|
||||
byte maxLength = read.Content[index];
|
||||
byte actualLength = read.Content[index + 1];
|
||||
|
||||
// 确保实际长度不超过最大长度和剩余可用空间
|
||||
actualLength = Math.Min(actualLength, (byte)Math.Min(maxLength, address[i].addresstypelegth - 2));
|
||||
|
||||
// 从第3个字节开始读取实际的字符串内容
|
||||
address[i].value = new string(
|
||||
Encoding.ASCII.GetString(read.Content, index + 2, actualLength)
|
||||
.Where(c => c > '\u0003' && !char.IsControl(c))
|
||||
.ToArray());
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -329,17 +340,23 @@ namespace WindowsFormsApp1.server
|
||||
break;
|
||||
|
||||
case "string":
|
||||
byte[] tochar = new byte[address[i].addresstypelegth];
|
||||
|
||||
var byte1 = Convert.ToByte((address[i].addresstypelegth - 2).ToString());
|
||||
var byte2 = Convert.ToByte( (((string)value).Length).ToString() );
|
||||
var byte3 = Encoding.ASCII.GetBytes((string)value);
|
||||
|
||||
tochar[0] = byte1;
|
||||
tochar[1] = byte2;
|
||||
|
||||
Array.Copy(byte3, 0, tochar, 2, byte3.Length);
|
||||
tochar.CopyTo(writebyte, index);
|
||||
string strValue = ((string)value ?? "").TrimEnd();
|
||||
// 计算实际字符串长度,不能超过最大长度(addresstypelegth-2)
|
||||
byte actualLength = (byte)Math.Min(strValue.Length, address[i].addresstypelegth - 2);
|
||||
// 创建包含头部信息的字节数组
|
||||
byte[] stringBytes = new byte[address[i].addresstypelegth];
|
||||
// 设置最大长度(第一个字节)
|
||||
stringBytes[0] = (byte)(address[i].addresstypelegth - 2);
|
||||
// 设置实际长度(第二个字节)
|
||||
stringBytes[1] = actualLength;
|
||||
// 转换字符串内容并复制到字节数组
|
||||
if (actualLength > 0)
|
||||
{
|
||||
byte[] contentBytes = Encoding.ASCII.GetBytes(strValue.Substring(0, actualLength));
|
||||
Array.Copy(contentBytes, 0, stringBytes, 2, actualLength);
|
||||
}
|
||||
// 复制到目标数组
|
||||
stringBytes.CopyTo(writebyte, index);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user