AppserverHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using System.Net;
  11. using System.IO;
  12. namespace DllEapCommon
  13. {
  14. public class AppserverHelper
  15. {
  16. /// <summary>
  17. /// 停止联网
  18. /// </summary>
  19. /// <param name="db"></param>
  20. /// <param name="maccode"></param>
  21. /// <param name="errorinfo"></param>
  22. /// <returns></returns>
  23. public int StopMachine(IDatabase db, Machine mac, ref string errorinfo)
  24. {
  25. try
  26. {
  27. EapAppserver eapapp = db.FindListForCondition<EapAppserver>($@" and a.id=(SELECT aa.EapAppserverID FROM eap.eapappservermac as aa
  28. inner join machine as bb on aa.macid = bb.id
  29. where bb.id = '{mac.ID}')", ref errorinfo).FirstOrDefault();
  30. if (eapapp == null)
  31. {
  32. errorinfo = $"未找到机台编号={mac.FCode}对应的服务器信息。";
  33. return 1;
  34. }
  35. //调用服务器停止联网接口
  36. // HttpClient client = HttpClientFactory.Create();
  37. string url = $"http://{eapapp.FIp}:{eapapp.FPort}/home/StopMachineById?macId={mac.ID}";
  38. var request = (HttpWebRequest)WebRequest.Create(url);
  39. request.Method = "GET";
  40. var response = (HttpWebResponse)request.GetResponse();
  41. var stream = response.GetResponseStream();
  42. var sr = new StreamReader(stream);
  43. var result = sr.ReadToEnd();
  44. //HttpResponseMessage response = client.GetAsync(url).Result;
  45. //if (!response.IsSuccessStatusCode)
  46. //{
  47. // errorinfo = $"访问API失败:{url},{response.Content}";
  48. // return -1;
  49. //}
  50. //string result = response.Content.ReadAsStringAsync().Result;
  51. JObject obj = JsonConvert.DeserializeObject<JObject>(result);
  52. if (int.Parse(obj["resultcode"].ToString()) <= 0)
  53. {
  54. errorinfo = obj["errorinfo"].ToString();
  55. }
  56. return 1;
  57. }
  58. catch (Exception ex)
  59. {
  60. errorinfo = $"从服务器停止机台联网发生异常:{ex.ToString()}";
  61. return -1;
  62. }
  63. }
  64. /// <summary>
  65. /// 启动联网
  66. /// </summary>
  67. /// <param name="db"></param>
  68. /// <param name="maccode"></param>
  69. /// <param name="errorinfo"></param>
  70. /// <returns></returns>
  71. public int StartMachine(IDatabase db, string maccode, ref string errorinfo)
  72. {
  73. try
  74. {
  75. EapAppserver eapapp = db.FindListForCondition<EapAppserver>($@" and a.id=(SELECT aa.EapAppserverID FROM eap.eapappservermac as aa
  76. inner join machine as bb on aa.macid = bb.id
  77. where bb.FCode = '{maccode}')", ref errorinfo).FirstOrDefault();
  78. if (eapapp == null)
  79. {
  80. errorinfo = $"未找到机台编号={maccode}对应的服务器信息。";
  81. return -1;
  82. }
  83. //调用服务器停止联网接口
  84. HttpClient client = HttpClientFactory.Create();
  85. string url = $"http://{eapapp.FIp}:{eapapp.FPort}/home/StartMachine?maccode={maccode}";
  86. HttpResponseMessage response = client.GetAsync(url).Result;
  87. if (!response.IsSuccessStatusCode)
  88. {
  89. errorinfo = $"访问API失败:{url},{response.Content}";
  90. return -1;
  91. }
  92. string result = response.Content.ReadAsStringAsync().Result;
  93. JObject obj = JsonConvert.DeserializeObject<JObject>(result);
  94. if (int.Parse(obj["resultcode"].ToString()) <= 0)
  95. {
  96. errorinfo = obj["errorinfo"].ToString();
  97. }
  98. return 1;
  99. }
  100. catch (Exception ex)
  101. {
  102. errorinfo = $"从服务器启动机台联网发生异常:{ex.ToString()}";
  103. return -1;
  104. }
  105. }
  106. /// <summary>
  107. /// 刷新RMS配置数据
  108. /// </summary>
  109. /// <param name="db"></param>
  110. /// <param name="maccode"></param>
  111. /// <param name="errorinfo"></param>
  112. /// <returns></returns>
  113. public int RmsRefreshOrders(IDatabase db, string maccode, ref string errorinfo)
  114. {
  115. try
  116. {
  117. EapAppserver eapapp = db.FindListForCondition<EapAppserver>($@" and a.id=(SELECT aa.EapAppserverID FROM eap.eapappservermac as aa
  118. inner join machine as bb on aa.macid = bb.id
  119. where bb.FCode = '{maccode}')", ref errorinfo).FirstOrDefault();
  120. if (eapapp == null)
  121. {
  122. errorinfo = $"未找到机台编号={maccode}对应的服务器信息。";
  123. return -1;
  124. }
  125. MQServer mq = db.FindListForCondition<MQServer>($" and a.id=(SELECT z.MstID FROM eap.mqserverdetail z where z.APServerID={eapapp.ID})", ref errorinfo).FirstOrDefault();
  126. if (mq == null)
  127. {
  128. errorinfo = $"未找到机台编号={maccode}对应的MQ信息。";
  129. return -1;
  130. }
  131. //RMS服务放在MQ服务器上,此为约定,如果RMS不是放在MQ服务器上,则不能这么调用
  132. //调用服务器停止联网接口
  133. HttpClient client = HttpClientFactory.Create();
  134. string url = $"http://{mq.IpAddress}:9602/home/RefreshOrders?maccode={maccode}";
  135. HttpResponseMessage response = client.GetAsync(url).Result;
  136. if (!response.IsSuccessStatusCode)
  137. {
  138. errorinfo = $"访问API失败:{url},{response.Content}";
  139. return -1;
  140. }
  141. string result = response.Content.ReadAsStringAsync().Result;
  142. JObject obj = JsonConvert.DeserializeObject<JObject>(result);
  143. if (int.Parse(obj["resultcode"].ToString()) <= 0)
  144. {
  145. errorinfo = obj["errorinfo"].ToString();
  146. }
  147. return 1;
  148. }
  149. catch (Exception ex)
  150. {
  151. errorinfo = $"从服务器刷新机台指令发生异常:{ex.ToString()}";
  152. return -1;
  153. }
  154. }
  155. }
  156. }