123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace Metis.AutoAnalysis
- {
- public class DataUpload
- {
- /// <summary>
- /// 检查项目 1 : 夹杂物 2 :晶粒度
- /// </summary>
- public int CheckItem { get; set; }
- public string CheckResult { get; set; }
- private const string TEST_CODE_INCLUSION = "G1PI";
- private const string TEST_CODE_GRAIN = "G1PN";
-
- public string TESTCODE { get; set; }
- public string SAMPLELOTNO { get; set; }
- public string SAMPLEID { get; set; }
- public string FD_SAMPLE_NO { get; set; }
- public string RECVTIME { get; set; }
- public string FD_SG_SIGN { get; set; }
- public Socket socketclient = null;
- public bool m_connected = false;
- public void ConnectServer()
- {
- string ip = ConfigurationManager.AppSettings["server_ip"];
- string portstr = ConfigurationManager.AppSettings["server_port"];
- int.TryParse(portstr, out int port);
- //定义一个套接字监听
- socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- //获取IP地址
- IPAddress address = IPAddress.Parse(ip);
- //将获取的IP地址和端口号绑定在网络节点上
- IPEndPoint point = new IPEndPoint(address, port);
- try
- {
- //客户端套接字连接到网络节点上,用的是Connect
- socketclient.Connect(point);
- m_connected = true;
- LogHelper.log.Debug("服务器连接成功");
- }
- catch (Exception ex)
- {
- m_connected = false;
- LogHelper.log.Error("服务器连接失败" + ex.Message);
- }
- }
- private string AllData()
- {
- JObject jsonSent = new JObject();
- if (CheckItem == 1)
- {
- jsonSent.Add("TESTCODE", TEST_CODE_INCLUSION);
- }
- else if (CheckItem == 2)
- {
- jsonSent.Add("TESTCODE", TEST_CODE_GRAIN);
- }
- JArray infoArray = new JArray();
- JObject info1 = new JObject();
- //string retime = DateTime.Now.ToLocalTime().ToString();
- info1.Add("SAMPLELOTNO", SAMPLELOTNO); //检验批号
- info1.Add("SAMPLEID", SAMPLEID); //试批顺序号
- info1.Add("FD_SAMPLE_NO", FD_SAMPLE_NO); //试样号
- //info1.Add("RECVTIME", retime); //收样时间
- JObject data = new JObject();
- if (CheckItem == 1)
- {
- data = InclusionData();
- }
- else if (CheckItem == 2)
- {
- data = GrainData();
- }
- info1.Add("DATA", data);
- infoArray.Add(info1);
- jsonSent.Add("INFO", infoArray);
- String sentStr = jsonSent.ToString();
- Console.WriteLine(sentStr);
- return sentStr;
- }
- /// <summary>
- /// 晶粒度json DATA
- /// </summary>
- /// <returns></returns>
- private JObject GrainData()
- {
- string checkResult = CheckResult;
- JObject result = new JObject();
- float.TryParse(checkResult, out float grade);
- result.Add("CRYSTAL_GRAN_VALUE", grade);
- return result;
- }
- /// <summary>
- /// 夹杂物json DATA
- /// </summary>
- /// <returns></returns>
- private JObject InclusionData()
- {
- string checkResult = CheckResult;
- string[] arr = checkResult.Split(';');
- List<string> gradeList = new List<string>();
- for (int i = 0; i < arr.Length; i++)
- {
- string str = arr[i].Trim();
- string[] arrItem = str.Split(' ');
- for (int j = 0; j < arrItem.Length; j++)
- {
- string gradeStr = arrItem[j].Trim();
- gradeList.Add(gradeStr);
- }
- }
- List<string> AThickList = new List<string>();
- List<string> AThinList = new List<string>();
- List<string> BThickList = new List<string>();
- List<string> BThinList = new List<string>();
- List<string> CThickList = new List<string>();
- List<string> CThinList = new List<string>();
- List<string> DThickList = new List<string>();
- List<string> DThinList = new List<string>();
- List<string> DSList = new List<string>();
- //e是粗系
- foreach (var grade in gradeList)
- {
- if (string.IsNullOrEmpty(grade))
- continue;
- string str = grade.Trim();
- string type = str.Substring(0, 1);
- if (type == "A")
- {
- if (grade.Substring(grade.Length - 1) == "e")
- {
- string gradeValStr = grade.Substring(1, grade.Length - 2);
- AThickList.Add(gradeValStr);
- }
- else
- {
- string gradeValStr = grade.Substring(1, grade.Length - 1);
- AThinList.Add(gradeValStr);
- }
- }
- if (type == "B")
- {
- if (grade.Substring(grade.Length - 1) == "e")
- {
- string gradeValStr = grade.Substring(1, grade.Length - 2);
- BThickList.Add(gradeValStr);
- }
- else
- {
- string gradeValStr = grade.Substring(1, grade.Length - 1);
- BThinList.Add(gradeValStr);
- }
- }
- if (type == "C")
- {
- if (grade.Substring(grade.Length - 1) == "e")
- {
- string gradeValStr = grade.Substring(1, grade.Length - 2);
- CThickList.Add(gradeValStr);
- }
- else
- {
- string gradeValStr = grade.Substring(1, grade.Length - 1);
- CThinList.Add(gradeValStr);
- }
- }
- if (type == "D")
- {
- if (grade.Substring(1, 1) == "S")
- {
- string gradeValStr = grade.Substring(2, grade.Length - 2);
- DSList.Add(gradeValStr);
- }
- else
- {
- if (grade.Substring(grade.Length - 1) == "e")
- {
- string gradeValStr = grade.Substring(1, grade.Length - 2);
- DThickList.Add(gradeValStr);
- }
- else
- {
- string gradeValStr = grade.Substring(1, grade.Length - 1);
- DThinList.Add(gradeValStr);
- }
- }
- }
- }
- JObject result1 = new JObject();
- //=====================================================
- //A类
- float maxVal = -10000;
- foreach (var item in AThickList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("A_THICK_VALUE", maxVal);
- }
- maxVal = -10000;
- foreach (var item in AThinList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("A_THIN_VALUE", maxVal);
- }
- //=====================================================
- //B类
- maxVal = -10000;
- foreach (var item in BThickList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("B_THICK_VALUE", maxVal);
- }
- maxVal = -10000;
- foreach (var item in BThinList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("B_THIN_VALUE", maxVal);
- }
- //=====================================================
- //C类
- maxVal = -10000;
- foreach (var item in CThickList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("C_THICK_VALUE", maxVal);
- }
- maxVal = -10000;
- foreach (var item in CThinList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("C_THIN_VALUE", maxVal);
- }
- //=====================================================
- //D类
- maxVal = -10000;
- foreach (var item in DThickList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("D_THICK_VALUE", maxVal);
- }
- maxVal = -10000;
- foreach (var item in DThinList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("D_THIN_VALUE", maxVal);
- }
- //=====================================================
- //DS类
- maxVal = -10000;
- foreach (var item in DSList)
- {
- float.TryParse(item, out float fVal);
- if (maxVal < fVal)
- {
- maxVal = fVal;
- }
- }
- if (maxVal > 0)
- {
- result1.Add("DS_VALUE", maxVal);
- }
- return result1;
- }
- public void Send()
- {
- if (m_connected == false)
- return;
- string strAll = AllData();
- byte[] arr = System.Text.Encoding.UTF8.GetBytes(strAll);
- byte[] sentBytes = new byte[arr.Length + 2];
- sentBytes[0] = 0x02;
- System.Array.Copy(arr, 0, sentBytes, 1, arr.Length);
- sentBytes[sentBytes.Length - 1] = 0x03;
- try
- {
- //调用客户端套接字发送字节数组 .ToHexString()
- int suc = socketclient.Send(sentBytes);
- LogHelper.log.Debug("消息发送成功 " + strAll);
- }
- catch (Exception ex)
- {
- LogHelper.log.Debug("消息发送失败 " + ex.Message);
- }
- }
- }
- }
|