| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using Curtain.DataAccess;
- using Curtain.Extension.ExCompareOperator;
- 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.Siemens;
- using PLC_S.Proxy;
- namespace PLC_S.ServerModel
- {
- /// <summary>
- /// 返回产品等级(1:不合格,2:智能,3:连体,4:扫码异常,5:MES异常)等信息(毛坯入库对接乾润)
- /// </summary>
- public class GoodsLevel_QR : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
- {
- public Dictionary<string, PLC_S_GL_QR> PLC_FLAGS = new Dictionary<string, PLC_S_GL_QR>();
- public const string M_NAME = "GL_QR";
- 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();
- private bool _S_STOP = false;
- public int Interval
- {
- get;
- set;
- }
- public string EPTS_CODE
- {
- get;
- set;
- }
- public GoodsLevel_QR()
- {
- 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 = 10;
- 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
- {
- _S_STOP = false;
- logger.OutputTrace($"{M_NAME}_ServerStarting");
- }
- catch { }
- }
- private void ServerSocket_ServerStarted(object sender, EventArgs e)
- {
- try
- {
- _S_STOP = false;
- logger.OutputTrace($"{M_NAME}_ServerStarted");
- }
- catch { }
- }
- private void ServerSocket_ServerStoping(object sender, CancelEventArgs e)
- {
- try
- {
- _S_STOP = false;
- logger.OutputTrace($"{M_NAME}_ServerStoping");
- }
- catch { }
- }
- private void ServerSocket_ServerStoped(object sender, EventArgs e)
- {
- try
- {
- _S_STOP = false;
- logger.OutputTrace($"{M_NAME}_ServerStoped");
- }
- catch { }
- }
- private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
- {
- try
- {
- _S_STOP = false;
- 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
- int int_code = 0;
- /// <summary>
- /// 返回产品型号
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ServerSocket_Received(object sender, ReceiveSession e)
- {
- PLC_S_GL_QR plc_s = null;
- string cIP = e?.Client?.IP;
- string logKeyT = $"{M_NAME}-T[{cIP}]";
- string logKeyE = $"{M_NAME}-E[{cIP}]";
- _S_STOP = false;
- SocketClient<SiemensS7_1200Model> plc = null;
- Logger LoggerT = null;
- try
- {
- LoggerT = Logger.CreateLogger(logKeyT, logger_t);
- lock (LoggerT)
- {
- LoggerT.BeginTracking();
- 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[] c = e.Content.Split('#');
- string flag = null;
- if (c.Length > 0)
- {
- flag = c[0];
- }
- string barcode = null;
- if (c.Length > 1)
- {
- barcode = c[1];
- }
- //if (flag == "1")
- //{
- // flag = "6311";
- //}
- //else if (flag == "2")
- //{
- // flag = "6312";
- //}
- //else
- //{
- // flag = null;
- //}
- 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_GC(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);
- //outputMessage = $"[{e.Content}]PLC_Connect=[{plc_s.IP}:{plc_s.Port}]";
- //Logger.Trace(outputMessage, logKeyT, logger_t);
- //FormLogShow?.ShowLog(outputMessage);
- if (string.IsNullOrWhiteSpace(barcode) ||
- barcode == "0" ||
- barcode.Length != 11 ||
- barcode.StartsWith("0"))
- {
- outputMessage = $"[{e.Content}]接收条码格式错误";
- Logger.Warn(outputMessage, logKeyT, logger_t);
- Logger.Warn(outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- SendCode(plc_s, e, logKeyT, logKeyE, plc, 4);
- return;
- }
- //int_code++;
- //if (int_code > 3)
- //{
- // int_code = 1;
- //}
- //if (int_code == 1)
- //{
- // SendCode(plc_s, e, logKeyT, logKeyE, plc, 1);
- //}
- //else if (int_code == 2)
- //{
- // SendCode(plc_s, e, logKeyT, logKeyE, plc, 2);
- //}
- //else if (int_code == 3)
- //{
- // SendCode(plc_s, e, logKeyT, logKeyE, plc, 3);
- //}
- //else
- //{
- // SendCode(plc_s, e, logKeyT, logKeyE, plc, 3);
- //}
- //return;
- DataRow codeRow = GetGoodsLevel(barcode, e, logKeyE);
- if (codeRow == null)
- {
- outputMessage = $"[{e.Content}]此条码[{barcode}]不存在";
- Logger.Warn(outputMessage, logKeyT, logger_t);
- Logger.Warn(outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- SendCode(plc_s, e, logKeyT, logKeyE, plc, 4);
- return;
- }
- string level = codeRow["GOODSLEVELTYPEID"].ToString();
- string gtid = codeRow["goodstypeid"].ToString();
- string p_id = codeRow["flowprocedureid"].ToString();
- int goodslinetype = Convert.ToInt32(codeRow["goodslinetype"]);
- // 1不合格,2智能,3连体
- short code = 1;
- string codeName = "不合格";
- if (string.IsNullOrEmpty(level) || level.EqualAny("1", "4"))
- {
- if (gtid.EqualAny("18", "19"))
- {
- code = 2;
- codeName = "良品[智能]";
- }
- else
- {
- code = 3;
- codeName = "良品[连体]";
- }
- }
- outputMessage = $"[{e.Content}]产品等级={codeName}[{code}][{level}]{gtid}";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- if (code > 1 && p_id != plc_s.P_ID.ToString())
- {
- //xuwei add 2022-02-18 调整为工位打卡模式=================
- string userCode = "";
- //默认读取userCode
- if ( plc_s.WS_ID == 0)
- {
- userCode = plc_s.USER_CODE;
- }
- //优先从工位取userCode
- if (plc_s.WS_ID != 0)
- {
- userCode = GetUserCodeFromWorkStation(plc_s.WS_ID.ToString(), e, logKeyE);
- outputMessage = $"[{e.Content}]DB_UserCode=[{userCode}]";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- }
- //xuwei end===============================================
- // 毛坯入库计件
- string result = SmartDeviceProxy.Instance.Invoke<string>(
- p => p.AddWorkPieceByStatus3(
- _mes_s.AccountCode,
- userCode,
- null,
- plc_s.P_ID,
- barcode,
- null));
- outputMessage = $"[{e.Content}]MES_Result=[{result}]";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- // 返回状态(成功、失败)
- if (result != "OK")
- {
- if (result == "EE")
- {
- outputMessage = $"MES服务端异常(详情参见MES端日志)";
- }
- else if (result == "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);
- SendCode(plc_s, e, logKeyT, logKeyE, plc, 5);
- return;
- }
- }
- //1. 产品分级(int)【1不合格,2智能,3连体】
- SendCode(plc_s, e, logKeyT, logKeyE, plc, code);
- //2.高压标识(int)【1:高压,0:其他】
- SET_GoodsLineType(e, plc_s, logKeyT, plc, goodslinetype);
- }
- }
- 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);
- if (plc?.Socket?.Connected ?? false)
- {
- SendCode(plc_s, e, logKeyT, logKeyE, plc, 5);
- }
- }
- else
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyT, logger_t);
- Logger.Error(ex, $"[{e.Content}]ERROR-NOFLAG", logKeyE, logger_e);
- }
- }
- finally
- {
- LoggerT?.EndTracking();
- plc?.Disconnect();
- plc?.Close();
- //e.ReturnMessage("0000");
- }
- }
- private void SendCode(PLC_S_GL_QR plc_s, ReceiveSession e, string loggerNameT,
- string loggerNameE, SocketClient<SiemensS7_1200Model> plc, short code)
- {
- try
- {
- //using (SocketClient<SiemensS7_1200Model> plc = new SocketClient<SiemensS7_1200Model>())
- {
- // 扫码结果:【1:不合格,2:智能,3:连体,4:扫码异常,5:MES异常】
- string outputMessage = $"[{e.Content}]SET_CODE=[{code}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
- Logger.Trace(outputMessage, loggerNameT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- PLCResult p_r = plc?.Write<short>(plc_s.Add_Code, plc_s.Add_CodeNum, code);
- outputMessage = $"[{e.Content}]END_CODE=[{p_r}]{plc_s.Add_Code + plc_s.Add_CodeNum}";
- Logger.Trace(outputMessage, loggerNameT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- }
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameT, logger_t);
- Logger.Error(ex, $"[{e.Content}]ERROR-SendCode", loggerNameE, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-SendCode={ex.Message}");
- }
- }
- //qq add 2025-05-27 增加高压标识
- private void SET_GoodsLineType(ReceiveSession e, PLC_S_GL_QR plc_s, string logKeyT, SocketClient<SiemensS7_1200Model> plc, int code)
- {
- if (!string.IsNullOrEmpty(plc_s.Add_GoodsLineType) && !string.IsNullOrEmpty(plc_s.Add_GoodsLineTypeNum))
- {
- string outputMessage = $"[{e.Content}]SET_GoodsLineType=[{code}]{plc_s.Add_GoodsLineType + plc_s.Add_GoodsLineTypeNum}";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- PLCResult p_r = plc.Write<short>(plc_s.Add_GoodsLineType, plc_s.Add_GoodsLineTypeNum, (short)(code));
- outputMessage = $"[{e.Content}]END_GoodsLineType=[{p_r}]{plc_s.Add_GoodsLineType + plc_s.Add_GoodsLineTypeNum}";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- }
- }
- private DataRow GetGoodsLevel(string barcode, ReceiveSession e, string loggerName)
- {
- IDataAccess dataAccess = null;
- try
- {
- dataAccess = PLC_S_DataAccess.GetDataAccess(e);
- string sqlString = "select gdd.GOODSLEVELTYPEID,g.goodstypeid, inp.flowprocedureid," +
- " case when g.GOODS_LINE_TYPE=1 then 1 else 2 end as goodslinetype \n" +
- " from tp_pm_groutingdailydetail gdd\n" +
- " LEFT JOIN tp_pm_inproduction inp on inp.groutingdailydetailid = gdd.groutingdailydetailid\n" +
- " inner join tp_mst_goods g on g.goodsid = gdd.goodsid\n" +
- " where gdd.barcode = :barcode";
- DataTable dataTable = dataAccess.ExecuteDatatable(sqlString, new CDAParameter(":barcode", barcode));
- if (dataTable.HasData())
- {
- return dataTable.Rows[0];
- }
- return null;
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-GetGoodsLevel", loggerName, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetGoodsLevel={ex.Message}");
- return null;
- }
- finally
- {
- dataAccess?.Close();
- }
- }
- private string GetUserCodeFromWorkStation(string workStationId, ReceiveSession e, string loggerName)
- {
- try
- {
- using (IDataAccess conn = PLC_S_DataAccess.GetDataAccess())
- {
- string sqlStr = @"
- SELECT
- T.USERCODE
- FROM
- TP_MST_WORKSTATION T
- INNER JOIN TP_MST_USER U ON U.USERCODE = T.USERCODE
- WHERE
- T.WORKSTATIONID = :WORKSTATIONID
- ";
- object result = conn.ExecuteScalar(sqlStr, new CDAParameter(":WORKSTATIONID", workStationId));
- return result == null ? "0" : result.ToString();
- }
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-GetUserCodeFromWorkStation", loggerName, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetUserCodeFromWorkStation={ex.Message}");
- return "0";
- }
- }
- private PLC_S_GL_QR GetPLC_S_GC(string flag, ReceiveSession e, string loggerName)
- {
- try
- {
- flag = M_NAME + flag;
- if (PLC_FLAGS.ContainsKey(flag))
- {
- return PLC_FLAGS[flag];
- }
- else
- {
- PLC_S_GL_QR plc_s = new PLC_S_GL_QR();
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_{M_NAME}.ini");
- plc_s.IP = ini.Read(flag, "IP");
- plc_s.Port = ini.Read(flag, "Port").ToInt32();
- plc_s.P_ID = ini.Read(flag, "P_ID").ToInt32();
- plc_s.USER_CODE = ini.Read(flag, "USER_CODE");
- plc_s.Add_Code = ini.Read(flag, "Add_Code");
- plc_s.Add_CodeNum = ini.Read(flag, "Add_CodeNum");
- plc_s.WS_ID = Convert.ToInt32(ini.Read(flag, "WS_ID"));
- plc_s.WS_NAME = ini.Read(flag, "WS_NAME");
- //plc_s.Add_F_Code = ini.Read(flag, "Add_F_Code");
- //plc_s.Add_F_CodeNum = ini.Read(flag, "Add_F_CodeNum");
- //plc_s.Add_Error = ini.Read(flag, "Add_Error");
- //plc_s.Add_ErrorNum = ini.Read(flag, "Add_ErrorNum");
- //qq add 2025-05-27 增加高压标识
- plc_s.Add_GoodsLineType = ini.Read(flag, "Add_GoodsLineType");
- plc_s.Add_GoodsLineTypeNum = ini.Read(flag, "Add_GoodsLineTypeNum");
- PLC_FLAGS.Add(flag, plc_s);
- return plc_s;
- }
- }
- catch (Exception ex)
- {
- Logger.Error(ex, $"[{e.Content}]ERROR-GetPLC_S_GC", loggerName, logger_e);
- FormLogShow?.ShowLog($"[{e.Content}]ERROR-GetPLC_S_GC={ex.Message}");
- return null;
- }
- }
- }
- }
|