DataUpload.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. }
  47. catch (Exception ex)
  48. {
  49. m_connected = false;
  50. LogHelper.log.Error("服务器连接失败" + ex.Message);
  51. }
  52. }
  53. private string AllData()
  54. {
  55. JObject jsonSent = new JObject();
  56. if (CheckItem == 1)
  57. {
  58. jsonSent.Add("TESTCODE", TEST_CODE_INCLUSION);
  59. }
  60. else if (CheckItem == 2)
  61. {
  62. jsonSent.Add("TESTCODE", TEST_CODE_GRAIN);
  63. }
  64. JArray infoArray = new JArray();
  65. JObject info1 = new JObject();
  66. //string retime = DateTime.Now.ToLocalTime().ToString();
  67. info1.Add("SAMPLELOTNO", SAMPLELOTNO); //检验批号
  68. info1.Add("SAMPLEID", SAMPLEID); //试批顺序号
  69. info1.Add("FD_SAMPLE_NO", FD_SAMPLE_NO); //试样号
  70. //info1.Add("RECVTIME", retime); //收样时间
  71. JObject data = new JObject();
  72. if (CheckItem == 1)
  73. {
  74. data = InclusionData();
  75. }
  76. else if (CheckItem == 2)
  77. {
  78. data = GrainData();
  79. }
  80. info1.Add("DATA", data);
  81. infoArray.Add(info1);
  82. jsonSent.Add("INFO", infoArray);
  83. String sentStr = jsonSent.ToString();
  84. Console.WriteLine(sentStr);
  85. return sentStr;
  86. }
  87. /// <summary>
  88. /// 晶粒度json DATA
  89. /// </summary>
  90. /// <returns></returns>
  91. private JObject GrainData()
  92. {
  93. string checkResult = CheckResult;
  94. JObject result = new JObject();
  95. float.TryParse(checkResult, out float grade);
  96. result.Add("CRYSTAL_GRAN_VALUE", grade);
  97. return result;
  98. }
  99. /// <summary>
  100. /// 夹杂物json DATA
  101. /// </summary>
  102. /// <returns></returns>
  103. private JObject InclusionData()
  104. {
  105. string checkResult = CheckResult;
  106. string[] arr = checkResult.Split(';');
  107. List<string> gradeList = new List<string>();
  108. for (int i = 0; i < arr.Length; i++)
  109. {
  110. string str = arr[i].Trim();
  111. string[] arrItem = str.Split(' ');
  112. for (int j = 0; j < arrItem.Length; j++)
  113. {
  114. string gradeStr = arrItem[j].Trim();
  115. gradeList.Add(gradeStr);
  116. }
  117. }
  118. List<string> AThickList = new List<string>();
  119. List<string> AThinList = new List<string>();
  120. List<string> BThickList = new List<string>();
  121. List<string> BThinList = new List<string>();
  122. List<string> CThickList = new List<string>();
  123. List<string> CThinList = new List<string>();
  124. List<string> DThickList = new List<string>();
  125. List<string> DThinList = new List<string>();
  126. List<string> DSList = new List<string>();
  127. //e是粗系
  128. foreach (var grade in gradeList)
  129. {
  130. if (string.IsNullOrEmpty(grade))
  131. continue;
  132. string str = grade.Trim();
  133. string type = str.Substring(0, 1);
  134. if (type == "A")
  135. {
  136. if (grade.Substring(grade.Length - 1) == "e")
  137. {
  138. string gradeValStr = grade.Substring(1, grade.Length - 2);
  139. AThickList.Add(gradeValStr);
  140. }
  141. else
  142. {
  143. string gradeValStr = grade.Substring(1, grade.Length - 1);
  144. AThinList.Add(gradeValStr);
  145. }
  146. }
  147. if (type == "B")
  148. {
  149. if (grade.Substring(grade.Length - 1) == "e")
  150. {
  151. string gradeValStr = grade.Substring(1, grade.Length - 2);
  152. BThickList.Add(gradeValStr);
  153. }
  154. else
  155. {
  156. string gradeValStr = grade.Substring(1, grade.Length - 1);
  157. BThinList.Add(gradeValStr);
  158. }
  159. }
  160. if (type == "C")
  161. {
  162. if (grade.Substring(grade.Length - 1) == "e")
  163. {
  164. string gradeValStr = grade.Substring(1, grade.Length - 2);
  165. CThickList.Add(gradeValStr);
  166. }
  167. else
  168. {
  169. string gradeValStr = grade.Substring(1, grade.Length - 1);
  170. CThinList.Add(gradeValStr);
  171. }
  172. }
  173. if (type == "D")
  174. {
  175. if (grade.Substring(1, 1) == "S")
  176. {
  177. string gradeValStr = grade.Substring(2, grade.Length - 2);
  178. DSList.Add(gradeValStr);
  179. }
  180. else
  181. {
  182. if (grade.Substring(grade.Length - 1) == "e")
  183. {
  184. string gradeValStr = grade.Substring(1, grade.Length - 2);
  185. DThickList.Add(gradeValStr);
  186. }
  187. else
  188. {
  189. string gradeValStr = grade.Substring(1, grade.Length - 1);
  190. DThinList.Add(gradeValStr);
  191. }
  192. }
  193. }
  194. }
  195. JObject result1 = new JObject();
  196. //=====================================================
  197. //A类
  198. float maxVal = -10000;
  199. foreach (var item in AThickList)
  200. {
  201. float.TryParse(item, out float fVal);
  202. if (maxVal < fVal)
  203. {
  204. maxVal = fVal;
  205. }
  206. }
  207. if (maxVal > 0)
  208. {
  209. result1.Add("A_THICK_VALUE", maxVal);
  210. }
  211. maxVal = -10000;
  212. foreach (var item in AThinList)
  213. {
  214. float.TryParse(item, out float fVal);
  215. if (maxVal < fVal)
  216. {
  217. maxVal = fVal;
  218. }
  219. }
  220. if (maxVal > 0)
  221. {
  222. result1.Add("A_THIN_VALUE", maxVal);
  223. }
  224. //=====================================================
  225. //B类
  226. maxVal = -10000;
  227. foreach (var item in BThickList)
  228. {
  229. float.TryParse(item, out float fVal);
  230. if (maxVal < fVal)
  231. {
  232. maxVal = fVal;
  233. }
  234. }
  235. if (maxVal > 0)
  236. {
  237. result1.Add("B_THICK_VALUE", maxVal);
  238. }
  239. maxVal = -10000;
  240. foreach (var item in BThinList)
  241. {
  242. float.TryParse(item, out float fVal);
  243. if (maxVal < fVal)
  244. {
  245. maxVal = fVal;
  246. }
  247. }
  248. if (maxVal > 0)
  249. {
  250. result1.Add("B_THIN_VALUE", maxVal);
  251. }
  252. //=====================================================
  253. //C类
  254. maxVal = -10000;
  255. foreach (var item in CThickList)
  256. {
  257. float.TryParse(item, out float fVal);
  258. if (maxVal < fVal)
  259. {
  260. maxVal = fVal;
  261. }
  262. }
  263. if (maxVal > 0)
  264. {
  265. result1.Add("C_THICK_VALUE", maxVal);
  266. }
  267. maxVal = -10000;
  268. foreach (var item in CThinList)
  269. {
  270. float.TryParse(item, out float fVal);
  271. if (maxVal < fVal)
  272. {
  273. maxVal = fVal;
  274. }
  275. }
  276. if (maxVal > 0)
  277. {
  278. result1.Add("C_THIN_VALUE", maxVal);
  279. }
  280. //=====================================================
  281. //D类
  282. maxVal = -10000;
  283. foreach (var item in DThickList)
  284. {
  285. float.TryParse(item, out float fVal);
  286. if (maxVal < fVal)
  287. {
  288. maxVal = fVal;
  289. }
  290. }
  291. if (maxVal > 0)
  292. {
  293. result1.Add("D_THICK_VALUE", maxVal);
  294. }
  295. maxVal = -10000;
  296. foreach (var item in DThinList)
  297. {
  298. float.TryParse(item, out float fVal);
  299. if (maxVal < fVal)
  300. {
  301. maxVal = fVal;
  302. }
  303. }
  304. if (maxVal > 0)
  305. {
  306. result1.Add("D_THIN_VALUE", maxVal);
  307. }
  308. //=====================================================
  309. //DS类
  310. maxVal = -10000;
  311. foreach (var item in DSList)
  312. {
  313. float.TryParse(item, out float fVal);
  314. if (maxVal < fVal)
  315. {
  316. maxVal = fVal;
  317. }
  318. }
  319. if (maxVal > 0)
  320. {
  321. result1.Add("DS_VALUE", maxVal);
  322. }
  323. return result1;
  324. }
  325. public void Send()
  326. {
  327. if (m_connected == false)
  328. return;
  329. string strAll = AllData();
  330. byte[] arr = System.Text.Encoding.UTF8.GetBytes(strAll);
  331. byte[] sentBytes = new byte[arr.Length + 2];
  332. sentBytes[0] = 0x02;
  333. System.Array.Copy(arr, 0, sentBytes, 1, arr.Length);
  334. sentBytes[sentBytes.Length - 1] = 0x03;
  335. try
  336. {
  337. //调用客户端套接字发送字节数组 .ToHexString()
  338. int suc = socketclient.Send(sentBytes);
  339. }
  340. catch (Exception ex)
  341. {
  342. //MessageBox.Show("发送数据失败 " + ex.Message);
  343. }
  344. }
  345. }
  346. }