Files
T-DAS/Epost.DAL/CommandDAL.cs

738 lines
27 KiB
C#
Raw Permalink Normal View History

2023-01-13 15:30:20 +08:00
using Epost.Common;
using Epost.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Epost.Common.LogHelper;
namespace Epost.DAL
{
public class CommandDAL
{
string IP = ConfigurationManager.AppSettings["WebAPIUrl"];
#region
public bool CONNECT(LabelParamModel model)
{
model.Address = "9999";
model.Command = "CONNECT";
model.Parameter = IP;
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool DISCONNECT()
{
LabelParamModel model = new LabelParamModel();
model.Address = "9999";
model.Command = "DISCONNECT";
model.Parameter = IP;
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public bool FORMATE_DEVICE(LabelParamModel model)
{
model.Command = "FORMATE_DEVICE";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool FORMATE_ALL_DATA()
{
LabelParamModel model = new LabelParamModel();
model.Address = "9999";
model.Command = "FORMATE_ALL_DATA";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool FORMATE_ADDRESS_DATA()
{
LabelParamModel model = new LabelParamModel();
model.Address = "9999";
model.Command = "FORMATE_ALL_DATA";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool FORMATE_JOB_DATA(LabelParamModel model)
{
model.Address = "9999";
model.Command = "FORMAT_JOB_DATA";
model.Parameter = "50";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool INPUT_ADDRESS_DATA()
{
LabelParamModel model = new LabelParamModel();
model.Address = "9999";
model.Command = "INPUT_ADDRESS_DATA";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool OUTPUT_ADDRESS_DATA()
{
LabelParamModel model = new LabelParamModel();
model.Address = "9999";
model.Command = "OUTPUT_ADDRESS_DATA";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool SET_DEBUG_LOG()
{
LabelParamModel model = new LabelParamModel();
model.Address = "9999";
model.Command = "SET_DEBUG_LOG";
model.Parameter = "normal/debug";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool OUTPUT_DEBUG_LOG()
{
LabelParamModel model = new LabelParamModel();
model.Address = "9999";
model.Command = "OUTPUT_DEBUG_LOG";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region 0/1/2/3/4/5
public bool SET_DEVICE_BR(LabelParamModel model)
{
model.Address = "9999";
model.Command = "SET_DEVICE_BR";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool SET_DEVICE_POS(LabelParamModel model)
{
model.Command = "SET_DEVICE_POS";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool SET_DEVICE_FORMAT(LabelParamModel model)
{
model.Command = "SET_DEVICE_FORMAT";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool ENTER_CONFIG_MODE(LabelParamModel model)
{
model.Parameter = "";
model.Command = "ENTER_CONFIG_MODE";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool SET_ADDRESS(LabelParamModel model)
{
model.Command = "SET_ADDRESS";
return Send(model);
}
#endregion
#region
public bool DELETE_ADDRESS(LabelParamModel model)
{
model.Address = "9999";
model.Command = "DELETE_ADDRESS";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region 退
public bool EXIT_CONFIG_MODE(LabelParamModel model)
{
model.Address = "9999";
model.Parameter = "";
model.Command = "EXIT_CONFIG_MODE";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool ENTER_STANDBY(LabelParamModel model)
{
model.Parameter = "";
model.Command = "ENTER_STANDBY";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool EXIT_WAKEUP(LabelParamModel model)
{
model.Parameter = "";
model.Command = "WAKEUP";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool LED_ON(LabelParamModel model)
{
model.Address = "9999";
model.Parameter = "";
model.Command = "LED_ON";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool LED_OFF(LabelParamModel model)
{
model.Address = "9999";
model.Parameter = "0";
model.Command = "LED_OFF";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region device/job/scan默认job
public bool START_POLLING(LabelParamModel model)
{
model.Address = "9999";
model.Command = "START_POLLING";
if (string.IsNullOrEmpty(model.Parameter))
{
model.Parameter = "job";
}
model.Timeout = "0.03,0.05";
return Send(model);
}
#endregion
#region device/job/scan job
public bool STOP_POLLING(LabelParamModel model)
{
model.Command = "STOP_POLLING";
if (string.IsNullOrEmpty(model.Parameter))
{
model.Parameter = "job";
}
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public void DISPLAY_JOBALL(List<LabelParamModel> model)
{
SendALL(model);
}
#endregion
#region ()
public ResultModel SendALL_Init(List<LabelParamModel> model)
{
List<ResultsModel> resultModel = new List<ResultsModel>();
ResultModel rest = new ResultModel();
rest.result = "0";
rest.msg = "成功!";
try
{
LogHelper.WriteLogInfo("********************" + DateTime.Now.ToString() + "***点亮条数:****" + model.Count + "***************");
if (model != null)
{
List<IGrouping<string, LabelParamModel>> alllist = model.GroupBy(m => m.ControlIP).ToList();
AddressstorageDAL dal = new AddressstorageDAL();
DataTable dt = dal.getControlList();//获取控制器列表
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow item in dt.Rows)
{
var sdlist = alllist.FirstOrDefault(x => x.Key == item["ControlIP"].ToString());
HttpHelper http = new HttpHelper();
2024-01-04 15:30:23 +08:00
string ControlIP = "http://" + item["ControlIP"].ToString() + ":81/cgi-bin/rpc.cgi";
2023-01-13 15:30:20 +08:00
// LogHelper.WriteLogInfo(ControlIP + "***************");
string result = "";
List<SendParamModel> list = new List<SendParamModel>();
if (sdlist != null)
{
var ControlDetail = sdlist.ToArray();
for (int i = 0; i < ControlDetail.Length; i++)
{
SendParamModel sendModel = new SendParamModel();
sendModel.Address = ControlDetail[i].Address;
sendModel.Command = ControlDetail[i].Command;
sendModel.Parameter = ControlDetail[i].Parameter;
sendModel.Uid = ControlDetail[i].Uid;
sendModel.Quantity = ControlDetail[i].Quantity;
sendModel.Title = ControlDetail[i].Title;
sendModel.Content = ControlDetail[i].Content;
sendModel.Type = ControlDetail[i].Type;
sendModel.Mode = ControlDetail[i].Mode;
sendModel.Order = ControlDetail[i].Order;
sendModel.Unit = ControlDetail[i].Unit;
sendModel.W_unit = ControlDetail[i].W_unit;
sendModel.W_quantity = ControlDetail[i].W_quantity;
sendModel.Loc = ControlDetail[i].Loc;
list.Add(sendModel);
if ((i + 1) % 15 == 0 || (i + 1) == ControlDetail.Length)
{
string postStr = JsonHelper.SerializeObject(list);
LogHelper.WriteLogInfo("初始化-发送拼接指令:" + postStr);
result = http.HttpPost_Old(ControlIP, postStr);
LogHelper.WriteLogInfo("初始化-返回:******" + result);
if (result.ToUpper() == "FAIL")
{
LogHelper.WriteLogInfo(ControlIP + "连接控制器异常!", Log_Type.ERROR);
rest.result = "-1";
rest.msg = "连接控制器失败!";
return rest;
}
resultModel = JsonHelper.DeserializeObject<List<ResultsModel>>(result);
foreach (ResultsModel res in resultModel)
{
2024-01-04 15:30:23 +08:00
if (res.Status == "ERROR"&&res.Command!= "START_POLLING")
2023-01-13 15:30:20 +08:00
{
LogHelper.WriteLogInfo("初始化指令" + postStr);
rest.result = "-1";
rest.msg = "初始化失败!";
return rest;
}
}
list.Clear();
}
}
}
}
}
}
return rest;
}
catch (Exception ex)
{
LogHelper.WriteLogInfo("发送指令异常ALL:" + ex.Message);
rest.result = "-1";
rest.msg = "初始化异常!";
return rest;
}
}
#endregion
#region ()
public bool DISPLAY_JOB(LabelParamModel model)
{
model.Command = "DISPLAY_JOB";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ( 011 010 )
public bool DISPLAY_LIGHT(LabelParamModel model)
{
model.Command = "DISPLAY_LIGHT";
model.Timeout = "0.5";
model.Type = "T";
return Send(model);
}
#endregion
#region ()
public bool REMOVE_JOB(LabelParamModel model)
{
model.Command = "REMOVE_JOB";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public bool LOCK(LabelParamModel model)
{
model.Command = "LOCK";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public bool UNLOCK(LabelParamModel model)
{
model.Command = "UNLOCK";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public bool CLEAR_MARK(LabelParamModel model)
{
model.Command = "CLEAR_MARK";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public bool SET_DEVICE_MODE(LabelParamModel model)
{
model.Command = "SET_DEVICE_MODE";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region 使
public bool SET_TIME_EXPIRE()
{
LabelParamModel model = new LabelParamModel();
model.Command = "SET_TIME_EXPIRE";
model.Address = "9999";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public bool READ_SN(LabelParamModel model)
{
model.Command = "READ_SN";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool RESTART()
{
LabelParamModel model = new LabelParamModel();
model.Command = "RESTART";
model.Address = "9999";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region ()
public bool RESTORE_DEVICE(LabelParamModel model)
{
model.Command = "RESTORE_DEVICE";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region 使
public bool SET_SCANDEVICE_ON(LabelParamModel model)
{
model.Command = "SET_SCANDEVICE_ON";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region 使
public bool SET_SCANDEVICE_OFF(LabelParamModel model)
{
model.Command = "SET_SCANDEVICE_OFF";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool SET_SCANDEVICE_CLEAR(LabelParamModel model)
{
model.Command = "SET_SCANDEVICE_CLEAR";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool SLEEP(LabelParamModel model)
{
model.Address = "9999";
model.Command = "SLEEP";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool SET_CONTROL_INFO()
{
LabelParamModel model = new LabelParamModel();
model.Command = "SET_CONTROL_INFO";
model.Address = "9999";
model.Parameter = "";
model.Timeout = "0.5";
return Send(model);
}
#endregion
#region
public bool Send(LabelParamModel model)
{
ResultsModel resultModel = new ResultsModel();
HttpHelper http = new HttpHelper();
2024-01-04 15:30:23 +08:00
string ControlIP = "http://" + model.ControlIP + ":81/cgi-bin/rpc.cgi";
2023-01-13 15:30:20 +08:00
string result = "";
SendParamModel sendModel = new SendParamModel();
sendModel.Address = model.Address;
sendModel.Command = model.Command;
sendModel.Parameter = model.Parameter;
sendModel.Timeout = model.Timeout;
try
{
string postStr = JsonHelper.SerializeObject(sendModel);
result = http.HttpPost_Old(ControlIP, postStr);
resultModel = JsonHelper.DeserializeObject<ResultsModel>(result);
LogHelper.WriteLogInfo("---------------------------------------" + ControlIP+"-----------"+ postStr);
if (!resultModel.Status.Equals("OK"))
{
LogHelper.WriteLogInfo("发送指令失败-发送指令:" + postStr);
LogHelper.WriteLogInfo("发送指令失败-返回信息:" + result);
}
}
catch (Exception e)
{
LogHelper.WriteLogInfo("发送指令异常:" + e.Message);
// throw;
}
if (resultModel != null && resultModel.Status.Equals("OK"))
{
return true;
}
else { return false; }
}
#endregion
#region
public void SendALL(List<LabelParamModel> model)
{
List<ResultsModel> resultModel = new List<ResultsModel>();
try
{
LogHelper.WriteLogInfo("********************"+DateTime.Now.ToString()+"***点亮条数:****"+ model.Count + "***************");
if (model != null)
{
List<IGrouping<string, LabelParamModel>> alllist = model.GroupBy(m => m.ControlIP).ToList();
AddressstorageDAL dal = new AddressstorageDAL();
DataTable dt = dal.getControlList();//获取控制器列表
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow item in dt.Rows)
{
var sdlist = alllist.FirstOrDefault(x => x.Key == item["ControlIP"].ToString());
HttpHelper http = new HttpHelper();
2024-01-04 15:30:23 +08:00
string ControlIP = "http://" + item["ControlIP"].ToString() + ":81/cgi-bin/rpc.cgi";
2023-01-13 15:30:20 +08:00
// LogHelper.WriteLogInfo(ControlIP + "***************");
string result = "";
List<SendParamModel> list = new List<SendParamModel>();
if (sdlist != null ) {
var ControlDetail = sdlist.ToArray();
for (int i =0;i< ControlDetail.Length;i++)
{
SendParamModel sendModel = new SendParamModel();
sendModel.Address = ControlDetail[i].Address;
sendModel.Command = ControlDetail[i].Command;
sendModel.Parameter = ControlDetail[i].Parameter;
sendModel.Uid = ControlDetail[i].Uid;
sendModel.Quantity = ControlDetail[i].Quantity;
sendModel.Title = ControlDetail[i].Title;
sendModel.Content = ControlDetail[i].Content;
sendModel.Type = ControlDetail[i].Type;
sendModel.Mode = ControlDetail[i].Mode;
sendModel.Order = ControlDetail[i].Order;
sendModel.Unit = ControlDetail[i].Unit;
sendModel.W_unit = ControlDetail[i].W_unit;
sendModel.W_quantity = ControlDetail[i].W_quantity;
sendModel.Loc = ControlDetail[i].Loc;
sendModel.Timeout ="0.5";
list.Add(sendModel);
if ((i + 1) % 15 == 0 || (i + 1) == ControlDetail.Length)
{
string postStr = JsonHelper.SerializeObject(list);
LogHelper.WriteLogInfo("#######################################发送拼接指令:"+postStr);
result = http.HttpPost_Old(ControlIP, postStr);
LogHelper.WriteLogInfo("返回:******************************"+result);
resultModel = JsonHelper.DeserializeObject<List<ResultsModel>>(result);
foreach (ResultsModel res in resultModel)
{
LogHelper.WriteLogInfo("点亮设备:"+res.Address+"IP:"+ item["ControlIP"].ToString() + "指令:"+res.Command+"返回:"+res.Status+res.Parameter+"", Log_Type.LIGHTUP);
if (res.Status == "ERROR")
{
LogHelper.WriteLogInfo("点亮指令" + postStr);
ErrorLogDAL errdal = new ErrorLogDAL();
ErrorLogModel errmodel = new ErrorLogModel();
2024-01-04 15:30:23 +08:00
errmodel.Remark = res.Address;
errmodel.Title = item["ControlIP"].ToString();
2023-01-13 15:30:20 +08:00
//if (res.Parameter.Contains("E16"))
//{
// string FORMAT_DEVICE = "{\"Address\":\""+res.Address+"\",\"Command\":\"FORMAT_DEVICE\",\"Parameter\":\"\",\"Timeout\":\"10\"}";
// result = http.HttpPost_Old(ControlIP, FORMAT_DEVICE);
// LogHelper.WriteLogInfo("E16----");
// errmodel.Remark = "E16----";
// errdal.InsertErrorLog(errmodel);
//}
//if (res.Parameter.Contains("E08"))
//{
// List<SendParamModel> sendlt= list.FindAll(p=>p.Command== "DISPLAY_JOB"&&p.Address== res.Address);
// if (sendlt.Any())
// {
// SendParamModel smol = sendlt.FirstOrDefault();
// string FORMAT_DEVICE = "[{\"Address\":\"" + res.Address + "\",\"Command\":\"FORMAT_DEVICE\",\"Parameter\":\"\",\"Timeout\":\"10\"}," +
// "{\"Address\":\"" + res.Address + "\",\"Command\":\"DISPLAY_JOB\",\"Parameter\":\""+ smol.Parameter+ "\",\"Timeout\":\"10\"}]";
// result = http.HttpPost_Old(ControlIP, FORMAT_DEVICE);
// LogHelper.WriteLogInfo("E08----");
// errmodel.Remark = "E08----";
// errdal.InsertErrorLog(errmodel);
// }
//}
if (res.Parameter == "device tagged")
{
AddressstorageDAL addal = new AddressstorageDAL();
LogHelper.WriteLogInfo(res.Address + item["ControlIP"].ToString() + "报错", Log_Type.ERROR);
addal.UpdateErrorState(res.Address, item["ControlIP"].ToString());
errmodel.Remark = "device tagged";
errdal.InsertErrorLog(errmodel);
}
}
}
list.Clear();
}
}
}
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogInfo("发送指令异常ALL:" + ex.Message);
}
}
#endregion
}
}