liuwf 4 months ago
parent 7346db6e2c
commit de2d79f2d7

@ -0,0 +1,313 @@
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
using System.Text;
namespace HttpTest
{
public class HttpHelper
{
public static string Get(string serviceAddress)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public static async Task<string> GetAsync(string serviceAddress)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = await myStreamReader.ReadToEndAsync();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public static string Post111(string url, string jsonContent)
{
HttpClient _client = new HttpClient();
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = _client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
else
{
throw new HttpRequestException($"POST request to {url} failed with status code {response.StatusCode}");
}
}
public static string SendPostMessage(string ip, int port, string url, string message, string contentType = "application/Text")
{
string retsult = HttpPost("http://" + ip + ":" + port + "/" + url, message, contentType, 30, null);
return retsult;
}
public static string SendGetMessage(string ip, int port, string url)
{
string retsult = HttpGet("http://" + ip + ":" + port + "/" + url);
return retsult;
}
/// <summary>
/// 发起GET同步请求
/// </summary>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public static string HttpGet(string url, Dictionary<string, string> headers = null)
{
using (HttpClient client = new HttpClient())
{
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
HttpResponseMessage response = client.GetAsync(url).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
{
try
{
postData = postData ?? "";
string result = Post("http://localhost:5001/wcs/RecieveRcs/AgvTaskComplete", "");
return result;
//using (HttpClient client = new HttpClient())
//{
// if (headers != null)
// {
// foreach (var header in headers)
// client.DefaultRequestHeaders.Add(header.Key, header.Value);
// }
// using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
// {
// if (contentType != null)
// httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
// HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
// return response.Content.ReadAsStringAsync().Result;
// }
//}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
public static string Post(string serviceAddress, string strContent = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "POST";
request.ContentType = "application/json";
//判断有无POST内容
if (!string.IsNullOrWhiteSpace(strContent))
{
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strContent);
dataStream.Close();
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
}
//public static void TestConnectionAsync(HttpClient client,string url)
//{
// try
// {
// HttpResponseMessage response = client.GetAsync(url).Result;
// if (response.IsSuccessStatusCode)
// {
// Console.WriteLine($"Success: Connected to {url}");
// }
// else
// {
// Console.WriteLine($"Failure: Unable to connect to {url}. Status code: {response.StatusCode}");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Exception: {ex.Message}");
// }
//}
//public static string Post(string serviceAddress, string strContent = null)
//{
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
// request.Method = "POST";
// request.ContentType = "application/Json";
// //判断有无POST内容
// if (!string.IsNullOrWhiteSpace(strContent))
// {
// using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
// {
// dataStream.Write(strContent);
// dataStream.Close();
// }
// }
// HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// string encoding = response.ContentEncoding;
// if (encoding.Length < 1)
// {
// encoding = "UTF-8"; //默认编码
// }
// StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
// string retString = reader.ReadToEnd();
// return retString;
//}
//public static string Post1(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
//{
// postData = postData ?? "";
// using (HttpClient client = new HttpClient())
// {
// if (headers != null)
// {
// foreach (var header in headers)
// client.DefaultRequestHeaders.Add(header.Key, header.Value);
// }
// using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
// {
// if (contentType != null)
// httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
// HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
// return response.Content.ReadAsStringAsync().Result;
// }
// }
//}
public static async Task<string> PostAsync(string serviceAddress, string strContent = null)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
request.Method = "POST";
request.ContentType = "application/Json";
//判断有无POST内容
if (!string.IsNullOrWhiteSpace(strContent))
{
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strContent);
dataStream.Close();
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = await reader.ReadToEndAsync();
return retString;
}
/// <summary>
/// Agv任务是否完成的接口
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
public static bool JudgeAgvTaskComplete()
{
try
{
string url = $"http://192.168.1.103:5001/wcs/RecieveRcs/callMaterial";
string v = HttpHelper.SendPostMessage("127.0.0.1", 5001, "wcs/RecieveRcs/AgvTaskComplete", "", "application/Json");
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 通知Agv小车送料
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
public static bool SendAgvTask(string orderId)
{
try
{
// 创建请求对象
var request = new
{
RawOutstockId = orderId
};
// 序列化为 JSON 字符串
string jsonData = JsonConvert.SerializeObject(request);
string url = $"http://127.0.0.1:5001/wcs/RecieveRcs/callMaterial";
var Result = Post(url, jsonData);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Net.Http.WinHttpHandler" Version="8.0.1" />
</ItemGroup>
</Project>

@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using SlnMesnac.Business.@base;
using SlnMesnac.Common;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
@ -80,6 +81,9 @@ namespace SlnMesnac.Business
/// </summary>
private void EmptyPalletHandle()
{
//读取PLC空托盘流转信号
while (true)
{

@ -1,13 +1,17 @@
using HslCommunication.Secs.Types;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SlnMesnac.Business.@base;
using SlnMesnac.Common;
using SlnMesnac.Common.Model;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.Model.enums;
using SlnMesnac.Plc;
using SlnMesnac.Repository.service;
using SlnMesnac.Repository.service.Impl;
using SlnMesnac.Rfid;
using System;
using System.Collections.Generic;
@ -52,16 +56,20 @@ namespace SlnMesnac.Business
public readonly IBaseRealTaskService _baseRealTaskService;
public readonly IWmsOutStockService _wmsOutStockService;
private List<RealPalletTask> _palletTasks;
private DebugConfig debugConfig = DebugConfig.Instance;
public ProdMgmtBusiness(ILogger<ProdMgmtBusiness> logger, AppConfig appConfig, List<PlcAbsractFactory> plcFactories, List<RfidAbsractFactory> rfidFactories, IMesProductPlanService mesProductPlanService, IBasePalletInfoService basePalletInfoService, IBaseRealTaskService baseRealTaskService, List<RealPalletTask> palletTasks,IServiceProvider serviceProvider) : base(logger, appConfig, plcFactories, rfidFactories, serviceProvider)
public ProdMgmtBusiness(IWmsOutStockService wmsOutStockService,ILogger<ProdMgmtBusiness> logger, AppConfig appConfig, List<PlcAbsractFactory> plcFactories, List<RfidAbsractFactory> rfidFactories, IMesProductPlanService mesProductPlanService, IBasePalletInfoService basePalletInfoService, IBaseRealTaskService baseRealTaskService, List<RealPalletTask> palletTasks,IServiceProvider serviceProvider) : base(logger, appConfig, plcFactories, rfidFactories, serviceProvider)
{
_wmsOutStockService = wmsOutStockService;
_mesProductPlanService = mesProductPlanService;
_basePalletInfoService = basePalletInfoService;
_baseRealTaskService = baseRealTaskService;
_palletTasks = palletTasks;
}
#region 委托事件
@ -101,6 +109,7 @@ namespace SlnMesnac.Business
{
try
{
// var aaa = _mesProductPlanService.GetMesProductPlans();
var info = _mesProductPlanService.GetPlanDtos();
RefreshProdPlanListEvent?.Invoke(info);
}
@ -137,21 +146,24 @@ namespace SlnMesnac.Business
continue;
}
if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("设备叫料")))
BaseRealTask task = _baseRealTaskService.GetExeTask();
if (task != null)
{
RefreshMessage("等待设备叫料信号触发......");
RefreshMessage("已经叫料请等待AGV送料......");
Thread.Sleep(5000);
continue;
}
BaseRealTask task = _baseRealTaskService.GetExeTask();
if (task!=null) {
RefreshMessage("已经叫料请等待AGV送料......");
if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("设备叫料")))
{
RefreshMessage("等待设备叫料信号触发......");
Thread.Sleep(5000);
continue;
}
RefreshMessage("设备要料信号触发成功");
RefreshProdPlanExecEvent?.Invoke(prodPlan);
@ -160,8 +172,12 @@ namespace SlnMesnac.Business
RefreshMessage($"执行计划:{prodPlan.PlanCode};计划数量:{Math.Round(prodPlan.PlanAmount,2)};完成数量:{Math.Round(prodPlan.CompleteAmount,2)};发起叫料申请等待AGV送料......");
//查询下发agv的主键
WmsRawOutstock wmsRaw = _wmsOutStockService.GetProdPlanByPlanCode(prodPlan.PlanCode);
//进行叫料
if (ApplyDeliveryHandle(productPlanDto.MaterialId.ToString()))
if (ApplyDeliveryHandle(wmsRaw.rawOutstockId.ToString()))
{
RefreshMessage("根据计划自动申请叫料成功");
#region 本地创建一个叫料任务
@ -210,19 +226,19 @@ namespace SlnMesnac.Business
if (plc == null)
{
_logger.LogInformation("读取物料到位信号,PLC连接信息为空......");
RefreshMessage("读取物料到位信号,PLC连接信息为空......");
Thread.Sleep(3000);
continue;
}
if (!plc.readBoolByAddress(GetPlcAddressByConfigKey("物料到位")))
{
_logger.LogInformation("等待物料到位信号触发......");
RefreshMessage("等待物料到位信号触发......");
Thread.Sleep(3000);
continue;
}
// TODO 时间校验
// TODO 时间校验,修改为计算设备运行20分钟
bool timeCheck = JudgeProductTime();
if (!timeCheck)
{
@ -356,38 +372,43 @@ namespace SlnMesnac.Business
}
/// <summary>
/// 申请叫料,TODO等待调度接口
/// 申请叫料,等待调度接口
/// </summary>
/// <param name="materialCode"></param>
/// <param name="result"></param>
/// <exception cref="InvalidOperationException"></exception>
public bool ApplyDeliveryHandle(string materialCode)
public bool ApplyDeliveryHandle(String taskId)
{
bool result = false;
try
{
var plc = base.GetPlcByKey("plc");
if (plc != null)
{
if (string.IsNullOrEmpty(materialCode))
{
throw new ArgumentException("申请叫料处理异常:物料参数为空");
}
string url = $"http://localhost:6001/wcs/RecieveRcs/callMaterial";
//询问小车是否离开接口 0已经离开
// string httpResult = HttpHelper.SendPostMessage("127.0.0.1", 6001, "wcs/RecieveRcs/AgvTaskComplete", "", "application/Json");
var content = new
{
rawOutstockId = taskId,
};
// TODO -- 修改为调用wcs接口,参数为申请单id,同时本地生成一个叫料任务,任务状态为执行中
// plc.writeInt16ByAddress(base.GetPlcAddressByConfigKey("WCS叫料"), 1);
// plc.writeStringByAddress(base.GetPlcAddressByConfigKey("物料型号"), materialCode);
string message = JsonConvert.SerializeObject(content);
//询问小车是否离开接口 0已经离开
string httpResult = HttpHelper.SendPostMessage("127.0.0.1", 6001, "wcs/RecieveRcs/callMaterial", message, "application/Json");
var reponseMessage = JsonConvert.DeserializeObject<ReponseMessage>(httpResult);
if (reponseMessage != null && reponseMessage.code == "0")
{
RefreshMessage("Agv叫料成功");
result = true;
}
}
catch (Exception e)
catch (Exception ex)
{
throw new InvalidOperationException($"申请叫料处理异常:{e.Message}");
RefreshMessage($"申请叫料处理异常:{ex.Message}");
result = false;
}
return result;

@ -31,7 +31,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Redis", "SlnMesna
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Extensions", "SlnMesnac.Extensions\SlnMesnac.Extensions.csproj", "{C2CEC75D-FA00-426A-9344-9CF3A8C43BE2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{DD53E273-786B-4947-8CF0-B7C472E10B8D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{DD53E273-786B-4947-8CF0-B7C472E10B8D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

@ -0,0 +1,14 @@
using HttpTest;
namespace Test111
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
// string v = HttpHelper.SendPostMessage("127.0.0.1", 5001, "wcs/RecieveRcs/AgvTaskComplete", "", "application/Json");
HttpHelper.JudgeAgvTaskComplete();
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Loading…
Cancel
Save