|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Net.Sockets;
|
|
|
using System.Net;
|
|
|
using System.Threading;
|
|
|
using System.IO;
|
|
|
using System.Drawing;
|
|
|
using ocean;
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
namespace demo.DbModule
|
|
|
{
|
|
|
public class ProductArgs
|
|
|
{
|
|
|
public int v { get; set; }
|
|
|
public int t { get; set; }
|
|
|
public TProduct product { get; set; }
|
|
|
public int location { get; set; }
|
|
|
|
|
|
public ProductArgs(TProduct _pro, int _v, int _t, int _location)
|
|
|
{
|
|
|
product = _pro;
|
|
|
v = _v;
|
|
|
t = _t;
|
|
|
location = _location;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public class BusiData
|
|
|
{
|
|
|
private static object obj;
|
|
|
private static BusiData _instance;
|
|
|
private Socket serverSock;
|
|
|
private bool stop = false;
|
|
|
private Thread receThread;
|
|
|
private Thread receConn;
|
|
|
private Socket clientSock;
|
|
|
private string filename = string.Empty;
|
|
|
private static int count = 0;
|
|
|
List<string> searchPro = new List<string>();
|
|
|
private string dirpath = string.Empty;
|
|
|
public event Action<int, string> PutEventHandle;
|
|
|
public event Action<Color, string> MsgShowHandle;
|
|
|
public event Action<string> RecvDataHandle;
|
|
|
public static BusiData Instance
|
|
|
{
|
|
|
get {
|
|
|
if (_instance == null)
|
|
|
_instance = new BusiData();
|
|
|
return _instance;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private BusiData()
|
|
|
{
|
|
|
receThread = new Thread(ReceMsg);
|
|
|
receConn = new Thread(ReceConnect);
|
|
|
if (serverSock == null)
|
|
|
serverSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void SetKeywords(string words)
|
|
|
{
|
|
|
searchPro.Clear();
|
|
|
words = words.Replace("\r\n"," ").TrimEnd();
|
|
|
string[] keywords = words.Split(new char[] { ' '});
|
|
|
foreach (string key in keywords)
|
|
|
{
|
|
|
searchPro.Add(key);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void Init()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
dirpath = System.Configuration.ConfigurationManager.AppSettings["Data"];
|
|
|
EndPoint ep = new IPEndPoint(IPAddress.Any, 6666);
|
|
|
serverSock.Bind(ep);
|
|
|
serverSock.Listen(100);
|
|
|
receConn.Start();
|
|
|
receThread.Start();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
//log
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void Close()
|
|
|
{
|
|
|
if(clientSock!=null)
|
|
|
{
|
|
|
clientSock.Close();
|
|
|
}
|
|
|
|
|
|
if (serverSock != null)
|
|
|
{
|
|
|
serverSock.Close();
|
|
|
}
|
|
|
stop = true;
|
|
|
receThread.Join();
|
|
|
receConn.Join();
|
|
|
}
|
|
|
private void ReceConnect()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
clientSock = serverSock.Accept();
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public void ReceMsg()
|
|
|
{
|
|
|
byte[] receBuffer=new byte[2048];
|
|
|
while (!stop)
|
|
|
{
|
|
|
if (clientSock != null && clientSock.Connected)
|
|
|
{
|
|
|
if(clientSock.Available>0)
|
|
|
{
|
|
|
int len = clientSock.Receive(receBuffer);
|
|
|
if (len > 0)
|
|
|
{
|
|
|
//接收到数据,开始解析
|
|
|
string msg = Encoding.ASCII.GetString(receBuffer, 0, len);
|
|
|
if (RecvDataHandle!=null)
|
|
|
{
|
|
|
RecvDataHandle(msg);
|
|
|
}
|
|
|
ParseData(msg);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
stop = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
Thread.Sleep(100);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void ParseData(string data)
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(data))
|
|
|
{
|
|
|
data = data.TrimEnd(new char[] { ';' });
|
|
|
string[] parameters = data.Split(new char[] { ',' });
|
|
|
string modulename = parameters[0];
|
|
|
switch (modulename)
|
|
|
{
|
|
|
case "select":
|
|
|
Select(parameters);
|
|
|
break;
|
|
|
case "put":
|
|
|
Put(parameters);
|
|
|
break;
|
|
|
case "show":
|
|
|
ShowMsg(parameters);
|
|
|
break;
|
|
|
//case "getproduct":
|
|
|
// GetProduct(parameters);
|
|
|
break;
|
|
|
case "InStock":
|
|
|
InStock(parameters);
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void InStock(string[] parameters)
|
|
|
{
|
|
|
if(parameters!=null&¶meters.Length==4)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
TProduct product = new TProduct();
|
|
|
int v = int.Parse(parameters[1]);
|
|
|
int t = int.Parse(parameters[2]);
|
|
|
int index = int.Parse(parameters[3]);
|
|
|
if (Enum.IsDefined(typeof(EMapPos), t))
|
|
|
{
|
|
|
EMapPos estock = (EMapPos)Enum.ToObject(typeof(EMapPos), t);
|
|
|
corework.get_product_by_pos(estock, index, ref product);
|
|
|
//这里分两部分存储
|
|
|
//1、存储到本地数据库
|
|
|
//2、存储到远程服务器上,供扫码机查询
|
|
|
//InFileData(product, v, t, index);
|
|
|
InDb(product, v, t, index, false);
|
|
|
string msg = "InStock1,0;";
|
|
|
clientSock.Send(Encoding.ASCII.GetBytes(msg));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
string msg = "InStock1,-1;";
|
|
|
clientSock.Send(Encoding.ASCII.GetBytes(msg));
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
string msg = "InStock1,-2;";
|
|
|
clientSock.Send(Encoding.ASCII.GetBytes(msg));
|
|
|
log.Err("InStock:" + ex.Message);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void InFileData(TProduct product, int v, int t, int index)
|
|
|
{
|
|
|
string savedirpath = "d:\\data\\product\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";
|
|
|
if (!Directory.Exists(savedirpath))
|
|
|
{
|
|
|
Directory.CreateDirectory(savedirpath);
|
|
|
}
|
|
|
string savefilepath = Path.Combine(savedirpath, "data.txt");
|
|
|
if (!File.Exists(savefilepath))
|
|
|
{
|
|
|
File.Create(savefilepath);
|
|
|
}
|
|
|
using (Stream stream = File.Open(savefilepath, FileMode.Append))
|
|
|
{
|
|
|
StreamWriter sw = new StreamWriter(stream);
|
|
|
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ">>> SN:" + product.sn + "吸嘴:" + v + " 料仓:" + t + " 穴位:" + index);
|
|
|
sw.Close();
|
|
|
stream.Close();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void InDb(TProduct product, int v, int t, int index, bool remote=false)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string proJson = JsonConvert.SerializeObject(product);
|
|
|
ProductArgs pargs = new ProductArgs(product, v, t, index);
|
|
|
ThreadPool.QueueUserWorkItem(new WaitCallback((obj) =>
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
ProductArgs args = (ProductArgs)obj;
|
|
|
string connStr = MySQLHelper.DBConnectionString;
|
|
|
MySQLHelper db = new MySQLHelper(connStr);
|
|
|
if (!string.IsNullOrEmpty(args.product.sn))
|
|
|
{
|
|
|
string insertSql = "insert into productRecords(sn,result,stock,location,vacm,createtime,projson)";
|
|
|
insertSql += " values('{0}','{1}',{2},{3},{4},'{5}','{6}')";
|
|
|
insertSql = string.Format(insertSql, args.product.sn, args.product.status == 1 ? "PASS" : "NG", t, args.location, args.v, DateTime.Now, proJson);
|
|
|
|
|
|
string tempSql = "insert into products(sn,status,createtime,upstatus,trynum,filename,mode)";
|
|
|
tempSql += " values('{0}','{1}','{2}',{3},{4},'{5}','{6}')";
|
|
|
tempSql = string.Format(tempSql, args.product.sn, args.product.status == 1 ? "PASS" : "NG", DateTime.Now, 0, 0, "Summary_" + DateTime.Now.ToString("yyyyMMdd") + ".csv", corework.getTestModel());
|
|
|
|
|
|
string waitdeal = "insert into waitdeal(SN,DXDJudge,DXDInspectionTime,DXDUpdateTime) values('{0}',{1},'{2}','{3}')";
|
|
|
waitdeal = string.Format(waitdeal, args.product.sn, args.product.status, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
insertSql = insertSql + ";" + tempSql + ";" + waitdeal;
|
|
|
int num = db.ExecuteNonQuery(insertSql);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.Err("InDb:"+ex.Message+ex.StackTrace);
|
|
|
}
|
|
|
|
|
|
}), pargs);
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.Err("InDb" + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#region 挑料逻辑
|
|
|
/// <summary>
|
|
|
/// 挑料逻辑
|
|
|
/// </summary>
|
|
|
/// <param name="parameters"></param>
|
|
|
private void Select(string[] parameters)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string index = parameters[1];
|
|
|
string sn = parameters[2];
|
|
|
|
|
|
if (index == "1")
|
|
|
{
|
|
|
count++;
|
|
|
//如果是新的料盘,则创建一个新的文件存储料盘的产品信息
|
|
|
if (Directory.Exists(dirpath))
|
|
|
{
|
|
|
Directory.CreateDirectory(dirpath);
|
|
|
}
|
|
|
filename = DateTime.Now.ToString("yyyy-MM-dd") + "-" + count + ".csv";
|
|
|
}
|
|
|
int row, col = 0;
|
|
|
CalcRowCol(int.Parse(index), out row, out col);
|
|
|
string content = string.Format("{0},{1},{2}\r\n", index, row + "_" + col, sn);
|
|
|
File.AppendAllText(Path.Combine(dirpath, filename), content);
|
|
|
int result = 0;
|
|
|
if (searchPro.Contains(sn))
|
|
|
{
|
|
|
result = 1;
|
|
|
searchPro.Remove(sn);
|
|
|
}
|
|
|
if (clientSock != null && clientSock.Connected)
|
|
|
{
|
|
|
string tomsg = "select{0},{1};";
|
|
|
tomsg = string.Format(tomsg, result, searchPro.Count == 0 ? 1 : 0);
|
|
|
clientSock.Send(Encoding.ASCII.GetBytes(tomsg));
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
log.Err("BusiData-Select:" + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 放料逻辑
|
|
|
/// <summary>
|
|
|
/// 放料逻辑
|
|
|
/// </summary>
|
|
|
/// <param name="parameters"></param>
|
|
|
private void Put(string[] parameters)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
int index = Convert.ToInt32(parameters[1]);
|
|
|
string sn = parameters[2];
|
|
|
if (PutEventHandle != null)
|
|
|
{
|
|
|
PutEventHandle(index, sn);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
private void ShowMsg(string[] parameters)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string color = parameters[1];
|
|
|
string msg = parameters[2];
|
|
|
if (MsgShowHandle != null)
|
|
|
{
|
|
|
//Color c = Color.FromName(color);
|
|
|
MsgShowHandle(Color.LimeGreen, msg);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
//log
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void GetProduct(string[] parameters)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
int index = Convert.ToInt32(parameters[1]);
|
|
|
TProduct productInfo = new TProduct();
|
|
|
corework.get_product_by_pos(EMapPos.LocationVacm, index, ref productInfo);
|
|
|
if (!string.IsNullOrEmpty(productInfo.sn))
|
|
|
{
|
|
|
string insertSql = "insert into products(sn,status,createtime,upstatus,trynum,filename,mode)";
|
|
|
insertSql += " values('{0}','{1}','{2}',{3},{4},'{5}','{6}')";
|
|
|
insertSql = string.Format(insertSql, productInfo.sn, productInfo.status == 1 ? "PASS" : "NG", DateTime.Now, 0, 0, "Summary_" + DateTime.Now.ToString("yyyyMMdd") + ".csv",corework.getTestModel());
|
|
|
int num = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, insertSql);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
//log
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void CalcRowCol(int index, out int _row, out int _col)
|
|
|
{
|
|
|
_row = _col = 0;
|
|
|
if (index % 16 == 0)
|
|
|
{
|
|
|
_row = index / 16;
|
|
|
_col = 16;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_row = (index / 16) + 1;
|
|
|
_col = index % 16;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|