CustomerApi.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using DbCommon.BusinessCore.BaseCore;
  2. using DbCommon.Enties.DbModels;
  3. using Newtonsoft.Json;
  4. using ProjectManagementSystem.Common.Function;
  5. using ProjectManagementSystem.Common.Logger;
  6. using ProjectManagementSystem.Common.Models;
  7. using ProjectManagementSystem.Common.Service;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace ProjectManagementSystem.Common.WebApi
  14. {
  15. public class CustomerApi
  16. {
  17. private static string url = AppSetting.TryGetValue<string>("CustomerAddress");
  18. private static RestApi<ResponseResult> restApi = new RestApi<ResponseResult>();
  19. private static RestApi<ResponseData> lesApi = new RestApi<ResponseData>();
  20. private static ApiCallFailedManager apiCallFailedManager = new ApiCallFailedManager();
  21. public static bool ReportAgvARrrived(string taskId, string station, string type)
  22. {
  23. try
  24. {
  25. var body = new RequestReportAgvARrrived();
  26. body.TaskId = taskId;
  27. body.Station = station;
  28. body.Type = type;
  29. var res = restApi.Post(url, null, body, out string content);
  30. CLog.Instance.SystemLog.WriteDebug($"Post {url}{Environment.NewLine} {JsonConvert.SerializeObject(body)}{Environment.NewLine} {content}");
  31. return res.Result == "0";
  32. }
  33. catch (Exception ex)
  34. {
  35. CLog.Instance.SystemLog.WriteError(ex.Message);
  36. }
  37. return false;
  38. }
  39. public static bool AgvCallback(RequestAgvCallback body, out string content)
  40. {
  41. //return LesApi("agvCallback", body, out content);
  42. var dataList = apiCallFailedManager.GetList();
  43. if (dataList.Count > 0)
  44. {
  45. InsertApiData(body);
  46. content = "有未回传的数据";
  47. return false;
  48. }
  49. bool result = LesApi("agvCallback", body, out content);
  50. if (!result)
  51. {
  52. InsertApiData(body);
  53. }
  54. return result;
  55. }
  56. private static void InsertApiData(RequestAgvCallback body)
  57. {
  58. ApiCallFailed data = new ApiCallFailed();
  59. data.Id = body.reqCode;
  60. data.Status = 0;
  61. data.CreateTime = DateTime.Now;
  62. data.ApiName = "agvCallback";
  63. data.Body = JsonConvert.SerializeObject(body);
  64. apiCallFailedManager.Insert(data);
  65. }
  66. public static bool AgvAndonRequestApi(RequestAgvAndonRequestApi body, out string content)
  67. {
  68. return LesApi("agvAndonRequestApi", body, out content);
  69. }
  70. public static bool AgvEmptyBoxReqApi(RequestAgvEmptyBoxReqApi body, out string content)
  71. {
  72. return LesApi("agvEmptyBoxReqApi", body, out content);
  73. }
  74. public static bool LesApi(string apiName, object body, out string content)
  75. {
  76. content = null;
  77. try
  78. {
  79. string apiUrl = AppSetting.TryGetValue<string>($"{apiName}Url");
  80. if(string.IsNullOrEmpty(apiUrl))
  81. {
  82. apiUrl = $@"http://{url}:8070/agvCallbackService/{apiName}";
  83. }
  84. var res = lesApi.Post(apiUrl, null, body, out content);
  85. CLog.Instance.SystemLog.WriteDebug($"Post {apiUrl}{Environment.NewLine} {(body is string ? body : JsonConvert.SerializeObject(body))}{Environment.NewLine} {content}");
  86. return res.code == "0";
  87. }
  88. catch (Exception ex)
  89. {
  90. CLog.Instance.SystemLog.WriteError(ex.Message);
  91. content = ex.Message;
  92. }
  93. return false;
  94. }
  95. }
  96. }