ParamsController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 
  2. using Cksoft.Data;
  3. using Cksoft.Data.Repository;
  4. using Cksoft.Unity;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Logging;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Net.Http;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using WebApplet.Models;
  16. using DllEapDal.OFILM;
  17. using Newtonsoft.Json;
  18. using DllEapEntity.Dtos;
  19. using Cksoft.Unity.Log4NetConfig;
  20. namespace WebApplet.Controllers
  21. {
  22. /// <summary>
  23. /// EAP 辅助应用sample校验调用的api接口
  24. /// </summary>
  25. [Route("eap/api/[controller]/[action]")]
  26. public class ParamsController : ControllerBase
  27. {
  28. IDatabase db;
  29. public IConfiguration Configuration { get; set; }
  30. public ParamsController(IConfiguration configuration)
  31. {
  32. Configuration = configuration;
  33. }
  34. /// <summary>
  35. /// 机台的机种,机台编号获取
  36. /// </summary>
  37. /// <param name="MId"></param>
  38. /// <returns></returns>
  39. [HttpGet]
  40. public SampleStandard Get(string MId)
  41. {
  42. SampleStandard sample = new SampleStandard();
  43. string type = string.Empty;
  44. string macNum = string.Empty;
  45. using (db = DbFactory.Base("eapslave"))
  46. {
  47. var dal = new SampleDal(db);
  48. type = dal.Get(MId);
  49. LogHelper<MachineMaterialService>.LogError(type, "DA sample 校验", string.Empty);
  50. macNum = dal.GetMacNumber(MId);
  51. }
  52. if (!string.IsNullOrEmpty(type))
  53. {
  54. using (db = DbFactory.Base("qis"))
  55. {
  56. var dal = new SampleDal(db);
  57. return dal.MesGet(MId, type, macNum);
  58. }
  59. }
  60. else
  61. {
  62. sample.code = "0";
  63. sample.msg = "未查询到机种信息,暂不支持Sample校验";
  64. return sample;
  65. }
  66. }
  67. /// <summary>
  68. /// 获取Sample校验参数
  69. /// </summary>
  70. /// <param name="paramModel"></param>
  71. /// <returns></returns>
  72. [HttpPost]
  73. public async Task<object> GetParams([FromBody] ParamModel paramModel)
  74. {
  75. var dal = new SampleDal();
  76. DateTime time = DateTime.Now;
  77. var result = dal.GetParamsAsync(paramModel);
  78. var s = result.Result;
  79. LayuiModel<Sample> sample = new LayuiModel<Sample>();
  80. if (s.code == "ok")
  81. {
  82. sample.code = 1;
  83. sample.msg = s.massage;
  84. Sample ss = new Sample();
  85. ss.sTime = time.ToString("yyyy-MM-dd HH:mm:ss");
  86. ss.macId = s.data.equipmentID;
  87. foreach (var item in s.data.parameters)
  88. {
  89. if (item.paramName == "560")
  90. {
  91. ss.xOffset = Math.Round(Convert.ToDouble(item.paramValue), 1).ToString();
  92. }
  93. if (item.paramName == "561")
  94. {
  95. ss.yOffset = Math.Round(Convert.ToDouble(item.paramValue), 1).ToString(); ;
  96. }
  97. if (item.paramName == "562")
  98. {
  99. ss.tOffset = Math.Round(Convert.ToDouble(item.paramValue), 2).ToString(); ;
  100. }
  101. }
  102. List<Sample> li = new List<Sample>();
  103. li.Add(ss);
  104. sample.data = li;
  105. }
  106. else
  107. {
  108. sample.code = 0;
  109. sample.msg = s.code;
  110. }
  111. return sample;
  112. }
  113. /// <summary>
  114. /// 命令调用
  115. /// </summary>
  116. /// <param name="paramModel"></param>
  117. /// <returns></returns>
  118. [HttpPost]
  119. public async Task<object> post([FromBody] ParamModell paramModel)
  120. {
  121. try
  122. {
  123. string str = string.Empty;
  124. string url = AppConfigurtaionServices.Configuration["url"]; ;
  125. foreach (var item in paramModel.MacCode)
  126. {
  127. Stopwatch sw = new Stopwatch();
  128. sw.Reset();
  129. sw.Start();
  130. var paras = paramModel.Paras;//new string[] { "283" };
  131. var obj = new Models.MachineInfo { EquipmentID = item, ParamsList = paras };
  132. HttpClient client = new HttpClient(new HttpClientHandler
  133. {
  134. AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
  135. });
  136. client.DefaultRequestHeaders.Add("Method", "POST");
  137. client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; charset=utf-8");
  138. var content = new MultipartFormDataContent();
  139. var uri = new Uri(url);
  140. var equipmentId = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.EquipmentID));
  141. content.Add(equipmentId, "EquipmentID");
  142. for (int i = 0; i < obj.ParamsList.Count(); i++)
  143. {
  144. var temp = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.ParamsList.ElementAt(i)));
  145. content.Add(temp, $"ParamsList[{i}]");
  146. }
  147. client.Timeout = new TimeSpan(0, 0, 600);
  148. LogHelper<MachineMaterialService>.LogError("请求地址: " + url + "请求内容: " + content.ToJson(), "DA sample 校验", string.Empty);
  149. var result = await client.PostAsync(uri, content);
  150. DateTime time = DateTime.Now;
  151. sw.Stop();
  152. // Console.WriteLine("请求用时:" + sw.ElapsedMilliseconds / 1000);
  153. if (result == null || result.IsSuccessStatusCode == false)
  154. {
  155. Console.WriteLine("----------------");
  156. LogHelper<MachineMaterialService>.LogError("请求返回的结果" + result.ToJson(), "DA sample 校验", string.Empty);
  157. Console.WriteLine(result);
  158. Console.WriteLine("----------------");
  159. }
  160. if (result != null && result.IsSuccessStatusCode)
  161. {
  162. var bytes = result.Content.ReadAsByteArrayAsync().Result;
  163. str+= Encoding.UTF8.GetString(bytes);
  164. }
  165. }
  166. return str;
  167. }
  168. catch (Exception ex)
  169. {
  170. return "请求超时";
  171. }
  172. }
  173. /// <summary>
  174. /// 数据上传接口
  175. /// </summary>
  176. /// <param name="sample"></param>
  177. /// <returns></returns>
  178. [HttpPost]
  179. public async Task<object> GetMesInfo([FromBody] Sample sample)
  180. {
  181. try
  182. {
  183. LogHelper<MachineMaterialService>.LogError("GetMesInfo: " + sample.ToJson(), "DA sample 校验", string.Empty);
  184. DASampleService.EapSampleServiceClient client = new DASampleService.EapSampleServiceClient();
  185. string x = JsonConvert.SerializeObject(sample);
  186. string result = client.checkSample(x);
  187. var temp = JsonConvert.DeserializeObject<Response>(result);
  188. LogHelper<MachineMaterialService>.LogError("返回值: " + result, "DA sample 校验", string.Empty);
  189. using (db = DbFactory.Base("eap"))
  190. {
  191. SampleDal dal = new SampleDal(db);
  192. dal.Set(sample, temp.message/*"test"*/);
  193. }
  194. return temp.message;
  195. /*return "TEST";*/
  196. #region 没有用
  197. //string url = "";
  198. //string macCode = paramModel.MacCode;
  199. //Stopwatch sw = new Stopwatch();
  200. //sw.Reset();
  201. //sw.Start();
  202. //var paras = paramModel.Paras;//new string[] { "283" };
  203. //var obj = new MachineInfo { EquipmentID = macCode, ParamsList = paras };
  204. //HttpClient client = new HttpClient(new HttpClientHandler
  205. //{
  206. // AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
  207. //});
  208. //client.DefaultRequestHeaders.Add("Method", "POST");
  209. //client.DefaultRequestHeaders.Add("ContentType", "multipart/form-data; charset=utf-8");
  210. //var content = new MultipartFormDataContent();
  211. //var uri = new Uri(url);
  212. //var equipmentId = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.EquipmentID));
  213. //content.Add(equipmentId, "EquipmentID");
  214. //for (int i = 0; i < obj.ParamsList.Count(); i++)
  215. //{
  216. // var temp = new ByteArrayContent(Encoding.UTF8.GetBytes(obj.ParamsList.ElementAt(i)));
  217. // content.Add(temp, $"ParamsList[{i}]");
  218. //}
  219. //client.Timeout = new TimeSpan(0, 0, 600);
  220. //var result = await client.PostAsync(uri, content);
  221. //sw.Stop();
  222. //// Console.WriteLine("请求用时:" + sw.ElapsedMilliseconds / 1000);
  223. //if (result == null || result.IsSuccessStatusCode == false)
  224. //{
  225. // Console.WriteLine("----------------");
  226. // Console.WriteLine(result);
  227. // Console.WriteLine("----------------");
  228. //}
  229. //if (result != null && result.IsSuccessStatusCode)
  230. //{
  231. // var bytes = result.Content.ReadAsByteArrayAsync().Result;
  232. // return Encoding.UTF8.GetString(bytes);
  233. //}
  234. /* return "请求Mes接口返回信息";
  235. */
  236. #endregion
  237. }
  238. catch (Exception ex)
  239. {
  240. return ex;
  241. }
  242. }
  243. }
  244. }