feat - 添加httpclient与MES交互,逻辑待开发

master
SoulStar 3 months ago
parent 3cf29b3435
commit 7f67a8c232

@ -29,6 +29,7 @@ namespace Sln.Iot.Business
_rfid06 = new RFID06Business();
_errorAnalyse = new ErrorAnalyseBusiness();
_recipeBusiness = new RecipeBusiness();
}
}
}

@ -0,0 +1,90 @@
using Microsoft.AspNetCore.Http;
using Sln.Iot.PLC;
using Sln.Iot.Serilog;
using Sln.Iot.Socket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace Sln.Iot.Business
{
public class MesReportBusiness
{
private Timer _timer;
private readonly SerilogHelper _log = SerilogHelper.Instance;
private readonly DeltaHttpClient _deltaHttpClient = DeltaHttpClient.Instance;
public string InterfaceID;
public string Status = "0";
public string StatusCode = "0";
public int PassQuantity = 0;
public int FailQuantity = 0;
public string ErrorCount = "0";
public string ErrorTime = "0";
public double CycleTime = 0;
public double RunningTime = 0;
public double WaitingTime = 0;
public int InputQuantity = 0;
public string Barcode = "0";
public int Cya = 0;
public MesReportBusiness()
{
_deltaHttpClient.Init();
_timer = new Timer(TimerCallback, null, 0, 1000*15);
}
public void TimerCallback(object? state)
{
_deltaHttpClient.PostRequestSend(tranter());
}
private string tranter()
{
#region 傳送字的資料
string strSendData;
strSendData = "[{";
// strSendData += "\"factory\":\"" + "DG5" + "\",";
strSendData += "\"interfaceID\":\"" + InterfaceID + "\",";
// strSendData += "\"EquipType\":" + EquipType + ",";
strSendData += "\"status\":" + Status + ",";
strSendData += "\"statusCode\":\"" + StatusCode + "\",";
strSendData += "\"passQty\":" + PassQuantity + ",";
strSendData += "\"failQty\":" + FailQuantity + ",";
strSendData += "\"errorCnt\":" + ErrorCount + ",";
strSendData += "\"errorTimes\":" + ErrorTime + ",";
strSendData += "\"cycleTime\":" + CycleTime + ",";
strSendData += "\"runningTime\":" + RunningTime + ",";
strSendData += "\"waitingTime\":" + WaitingTime + ",";
// strSendData += "\"selfCheck\":" + (SELFCHECK ? "1" : "0") + ",";
strSendData += "\"selfCheck\":" + "1" + ",";
strSendData += "\"inputQty\":" + InputQuantity + ",";
strSendData += "\"barcode\":\"" + Barcode + "\",";
strSendData += "\"model\":\"" + Cya + "\",";
//端子壽命
strSendData += "\"paramList\":[";
// strSendData += "{\"paramCode\":\"" + Key1 + "\",";
// strSendData += "\"paramValue\":\"" + Value1 + "\"},";
// strSendData += "{\"paramCode\":\"" + Key2 + "\",";
// strSendData += "\"paramValue\":\"" + Value2 + "\"},";
// strSendData += "{\"paramCode\":\"" + Key3 + "\",";
// strSendData += "\"paramValue\":\"" + Value3 + "\"},";
//因为我们的数据此部分没有如果再remove将删除[,导致格式错误
// strSendData = strSendData.Remove(strSendData.Length - 1);//20170525 Tim 修改最後一個,沒刪除的bug
strSendData += "]";
strSendData += "}]";
#endregion
return strSendData;
}
}
}

@ -72,6 +72,7 @@ namespace Sln.Iot.Business
//转换托盘码
string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
traycode = traycode.Replace("?", "");
traycode = traycode.Replace("\0", "");
_log.Info($"托盘到达-RFID01-托盘号-[{traycode}]");
byte[] prodBytes = pordBytesResult1.Content.Concat(pordBytesResult2.Content).ToArray();

@ -65,6 +65,7 @@ namespace Sln.Iot.Business
{
//转换托盘码
string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
traycode = traycode.Replace("?", "");
traycode = traycode.Replace("\0", "");
_log.Info($"托盘到达-RFID02-托盘号-[{traycode}]");
@ -79,10 +80,7 @@ namespace Sln.Iot.Business
}
bool res = false;
if (!res)
{
_log.Error("胶机1真空箱内数据库写入异常");
}
//写入完成信号
res = _plc.PlcWrite(_plc.DeltaInstance1, "D800", 2002, DataTypeEnum.UInt16).IsSuccess;

@ -59,6 +59,7 @@ namespace Sln.Iot.Business
{
//转换托盘码
string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
traycode = traycode.Replace("?", "");
traycode = traycode.Replace("\0", "");
_log.Info($"托盘到达-RFID03-托盘号-[{traycode}]");
@ -73,7 +74,7 @@ namespace Sln.Iot.Business
bool res = trayBindingService.UpDateTime(DateTime.Now.ToString(), traycode, "1");
if (!res)
{
_log.Error("胶机1真空箱外数据库写入异常");
_log.Error("胶机1真空箱外时间更新失败(未扫托盘号)");
}
//写入完成信号

@ -65,6 +65,7 @@ namespace Sln.Iot.Business
{
//转换托盘吗
string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
traycode = traycode.Replace("?", "");
traycode = traycode.Replace("\0", "");
_log.Info($"托盘到达-RFID04-托盘号-[{traycode}]");
@ -80,7 +81,7 @@ namespace Sln.Iot.Business
bool res = trayBindingService.UpDateTime(DateTime.Now.ToString(), traycode, "2");
if (!res)
{
_log.Error("胶机2真空箱前开始时间数据库写入异常");
_log.Error("胶机2真空箱前时间数据库更新异常");
}
//写入完成信号
@ -126,14 +127,16 @@ namespace Sln.Iot.Business
{
//转换托盘吗
string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
traycode = traycode.Replace("?", "");
traycode = traycode.Replace("\0", "");
_log.Info($"托盘号-[{traycode}]-开始计算时间");
//sql更新
string nowTime = DateTime.Now.ToString();
bool res = TrayBindingService.Instance.UpDateTime(nowTime, traycode, "3");
if (!res)
{
_log.Error("胶机2真空箱前时间计算数据库写入异常");
_log.Error("胶机2真空箱前时间计算数据库更新异常");
}
//取之前存的时间
string time2 = TrayBindingService.Instance.GetTime2ByTrayCode(traycode);

@ -67,6 +67,7 @@ namespace Sln.Iot.Business
{
//转换托盘吗
string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
traycode = traycode.Replace("?", "");
traycode = traycode.Replace("\0", "");
_log.Info($"托盘到达-RFID05-托盘号-[{traycode}]");

@ -77,6 +77,7 @@ namespace Sln.Iot.Business
{
//转换托盘吗
string traycode = Encoding.ASCII.GetString(trayBytesResult.Content);
traycode = traycode.Replace("?", "");
traycode = traycode.Replace("\0", "");
_log.Info($"托盘到达-RFID06-托盘号-[{traycode}]");

@ -77,9 +77,9 @@ public abstract class CFXHelper
PublishEvent(new CFXEnvelope(connected.Handle(cfxHandle, selfURI.ToString(), upperURI.ToString())));
}
catch (Exception e)
catch (Exception ex)
{
throw new InvalidOperationException($"CFX 接口初始化异常:{e.Message}");
SerilogHelper.Instance.Error("CFX接口启动异常", ex);
}
}
@ -118,7 +118,7 @@ public abstract class CFXHelper
//Task.Run(() =>
//{
Endpoint.Publish(env);
_log.Iot($"{Endpoint.CFXHandle}推送事件{env.MessageName}");
//_log.Iot($"{Endpoint.CFXHandle}推送事件{env.MessageName}");
//});
}
catch (Exception e)

@ -0,0 +1,39 @@
using Sln.Iot.Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace Sln.Iot.Common
{
public class GetLocalIP
{
public static string GetLocalIPAddress()
{
Dictionary<string, string> localIP = new Dictionary<string, string>();
try
{
string name = Dns.GetHostName();
IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
foreach (IPAddress ipa in ipadrlist)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine(ipa.ToString());
//localIP.Add(ipa.)
}
}
return "";
}
catch (Exception ex)
{
SerilogHelper.Instance.Error("获取本地IP地址异常", ex);
return string.Empty;
}
}
}
}

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sln.Iot.Common
{
public class GlobalVar
{
//public static int
}
}

@ -10,4 +10,8 @@
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Sln.Iot.Serilog\Sln.Iot.Serilog.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,90 @@
using Sln.Iot.Serilog;
using System.Text;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Sockets;
using HttpClient = TouchSocket.Http.HttpClient;
namespace Sln.Iot.Socket
{
public class DeltaHttpClient
{
private static readonly Lazy<DeltaHttpClient> lazy = new Lazy<DeltaHttpClient>(() => new DeltaHttpClient());
public static DeltaHttpClient Instance
{
get
{
return lazy.Value;
}
}
HttpClient DeltaClent = new HttpClient();
SerilogHelper _log = SerilogHelper.Instance;
/// <summary>
/// 连接台达MES服务器
/// </summary>
public async void Init()
{
try
{
var config = new TouchSocketConfig();
#region Http设置远程服务器地址
config.SetRemoteIPHost("http://10.148.192.32:8090");
#endregion
#region Http客户端获取断线通知
config.ConfigurePlugins(a =>
{
a.AddTcpClosedPlugin(async (c, e) =>
{
Console.WriteLine("客户端断开连接");
await e.InvokeNext();
});
});
#endregion
//配置config
await DeltaClent.SetupAsync(config);
await DeltaClent.ConnectAsync();//先做连接
}
catch (Exception ex)
{
_log.Error("MES连接失败请检查网线连接", ex);
}
}
/// <summary>
/// post请求发送数据到台达MES
/// </summary>
/// <param name="jsonData"></param>
public async void PostRequestSend(string jsonData)
{
var request = new HttpRequest();
request.SetContent(new StringHttpContent(jsonData, Encoding.UTF8, "application/json"))
.InitHeaders()
.SetUrl("/sensordata?sensorId=UploadMachineData")
.SetHost(DeltaClent.RemoteIPHost.Host)
.AsPost();
try
{
//创建一个超时操作10秒后取消。
using var cts = new CancellationTokenSource(1000 * 10);
using (var responseResult = await DeltaClent.RequestAsync(request, cts.Token))
{
var response = responseResult.Response;
var result = await response.GetBodyAsync();
_log.Info($"响应结果:{result}");
}
}
catch (Exception ex)
{
_log.Error("请求发送失败", ex);
}
}
}
}

@ -7,7 +7,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TouchSocket" Version="2.0.0" />
<PackageReference Include="TouchSocket" Version="4.2.1" />
<PackageReference Include="TouchSocket.Http" Version="4.2.1" />
</ItemGroup>
<ItemGroup>

@ -5,6 +5,7 @@ using Sln.Iot.Repository.service;
using System.Collections;
using Sln.Iot.Business;
using Sln.Iot.PLC;
using Sln.Iot.Common;
namespace Sln.Iot.Test
{
@ -76,5 +77,12 @@ namespace Sln.Iot.Test
}
[Fact]
public void Test3()
{
GetLocalIP.GetLocalIPAddress();
}
}
}
}

Loading…
Cancel
Save