apiclient 部分修改
This commit is contained in:
parent
16586d67aa
commit
8d679bf00c
@ -14,4 +14,19 @@
|
||||
</providers>
|
||||
</entityFramework>
|
||||
|
||||
<runtime>
|
||||
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
|
||||
<dependentAssembly>
|
||||
|
||||
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
|
||||
|
||||
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
|
||||
|
||||
</dependentAssembly>
|
||||
|
||||
</assemblyBinding>
|
||||
|
||||
</runtime>
|
||||
</configuration>
|
||||
@ -38,13 +38,37 @@
|
||||
<Reference Include="HslCommunication, Version=7.0.1.0, Culture=neutral, PublicKeyToken=cdb2261fa039ed67, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\HslCommunication.7.0.1\lib\net451\HslCommunication.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.6.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Bcl.TimeProvider, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Bcl.TimeProvider.8.0.0\lib\net462\Microsoft.Bcl.TimeProvider.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Polly, Version=8.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Polly.8.5.2\lib\net472\Polly.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Polly.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=c8a3ffc3f8f825cc, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Polly.Core.8.5.2\lib\net472\Polly.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.ComponentModel.Annotations.4.5.0\lib\net461\System.ComponentModel.Annotations.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Numerics" />
|
||||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
|
||||
@ -63,6 +63,15 @@ namespace WindowsFormsApp1
|
||||
continue;
|
||||
}
|
||||
|
||||
// test
|
||||
ApiClient apiClient = new ApiClient();
|
||||
otherdata otherdata = new otherdata();
|
||||
otherdata.JS_SN = "12345";
|
||||
plcdata PlcData = new plcdata();
|
||||
|
||||
var res = apiClient.CallFunctionByName("SN_CheckIN", PlcData, otherdata);
|
||||
|
||||
|
||||
SiemensHelp siemensHelp = Plc.plchelp;
|
||||
|
||||
var openresult = siemensHelp.OpenConn();
|
||||
@ -75,7 +84,7 @@ namespace WindowsFormsApp1
|
||||
}
|
||||
|
||||
List<t_plcaddress> plcaddress = Init.t_Plcaddresses.FindAll(a => a.plcid == Plc.id);
|
||||
plcdata PlcData = new plcdata();
|
||||
|
||||
|
||||
var resdresult = siemensHelp.ReadAddress(plcaddress, PlcData);
|
||||
if (!resdresult.Success)
|
||||
@ -123,9 +132,7 @@ namespace WindowsFormsApp1
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ApiClient apiClient = new ApiClient();
|
||||
otherdata otherdata = new otherdata();
|
||||
otherdata.JS_SN = "12345";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
@ -1,6 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="HslCommunication" version="7.0.1" targetFramework="net48" />
|
||||
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" />
|
||||
<package id="Microsoft.Bcl.TimeProvider" version="8.0.0" targetFramework="net48" />
|
||||
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
|
||||
<package id="Polly" version="8.5.2" targetFramework="net48" />
|
||||
<package id="Polly.Core" version="8.5.2" targetFramework="net48" />
|
||||
<package id="sqlite-net" version="1.6.292" targetFramework="net48" />
|
||||
<package id="System.ComponentModel.Annotations" version="4.5.0" targetFramework="net48" />
|
||||
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
|
||||
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
|
||||
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
|
||||
</packages>
|
||||
@ -8,72 +8,429 @@ using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
using WindowsFormsApp1.mod;
|
||||
using WindowsFormsApp1.mod.server;
|
||||
using Polly;
|
||||
using Polly.Timeout;
|
||||
using System.Net.Http;
|
||||
using WindowsFormsApp1.db;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace WindowsFormsApp1.server
|
||||
{
|
||||
public class ApiClient
|
||||
{
|
||||
public ApiFormResult SN_CheckIN(plcdata PlcData, otherdata OtherData)
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly Dictionary<string,string> apiUrlDict;
|
||||
|
||||
public ApiClient()
|
||||
{
|
||||
_httpClient = new HttpClient();
|
||||
SQLiteHelper<t_interface> helper = new db.SQLiteHelper<t_interface>();
|
||||
var apiList = helper.QueryAll().Data.ToList();
|
||||
apiUrlDict = apiList.ToDictionary(x => x.code, x => x.url);
|
||||
}
|
||||
|
||||
public ApiFormResult SN_CheckIN(string url,plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "SN_CheckIN",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
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)
|
||||
{
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT))
|
||||
{
|
||||
// 回写对应PLC
|
||||
|
||||
return res.RESULT == "pass" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG, 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);
|
||||
}
|
||||
|
||||
public ApiFormResult Carrier_Check(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Carrier_Check",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
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)
|
||||
{
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT))
|
||||
{
|
||||
// 回写对应PLC
|
||||
|
||||
return res.RESULT == "pass" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG, 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);
|
||||
}
|
||||
|
||||
public ApiFormResult SN_FeedingCheck(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Carrier_Check",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "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)
|
||||
{
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT))
|
||||
{
|
||||
// 回写对应PLC
|
||||
|
||||
return res.RESULT == "pass" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG, 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);
|
||||
}
|
||||
|
||||
public ApiFormResult Link_Comp(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Carrier_Check(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult SN_Release(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult SN_FeedingCheck(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult Glue_CheckOut(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Glue_CheckOut",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
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)
|
||||
{
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT))
|
||||
{
|
||||
// 回写对应PLC
|
||||
|
||||
return res.RESULT == "pass" ? ApiFormResult.Sucess(mesresult.PASS, true, res.MSG, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG, 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);
|
||||
}
|
||||
|
||||
public ApiFormResult Get_Para(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Link_Comp(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult DispatchPara(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult SN_Release(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult Data_Collection(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Glue_CheckOut(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult Status(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Carrier_Check",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
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)
|
||||
{
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT))
|
||||
{
|
||||
// 回写对应PLC
|
||||
|
||||
return res.RESULT == "pass" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG, 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);
|
||||
}
|
||||
public ApiFormResult Get_Para(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult Alarm(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
// 组装 request json
|
||||
var request = new
|
||||
{
|
||||
EventID = "Carrier_Check",
|
||||
Line = "Testline",
|
||||
StationID = "TestStation",
|
||||
MachineID = "TestMachine",
|
||||
OPID = "OPID",
|
||||
AlarmID = OtherData.Plc_ErrorMsg,
|
||||
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)
|
||||
{
|
||||
// 成功!
|
||||
if (!string.IsNullOrEmpty(res.RESULT))
|
||||
{
|
||||
// 回写对应PLC
|
||||
|
||||
return res.RESULT == "pass" ? ApiFormResult.Sucess(mesresult.PASS, false, res.MSG, PlcData, DateTime.Now)
|
||||
: ApiFormResult.Sucess(mesresult.FAIL, false, res.MSG, 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);
|
||||
}
|
||||
public ApiFormResult DispatchPara(plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Data_Collection(plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Status(plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Alarm(plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult OEEDataCollection(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult OEEDataCollection(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult SN_FileUpload(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult SN_FileUpload(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Test(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult Test(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult SN_Checkout(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult SN_Checkout(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
return ApiFormResult.Sucess(mesresult.PASS, false, "", PlcData,DateTime.Now);
|
||||
}
|
||||
public ApiFormResult Initialization(plcdata PlcData, otherdata OtherData)
|
||||
|
||||
public ApiFormResult Initialization(string url, plcdata PlcData, otherdata OtherData)
|
||||
{
|
||||
PlcData.Write_Status = 3;
|
||||
return ApiFormResult.Sucess(mesresult.PASS,false,"", PlcData,DateTime.Now);
|
||||
@ -81,6 +438,62 @@ namespace WindowsFormsApp1.server
|
||||
|
||||
public ApiFormResult CallFunctionByName(string functionName, plcdata plcdata,otherdata otherdata)
|
||||
{
|
||||
// 定义重试策略
|
||||
var retryPolicy = Policy
|
||||
.Handle<Exception>() // 处理HTTP请求异常
|
||||
.WaitAndRetry(2, retryAttempt =>
|
||||
{
|
||||
// 自定义重试间隔
|
||||
switch (retryAttempt)
|
||||
{
|
||||
case 1:
|
||||
return TimeSpan.FromSeconds(3); // 第一次重试间隔3秒
|
||||
case 2:
|
||||
return TimeSpan.FromSeconds(5); // 第二次重试间隔5秒
|
||||
default:
|
||||
return TimeSpan.FromSeconds(0); // 默认间隔0秒
|
||||
}
|
||||
}, (exception, timeSpan, retryCount, context) =>
|
||||
{
|
||||
LogExecute.logNet.WriteError($"重试第 {retryCount} 次,异常信息: {exception.Message}");
|
||||
});
|
||||
|
||||
// 定义超时策略
|
||||
var timeoutPolicy = Policy
|
||||
.Timeout(5, // 超时时间3秒
|
||||
TimeoutStrategy.Pessimistic); // 使用悲观超时策略
|
||||
|
||||
// 获取当前类的类型
|
||||
Type type = this.GetType();
|
||||
|
||||
// 使用反射调用指定名称的方法
|
||||
MethodInfo methodInfo = type.GetMethod(functionName);
|
||||
|
||||
try
|
||||
{
|
||||
// 打印调用开始日志
|
||||
LogExecute.logNet.WriteError("开始调用API...");
|
||||
|
||||
// 使用同步方法调用API,并结合重试策略
|
||||
var result = retryPolicy.Execute(() =>
|
||||
{
|
||||
LogExecute.logNet.WriteError("正在尝试调用API...");
|
||||
// 调用该方法
|
||||
return (ApiFormResult)methodInfo.Invoke(this, new object[] { apiUrlDict[functionName], plcdata, otherdata });
|
||||
});
|
||||
|
||||
// 打印调用结束日志
|
||||
LogExecute.logNet.WriteError("API调用成功。");
|
||||
return result;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// 如果所有调用都失败,打印失败日志
|
||||
LogExecute.logNet.WriteError("所有调用都失败了!");
|
||||
return ApiFormResult.Error(mesresult.FAIL, false, "", null,DateTime.Now);
|
||||
}
|
||||
|
||||
/*
|
||||
// 获取当前类的类型
|
||||
Type type = this.GetType();
|
||||
|
||||
@ -97,6 +510,8 @@ namespace WindowsFormsApp1.server
|
||||
LogExecute.logNet.WriteError($"未找到对应的函数{functionName}");
|
||||
return ApiFormResult.Error(mesresult.NONE, false, $"未找到对应的函数{functionName}", null,DateTime.Now);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user