| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using Curtain.Core;
- using Curtain.DataAccess;
- using Curtain.Extension.ExObjectConvert;
- using Curtain.Extension.ExSystemData;
- using Curtain.Framework.Json;
- using Curtain.Helpers;
- using Curtain.Log;
- using Curtain.Net.Sockets.PLC;
- using Curtain.Net.Sockets.PLC.Model;
- using Curtain.Net.Sockets.PLC.Model.Melsec;
- using Curtain.Net.Sockets.PLC.Model.Siemens;
- using Dongke.IBOSS.PRD.WCF.DataModels;
- using Newtonsoft.Json.Linq;
- using PLC_S.Proxy;
- namespace PLC_S.ServerModel
- {
- /// <summary>
- /// 高压注浆登记
- /// </summary>
- public class H_Grouting : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
- {
- public static Dictionary<string, PLC_S_HPG> PLC_FLAGS = new Dictionary<string, PLC_S_HPG>();
- public const string M_NAME = "HPG";
- private MES_S _mes_s = null;
- private readonly Logger logger = Logger.CreateLogger(M_NAME);
- private readonly LogInfo logger_t = new LogInfo();
- private readonly LogInfo logger_e = new LogInfo();
- public int Interval
- {
- get;
- set;
- }
- public string EPTS_CODE
- {
- get;
- set;
- }
- public H_Grouting()
- {
- logger.FileExistDays = 30;
- logger.FilePrefix = "";
- logger.FileSuffix = M_NAME;
- logger.LevelOneFile = true;
- logger.FileNameWithoutDate = true;
- logger_t.SubFolderFormat = "<name>";
- logger_t.FileExistDays = 30;
- logger_t.LevelOneFile = true;
- logger_e.FileExistDays = 0;
- this.Model.FormatType = CommandFormatType.StartStopChar;
- this.ServerStarting += ServerSocket_ServerStarting;
- this.ServerStarted += ServerSocket_ServerStarted;
- this.ServerStoping += ServerSocket_ServerStoping;
- this.ServerStoped += ServerSocket_ServerStoped;
- this.ServerMessage += ServerSocket_ServerMessage;
- this.Received += ServerSocket_Received;
- _mes_s = MES_S.Get();
- }
- #region Server
- public IFormLogShow FormLogShow
- {
- get;
- set;
- }
- private void ServerSocket_ServerStarting(object sender, CancelEventArgs e)
- {
- try
- {
- logger.OutputTrace($"{M_NAME}_ServerStarting");
- }
- catch { }
- }
- private void ServerSocket_ServerStarted(object sender, EventArgs e)
- {
- try
- {
- logger.OutputTrace($"{M_NAME}_ServerStarted");
- }
- catch { }
- }
- private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
- {
- try
- {
- logger.OutputTrace($"{M_NAME}_ServerStoping");
- }
- catch { }
- }
- private void ServerSocket_ServerStoped(object sender, EventArgs e)
- {
- try
- {
- logger.OutputTrace($"{M_NAME}_ServerStoped");
- }
- catch { }
- }
- private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
- {
- try
- {
- string message = e.ToString();
- string cip = e?.Client?.IP;
- if (e.Type == ServerMessageType.Debug)
- {
- Logger.Debug(message, $"{M_NAME}-T[{cip}]", logger_t);
- }
- if (e.Type == ServerMessageType.Error)
- {
- Logger.Error(e.Exception, message, $"{M_NAME}-E[{cip}]", logger_e);
- }
- if (e.Type == ServerMessageType.Trace)
- {
- Logger.Trace(message, $"{M_NAME}-T[{cip}]", logger_t);
- }
- if (e.Type == ServerMessageType.Warning)
- {
- Logger.Warn(message, $"{M_NAME}-E[{cip}]", logger_e);
- }
- FormLogShow?.ShowLog("ServerMessage=" + message);
- }
- catch { }
- }
- #endregion
- /// <summary>
- /// 返回产品型号
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ServerSocket_Received(object sender, ReceiveSession e)
- {
- PLC_S_HPG plc_s = null;
- string cIP = e?.Client?.IP;
- string logKeyT = $"{M_NAME}-T[{cIP}]";
- string logKeyE = $"{M_NAME}-E[{cIP}]";
- SocketClient<SiemensS7_1200Model> plc = null;
- try
- {
- string outputMessage = e.ToString();
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- if (string.IsNullOrWhiteSpace(e.Content))
- {
- outputMessage = "接收数据为空";
- Logger.Warn(outputMessage, logKeyT, logger_t);
- Logger.Warn(outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- return;
- }
- outputMessage = e.Content;
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- string flag = e.Content;
- //string[] c = e.Content.Split('#');
- //
- //if (c.Length > 0)
- //{
- // flag = c[0];
- //}
- //string barcode = null;
- //if (c.Length > 1)
- //{
- // barcode = c[1];
- //}
- if (string.IsNullOrWhiteSpace(flag))
- {
- outputMessage = $"[{e.Content}]接收设备代码为空";
- Logger.Warn(outputMessage, logKeyT, logger_t);
- Logger.Warn(outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- return;
- }
- plc_s = GetPLC_S_HPG(flag, e, logKeyE);
- if (plc_s == null)
- {
- outputMessage = $"[{e.Content}]设备代码[{flag}]未找到ini配置或有错误";
- Logger.Error(null, outputMessage, logKeyT, logger_t);
- Logger.Error(null, outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- return;
- }
- outputMessage = $"[{e.Content}]PLC_Connect={JsonHelper.FromObject(plc_s)}";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- plc = new SocketClient<SiemensS7_1200Model>();
- plc.Connect(plc_s.IP, plc_s.Port);
- // 查询当前成型工位对应的成型工号,对应的高压线号,
- DataRow dataRow = GetGUserCode(plc_s, e, logKeyE);
- string usercode = dataRow["usercode"].ToString();
- //string H_LineCode = dataRow["H_LineCode"].ToString();
- //int H_LineIndex = dataRow["H_LineIndex"].ToInt32();
- //int H_BatchNo = dataRow["H_BatchNo"].ToInt32();
- //int H_BatchIndex = dataRow["H_BatchIndex"].ToInt32();
- // 设置注浆或未注浆原因
- string lineCode = "D04A01";
- JObject jGroutingData = new JObject();
- jGroutingData.Add("GROUTINGDATE", DateTime.Now.ToString("yyyy-MM-dd"));
- jGroutingData.Add("GROUTINGLINECODE", lineCode);
- jGroutingData.Add("GROUTINGTIMES", 1);
- JArray jGROUTINGLINEDETAIL = new JArray();
- JObject jToken = new JObject();
- jToken.Add("GROUTINGMOULDCODE", "D04A01-001");
- jToken.Add("GROUTINGFLAG", "1");
- jToken.Add("NOGROUTINGRREASON", "null");
- jGROUTINGLINEDETAIL.Add(jToken);
- jToken = new JObject();
- jToken.Add("GROUTINGMOULDCODE", "D04A01-002");
- jToken.Add("GROUTINGFLAG", "0");
- jToken.Add("NOGROUTINGRREASON", "47");
- jGROUTINGLINEDETAIL.Add(jToken);
- jGroutingData.Add("GROUTINGLINEDETAIL", jGROUTINGLINEDETAIL);
- string groutingData = jGroutingData.ToString();
- //MES_S _mes_s = MES_S.Get();
- ServiceResultEntity result = SmartDeviceProxy.Instance.Invoke<ServiceResultEntity>(
- p => p.SetHGrouting(
- _mes_s.AccountCode,
- usercode,
- lineCode,
- groutingData,
- null));
- // 返回状态(成功、失败)
- if (result.Status != ServiceResultStatus.Success)
- {
- if (result.Status == ServiceResultStatus.SystemError)
- {
- outputMessage = $"MES服务端异常(详情参见MES端日志)";
- }
- else if (result.Message == "EU-01")
- {
- outputMessage = $"无效的帐套或工号(详情参见MES端日志)";
- }
- else
- {
- outputMessage = $"其他错误(详情参见MES端日志)[{result}]";
- }
- Logger.Error(null, outputMessage, logKeyT, logger_t);
- Logger.Error(null, outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- SendError(plc_s, e, logKeyT, logKeyE, plc);
- return;
- }
- // 读取注浆参数
- // 插入注浆参数
- int gd_id = result.Result.ToInt32();
- int r = SetHGroutingParas(plc_s, e, logKeyE, gd_id, dataRow, null);
- }
- catch (Exception ex)
- {
- FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
- if (plc_s != null)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR", logKeyT, logger_t);
- Logger.Error(ex, $"[{e.Content}]ERROR", logKeyE, logger_e);
- SendError(plc_s, e, logKeyT, logKeyE, plc);
- }
- else
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
- Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
- }
- }
- finally
- {
- e.ReturnMessage("0000");
- }
- }
- private void SendError(PLC_S_HPG plc_s, ReceiveSession e, string loggerNameT,
- string loggerNameE, SocketClient<SiemensS7_1200Model> plc)
- {
- try
- {
- //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
- {
- string outputMessage = $"[{e.Content}]SET_ErrorNum=[1]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
- Logger.Trace(outputMessage, loggerNameT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- plc?.Write<short>(plc_s.Add_Error, plc_s.Add_ErrorNum, (short)1);
- outputMessage = $"[{e.Content}]END_ErrorNum=[1]{plc_s.Add_Error + plc_s.Add_ErrorNum}";
- Logger.Trace(outputMessage, loggerNameT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- }
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameT, logger_t);
- Logger.Error(ex, $"[{e.Content}]ERROR-SendError", loggerNameE, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendError={ex.Message}");
- }
- }
- private PLC_S_HPG GetPLC_S_HPG(string flag, ReceiveSession e, string loggerName)
- {
- try
- {
- flag = M_NAME + flag;
- if (PLC_FLAGS.ContainsKey(flag))
- {
- return PLC_FLAGS[flag];
- }
- else
- {
- PLC_S_HPG plc_s = new PLC_S_HPG();
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
- plc_s.GL_ID = ini.Read(flag, "GL_ID").ToInt32();
- plc_s.GL_CODE = ini.Read(flag, "GL_CODE");
- plc_s.GL_H_CODE = ini.Read(flag, "GL_H_CODE");
- plc_s.GL_H_INDEX = ini.Read(flag, "GL_H_INDEX").ToInt32();
- plc_s.WORKSTATIONID = ini.Read(flag, "WORKSTATIONID").ToInt32();
- plc_s.NOGROUTINGRREASON = ini.Read(flag, "NOGROUTINGRREASON").ToInt32();
- plc_s.IP = ini.Read(flag, "IP");
- plc_s.Port = ini.Read(flag, "Port").ToInt32();
- plc_s.Add_Code = ini.Read(flag, "Add_Code");
- plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
- plc_s.Add_Error = ini.Read(flag, "Add_Error");
- plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
- PLC_FLAGS.Add(flag, plc_s);
- return plc_s;
- }
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_HPG", loggerName, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_HPG={ex.Message}");
- return null;
- }
- }
- private DataRow GetGUserCode(PLC_S_HPG plc_s, ReceiveSession e, string loggerName)
- {
- IDataAccess dataAccess = null;
- try
- {
- dataAccess = PLC_S_DataAccess.GetDataAccess(e);
- string sqlString = @"
- SELECT t.usercode
- ,gl.h_linecode
- ,gl.h_lineindex
- ,gl.h_batchno
- ,gl.h_batchindex
- FROM tp_mst_workstation t
- INNER JOIN tp_pc_groutingline gl
- ON gl.groutinglineid = t.groutinglineid
- WHERE t.workstationtypeid = 301
- AND t.workstationid = @workstationid@
- AND t.groutinglineid = @groutinglineid@";
- CDAParameter[] ps = new CDAParameter[]
- {
- new CDAParameter("groutinglineid", plc_s.GL_ID),
- new CDAParameter("workstationid", plc_s.WORKSTATIONID),
- };
- DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, ps);
- if (dataTable.HasData())
- {
- return dataTable.Rows[0];
- }
- return null;
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-GetGUserCode", loggerName, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGUserCode={ex.Message}");
- return null;
- }
- finally
- {
- dataAccess?.Close();
- }
- }
- private int SetHGroutingParas(PLC_S_HPG plc_s, ReceiveSession e, string loggerName,
- int GroutingDailyID, DataRow dataRow, object jj)
- {
- IDataAccess dataAccess = null;
- try
- {
- dataAccess = PLC_S_DataAccess.GetDataAccess(e);
- string sqlString = @"
- INSERT INTO TP_PM_H_GroutingParas
- (GroutingDailyID
- ,GroutingLineID
- ,GroutingLineCode
- ,H_LineCode
- ,H_LineIndex
- ,H_BatchNo
- ,H_BatchIndex
- --,a
- )
- VALUES
- (@GroutingDailyID@
- ,@GroutingLineID@
- ,@GroutingLineCode@
- ,@H_LineCode@
- ,@H_LineIndex@
- ,@H_BatchNo@
- ,@H_BatchIndex@
- --,1
- )";
- CDAParameter[] ps = new CDAParameter[]
- {
- new CDAParameter("GroutingDailyID", GroutingDailyID),
- new CDAParameter("GroutingLineID", plc_s.GL_ID),
- new CDAParameter("GroutingLineCode", plc_s.GL_CODE),
- new CDAParameter("H_LineCode", plc_s.GL_H_CODE),
- new CDAParameter("H_LineIndex", plc_s.GL_H_INDEX),
- new CDAParameter("H_BatchNo", dataRow["H_BatchNo"]),
- new CDAParameter("H_BatchIndex", dataRow["H_BatchIndex"]),
- //new CDAParameter("H_BatchIndex", dataRow["H_BatchIndex"]),
- };
- return dataAccess.ExecuteNonQuery(sqlString, ps);
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-SetHGroutingParas", loggerName, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-SetHGroutingParas={ex.Message}");
- return -1;
- }
- finally
- {
- dataAccess?.Close();
- }
- }
- }
- }
|