using Cksoft.Data;
using Cksoft.Data.Repository;
using Cksoft.Unity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using WebApplet.Models;
using DllEapDal.OFILM;
using Newtonsoft.Json;
using DllEapEntity.Dtos;
using Cksoft.Unity.Log4NetConfig;
namespace WebApplet.Controllers
{
///
/// EAP 辅助应用sample校验调用的api接口
///
[Route("eap/api/[controller]/[action]")]
public class ParamsController : ControllerBase
{
IDatabase db;
public IConfiguration Configuration { get; set; }
public ParamsController(IConfiguration configuration)
{
Configuration = configuration;
}
///
/// 机台的机种,机台编号获取
///
///
///
[HttpGet]
public SampleStandard Get(string MId)
{
SampleStandard sample = new SampleStandard();
string type = string.Empty;
string macNum = string.Empty;
using (db = DbFactory.Base("eapslave"))
{
var dal = new SampleDal(db);
type = dal.Get(MId);
LogHelper.LogError(type, "DA sample 校验", string.Empty);
macNum = dal.GetMacNumber(MId);
}
if (!string.IsNullOrEmpty(type))
{
using (db = DbFactory.Base("qis"))
{
var dal = new SampleDal(db);
return dal.MesGet(MId, type, macNum);
}
}
else
{
sample.code = "0";
sample.msg = "未查询到机种信息,暂不支持Sample校验";
return sample;
}
}
///
/// 获取Sample校验参数
///
///
///
[HttpPost]
public async Task GetParams([FromBody] ParamModel paramModel)
{
var dal = new SampleDal();
DateTime time = DateTime.Now;
var result = dal.GetParamsAsync(paramModel);
var s = result.Result;
LayuiModel sample = new LayuiModel();
if (s.code == "ok")
{
sample.code = 1;
sample.msg = s.massage;
Sample ss = new Sample();
ss.sTime = time.ToString("yyyy-MM-dd HH:mm:ss");
ss.macId = s.data.equipmentID;
foreach (var item in s.data.parameters)
{
if (item.paramName == "560")
{
ss.xOffset = Math.Round(Convert.ToDouble(item.paramValue), 1).ToString();
}
if (item.paramName == "561")
{
ss.yOffset = Math.Round(Convert.ToDouble(item.paramValue), 1).ToString(); ;
}
if (item.paramName == "562")
{
ss.tOffset = Math.Round(Convert.ToDouble(item.paramValue), 2).ToString(); ;
}
}
List li = new List();
li.Add(ss);
sample.data = li;
}
else
{
sample.code = 0;
sample.msg = s.code;
}
return sample;
}
///
/// 命令调用
///
///
///
[HttpPost]
public async Task post([FromBody] ParamModell paramModel)
{
try
{
string str = string.Empty;
string url = AppConfigurtaionServices.Configuration["url"]; ;
foreach (var item in paramModel.MacCode)
{
Stopwatch sw = new Stopwatch();
sw.Reset();
sw.Start();
var paras = paramModel.Paras;//new string[] { "283" };
var obj = new Models.MachineInfo { EquipmentID = item, ParamsList = paras };
HttpClient client = new HttpClient(new HttpClientHandler
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
});
client.DefaultRequestHeaders.Add("Method", "POST");
client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; charset=utf-8");
var content = new MultipartFormDataContent();
var uri = new Uri(url);
var equipmentId = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.EquipmentID));
content.Add(equipmentId, "EquipmentID");
for (int i = 0; i < obj.ParamsList.Count(); i++)
{
var temp = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.ParamsList.ElementAt(i)));
content.Add(temp, $"ParamsList[{i}]");
}
client.Timeout = new TimeSpan(0, 0, 600);
LogHelper.LogError("请求地址: " + url + "请求内容: " + content.ToJson(), "DA sample 校验", string.Empty);
var result = await client.PostAsync(uri, content);
DateTime time = DateTime.Now;
sw.Stop();
// Console.WriteLine("请求用时:" + sw.ElapsedMilliseconds / 1000);
if (result == null || result.IsSuccessStatusCode == false)
{
Console.WriteLine("----------------");
LogHelper.LogError("请求返回的结果" + result.ToJson(), "DA sample 校验", string.Empty);
Console.WriteLine(result);
Console.WriteLine("----------------");
}
if (result != null && result.IsSuccessStatusCode)
{
var bytes = result.Content.ReadAsByteArrayAsync().Result;
str+= Encoding.UTF8.GetString(bytes);
}
}
return str;
}
catch (Exception ex)
{
return "请求超时";
}
}
///
/// 数据上传接口
///
///
///
[HttpPost]
public async Task GetMesInfo([FromBody] Sample sample)
{
try
{
LogHelper.LogError("GetMesInfo: " + sample.ToJson(), "DA sample 校验", string.Empty);
DASampleService.EapSampleServiceClient client = new DASampleService.EapSampleServiceClient();
string x = JsonConvert.SerializeObject(sample);
string result = client.checkSample(x);
var temp = JsonConvert.DeserializeObject(result);
LogHelper.LogError("返回值: " + result, "DA sample 校验", string.Empty);
using (db = DbFactory.Base("eap"))
{
SampleDal dal = new SampleDal(db);
dal.Set(sample, temp.message/*"test"*/);
}
return temp.message;
/*return "TEST";*/
#region 没有用
//string url = "";
//string macCode = paramModel.MacCode;
//Stopwatch sw = new Stopwatch();
//sw.Reset();
//sw.Start();
//var paras = paramModel.Paras;//new string[] { "283" };
//var obj = new MachineInfo { EquipmentID = macCode, ParamsList = paras };
//HttpClient client = new HttpClient(new HttpClientHandler
//{
// AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
//});
//client.DefaultRequestHeaders.Add("Method", "POST");
//client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; charset=utf-8");
//var content = new MultipartFormDataContent();
//var uri = new Uri(url);
//var equipmentId = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.EquipmentID));
//content.Add(equipmentId, "EquipmentID");
//for (int i = 0; i < obj.ParamsList.Count(); i++)
//{
// var temp = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.ParamsList.ElementAt(i)));
// content.Add(temp, $"ParamsList[{i}]");
//}
//client.Timeout = new TimeSpan(0, 0, 600);
//var result = await client.PostAsync(uri, content);
//sw.Stop();
//// Console.WriteLine("请求用时:" + sw.ElapsedMilliseconds / 1000);
//if (result == null || result.IsSuccessStatusCode == false)
//{
// Console.WriteLine("----------------");
// Console.WriteLine(result);
// Console.WriteLine("----------------");
//}
//if (result != null && result.IsSuccessStatusCode)
//{
// var bytes = result.Content.ReadAsByteArrayAsync().Result;
// return Encoding.UTF8.GetString(bytes);
//}
/* return "请求Mes接口返回信息";
*/
#endregion
}
catch (Exception ex)
{
return ex;
}
}
}
}