Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. // stores the printer info
  2. class PrinterBench
  3. {
  4. public PrinterBench(string sArg_PostageLabelIP, string sArg_SmallLabelIP)
  5. {
  6. PostageLabelIP = sArg_PostageLabelIP;
  7. SmallLabelIP = sArg_SmallLabelIP;
  8. }
  9. public string PostageLabelIP;
  10. public string SmallLabelIP;
  11. }
  12.  
  13. // main entry point
  14. class HomeController{
  15.  
  16. PrintController oPrintController;
  17. List<string> lsLabelResults = new List<string>("label result");
  18. PrinterBench pbBench = new PrinterBench("192.168.2.20","192.168.2.21");
  19.  
  20. void Process(){
  21.  
  22. oPrintController = new PrintController(this);
  23.  
  24. if(GetLabel()){
  25. // should always come out of the big printer (runs in background)
  26. oPrintController.PrintBySocketThreaded(lsLabelResults, pbBench.PostageLabelIP);
  27. // should always come out of the small printer
  28. oPrintController.PrintWarningLabel();
  29. }
  30. }
  31. }
  32.  
  33.  
  34. class PrintController{
  35.  
  36. HomeController oHC;
  37. public EndPoint ep { get; set; }
  38. public Socket sock { get; set; }
  39. public NetworkStream ns { get; set; }
  40.  
  41. private static Dictionary<string, Socket> lSocks = new Dictionary<string, Socket>();
  42.  
  43. private BackgroundWorker _backgroundWorker;
  44. static readonly object locker = new object();
  45. double dProgress;
  46. bool bPrintSuccess = true;
  47.  
  48. public PrintController(HomeController oArg_HC)
  49. {
  50. oHC = oArg_HC;
  51. }
  52.  
  53. public bool InitSocks()
  54. {
  55. // Ensure the IP's / endpoints of users printers are assigned
  56. if (!lSocks.ContainsKey(oHC.pbBench.PostageLabelIP))
  57. {
  58. lSocks.Add(oHC.pbBench.PostageLabelIP, null);
  59. }
  60. if (!lSocks.ContainsKey(oHC.pbBench.SmallLabelIP))
  61. {
  62. lSocks.Add(oHC.pbBench.SmallLabelIP, null);
  63. }
  64.  
  65. // attempt to create a connection to each socket
  66. foreach (string sKey in lSocks.Keys.ToList())
  67. {
  68. if (lSocks[sKey] == null || !lSocks[sKey].Connected )
  69. {
  70. ep = new IPEndPoint(IPAddress.Parse(sKey), 9100);
  71. lSocks[sKey] = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  72. lSocks[sKey].Connect(ep);
  73. }
  74. }
  75. return true;
  76. }
  77.  
  78.  
  79. public bool PrintBySocketThreaded(List<string> lsToPrint, string sIP)
  80. {
  81. // open both the sockets
  82. InitSocks();
  83.  
  84. bBatchPrintSuccess = false;
  85. _backgroundWorker = new BackgroundWorker();
  86.  
  87. _backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
  88. _backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
  89. _backgroundWorker.WorkerReportsProgress = true;
  90. _backgroundWorker.WorkerSupportsCancellation = true;
  91.  
  92. object[] parameters = new object[] { lsToPrint, sIP, lSocks };
  93.  
  94. _backgroundWorker.RunWorkerAsync(parameters);
  95. return true;
  96. }
  97.  
  98.  
  99. // On worker thread, send to print!
  100. public void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  101. {
  102. object[] parameters = e.Argument as object[];
  103.  
  104. double dProgressChunks = (100 / ((List<string>)parameters[0]).Count);
  105. int iPos = 1;
  106.  
  107. Dictionary<string, Socket> dctSocks = (Dictionary<string, Socket>)parameters[2];
  108.  
  109. foreach (string sLabel in (List<string>)parameters[0] )
  110. {
  111. bool bPrinted = false;
  112.  
  113. // thread lock print by socket to ensure its not accessed twice
  114. lock (locker)
  115. {
  116. // get relevant socket from main thread
  117. bPrinted = PrintBySocket(sLabel, (string)parameters[1], dctSocks[(string)parameters[1]]);
  118. }
  119.  
  120. iPos++;
  121. }
  122.  
  123. while (!((BackgroundWorker)sender).CancellationPending)
  124. {
  125. ((BackgroundWorker)sender).CancelAsync();
  126. ((BackgroundWorker)sender).Dispose();
  127. //Thread.Sleep(500);
  128. }
  129. return;
  130. }
  131.  
  132.  
  133. // Back on the 'UI' thread so we can update the progress bar (have access to main thread data)!
  134. private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  135. {
  136. if (e.Error != null) MessageBox.Show(e.Error.Message);
  137. if (bPrintSuccess) oHC.WriteLog("Printing Complete");
  138.  
  139. bBatchPrintSuccess = true;
  140.  
  141. ((BackgroundWorker)sender).CancelAsync();
  142. ((BackgroundWorker)sender).Dispose();
  143. }
  144.  
  145. /// sends to printer via socket
  146. public bool PrintBySocket(string sArg_ToPrint, string sIP, Socket sock = null)
  147. {
  148. Socket sTmpSock = sock;
  149.  
  150. if (sTmpSock == null)
  151. {
  152. InitSocks();
  153.  
  154. if (!lSocks.ContainsKey(sIP)){
  155. throw new Exception("Sock not init");
  156. }else{
  157. sTmpSock = lSocks[sIP];
  158. }
  159. }
  160.  
  161. using (ns = new NetworkStream(sTmpSock))
  162. {
  163. byte[] toSend = Encoding.ASCII.GetBytes(sEOL + sArg_ToPrint);
  164. ns.BeginWrite(toSend, 0, toSend.Length, OnWriteComplete, null);
  165. ns.Flush();
  166. }
  167. return true;
  168. }
  169.  
  170.  
  171. public bool PrintWarningLabel()
  172. {
  173. string sOut = sEOL + "N" + sEOL;
  174. sOut += "LL411" + sEOL;
  175. sOut += "R40,0" + sEOL;
  176. sOut += "S5" + sEOL;
  177. sOut += "D15" + sEOL;
  178. sOut += "A0,0,0,4,4,3,N,"!!!!!!!!!!!!!!!!!!!!!!!"" + sEOL;
  179. sOut += "A0,150,0,4,3,3,N,"WARNING MESSAGE TO PRINT"" + sEOL;
  180. sOut += "A0,280,0,4,4,3,N,"!!!!!!!!!!!!!!!!!!!!!!!"" + sEOL;
  181. sOut += "P1";
  182. sOut += sEOL;
  183.  
  184. if (PrintBySocket(sOut, oHC.pbBench.SmallLabelIP))
  185. {
  186. oHC.WriteLog("WARNING LABEL PRINTED");
  187. return true;
  188. }
  189. return false;
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement