天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

696 lines
25 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.Operation;
using QMAPP.FJC.Entity.ProcessParameter;
using QMFrameWork.Data;
using QMFrameWork.Common.Util;
using System.Net;
using System.Net.Sockets;
using QMAPP.BLL;
using QMAPP.FJC.Entity.FwMark;
namespace QMAPP.FJC.BLL.FwMark
{
public class FWMARKDeal : BaseBLL
{
int count = 0;
public string SetMark(string WORKPLACECODE, List<String> PARTSCODEList)
{
//FWMarkParameter
List<FWMarkParameter> LightList = new List<FWMarkParameter>();
List<FWMarkParameter> DarkList = new List<FWMarkParameter>();
string PARTSCODE = "";
if (PARTSCODEList.Count > 0)
{
foreach (var p in PARTSCODEList)
{
if (PARTSCODE == "")
{
PARTSCODE = "'" + p + "'";
}
else
{
PARTSCODE = PARTSCODE + ",'" + p + "'";
}
}
}
//else
//{
// return "";
//}
string LightSql = @"select * from T_FA_MARKADDR where
WORKPLACECODE='" + WORKPLACECODE + "' and PARTSCODE in (" + PARTSCODE + ")";
string DarkSql = @"select * from T_FA_MARKADDR where
WORKPLACECODE='" + WORKPLACECODE + "'";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
if (PARTSCODEList.Count > 0)
{
LightList = session.GetList<FWMarkParameter>(LightSql, new List<DataParameter>().ToArray()).ToList<FWMarkParameter>();
}
DarkList = session.GetList<FWMarkParameter>(DarkSql, new List<DataParameter>().ToArray()).ToList<FWMarkParameter>();
}
if (DarkList.Count() > 0)
{
foreach (var D in DarkList)
{
D.MARKADDR = D.MARKADDR.PadLeft(4, '0');
}
//首先关掉DarkList当中的所有灯
CloseLight(DarkList);
}
if (LightList.Count() > 0)
{
foreach (var L in LightList)
{
L.MARKADDR = L.MARKADDR.PadLeft(4, '0');
}
//开启LightList当中的所有灯
SendtoLight(LightList);
}
return "";
}
public string CloseMark()
{
List<FWMarkParameter> DarkList = new List<FWMarkParameter>();
string DarkSql = @"select distinct url,PORT,'' PID,'' WORKPLACECODE,'' PARTSCODE,'' MARKADDR from T_FA_MARKADDR";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
DarkList = session.GetList<FWMarkParameter>(DarkSql, new List<DataParameter>().ToArray()).ToList<FWMarkParameter>();
}
//发送Z指令
foreach (var D in DarkList)
{
SendZ(D);
}
return "";
}
private bool CloseLight(List<FWMarkParameter> add)
{
try
{
//初始化计数
count = 0;
//int port = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["port"].ToString());
//string host = System.Configuration.ConfigurationSettings.AppSettings["host"].ToString();
FWMarkParameter _tfp = add[0];
int port = int.Parse(_tfp.PORT);
string host = _tfp.URL;
//初始化socket
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
//创建Socket并连接到服务器
using (Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) // 创建Socket
{
Console.WriteLine("Connecting...");
var result = c.BeginConnect(ipe, null, null); //连接到服务器
if (!result.AsyncWaitHandle.WaitOne(4000))
{
throw new Exception("指示灯控制器连接失败!");
}
string command = "";
//添加灭灯指令D
string temp = "D";
//添加灭灯地址
foreach (var a in add)
{
string markaddr = a.MARKADDR.PadLeft(4, '0');
temp += markaddr;
}
//获取指令长度
string strlong = temp.Length.ToString().PadLeft(4, '0');
//组合指令 02 +顺序号 + 指令长度 + 指令 + 03
command = "02 " + GetASCII(GetCount()) + " " + GetASCII(strlong) + " " + GetASCII(temp) + " 03";
/*
LogManager.LogHelper.Info(new LogInfo()
{
ErrorInfo = new Exception(""),
Info = command,
Tag = "发送指令到LED-灭灯指令D"
});*/
//发送灭灯指令********
byte[] bs;
bs = strToToByte(command);
Console.WriteLine("Send message");
c.Send(bs, bs.Length, 0); //发送信息
//获取返回值
//***********************
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受返回信息
#region 返回值记录日志
StringBuilder rDLog = new StringBuilder();
for (int i = 0; i < recvBytes.Length; i++)
{
rDLog.Append(recvBytes[i].ToString("X"));
}
/*
LogManager.LogHelper.Info(new LogInfo()
{
ErrorInfo = new Exception(""),
Info = rDLog.ToString(),
Tag = "发送指令到LED-灭灯返回指令D"
});*/
#endregion
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("灭灯返回消息");
Console.WriteLine(recvStr);
//***********************
}
return true;
}
catch (Exception ex)
{
/*
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Info = ex.StackTrace,
Tag = "发送指令到LED异常"
});*/
throw ex;
//return false;
}
}
private bool SendtoLight(List<FWMarkParameter> add)//, int color)
{
int color = 6;//3白色
try
{
//初始化计数
count = 0;
int port = int.Parse(add[0].PORT);
string host = add[0].URL;
//初始化socket
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
//创建Socket并连接到服务器
using (Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) // 创建Socket
{
Console.WriteLine("Connecting...");
var result= c.BeginConnect(ipe,null,null); //连接到服务器
if (!result.AsyncWaitHandle.WaitOne(4000))
{
throw new Exception("指示灯控制器连接失败!");
}
string command = "";
//添加灭灯指令D
string temp = "D";
//添加灭灯地址
foreach (var a in add)
{
temp += a;
}
//获取指令长度
string strlong = temp.Length.ToString().PadLeft(4, '0');
//组合指令 02 +顺序号 + 指令长度 + 指令 + 03
command = "02 " + GetASCII(GetCount()) + " " + GetASCII(strlong) + " " + GetASCII(temp) + " 03";
/*
LogManager.LogHelper.Info(new LogInfo()
{
ErrorInfo = new Exception(""),
Info = command,
Tag = "发送指令到LED-灭灯指令D"
});*/
//发送灭灯指令********
byte[] bs;
bs = strToToByte(command);
//Console.WriteLine("Send message");
//c.Send(bs, bs.Length, 0); //发送信息
//获取返回值
//***********************
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
//bytes = c.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受返回信息
#region 返回值记录日志
StringBuilder rDLog = new StringBuilder();
for (int i = 0; i < recvBytes.Length; i++)
{
rDLog.Append(recvBytes[i].ToString("X"));
}
/*
LogManager.LogHelper.Info(new LogInfo()
{
ErrorInfo = new Exception(""),
Info = rDLog.ToString(),
Tag = "发送指令到LED-灭灯返回指令D"
});*/
#endregion
//recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
//Console.WriteLine("灭灯返回消息");
//Console.WriteLine(recvStr);
//***********************
//Thread.Sleep(1000);
//亮灯
string colorstr = "";
switch (color)
{/* case 0: colorstr = "31 21 23"; break;//绿
case 1: colorstr = "32 21 23"; break;//黄
case 2: colorstr = "32 11 23"; break;//红*/
case 0: colorstr = "31 21 21"; break;//绿
case 1: colorstr = "32 21 21"; break;//黄
case 2: colorstr = "32 11 21"; break;//红
case 3: colorstr = "32 22 21"; break;//白
case 4: colorstr = "31 12 21"; break;//蓝
case 5: colorstr = "32 21 21"; break;//桔
case 6: colorstr = "32 12 21"; break;//紫
case 7: colorstr = "31 22 21"; break;//青/蓝绿
}
//亮灯指令
//temp = "PP5050000m1";
//Mm1?(FF)(FF)
//temp = "Mm1";
temp = "P2";
//Mm1
command = getCommstr("设定颜色", colorstr, add);
bs = strToToByte(command);
//Console.WriteLine("Send message");
c.Send(bs, bs.Length, 0); //发送信息
//获取返回值
//***********************
recvStr = "";
recvBytes = new byte[1024];
bytes = c.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受返回信息
StringBuilder rPLog = new StringBuilder();
for (int i = 0; i < recvBytes.Length; i++)
{
rPLog.Append(recvBytes[i].ToString("X"));
}
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("亮灯返回消息");
Console.WriteLine(recvStr);
//***********************
command = getCommstr("点亮", colorstr, add);
bs = strToToByte(command);
//Console.WriteLine("Send message");
c.Send(bs, bs.Length, 0); //发送信息
//获取返回值
//***********************
recvStr = "";
recvBytes = new byte[1024];
bytes = c.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受返回信息
StringBuilder rPLog2 = new StringBuilder();
for (int i = 0; i < recvBytes.Length; i++)
{
rPLog2.Append(recvBytes[i].ToString("X"));
}
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("亮灯返回消息");
Console.WriteLine(recvStr);
//***********************
//ResetController(recvStr);
}
return true;
}
catch (Exception ex)
{
/*
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Info = ex.StackTrace,
Tag = "发送指令到LED异常"
});*/
throw ex;
return false;
}
}
private string getCommstr(string commtype, string colorstr, List<FWMarkParameter> add)
{
if (commtype == "设定颜色")
{
string temp = "Mm1";
//Mm1
string temp2 = "";
foreach (var a in add)
{
if (string.IsNullOrEmpty(a.MARKADDR))
{
continue;
}
temp2 += a.MARKADDR;
}
//指令长度=指令 + 颜色3位 + 地址
string strlong = ((temp + temp2).Length + 3).ToString().PadLeft(4, '0');// + 3
//组合指令 02 + 顺序号 + 长度 + 指令 + 颜色 + 地址
Console.WriteLine("common ascii=" + GetASCII(temp).ToString());
string command = "02 " + GetASCII(GetCount()) + " " + GetASCII(strlong) + " " + GetASCII(temp) + " " + colorstr + " " + GetASCII(temp2) + " 03";//
return command;
}
else if (commtype == "点亮")
{
string temp = "P2";
//Mm1
string temp2 = "";
foreach (var a in add)
{
if (string.IsNullOrEmpty(a.MARKADDR))
{
continue;
}
temp2 += a.MARKADDR + "88888";
}
//指令长度=指令 + 颜色3位 + 地址
string strlong = ((temp + temp2).Length).ToString().PadLeft(4, '0');// + 3
//组合指令 02 + 顺序号 + 长度 + 指令 + 颜色 + 地址
Console.WriteLine("common ascii=" + GetASCII(temp).ToString());
string command = "02 " + GetASCII(GetCount()) + " " + GetASCII(strlong) + " " + GetASCII(temp) + " " + GetASCII(temp2) + " 03";//" + colorstr + "
return command;
}
else return "";
}
private bool SendZ(FWMarkParameter D)
{
try
{
string command = "";
//添加灭灯指令D
string temp = "Z";
//获取指令长度
string strlong = temp.Length.ToString().PadLeft(4, '0');
//组合指令 02 +顺序号 + 指令长度 + 指令 + 03
command = "02 " + GetASCII(GetCount()) + " " + GetASCII(strlong) + " " + GetASCII(temp) + " 03";
SocketSend(command, D.URL, Convert.ToInt32(D.PORT));
return true;
}
catch (Exception ex)
{
/*
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Info = ex.StackTrace,
Tag = "发送指令到LED异常"
});*/
return false;
}
}
/// <summary>
/// 重置控制器
/// </summary>
/// <param name="recvStr"></param>
public void ResetController(string recvStr)
{
try
{
//初始化计数
count = 0;
int port = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["port"].ToString());
string host = System.Configuration.ConfigurationSettings.AppSettings["host"].ToString();
//初始化socket
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
//创建Socket并连接到服务器
using (Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) // 创建Socket
{
Console.WriteLine("Connecting...");
c.Connect(ipe); //连接到服务器
string command = "";
string temp = "";
byte[] bs;
byte[] recvBytes = new byte[1024];
int bytes;
#region 重置控制器
//如果返回消息中包括t,则重置控制器
if (!recvStr.Contains("t"))
{
return;
}
temp = GetCount() + "0001Z";
command = "02 " + GetASCII(temp) + " 03";
bs = strToToByte(command);
Console.WriteLine("Send message");
c.Send(bs, bs.Length, 0); //发送信息
//获取返回值
recvStr = "";
recvBytes = new byte[1024];
bytes = c.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受返回信息
StringBuilder rTLog = new StringBuilder();
for (int i = 0; i < recvBytes.Length; i++)
{
rTLog.Append(recvBytes[i].ToString("X"));
}
/*
LogManager.LogHelper.Info(new LogInfo()
{
ErrorInfo = new Exception(""),
Info = rTLog.ToString(),
Tag = "发送指令到LED-重置控制器指令T"
});*/
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("重置控制器消息");
Console.WriteLine(recvStr);
c.Close();
#endregion
}
}
catch (Exception ex)
{
/*
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Info = ex.StackTrace,
Tag = "重置控制器LED异常"
});*/
}
}
public byte[] strToToByte(string str)
{
List<byte> b = new List<byte>();
foreach (string s in str.Split(' '))
{
byte by = Convert.ToByte(s, 16);
b.Add(by);
}
return b.ToArray();
}
/// <summary>
/// 获取指令序号
/// </summary>
/// <returns></returns>
public string GetCount()
{
string s = count.ToString().PadLeft(3, '0');
if (count == 999)
{
count = 000;
}
else
{
count++;
}
return s;
}
/// <summary>
/// 获取16位ASCII
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string GetASCII(string str)
{
byte[] array = System.Text.Encoding.ASCII.GetBytes(str); //数组array为对应的ASCII数组
string ASCIIstr2 = null;
for (int i = 0; i < array.Length; i++)
{
int asciicode = (int)(array[i]);
ASCIIstr2 += asciicode.ToString("X2") + " ";//字符串ASCIIstr2 为对应的ASCII字符串
}
return ASCIIstr2.TrimEnd(' ');
}
public bool SocketSend(string sendStr, string host, int port)
{
try
{
//int port = 2000;
//string host = "127.0.0.1";
//创建终结点EndPoint
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port); //把ip和端口转化为IPEndPoint的实例
//创建Socket并连接到服务器
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 创建Socket
Console.WriteLine("Connecting...");
c.Connect(ipe); //连接到服务器
//向服务器发送信息
//byte[] bs = Encoding.ASCII.GetBytes(sendStr); //把字符串编码为字节
byte[] bs = strToToHexByte(sendStr);
Console.WriteLine("Send message");
c.Send(bs, bs.Length, 0); //发送信息
//接受从服务器返回的信息
string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0); //从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Console.WriteLine("client get message:{0}", recvStr); //回显服务器的返回信息
Console.ReadLine();
c.Close();
return true;
}
catch (ArgumentException e)
{
return false;
Console.WriteLine("argumentNullException:{0}", e);
}
catch (SocketException e)
{
return false;
Console.WriteLine("SocketException:{0}", e);
}
}
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16); return returnBytes;
}
/// <summary>
/// 点亮工作中心对应的灯
/// </summary>
/// <param name="str">借用T_FA_MARKADDR里的WORKPLACECODE字段保存WORKCENTER_CODE的值</param>
/// <returns></returns>
public bool ADSendtoLight(string WORKCENTER_CODE)
{
List<FWMarkParameter> FWMarkList = new List<FWMarkParameter>();
string LightSql = @"select * from T_FA_MARKADDR where WORKPLACECODE='" + WORKCENTER_CODE + "'";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
FWMarkList = session.GetList<FWMarkParameter>(LightSql, new List<DataParameter>().ToArray()).ToList<FWMarkParameter>();
}
return SendtoLight(FWMarkList);
}
/// <summary>
/// 关闭工作中心对应的灯
/// </summary>
/// <param name="str">借用T_FA_MARKADDR里的WORKPLACECODE字段保存WORKCENTER_CODE的值</param>
/// <returns></returns>
public bool ADCloseLight(string WORKCENTER_CODE)
{
List<FWMarkParameter> FWMarkList = new List<FWMarkParameter>();
string LightSql = @"select * from T_FA_MARKADDR where WORKPLACECODE='" + WORKCENTER_CODE + "'";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
FWMarkList = session.GetList<FWMarkParameter>(LightSql, new List<DataParameter>().ToArray()).ToList<FWMarkParameter>();
}
return CloseLight(FWMarkList);
}
}
}