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
{
///
/// 检查项目 1 : 夹杂物 2 :晶粒度
///
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;
}
///
/// 晶粒度json DATA
///
///
private JObject GrainData()
{
string checkResult = CheckResult;
JObject result = new JObject();
float.TryParse(checkResult, out float grade);
result.Add("CRYSTAL_GRAN_VALUE", grade);
return result;
}
///
/// 夹杂物json DATA
///
///
private JObject InclusionData()
{
string checkResult = CheckResult;
string[] arr = checkResult.Split(';');
List gradeList = new List();
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 AThickList = new List();
List AThinList = new List();
List BThickList = new List();
List BThinList = new List();
List CThickList = new List();
List CThinList = new List();
List DThickList = new List();
List DThinList = new List();
List DSList = new List();
//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);
}
}
}
}