using Edge.Core.Processor;using Edge.Core.IndustryStandardInterface.Pump; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using Wayne.FDCPOSLibrary; namespace Dfs.WayneChina.HyperPrinterHandler { /// /// Entity to communicate to a proxy on Screen App to send the receipt to the printer on dispenser. /// public class PrinterHandler : IAppProcessor, IFdcCommunicableController { #region IFdcCommunicableController implementation public string MetaConfigName { get; set; } public Func BroadcastMessageViaFdc { get; set; } public Func SendMessageViaFdc { get; set; } public Func> OnMessageReceivedViaFdc { get; set; } #endregion #region IApplication implementaion public void Init(IEnumerable processors) { } public Task Start() { return Task.FromResult(true); } public Task Stop() { return Task.FromResult(true); } #endregion #region Fields private static int request = 0; private const string newLineCmd = "0A"; private const string cutPaperCmd = "1D564200"; #endregion #region Logger NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("HyperPrinter"); #endregion #region Constructor public PrinterHandler(int id, int maxLength) { //Receive HTML receipt from `On Screen POS` OnMessageReceivedViaFdc += HandlePrintingRequestFromOnScreenPos; } #endregion private Tuple HandlePrintingRequestFromOnScreenPos(string message) { var htmlReceipt = message; if (!string.IsNullOrEmpty(htmlReceipt)) { logger.Info(htmlReceipt); if (htmlReceipt.Contains("("Shit", OverallResult.Failure); } return new Tuple("I have received your receipt", OverallResult.Success); } else if (htmlReceipt.Contains("("DisplayResponse, run into expcetion", OverallResult.Failure); } return new Tuple("OK", OverallResult.Success); } } return new Tuple("Received unrecognized message", OverallResult.Failure); } /// /// Send the printer specific receipt raw bytes to the pump printer via 'On Screen POS' /// /// The associated nozzle number. /// expected format for this part like "" public void SendFormattedReceipt(int nozzleNo, string htmlReceipt) { var receiptContext = GetFormattedReceipt(htmlReceipt, nozzleNo); //var sent = SendMessageViaFdc.Invoke("100", $"Nozzle {nozzleNo}", receiptContext); if (receiptContext != null) { var sent = BroadcastMessageViaFdc(receiptContext); logger.Info($"sent out receipt: {receiptContext}, result success? {sent}"); } } private string FormatReceiptFromHtml(string htmlReceipt) { logger.Info("receipt test begins...."); return GetFormattedReceipt(htmlReceipt); } private int GetNextRequestId() { return request++; } /// /// /// /// /// /// private string GetFormattedReceipt(string rawData, int nozzleNo = 1) { logger.Info("Receipt HTML data: " + rawData); try { var receiptLineItems = (new Parser()).ParseToReceiptLineItems(System.Web.HttpUtility.HtmlDecode(rawData)); string receiptForPrinter = string.Empty; List receipt = new List(); foreach (var rl in receiptLineItems) { receipt.AddRange(rl.Format()); } foreach (var text in receipt) { receiptForPrinter += text; receiptForPrinter += newLineCmd; } //add an additional empty line for (int i = 0; i < 32; i++) { receiptForPrinter += "20"; } receiptForPrinter += newLineCmd; receiptForPrinter += cutPaperCmd; XDocument doc = new XDocument(); var rootElement = new XElement("Display"); rootElement.Add(new XAttribute("RequestId", GetNextRequestId())); rootElement.Add(new XAttribute("NozzleNo", nozzleNo)); rootElement.Add(new XAttribute("FormattedReceiptString", receiptForPrinter)); doc.Add(rootElement); return doc.ToString(); } catch (Exception ex) { logger.Info($"Create receipt exception: {ex}"); } return string.Empty; } public string ByteArrayToString(byte[] ba) { return BitConverter.ToString(ba).Replace("-", ""); } } }