DataUpload.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. namespace Metis.AutoAnalysis
  12. {
  13. public class DataUpload
  14. {
  15. /// <summary>
  16. /// 检查项目 1 : 夹杂物 2 :晶粒度
  17. /// </summary>
  18. public int CheckItem { get; set; }
  19. public string CheckResult { get; set; }
  20. private const string TEST_CODE_INCLUSION = "G1PI";
  21. private const string TEST_CODE_GRAIN = "G1PN";
  22. public string TESTCODE { get; set; }
  23. public string SAMPLELOTNO { get; set; }
  24. public string SAMPLEID { get; set; }
  25. public string FD_SAMPLE_NO { get; set; }
  26. public string RECVTIME { get; set; }
  27. public string FD_SG_SIGN { get; set; }
  28. public Socket socketclient = null;
  29. public bool m_connected = false;
  30. public void ConnectServer()
  31. {
  32. string ip = ConfigurationManager.AppSettings["server_ip"];
  33. string portstr = ConfigurationManager.AppSettings["server_port"];
  34. int.TryParse(portstr, out int port);
  35. //定义一个套接字监听
  36. socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  37. //获取IP地址
  38. IPAddress address = IPAddress.Parse(ip);
  39. //将获取的IP地址和端口号绑定在网络节点上
  40. IPEndPoint point = new IPEndPoint(address, port);
  41. try
  42. {
  43. //客户端套接字连接到网络节点上,用的是Connect
  44. socketclient.Connect(point);
  45. m_connected = true;
  46. LogHelper.log.Debug("服务器连接成功");
  47. }
  48. catch (Exception ex)
  49. {
  50. m_connected = false;
  51. LogHelper.log.Error("服务器连接失败" + ex.Message);
  52. }
  53. }
  54. private string AllData()
  55. {
  56. JObject jsonSent = new JObject();
  57. if (CheckItem == 1)
  58. {
  59. jsonSent.Add("TESTCODE", TEST_CODE_INCLUSION);
  60. }
  61. else if (CheckItem == 2)
  62. {
  63. jsonSent.Add("TESTCODE", TEST_CODE_GRAIN);
  64. }
  65. JArray infoArray = new JArray();
  66. JObject info1 = new JObject();
  67. //string retime = DateTime.Now.ToLocalTime().ToString();
  68. info1.Add("SAMPLELOTNO", SAMPLELOTNO); //检验批号
  69. info1.Add("SAMPLEID", SAMPLEID); //试批顺序号
  70. info1.Add("FD_SAMPLE_NO", FD_SAMPLE_NO); //试样号
  71. //info1.Add("RECVTIME", retime); //收样时间
  72. JObject data = new JObject();
  73. if (CheckItem == 1)
  74. {
  75. data = InclusionData();
  76. }
  77. else if (CheckItem == 2)
  78. {
  79. data = GrainData();
  80. }
  81. info1.Add("DATA", data);
  82. infoArray.Add(info1);
  83. jsonSent.Add("INFO", infoArray);
  84. String sentStr = jsonSent.ToString();
  85. Console.WriteLine(sentStr);
  86. return sentStr;
  87. }
  88. /// <summary>
  89. /// 晶粒度json DATA
  90. /// </summary>
  91. /// <returns></returns>
  92. private JObject GrainData()
  93. {
  94. string checkResult = CheckResult;
  95. JObject result = new JObject();
  96. float.TryParse(checkResult, out float grade);
  97. result.Add("CRYSTAL_GRAN_VALUE", grade);
  98. return result;
  99. }
  100. /// <summary>
  101. /// 夹杂物json DATA
  102. /// </summary>
  103. /// <returns></returns>
  104. private JObject InclusionData()
  105. {
  106. string checkResult = CheckResult;
  107. string[] arr = checkResult.Split(';');
  108. List<string> gradeList = new List<string>();
  109. for (int i = 0; i < arr.Length; i++)
  110. {
  111. string str = arr[i].Trim();
  112. string[] arrItem = str.Split(' ');
  113. for (int j = 0; j < arrItem.Length; j++)
  114. {
  115. string gradeStr = arrItem[j].Trim();
  116. gradeList.Add(gradeStr);
  117. }
  118. }
  119. List<string> AThickList = new List<string>();
  120. List<string> AThinList = new List<string>();
  121. List<string> BThickList = new List<string>();
  122. List<string> BThinList = new List<string>();
  123. List<string> CThickList = new List<string>();
  124. List<string> CThinList = new List<string>();
  125. List<string> DThickList = new List<string>();
  126. List<string> DThinList = new List<string>();
  127. List<string> DSList = new List<string>();
  128. //e是粗系
  129. foreach (var grade in gradeList)
  130. {
  131. if (string.IsNullOrEmpty(grade))
  132. continue;
  133. string str = grade.Trim();
  134. string type = str.Substring(0, 1);
  135. if (type == "A")
  136. {
  137. if (grade.Substring(grade.Length - 1) == "e")
  138. {
  139. string gradeValStr = grade.Substring(1, grade.Length - 2);
  140. AThickList.Add(gradeValStr);
  141. }
  142. else
  143. {
  144. string gradeValStr = grade.Substring(1, grade.Length - 1);
  145. AThinList.Add(gradeValStr);
  146. }
  147. }
  148. if (type == "B")
  149. {
  150. if (grade.Substring(grade.Length - 1) == "e")
  151. {
  152. string gradeValStr = grade.Substring(1, grade.Length - 2);
  153. BThickList.Add(gradeValStr);
  154. }
  155. else
  156. {
  157. string gradeValStr = grade.Substring(1, grade.Length - 1);
  158. BThinList.Add(gradeValStr);
  159. }
  160. }
  161. if (type == "C")
  162. {
  163. if (grade.Substring(grade.Length - 1) == "e")
  164. {
  165. string gradeValStr = grade.Substring(1, grade.Length - 2);
  166. CThickList.Add(gradeValStr);
  167. }
  168. else
  169. {
  170. string gradeValStr = grade.Substring(1, grade.Length - 1);
  171. CThinList.Add(gradeValStr);
  172. }
  173. }
  174. if (type == "D")
  175. {
  176. if (grade.Substring(1, 1) == "S")
  177. {
  178. string gradeValStr = grade.Substring(2, grade.Length - 2);
  179. DSList.Add(gradeValStr);
  180. }
  181. else
  182. {
  183. if (grade.Substring(grade.Length - 1) == "e")
  184. {
  185. string gradeValStr = grade.Substring(1, grade.Length - 2);
  186. DThickList.Add(gradeValStr);
  187. }
  188. else
  189. {
  190. string gradeValStr = grade.Substring(1, grade.Length - 1);
  191. DThinList.Add(gradeValStr);
  192. }
  193. }
  194. }
  195. }
  196. JObject result1 = new JObject();
  197. //=====================================================
  198. //A类
  199. float maxVal = -10000;
  200. foreach (var item in AThickList)
  201. {
  202. float.TryParse(item, out float fVal);
  203. if (maxVal < fVal)
  204. {
  205. maxVal = fVal;
  206. }
  207. }
  208. if (maxVal > 0)
  209. {
  210. result1.Add("A_THICK_VALUE", maxVal);
  211. }
  212. maxVal = -10000;
  213. foreach (var item in AThinList)
  214. {
  215. float.TryParse(item, out float fVal);
  216. if (maxVal < fVal)
  217. {
  218. maxVal = fVal;
  219. }
  220. }
  221. if (maxVal > 0)
  222. {
  223. result1.Add("A_THIN_VALUE", maxVal);
  224. }
  225. //=====================================================
  226. //B类
  227. maxVal = -10000;
  228. foreach (var item in BThickList)
  229. {
  230. float.TryParse(item, out float fVal);
  231. if (maxVal < fVal)
  232. {
  233. maxVal = fVal;
  234. }
  235. }
  236. if (maxVal > 0)
  237. {
  238. result1.Add("B_THICK_VALUE", maxVal);
  239. }
  240. maxVal = -10000;
  241. foreach (var item in BThinList)
  242. {
  243. float.TryParse(item, out float fVal);
  244. if (maxVal < fVal)
  245. {
  246. maxVal = fVal;
  247. }
  248. }
  249. if (maxVal > 0)
  250. {
  251. result1.Add("B_THIN_VALUE", maxVal);
  252. }
  253. //=====================================================
  254. //C类
  255. maxVal = -10000;
  256. foreach (var item in CThickList)
  257. {
  258. float.TryParse(item, out float fVal);
  259. if (maxVal < fVal)
  260. {
  261. maxVal = fVal;
  262. }
  263. }
  264. if (maxVal > 0)
  265. {
  266. result1.Add("C_THICK_VALUE", maxVal);
  267. }
  268. maxVal = -10000;
  269. foreach (var item in CThinList)
  270. {
  271. float.TryParse(item, out float fVal);
  272. if (maxVal < fVal)
  273. {
  274. maxVal = fVal;
  275. }
  276. }
  277. if (maxVal > 0)
  278. {
  279. result1.Add("C_THIN_VALUE", maxVal);
  280. }
  281. //=====================================================
  282. //D类
  283. maxVal = -10000;
  284. foreach (var item in DThickList)
  285. {
  286. float.TryParse(item, out float fVal);
  287. if (maxVal < fVal)
  288. {
  289. maxVal = fVal;
  290. }
  291. }
  292. if (maxVal > 0)
  293. {
  294. result1.Add("D_THICK_VALUE", maxVal);
  295. }
  296. maxVal = -10000;
  297. foreach (var item in DThinList)
  298. {
  299. float.TryParse(item, out float fVal);
  300. if (maxVal < fVal)
  301. {
  302. maxVal = fVal;
  303. }
  304. }
  305. if (maxVal > 0)
  306. {
  307. result1.Add("D_THIN_VALUE", maxVal);
  308. }
  309. //=====================================================
  310. //DS类
  311. maxVal = -10000;
  312. foreach (var item in DSList)
  313. {
  314. float.TryParse(item, out float fVal);
  315. if (maxVal < fVal)
  316. {
  317. maxVal = fVal;
  318. }
  319. }
  320. if (maxVal > 0)
  321. {
  322. result1.Add("DS_VALUE", maxVal);
  323. }
  324. return result1;
  325. }
  326. public void Send()
  327. {
  328. if (m_connected == false)
  329. return;
  330. string strAll = AllData();
  331. byte[] arr = System.Text.Encoding.UTF8.GetBytes(strAll);
  332. byte[] sentBytes = new byte[arr.Length + 2];
  333. sentBytes[0] = 0x02;
  334. System.Array.Copy(arr, 0, sentBytes, 1, arr.Length);
  335. sentBytes[sentBytes.Length - 1] = 0x03;
  336. try
  337. {
  338. //调用客户端套接字发送字节数组 .ToHexString()
  339. int suc = socketclient.Send(sentBytes);
  340. LogHelper.log.Debug("消息发送成功 " + strAll);
  341. }
  342. catch (Exception ex)
  343. {
  344. LogHelper.log.Debug("消息发送失败 " + ex.Message);
  345. }
  346. }
  347. }
  348. }