// *********************************************************************** // Assembly : GeneratorCode // Author : 黄昕 // Created : 02-14-2019 // // Last Modified By : 黄昕 // Last Modified On : 02-26-2019 // *********************************************************************** // // Copyright © 2019 // // // *********************************************************************** using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; using GeneratorCode.Configure; using GeneratorCode.Logs; using JetBrains.Annotations; using Newtonsoft.Json; using TmatrixLibrary; namespace GeneratorCode { /// /// Class GeneratorParams. /// [JsonObject(MemberSerialization.OptOut)] public class GeneratorParams { /// /// Initializes a new instance of the class. /// public GeneratorParams() { dpi = new[] {0, 0, 0, 0}; pointType = new[] {0, 0, 0, 0}; imageType = new[] {false, false, false, true}; startPageId = 0; key = "S0,O000,B0000,P000-255,D2018/12/31;CCAFBQMXYPOAOCIRK52S8QC8SO4A0AGA8Y"; //filePath = "E:\\NetEase\\轨迹笔\\Sample\\123.pdf"; //"C:\\Works\\pdf\\123.pdf"; filePath = "C:\\Works\\pdf\\123.pdf"; sessionId = "4BD5D923-47EA-4DEF-A1CD-9B85B515B191"; } /// /// Gets or sets the dpi. /// /// The dpi. public int[] dpi { get; set; } /// /// Gets or sets the file path. /// /// The file path. public string filePath { get; set; } /// /// Gets or sets the type of the image. /// /// The type of the image. public bool[] imageType { get; set; } /// /// Gets or sets the key. /// /// The key. public string key { get; set; } /// /// Gets or sets the type of the point. /// /// The type of the point. public int[] pointType { get; set; } /// /// Gets or sets the session identifier. /// /// The session identifier. [JsonIgnore] public string sessionId { get; set; } /// /// Gets or sets the start page identifier. /// /// The start page identifier. public int startPageId { get; set; } } /// /// Class GenerCodeRet. /// public class GenerCodeRet { /// /// Initializes a new instance of the class. /// /// The s identifier. /// The PRG. public GenerCodeRet(string sId, int prg) { sessionId = sId; progress = prg; } /// /// Gets or sets the progress. /// /// The progress. public int progress { get; set; } /// /// Gets or sets the session identifier. /// /// The session identifier. public string sessionId { get; set; } } /// /// Class MainConfig. /// public class MainConfig { /// /// Delegate MainCfgChangedHandle /// public delegate void MainCfgChangedHandle(); /// /// The CFG lock /// private readonly object _cfgLock = new object(); /// /// Initializes a new instance of the class. /// public MainConfig() { LoadConfig(); OnMainCfgChanged += MainCfgChanged; } /// /// Gets or sets a value indicating whether [base64 MSG content]. /// /// true if [base64 MSG content]; otherwise, false. public bool Base64MsgContent { get; set; } /// /// Gets or sets the server addr. /// /// The server addr. public string ServerAddr { get; set; } /// /// Gets or sets the server port. /// /// The server port. public int ServerPort { get; set; } /// /// Occurs when [on main CFG changed]. /// public static event MainCfgChangedHandle OnMainCfgChanged; /// /// Loads the configuration. /// public void LoadConfig() { lock (_cfgLock) { Base64MsgContent = NConfig.GetCfgValue("Main", "ProBase64Msg", true); ServerPort = NConfig.GetCfgValue("Main", "ProServerPort", 10088); ServerAddr = NConfig.GetCfgValue("Main", "ProServerAddr", "127.0.0.1"); } } /// /// Mains the CFG changed. /// protected static void MainCfgChanged() { OnMainCfgChanged?.Invoke(); } } /// /// Class MessageProcess. /// public class MessageProcess { /// /// The UDP /// private readonly UdpClient _udp = new UdpClient(); /// /// Initializes a new instance of the class. /// /// The CFG. public MessageProcess(MainConfig cfg) { var svrAddr = IPAddress.Parse(cfg.ServerAddr); _server = new IPEndPoint(svrAddr, cfg.ServerPort); } /// /// Gets the server. /// /// The server. private IPEndPoint _server { get; } /// /// Sends the message. /// /// The MSG. public void SendMessage([NotNull] string msg) { if (msg.Length > 0) { var sendData = Encoding.Default.GetBytes(msg); _udp.Send(sendData, sendData.Length, _server); NLog.Debug("Send: " + msg + Environment.NewLine); } } } /// /// Class RspMessage. /// public class RspMessage { /// /// Initializes a new instance of the class. /// /// The s identifier. public RspMessage(string sId) { err = 0; msg = ""; data = new GenerCodeRet(sId, 0); } /// /// Initializes a new instance of the class. /// /// The s identifier. /// The PRG. public RspMessage(string sId, int prg) { err = 0; msg = ""; data = new GenerCodeRet(sId, prg); } /// /// Gets or sets the data. /// /// The data. public GenerCodeRet data { get; set; } /// /// Gets or sets the error. /// /// The error. public int err { get; set; } /// /// Gets or sets the MSG. /// /// The MSG. public string msg { get; set; } /// /// Formats the RSP message. /// /// The error code. /// The readme. /// The PRG. /// if set to true [en base64]. /// System.String. public string FormatRspMessage(int errCode, string readme, int prg, bool enBase64 = true) { var rsp = new RspMessage(data.sessionId, prg); rsp.err = errCode; if (enBase64) rsp.msg = Convert.ToBase64String(Encoding.Default.GetBytes(readme)); else rsp.msg = readme; return JsonConvert.SerializeObject(rsp); } } /// /// Class MainCode. /// internal class MainCode { /// /// Defines the entry point of the application. /// /// The arguments. /// System.Int32. private static int Main(string[] args) { NConfig.InitConfigure(); NLog.NLog_Init(); GeneratorParams inParams = null; RspMessage rspMsg; var mainCfg = new MainConfig(); var msgProcess = new MessageProcess(mainCfg); var tmObj = new TmatrixClass(); AppDomain.CurrentDomain.UnhandledException += (sender, e) => { NLog.Crash("UnHandled Exception: " + e.ToString()); }; AppDomain.CurrentDomain.ProcessExit += (sender, ev) => { try { tmObj.TmatrixUninitialize(); } catch (Exception e) { //var msg = rspMsg.FormatRspMessage(9, e.Message, 0); //RspMessage.SendRspMessage(msg); NLog.Crash(e.ToString()); } NLog.NLog_Finish(); }; //string inputArg = // "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpbWFnZV90eXBlI" + // "jpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6IlMwLE8wMDAsQjAwMDAsUDAwMC0" + // "yNTUsRDIwMTgvMTIvMzE7Q0NBRkJRTVhZUE9BT0NJUks1MlM4UUM4U080QTBBR0E4WSIsIm" + // "ZpbGVQYXRoIjoiRTpcXE5ldEVhc2VcXLnsvKOxylxcU2FtcGxlXFwxMjMucGRmIiwiU3Rhcn" + // "RQYWdlSUQiOjAsInNlc3Npb25JZCI6IjRCRDVEOTIzLTQ3RUEtNERFRi1BMUNELTlCODVCNT" + // "E1QjE5MSJ9"; //string inputArg2 = "eyJkcGkiOlswLDAsMCwwXSwicG9pbnRfdHlwZSI6WzAsMCwwLDBdLCJpb" + // "WFnZV90eXBlIjpbZmFsc2UsZmFsc2UsZmFsc2UsdHJ1ZV0sImtleSI6Il" + // "MwLE8wMDAsQjAwMDAsUDAwMC0yNTUsRDIwMTgvMTIvMzE7Q0NBRkJRTVh" + // "ZUE9BT0NJUks1MlM4UUM4U080QTBBR0E4WSIsImZpbGVQYXRoIjoiQzpc" + // "XFdvcmtzXFxwZGZcXDEyMy5wZGYiLCJTdGFydFBhZ2VJRCI6MCwic2Vzc" + // "2lvbklkIjoiNEJENUQ5MjMtNDdFQS00REVGLUExQ0QtOUI4NUI1MTVCMTkxIn0="; try { if (args.Length == 1) { var deCode = Convert.FromBase64String(args[0]); var strParams = Encoding.Default.GetString(deCode); inParams = JsonConvert.DeserializeObject(strParams); } else if (args.Length == 2) { var deCode = Convert.FromBase64String(args[1]); var strParams = Encoding.Default.GetString(deCode); inParams = JsonConvert.DeserializeObject(strParams); inParams.sessionId = args[0]; } else { inParams = new GeneratorParams(); } } catch (Exception e) { rspMsg = new RspMessage(""); var msg = rspMsg.FormatRspMessage(10, e.Message, 0, mainCfg.Base64MsgContent); msgProcess.SendMessage(msg); if (inParams != null) NLog.Crash(string.Format("[{0}]: ", inParams.sessionId) + e.Message); return -(int) ErrCode.ErrJsonDecode; } rspMsg = new RspMessage(inParams.sessionId); tmObj.ProgressChange += step => { var msgContent = rspMsg.FormatRspMessage(0, "Progress", step, mainCfg.Base64MsgContent); var msg = JsonConvert.SerializeObject(msgContent); msgProcess.SendMessage(msg); }; var jsInput = JsonConvert.SerializeObject(inParams); NLog.Debug("Input:" + jsInput + Environment.NewLine); if (!File.Exists(inParams.filePath)) { NLog.Error("File Not Exists: " + inParams.filePath + Environment.NewLine); return -(int) ErrCode.ErrFileNotexists; } try { tmObj.TmatrixInitialize(); } catch (Exception e) { var msg = rspMsg.FormatRspMessage(7, e.Message, 0, mainCfg.Base64MsgContent); msgProcess.SendMessage(msg); NLog.Crash(e.ToString()); return -(int) ErrCode.ErrExceptThrow; } try { var ret = tmObj.GenerateTmatrixCode_OID4(inParams.key, inParams.filePath, inParams.startPageId, inParams.pointType, inParams.imageType, inParams.dpi); if (ret.Substring(0, 1) != "0") { var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)), ret.Substring(1, ret.Length - 1), tmObj.GetProgerss(), mainCfg.Base64MsgContent); NLog.Error(msg + Environment.NewLine); msgProcess.SendMessage(msg); } else { var msg = rspMsg.FormatRspMessage(int.Parse(ret.Substring(0, 1)), ret.Substring(1, ret.Length - 1), 100, mainCfg.Base64MsgContent); NLog.Debug(msg + Environment.NewLine); msgProcess.SendMessage(msg); } } catch (Exception e) { var msg = rspMsg.FormatRspMessage(8, e.Message, tmObj.GetProgerss(), mainCfg.Base64MsgContent); msgProcess.SendMessage(msg); NLog.Crash(e.ToString()); return -(int) ErrCode.ErrExceptThrow; } return 0; } /// /// Enum ErrCode /// private enum ErrCode { /// /// The error input parameters /// ErrInputParams = 1, /// /// The error file notexists /// ErrFileNotexists, /// /// The error except throw /// ErrExceptThrow, /// /// The error json decode /// ErrJsonDecode } } }