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 model) { SendALL(model); } #endregion #region 发送指令(初始化) public ResultModel SendALL_Init(List model) { List resultModel = new List(); ResultModel rest = new ResultModel(); rest.result = "0"; rest.msg = "成功!"; try { LogHelper.WriteLogInfo("********************" + DateTime.Now.ToString() + "***点亮条数:****" + model.Count + "***************"); if (model != null) { List> 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(); string ControlIP = "http://" + item["ControlIP"].ToString() + ":81/cgi-bin/rpc.cgi"; // LogHelper.WriteLogInfo(ControlIP + "***************"); string result = ""; List list = new List(); 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>(result); foreach (ResultsModel res in resultModel) { if (res.Status == "ERROR"&&res.Command!= "START_POLLING") { 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(); string ControlIP = "http://" + model.ControlIP + ":81/cgi-bin/rpc.cgi"; 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(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 model) { List resultModel = new List(); try { LogHelper.WriteLogInfo("********************"+DateTime.Now.ToString()+"***点亮条数:****"+ model.Count + "***************"); if (model != null) { List> 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(); string ControlIP = "http://" + item["ControlIP"].ToString() + ":81/cgi-bin/rpc.cgi"; // LogHelper.WriteLogInfo(ControlIP + "***************"); string result = ""; List list = new List(); 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>(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(); errmodel.Remark = res.Address; errmodel.Title = item["ControlIP"].ToString(); //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 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 } }