using System.Dynamic; using Azure; using Microsoft.AspNetCore.Mvc; using NewPdaSqlServer.Dto.service; using NewPdaSqlServer.entity; using NewPdaSqlServer.util; using Newtonsoft.Json.Linq; using System.ServiceModel; using System.IO; namespace NewPdaSqlServer.Controllers.AGV; [Route("api/[controller]")] [ApiController] public class AgvApiController : ControllerBase { OaApiService m = new OaApiService(); //在类内添加日志方法 // private void LogToFile(string methodName, object request, string response) //{ // string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs", $"AgvLog_{DateTime.Now:yyyyMMdd}.log"); // Directory.CreateDirectory(Path.GetDirectoryName(logPath)); // string logContent = $"[{DateTime.Now:HH:mm:ss}] 方法:{methodName}\n" + // $"请求参数:{JObject.FromObject(request)}\n" + // $"响应结果:{response}\n\n"; // File.AppendAllText(logPath, logContent); //} // 修改现有方法(以AgvTest为例) [HttpPost("AgvTest")] public ResponseResult AgvTest(dynamic queryObj) { try { var binding = new BasicHttpBinding { /* 原有配置 */ }; var endpoint = new EndpointAddress("http://192.168.35.251:8000/MesGet"); var factory = new ChannelFactory(binding, endpoint); var channel = factory.CreateChannel(); string result = channel.PrintHello(); ((IClientChannel)channel).Close(); // 记录日志 //LogToFile(nameof(AgvTest), new { queryObj }, result); return new ResponseResult { status = 0, message = "OK", data = result }; } catch (Exception ex) { //LogToFile(nameof(AgvTest), new { queryObj }, $"异常:{ex}"); return ResponseResult.ResponseError(ex); } } //立库送检 [HttpPost("DefineTask_001")] public ResponseResult DefineTask_001(dynamic queryObj) { try { var binding = new BasicHttpBinding { MaxReceivedMessageSize = 10485760, // 10MB TextEncoding = System.Text.Encoding.UTF8 }; var endpoint = new EndpointAddress("http://192.168.35.251:8000/MesGet"); var factory = new ChannelFactory(binding, endpoint); var channel = factory.CreateChannel(); string result = channel.DefineTask_001("GS2025070700001","TEST.01","AGV测试物料名称", "AGV测试物料规格",100,"台",1); ((IClientChannel)channel).Close(); return new ResponseResult { status = 0, message = "OK", data = result }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //立库检验 [HttpPost("DefineTask_002")] public ResponseResult DefineTask_002(dynamic queryObj) { try { var binding = new BasicHttpBinding { MaxReceivedMessageSize = 10485760, // 10MB TextEncoding = System.Text.Encoding.UTF8 }; var endpoint = new EndpointAddress("http://192.168.35.251:8000/MesGet"); var factory = new ChannelFactory(binding, endpoint); var channel = factory.CreateChannel(); string result = channel.DefineTask_002("GS2025070900010", "合格"); ((IClientChannel)channel).Close(); return new ResponseResult { status = 0, message = "OK", data = result }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //立库呼叫 [HttpPost("DefineTask_005")] public ResponseResult DefineTask_005(dynamic queryObj) { try { var binding = new BasicHttpBinding { MaxReceivedMessageSize = 10485760, // 10MB TextEncoding = System.Text.Encoding.UTF8 }; var endpoint = new EndpointAddress("http://192.168.35.251:8000/MesGet"); var factory = new ChannelFactory(binding, endpoint); var channel = factory.CreateChannel(); string result = channel.DefineTask_005(); ((IClientChannel)channel).Close(); return new ResponseResult { status = 0, message = "OK", data = result }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } //立库入库 [HttpPost("DefineTask_006")] public ResponseResult DefineTask_006(dynamic queryObj) { try { var binding = new BasicHttpBinding { MaxReceivedMessageSize = 10485760, // 10MB TextEncoding = System.Text.Encoding.UTF8 }; var endpoint = new EndpointAddress("http://192.168.35.251:8000/MesGet"); var factory = new ChannelFactory(binding, endpoint); var channel = factory.CreateChannel(); string result = channel.DefineTask_006("C2025070400040", "2.02.51.463", "管线饮水机", "C638-1HA(万和VGX-SR-1A-XM白)", 48, "台", 4); ((IClientChannel)channel).Close(); return new ResponseResult { status = 0, message = "OK", data = result }; } catch (Exception ex) { return ResponseResult.ResponseError(ex); } } } // 在类外部添加服务契约接口 [ServiceContract(Namespace = "http://tempuri.org/")] public interface IImesInterface { [OperationContract(Action = "http://tempuri.org/ImesInterface/PrintHello", ReplyAction = "http://tempuri.org/ImesInterface/PrintHelloResponse")] string PrintHello(); //立库送检 [OperationContract(Action = "http://tempuri.org/ImesInterface/DefineTask_001", ReplyAction = "http://tempuri.org/ImesInterface/DefineTask_001Response")] string DefineTask_001(string MesId, string MatId, string MatName, string MatStandard, int MatNumber, string MatUnit, int Postition); //立库检验 [OperationContract(Action = "http://tempuri.org/ImesInterface/DefineTask_002", ReplyAction = "http://tempuri.org/ImesInterface/DefineTask_002Response")] string DefineTask_002(string MesId, string MatStatus); //立库呼叫 [OperationContract(Action = "http://tempuri.org/ImesInterface/DefineTask_005", ReplyAction = "http://tempuri.org/ImesInterface/DefineTask_005Response")] string DefineTask_005(); //立库入库 [OperationContract(Action = "http://tempuri.org/ImesInterface/DefineTask_006", ReplyAction = "http://tempuri.org/ImesInterface/DefineTask_006Response")] string DefineTask_006(string MesId,string MatId,string MatName,string MatStandard,int MatNumber,string MatUnit,int Postition); }