| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Threading;
- using Curtain.Core;
- using Curtain.DataAccess;
- 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 PLC_S.Proxy;
- namespace PLC_S.ServerModel
- {
- /// <summary>
- /// 计件(中陶,机械臂施釉)
- /// </summary>
- public class FGC_BZ : SocketServer<SimpleSocketServerModel>, IShowFormLog, IWindowsServer
- {
- //public Dictionary<string, PLC_S_PW> PLC_FLAGS = new Dictionary<string, PLC_S_PW>();
- //public Dictionary<string, PLC_S_GC> PLC_FLAGS_GC = new Dictionary<string, PLC_S_GC>();
- public const string M_NAME = "FGC_BZ";
- private readonly 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 FGC_BZ_INI _FGC_BZ_INI = null;
- private bool _S_STOP = false;
- public FGC_BZ()
- {
- logger.FileExistDays = 30;
- logger.FilePrefix = "";
- logger.FileSuffix = M_NAME;
- logger_t.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();
- _FGC_BZ_INI = FGC_BZ_INI.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 = true;
- logger.OutputTrace($"{M_NAME}_ServerStoping");
- }
- catch { }
- }
- private void ServerSocket_ServerStoped(object sender, EventArgs e)
- {
- try
- {
- _S_STOP = true;
- logger.OutputTrace($"{M_NAME}_ServerStoped");
- }
- catch { }
- }
- private void ServerSocket_ServerMessage(object sender, ServerMessageEventArgs e)
- {
- try
- {
- _S_STOP = true;
- 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
- private string _RECEIVED_CODE = null;
- /// <summary>
- /// 返回产品型号
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void ServerSocket_Received(object sender, ReceiveSession e)
- {
- //string receivedCode = Guid.NewGuid().ToString();
- //_RECEIVED_CODE = receivedCode;
- //_S_STOP = true;
- //lock (this)
- {
- string cIP = e?.Client?.IP;
- string logKeyT = $"{M_NAME}-T[{cIP}]";
- string logKeyE = $"{M_NAME}-E[{cIP}]";
- //if (_RECEIVED_CODE != receivedCode)
- //{
- // string outputMessage = $"[{e.Content}]不是当前请求,退出排队";
- // Logger.Warn(outputMessage, logKeyT, logger_t);
- // Logger.Warn(outputMessage, logKeyE, logger_e);
- // FormLogShow?.ShowLog(outputMessage);
- // return;
- //}
- //_S_STOP = false;
- //PLC_S_PW plc_s = null;
- //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);
- SendError(e, logKeyT, logKeyE);
- return;
- }
- outputMessage = e.Content;
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- string flag = null;
- 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(barcode) || barcode == "0" || barcode.StartsWith("0"))
- {
- outputMessage = $"[{e.Content}]接收条码为空";
- Logger.Warn(outputMessage, logKeyT, logger_t);
- Logger.Warn(outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- SendError(e, logKeyT, logKeyE);
- return;
- }
- outputMessage = JsonHelper.FromObject(_FGC_BZ_INI);
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- string usercode = _FGC_BZ_INI.USER_CODE;//"XC01";
- int p_id = _FGC_BZ_INI.P_ID;//15;
- outputMessage = $"[{e.Content}]AddWorkPieceByStatus3=[P_ID:{p_id}]";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- string result = SmartDeviceProxy.Instance.Invoke<string>(
- p => p.AddWorkPieceByStatus3(
- _mes_s.AccountCode,
- usercode,
- null,
- p_id,
- barcode,
- null));
- //string result = "OK";
- // 返回状态(成功、失败)
- if (result == "OK")
- {
- outputMessage = $"[{e.Content}]result=[OK]";
- Logger.Trace(outputMessage, logKeyT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- return;
- }
- if (result == "EE")
- {
- outputMessage = $"[{e.Content}]result=[{result}]MES服务端异常(详情参见MES端日志)";
- }
- else if (result == "EU-01")
- {
- outputMessage = $"[{e.Content}]result=[{result}]无效的帐套或工号(详情参见MES端日志)";
- }
- else
- {
- outputMessage = $"[{e.Content}]result=[{result}]其他错误(详情参见MES端日志)";
- }
- Logger.Warn(outputMessage, logKeyT, logger_t);
- Logger.Warn(outputMessage, logKeyE, logger_e);
- FormLogShow?.ShowLog(outputMessage);
- SendError(e, logKeyT, logKeyE);
- }
- catch (Exception ex)
- {
- FormLogShow?.ShowLog($"[{e.Content}]ERROR={ex.Message}");
- SendError(e, logKeyT, logKeyE);
- }
- finally
- {
- //e.ReturnMessage("0000");
- }
- }
- }
- //private void SendError(PLC_S_PW plc_s, ReceiveSession e, string loggerNameT,
- // string loggerNameE, SocketClient<SiemensS7_1200Model> plc)
- private void SendError(ReceiveSession e, string loggerNameT, string loggerNameE)
- {
- try
- {
- using (SocketClient<SimpleSocketClientModel> plc = new SocketClient<SimpleSocketClientModel>())
- {
- plc.Connect(_FGC_BZ_INI.SMT_IP, _FGC_BZ_INI.SMT_PORT);
- string outputMessage = $"[{e.Content}]SET_ErrorNum=[{_FGC_BZ_INI.SMT_ECODE}]";
- Logger.Trace(outputMessage, loggerNameT, logger_t);
- FormLogShow?.ShowLog(outputMessage);
- PLCMessage cm = new PLCMessage();
- cm.Command = _FGC_BZ_INI.SMT_ECODE;
- plc.DoCommandOneWay(cm);
- outputMessage = $"[{e.Content}]END_ErrorNum=[{_FGC_BZ_INI.SMT_ECODE}]";
- 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}");
- }
- }
- public class FGC_BZ_INI
- {
- //包装入仓工序ID、生产工号
- public int P_ID = 15;
- public string USER_CODE = "XC01";
- //扫描头IP、端口、错误命令
- public string SMT_IP = "10.0.8.11";
- public int SMT_PORT = 1000;
- public string SMT_ECODE = "111111";
- public static FGC_BZ_INI Get()
- {
- FGC_BZ_INI plc_s = new FGC_BZ_INI();
- INIHelper ini = INIHelper.IniFile($@"PLC_S_INI\FGC_BZ.ini");
- plc_s.P_ID = ini.Read("FGC_BZ", "P_ID").ToInt32();
- plc_s.USER_CODE = ini.Read("FGC_BZ", "USER_CODE");
- plc_s.SMT_IP = ini.Read("FGC_BZ", "SMT_IP");
- plc_s.SMT_PORT = ini.Read("FGC_BZ", "SMT_PORT").ToInt32();
- plc_s.SMT_ECODE = ini.Read("FGC_BZ", "SMT_ECODE");
- return plc_s;
- }
- }
- }
- }
|