PrinterHandler.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. using Wayne.FDCPOSLibrary;
  9. namespace Dfs.WayneChina.HyperPrinterHandler
  10. {
  11. /// <summary>
  12. /// Entity to communicate to a proxy on Screen App to send the receipt to the printer on dispenser.
  13. /// </summary>
  14. public class PrinterHandler : IAppProcessor, IFdcCommunicableController
  15. {
  16. #region IFdcCommunicableController implementation
  17. public string MetaConfigName { get; set; }
  18. public Func<string, bool> BroadcastMessageViaFdc { get; set; }
  19. public Func<string, string, string, bool> SendMessageViaFdc { get; set; }
  20. public Func<string, Tuple<string, OverallResult>> OnMessageReceivedViaFdc { get; set; }
  21. #endregion
  22. #region IApplication implementaion
  23. public void Init(IEnumerable<IProcessor> processors)
  24. {
  25. }
  26. public Task<bool> Start()
  27. {
  28. return Task.FromResult(true);
  29. }
  30. public Task<bool> Stop()
  31. {
  32. return Task.FromResult(true);
  33. }
  34. #endregion
  35. #region Fields
  36. private static int request = 0;
  37. private const string newLineCmd = "0A";
  38. private const string cutPaperCmd = "1D564200";
  39. #endregion
  40. #region Logger
  41. NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("HyperPrinter");
  42. #endregion
  43. #region Constructor
  44. public PrinterHandler(int id, int maxLength)
  45. {
  46. //Receive HTML receipt from `On Screen POS`
  47. OnMessageReceivedViaFdc += HandlePrintingRequestFromOnScreenPos;
  48. }
  49. #endregion
  50. private Tuple<string, OverallResult> HandlePrintingRequestFromOnScreenPos(string message)
  51. {
  52. var htmlReceipt = message;
  53. if (!string.IsNullOrEmpty(htmlReceipt))
  54. {
  55. logger.Info(htmlReceipt);
  56. if (htmlReceipt.Contains("<TransactionOperation"))
  57. {
  58. logger.Info("Received printing request from screen");
  59. try
  60. {
  61. XmlDocument doc = new XmlDocument();
  62. doc.LoadXml(htmlReceipt);
  63. var nozzleNoAttribute = doc.DocumentElement.Attributes["NozzleNo"];
  64. int nozzleNo = Convert.ToInt32(nozzleNoAttribute.Value);
  65. //Console.WriteLine($"NozzleNo: {nozzleNo}");
  66. var operationTypeAttribute = doc.DocumentElement.Attributes["OpType"];
  67. string opType = operationTypeAttribute.Value;
  68. //Console.WriteLine($"Operation type: {opType}");
  69. var receiptHtmlAttribute = doc.DocumentElement.Attributes["ReceiptString"];
  70. string receiptHtml = receiptHtmlAttribute.Value;
  71. //Console.WriteLine($"Receipt HTML: {receiptHtml}");
  72. SendFormattedReceipt(Convert.ToInt32(nozzleNo), receiptHtml);
  73. }
  74. catch (Exception ex)
  75. {
  76. logger.Info($"Parsing TransactionOperation request from On Screen POS: {ex}");
  77. return new Tuple<string, OverallResult>("Shit", OverallResult.Failure);
  78. }
  79. return new Tuple<string, OverallResult>("I have received your receipt", OverallResult.Success);
  80. }
  81. else if (htmlReceipt.Contains("<DisplayResponse"))
  82. {
  83. logger.Info("Sending out receipt got acked");
  84. try
  85. {
  86. logger.Info($"Got display response: {htmlReceipt}");
  87. }
  88. catch (Exception ex)
  89. {
  90. logger.Error($"Parsing Display response: {ex}");
  91. return new Tuple<string, OverallResult>("DisplayResponse, run into expcetion", OverallResult.Failure);
  92. }
  93. return new Tuple<string, OverallResult>("OK", OverallResult.Success);
  94. }
  95. }
  96. return new Tuple<string, OverallResult>("Received unrecognized message", OverallResult.Failure);
  97. }
  98. /// <summary>
  99. /// Send the printer specific receipt raw bytes to the pump printer via 'On Screen POS'
  100. /// </summary>
  101. /// <param name="nozzleNo">The associated nozzle number.</param>
  102. /// <param name="htmlReceipt">expected format for this part like ""</param>
  103. public void SendFormattedReceipt(int nozzleNo, string htmlReceipt)
  104. {
  105. var receiptContext = GetFormattedReceipt(htmlReceipt, nozzleNo);
  106. //var sent = SendMessageViaFdc.Invoke("100", $"Nozzle {nozzleNo}", receiptContext);
  107. if (receiptContext != null)
  108. {
  109. var sent = BroadcastMessageViaFdc(receiptContext);
  110. logger.Info($"sent out receipt: {receiptContext}, result success? {sent}");
  111. }
  112. }
  113. private string FormatReceiptFromHtml(string htmlReceipt)
  114. {
  115. logger.Info("receipt test begins....");
  116. return GetFormattedReceipt(htmlReceipt);
  117. }
  118. private int GetNextRequestId()
  119. {
  120. return request++;
  121. }
  122. /// <summary>
  123. ///
  124. /// </summary>
  125. /// <param name="rawData"></param>
  126. /// <param name="nozzleNo"></param>
  127. /// <returns></returns>
  128. private string GetFormattedReceipt(string rawData, int nozzleNo = 1)
  129. {
  130. logger.Info("Receipt HTML data: " + rawData);
  131. try
  132. {
  133. var receiptLineItems = (new Parser()).ParseToReceiptLineItems(System.Web.HttpUtility.HtmlDecode(rawData));
  134. string receiptForPrinter = string.Empty;
  135. List<string> receipt = new List<string>();
  136. foreach (var rl in receiptLineItems)
  137. {
  138. receipt.AddRange(rl.Format());
  139. }
  140. foreach (var text in receipt)
  141. {
  142. receiptForPrinter += text;
  143. receiptForPrinter += newLineCmd;
  144. }
  145. //add an additional empty line
  146. for (int i = 0; i < 32; i++)
  147. {
  148. receiptForPrinter += "20";
  149. }
  150. receiptForPrinter += newLineCmd;
  151. receiptForPrinter += cutPaperCmd;
  152. XDocument doc = new XDocument();
  153. var rootElement = new XElement("Display");
  154. rootElement.Add(new XAttribute("RequestId", GetNextRequestId()));
  155. rootElement.Add(new XAttribute("NozzleNo", nozzleNo));
  156. rootElement.Add(new XAttribute("FormattedReceiptString", receiptForPrinter));
  157. doc.Add(rootElement);
  158. return doc.ToString();
  159. }
  160. catch (Exception ex)
  161. {
  162. logger.Info($"Create receipt exception: {ex}");
  163. }
  164. return string.Empty;
  165. }
  166. public string ByteArrayToString(byte[] ba)
  167. {
  168. return BitConverter.ToString(ba).Replace("-", "");
  169. }
  170. }
  171. }