559 lines
17 KiB
C#
559 lines
17 KiB
C#
// ***********************************************************************
|
|
// Assembly : GeneratorCode
|
|
// Author : 黄昕 <hzhuangxin01@corp.netease.com>
|
|
// Created : 02-14-2019
|
|
//
|
|
// Last Modified By : 黄昕 <hzhuangxin01@corp.netease.com>
|
|
// Last Modified On : 03-04-2019
|
|
// ***********************************************************************
|
|
// <copyright file="MainCode.cs" company="NetEase">
|
|
// Copyright © 2019
|
|
// </copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
|
|
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
|
|
{
|
|
/// <summary>
|
|
/// Class GeneratorParams.
|
|
/// </summary>
|
|
[JsonObject(MemberSerialization.OptOut)]
|
|
public class GeneratorParams
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="GeneratorParams" /> class.
|
|
/// </summary>
|
|
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";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the dpi.
|
|
/// </summary>
|
|
/// <value>The dpi.</value>
|
|
public int[] dpi { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the file path.
|
|
/// </summary>
|
|
/// <value>The file path.</value>
|
|
public string filePath { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the type of the image.
|
|
/// </summary>
|
|
/// <value>The type of the image.</value>
|
|
public bool[] imageType { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the key.
|
|
/// </summary>
|
|
/// <value>The key.</value>
|
|
public string key { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the type of the point.
|
|
/// </summary>
|
|
/// <value>The type of the point.</value>
|
|
public int[] pointType { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the session identifier.
|
|
/// </summary>
|
|
/// <value>The session identifier.</value>
|
|
[JsonIgnore]
|
|
public string sessionId { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the start page identifier.
|
|
/// </summary>
|
|
/// <value>The start page identifier.</value>
|
|
public int startPageId { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class GenerCodeRet.
|
|
/// </summary>
|
|
public class GenerCodeRet
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="GenerCodeRet" /> class.
|
|
/// </summary>
|
|
/// <param name="sId">The s identifier.</param>
|
|
/// <param name="prg">The PRG.</param>
|
|
public GenerCodeRet(string sId, int prg)
|
|
{
|
|
sessionId = sId;
|
|
progress = prg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the progress.
|
|
/// </summary>
|
|
/// <value>The progress.</value>
|
|
public int progress { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the session identifier.
|
|
/// </summary>
|
|
/// <value>The session identifier.</value>
|
|
public string sessionId { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class MainConfig.
|
|
/// </summary>
|
|
public class MainConfig
|
|
{
|
|
/// <summary>
|
|
/// Delegate MainCfgChangedHandle
|
|
/// </summary>
|
|
public delegate void MainCfgChangedHandle();
|
|
|
|
/// <summary>
|
|
/// The CFG lock
|
|
/// </summary>
|
|
private readonly object _cfgLock = new object();
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MainConfig" /> class.
|
|
/// </summary>
|
|
public MainConfig()
|
|
{
|
|
LoadConfig();
|
|
OnMainCfgChanged += MainCfgChanged;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether [base64 MSG content].
|
|
/// </summary>
|
|
/// <value><c>true</c> if [base64 MSG content]; otherwise, <c>false</c>.</value>
|
|
public bool Base64MsgContent { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the server addr.
|
|
/// </summary>
|
|
/// <value>The server addr.</value>
|
|
public string ServerAddr { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the server port.
|
|
/// </summary>
|
|
/// <value>The server port.</value>
|
|
public int ServerPort { get; set; }
|
|
|
|
/// <summary>
|
|
/// Occurs when [on main CFG changed].
|
|
/// </summary>
|
|
public static event MainCfgChangedHandle OnMainCfgChanged;
|
|
|
|
/// <summary>
|
|
/// Loads the configuration.
|
|
/// </summary>
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Mains the CFG changed.
|
|
/// </summary>
|
|
protected static void MainCfgChanged()
|
|
{
|
|
OnMainCfgChanged?.Invoke();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class MessageProcess.
|
|
/// </summary>
|
|
public class MessageProcess
|
|
{
|
|
/// <summary>
|
|
/// The UDP
|
|
/// </summary>
|
|
private readonly UdpClient _udp = new UdpClient();
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MessageProcess" /> class.
|
|
/// </summary>
|
|
/// <param name="cfg">The CFG.</param>
|
|
public MessageProcess(MainConfig cfg)
|
|
{
|
|
var svrAddr = IPAddress.Parse(cfg.ServerAddr);
|
|
_server = new IPEndPoint(svrAddr, cfg.ServerPort);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the server.
|
|
/// </summary>
|
|
/// <value>The server.</value>
|
|
private IPEndPoint _server { get; }
|
|
|
|
/// <summary>
|
|
/// Sends the message.
|
|
/// </summary>
|
|
/// <param name="msg">The MSG.</param>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class RspMessage.
|
|
/// </summary>
|
|
public class RspMessage
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RspMessage" /> class.
|
|
/// </summary>
|
|
/// <param name="sId">The s identifier.</param>
|
|
public RspMessage(string sId)
|
|
{
|
|
err = 0;
|
|
msg = "";
|
|
data = new GenerCodeRet(sId, 0);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RspMessage" /> class.
|
|
/// </summary>
|
|
/// <param name="sId">The s identifier.</param>
|
|
/// <param name="prg">The PRG.</param>
|
|
public RspMessage(string sId, int prg)
|
|
{
|
|
err = 0;
|
|
msg = "";
|
|
data = new GenerCodeRet(sId, prg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the data.
|
|
/// </summary>
|
|
/// <value>The data.</value>
|
|
public GenerCodeRet data { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the error.
|
|
/// </summary>
|
|
/// <value>The error.</value>
|
|
public int err { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the MSG.
|
|
/// </summary>
|
|
/// <value>The MSG.</value>
|
|
public string msg { get; set; }
|
|
|
|
/// <summary>
|
|
/// Formats the RSP message.
|
|
/// </summary>
|
|
/// <param name="errCode">The error code.</param>
|
|
/// <param name="readme">The readme.</param>
|
|
/// <param name="prg">The PRG.</param>
|
|
/// <param name="enBase64">if set to <c>true</c> [en base64].</param>
|
|
/// <returns>System.String.</returns>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Class MainCode.
|
|
/// </summary>
|
|
internal class MainCode
|
|
{
|
|
/// <summary>
|
|
/// Gets the offical error code.
|
|
/// </summary>
|
|
/// <param name="err">The error.</param>
|
|
/// <returns>System.Int32.</returns>
|
|
private static int GetOfficalErrCode([NotNull] string err = "0")
|
|
{
|
|
switch (err)
|
|
{
|
|
case "0":
|
|
return (int) ErrCode.ErrSuccesed;
|
|
|
|
case "1":
|
|
return (int) ErrCode.ErrKeyTampered;
|
|
|
|
case "2":
|
|
return (int) ErrCode.ErrKeyOldStyle;
|
|
|
|
case "3":
|
|
return (int) ErrCode.ErrKeyExpired;
|
|
|
|
case "4":
|
|
return (int) ErrCode.ErrFileNotExists;
|
|
|
|
case "5":
|
|
return (int) ErrCode.ErrStartPage;
|
|
|
|
case "6":
|
|
return (int) ErrCode.ErrPageNotEnough;
|
|
|
|
case "7":
|
|
return (int) ErrCode.ErrSizeMoreThan780;
|
|
|
|
case "8":
|
|
return (int) ErrCode.ErrSizeMoreThan6242;
|
|
}
|
|
|
|
return (int) ErrCode.ErrUnknownCode;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Defines the entry point of the application.
|
|
/// </summary>
|
|
/// <param name="args">The arguments.</param>
|
|
/// <returns>System.Int32.</returns>
|
|
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();
|
|
};
|
|
|
|
try
|
|
{
|
|
if (args.Length == 1)
|
|
{
|
|
var deCode = Convert.FromBase64String(args[0]);
|
|
var strParams = Encoding.Default.GetString(deCode);
|
|
inParams = JsonConvert.DeserializeObject<GeneratorParams>(strParams);
|
|
}
|
|
else if (args.Length == 2)
|
|
{
|
|
var deCode = Convert.FromBase64String(args[1]);
|
|
var strParams = Encoding.Default.GetString(deCode);
|
|
inParams = JsonConvert.DeserializeObject<GeneratorParams>(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);
|
|
|
|
var errCode = GetOfficalErrCode(ret.Substring(0, 1));
|
|
|
|
if (errCode != 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);
|
|
}
|
|
|
|
return errCode;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enum ErrCode
|
|
/// </summary>
|
|
private enum ErrCode
|
|
{
|
|
/// <summary>
|
|
/// The error succesed
|
|
/// </summary>
|
|
ErrSuccesed = 0,
|
|
|
|
/// <summary>
|
|
/// The error size more than780
|
|
/// </summary>
|
|
ErrSizeMoreThan780 = 105,
|
|
|
|
/// <summary>
|
|
/// The error size more than6242
|
|
/// </summary>
|
|
ErrSizeMoreThan6242 = 106,
|
|
|
|
/// <summary>
|
|
/// The error start page
|
|
/// </summary>
|
|
ErrStartPage = 110,
|
|
|
|
/// <summary>
|
|
/// The error input parameters
|
|
/// </summary>
|
|
ErrInputParams = 122,
|
|
|
|
/// <summary>
|
|
/// The error key tampered
|
|
/// </summary>
|
|
ErrKeyTampered = 123,
|
|
|
|
/// <summary>
|
|
/// The error key old style
|
|
/// </summary>
|
|
ErrKeyOldStyle = 124,
|
|
|
|
/// <summary>
|
|
/// The error key expired
|
|
/// </summary>
|
|
ErrKeyExpired = 125,
|
|
|
|
/// <summary>
|
|
/// The error page not enough
|
|
/// </summary>
|
|
ErrPageNotEnough = 126,
|
|
|
|
/// <summary>
|
|
/// The error file not exists
|
|
/// </summary>
|
|
ErrFileNotExists = 127,
|
|
|
|
/// <summary>
|
|
/// The error except throw
|
|
/// </summary>
|
|
ErrExceptThrow = 128,
|
|
|
|
/// <summary>
|
|
/// The error json decode
|
|
/// </summary>
|
|
ErrJsonDecode = 129,
|
|
|
|
/// <summary>
|
|
/// The error unknown code
|
|
/// </summary>
|
|
ErrUnknownCode = 130
|
|
}
|
|
}
|
|
} |