FormMain.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Newtonsoft.Json;
  2. using StackExchange.Redis;
  3. using System.Reflection;
  4. using WebSupergoo.ABCpdf11;
  5. using static PDFMonitor_SVG.Entities;
  6. namespace PDFMonitor_SVG {
  7. public partial class FormMain : Form {
  8. private ConnectionMultiplexer? redisClient;
  9. private string? redisServerUrl;
  10. private string? redisServerPwd;
  11. private string? redisTaskKey;
  12. private string? redisCallbackKey;
  13. //生成的PDF文件目录
  14. private string? completedPDFPath;
  15. //private FixedThreadPool threadPool;
  16. private SynchronizationContext context;
  17. public FormMain() {
  18. InitializeComponent();
  19. ReadServerConfig();
  20. try {
  21. redisClient = ConnectionMultiplexer.Connect(redisServerUrl + ",password=" + redisServerPwd);
  22. memoLog.Items.Add("redis连接成功");
  23. }
  24. catch (Exception ex) {
  25. memoLog.Items.Add("redis连接失败,请检查程序设置");
  26. memoLog.Items.Add(ex.Message);
  27. }
  28. //threadPool = new FixedThreadPool(8); //默认8个线程
  29. ThreadPool.SetMinThreads(4, 4);
  30. ThreadPool.SetMaxThreads(8, 8);
  31. // 获取当前的SynchronizationContext
  32. context = SynchronizationContext.Current;
  33. WebSupergoo.ABCpdf11.XSettings.InstallLicense("X/VKS0cMn8tAun4hGNvFONyWaelyItt2pqrH5srEKm4brmiSKC69N5FsGmRijGFheK9mhXoi/HVdi/VrNv0Vv1RyfQWQCg==");
  34. }
  35. ~FormMain() {
  36. redisClient?.Dispose();
  37. //threadPool.Shutdown();
  38. }
  39. private void ReadServerConfig() {
  40. string iniPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\PDFMonitor_SVG.ini";
  41. IniFile ini = new(iniPath);
  42. redisServerUrl = ini.Read("redis", "redisServerUrl", "127.0.0.1:6379");
  43. redisServerPwd = ini.Read("redis", "redisServerPwd", "Admin@dounengyin@123");
  44. redisTaskKey = ini.Read("redis", "redisTaskKey", "sdtool:mall:designer:svg:task:list:1000");
  45. redisCallbackKey = ini.Read("redis", "redisCallbackKey", "sdtool:mall:designer:pdf:callback:list:1000");
  46. completedPDFPath = ini.Read("path", "completedPDFPath", "D:\\testPdfOut\\");
  47. if (!Directory.Exists(completedPDFPath)) {
  48. Directory.CreateDirectory(completedPDFPath);
  49. }
  50. }
  51. private void TimerGetTask_Tick(object sender, EventArgs e) {
  52. RedisValue taskInfo = redisClient.GetDatabase().ListLeftPop(redisTaskKey);
  53. if (!taskInfo.IsNullOrEmpty) {
  54. // 有任务了,干活
  55. SVGTaskInfo svgTaskInfo = JsonConvert.DeserializeObject<SVGTaskInfo>(taskInfo);
  56. if (svgTaskInfo == null) return;
  57. memoLog.Items.Add("获取任务[" + svgTaskInfo.SvgFileName + "]成功");
  58. //todo
  59. //threadPool.Execute(() => {
  60. // doConvertTask(svgTaskInfo);
  61. //});
  62. ThreadPool.QueueUserWorkItem(new WaitCallback(doConvertTask), svgTaskInfo);
  63. }
  64. }
  65. private void doConvertTask(object state) {
  66. SVGTaskInfo svgTaskInfo = (SVGTaskInfo)state;
  67. context.Post(_ => {
  68. memoLog.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
  69. " 任务[" + svgTaskInfo.SvgFileName + "]开始");
  70. }, null);
  71. //传过来的是px,转成mm
  72. int fileWidth = px2mm(double.Parse(svgTaskInfo.Width));
  73. int fileHeight = px2mm(double.Parse(svgTaskInfo.Height));
  74. int fileWidthPx = (int)Math.Ceiling(double.Parse(svgTaskInfo.Width));
  75. Doc doc = new Doc();
  76. doc.Units = UnitType.Mm;
  77. //doc.MediaBox.Top = 0;
  78. //doc.MediaBox.Left = 0;
  79. //doc.MediaBox.Right = fileWidth;
  80. //doc.MediaBox.Bottom = fileHeight;
  81. string mediaBoxStr = "0 0 " + fileWidth + " " + fileHeight;
  82. doc.MediaBox.String = mediaBoxStr;
  83. doc.MediaBox.Pin = XRect.Corner.TopLeft;
  84. doc.Rect.String = doc.MediaBox.String;
  85. doc.Rect.Pin = XRect.Corner.TopLeft;
  86. doc.HtmlOptions.Media = MediaType.Print;
  87. string convertedFileName = Path.ChangeExtension(svgTaskInfo.SvgFileName, ".pdf");
  88. string convertedFilePath = completedPDFPath + convertedFileName;
  89. doc.AddImageUrl(svgTaskInfo.SvgPath, true, fileWidthPx, true);
  90. doc.Save(convertedFilePath);
  91. doc.Clear();
  92. doc.Dispose();
  93. PDFCallbackInfo pdfCallbackInfo = new PDFCallbackInfo();
  94. pdfCallbackInfo.IdDesignOrder = svgTaskInfo.IdDesignOrder;
  95. pdfCallbackInfo.PdfFileName = convertedFileName;
  96. redisClient.GetDatabase().ListRightPush(redisCallbackKey, JsonConvert.SerializeObject(pdfCallbackInfo));
  97. context.Post(_ => {
  98. memoLog.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +
  99. " 任务[" + svgTaskInfo.SvgFileName + "]完成");
  100. }, null);
  101. }
  102. private void btnStartService_Click(object sender, EventArgs e) {
  103. if (redisClient == null) {
  104. memoLog.Items.Add("redis客户端未连接,请修正程序设置后重启");
  105. return;
  106. }
  107. memoLog.Items.Add("服务已启动");
  108. timerGetTask.Start();
  109. }
  110. private void btnStopService_Click(object sender, EventArgs e) {
  111. timerGetTask.Stop();
  112. memoLog.Items.Add("服务已停止");
  113. }
  114. }
  115. }