private void DisplayMessageButton_Click(object sender, EventArgs e) { cloverConnector.ShowMessage(DisplayMessageTextbox.Text); }
protected override void OnStart(string[] args) { base.OnStart(args); string logSource = "_TransportEventLog"; if (!EventLog.SourceExists(logSource)) { EventLog.CreateEventSource(logSource, logSource); } EventLogTraceListener myTraceListener = new EventLogTraceListener(logSource); // Add the event log trace listener to the collection. Trace.Listeners.Add(myTraceListener); if (args.Length > 0) { if (((ICollection <string>)args).Contains("-debug")) { Debug = true; } if (((ICollection <string>)args).Any(a => a.Contains("-timer"))) { IEnumerable <string> timerStrings = ((ICollection <string>)args).Where(a => a.Contains("-timer")); if (timerStrings.Count() == 1) { try { string timerString = timerStrings.First(); int index = timerString.IndexOf('='); string timerSeconds = timerString.Substring(index + 1); Timer = Convert.ToInt32(timerSeconds); } catch (Exception e) { Timer = 1; EventLog.WriteEntry(SERVICE_NAME, "Error parsing the -timer command line argument. Setting timer to 1 second."); } } } } //load args in to dictionary Dictionary <string, string> parameters = new Dictionary <string, string>(); if (args != null) { for (int i = 0; i < args.Length; i++) { if ("/T".Equals(args[i])) { parameters.Add("/T", "true"); } else if (i + 1 < args.Length) { parameters.Add(args[i], args[++i]); } } } IWebSocketConnection sendSocket = null; Action <IWebSocketConnection> serverSocket = socket => { socket.OnOpen = () => { Console.WriteLine("Open! " + clientConnections.Count); if (clientConnections.Count > 0) { if (clientConnections[0].IsAvailable) { socket.Close(); connectorListener.OnDeviceError(new CloverDeviceErrorEvent(CloverDeviceErrorEvent.CloverDeviceErrorType.EXCEPTION, 0, "Another client is already connected")); return; } } sendSocket = socket; clientConnections.Add(socket); connectorListener.WebSocket = sendSocket; connectorListener.SendConnectionStatus(); }; socket.OnClose = () => { clientConnections.Remove(socket); Console.WriteLine("Close!"); connectorListener.WebSocket = null; }; socket.OnMessage = message => { try { JObject jsonObj = (JObject)JsonConvert.DeserializeObject(message); JToken method = jsonObj.GetValue(ServicePayloadConstants.PROP_METHOD); JObject payload = (JObject)jsonObj.GetValue(ServicePayloadConstants.PROP_PAYLOAD); WebSocketMethod wsm = (WebSocketMethod)Enum.Parse(typeof(WebSocketMethod), method.ToString()); switch (wsm) { case WebSocketMethod.Status: { connectorListener.SendConnectionStatus(); break; } case WebSocketMethod.OpenCashDrawer: { string reason = ((JObject)payload).GetValue("Reason").Value <string>(); cloverConnector.OpenCashDrawer(reason); break; } case WebSocketMethod.ShowMessage: { string msg = ((JObject)payload).GetValue("Message").Value <string>(); cloverConnector.ShowMessage(msg); break; } case WebSocketMethod.ShowThankYouScreen: { cloverConnector.ShowThankYouScreen(); break; } case WebSocketMethod.ShowWelcomeScreen: { cloverConnector.ShowWelcomeScreen(); break; } case WebSocketMethod.Cancel: { cloverConnector.Cancel(); break; } case WebSocketMethod.Break: { cloverConnector.ResetDevice(); break; } case WebSocketMethod.PrintText: { JArray messages = (JArray)payload.GetValue("Messages"); List <string> messageList = new List <string>(); foreach (string msg in messages) { messageList.Add(msg); } cloverConnector.PrintText(messageList); break; } case WebSocketMethod.PrintImage: { string base64Img = ((JObject)payload).GetValue("Bitmap").Value <string>(); byte[] imgBytes = Convert.FromBase64String(base64Img); MemoryStream ms = new MemoryStream(); ms.Write(imgBytes, 0, imgBytes.Length); Bitmap bp = new Bitmap(ms); ms.Close(); cloverConnector.PrintImage(bp); break; } case WebSocketMethod.PrintImageFromURL: { string url = ((JObject)payload).GetValue("Url").Value <string>(); cloverConnector.PrintImageFromURL(url); break; } case WebSocketMethod.Auth: { AuthRequest authRequest = JsonUtils.deserialize <AuthRequest>(payload.ToString()); cloverConnector.Auth(authRequest); break; } case WebSocketMethod.PreAuth: { PreAuthRequest preAuthRequest = JsonUtils.deserialize <PreAuthRequest>(payload.ToString()); cloverConnector.PreAuth(preAuthRequest); break; } case WebSocketMethod.TipAdjustAuth: { TipAdjustAuthRequest tipAdjustRequest = JsonUtils.deserialize <TipAdjustAuthRequest>(payload.ToString()); cloverConnector.TipAdjustAuth(tipAdjustRequest); break; } case WebSocketMethod.CapturePreAuth: { CapturePreAuthRequest capturePreAuthRequest = JsonUtils.deserialize <CapturePreAuthRequest>(payload.ToString()); cloverConnector.CapturePreAuth(capturePreAuthRequest); break; } case WebSocketMethod.Sale: { SaleRequest saleRequest = JsonUtils.deserialize <SaleRequest>(payload.ToString()); cloverConnector.Sale(saleRequest); break; } case WebSocketMethod.InvokeInputOption: { InputOption io = JsonUtils.deserialize <InputOption>(payload.ToString()); cloverConnector.InvokeInputOption(io); break; } case WebSocketMethod.VoidPayment: { VoidPaymentRequest request = JsonUtils.deserialize <VoidPaymentRequest>(payload.ToString()); cloverConnector.VoidPayment(request); break; } case WebSocketMethod.ManualRefund: { ManualRefundRequest mrr = JsonUtils.deserialize <ManualRefundRequest>(payload.ToString()); cloverConnector.ManualRefund(mrr); break; } case WebSocketMethod.RefundPayment: { RefundPaymentRequest request = JsonUtils.deserialize <RefundPaymentRequest>(payload.ToString()); cloverConnector.RefundPayment(request); break; } case WebSocketMethod.DisplayPaymentReceiptOptions: { DisplayPaymentReceiptOptionsRequest request = JsonUtils.deserialize <DisplayPaymentReceiptOptionsRequest>(payload.ToString()); cloverConnector.DisplayPaymentReceiptOptions(request.OrderID, request.PaymentID); break; } case WebSocketMethod.ShowDisplayOrder: { com.clover.remote.order.DisplayOrder displayOrder = JsonUtils.deserialize <com.clover.remote.order.DisplayOrder>(payload.ToString()); cloverConnector.ShowDisplayOrder(displayOrder); break; } case WebSocketMethod.AcceptSignature: { WSVerifySignatureRequest svr = JsonUtils.deserialize <WSVerifySignatureRequest>(payload.ToString()); cloverConnector.AcceptSignature(svr); break; } case WebSocketMethod.RejectSignature: { WSVerifySignatureRequest svr = JsonUtils.deserialize <WSVerifySignatureRequest>(payload.ToString()); cloverConnector.RejectSignature(svr); break; } case WebSocketMethod.ConfirmPayment: { AcceptPayment acceptPayment = JsonUtils.deserialize <AcceptPayment>(payload.ToString()); cloverConnector.AcceptPayment(acceptPayment.Payment); break; } case WebSocketMethod.RejectPayment: { RejectPayment rp = JsonUtils.deserialize <RejectPayment>(payload.ToString()); cloverConnector.RejectPayment(rp.Payment, rp.Challenge); break; } case WebSocketMethod.VaultCard: { VaultCardMessage vcm = JsonUtils.deserialize <VaultCardMessage>(payload.ToString()); cloverConnector.VaultCard(vcm.cardEntryMethods); break; } case WebSocketMethod.ReadCardData: { ReadCardDataRequest request = JsonUtils.deserialize <ReadCardDataRequest>(payload.ToString()); cloverConnector.ReadCardData(request); break; } case WebSocketMethod.Closeout: { CloseoutRequest cr = new CloseoutRequest(); cloverConnector.Closeout(cr); break; } case WebSocketMethod.RetrievePendingPayments: { cloverConnector.RetrievePendingPayments(); break; } default: { Console.WriteLine("received unknown websocket method: " + method.ToString() + " in CloverWebSocketService."); break; } } } catch (InvalidOperationException ioe) { Console.WriteLine(ioe.Message); socket.Send("Error Deserializing"); } catch (Exception e) { Console.WriteLine(e.Message); socket.Send("Error Parsing: " + message); } }; }; InitializeConnector(parameters); server.Start(serverSocket); }
static void Main(string[] args) { string startFilePath = "c:/clover/SaleRequest.txt"; //testFile //string startFilePath = "c:/users/bryanc/Desktop/test.txt"; ICloverConnector cloverConnector; CloverDeviceConfiguration USBConfig = new USBCloverDeviceConfiguration("__deviceID__", "com.Fromuth.BC.tech", false, 1); cloverConnector = new CloverConnector(USBConfig); cloverConnector.AddCloverConnectorListener(new YourListener(cloverConnector)); cloverConnector.InitializeConnection(); Logging log = new Logging("CloverLog"); log.WriteMessage("Clover Program Started"); DateTime timeStamp = DateTime.Now; Thread.Sleep(5000); do { if (cloverConnector.IsReady && !isBusy) { try { //output a clover isConnected file if (File.Exists("c:/clover/isConnected.txt")) { File.WriteAllText("c:/clover/isConnected.txt", DateTime.Now.ToString()); } else { File.Create("c:/clover/isConnected.txt"); File.WriteAllText("c:/clover/isConnected.txt", DateTime.Now.ToString()); } //If the SaleRequest is made, proceed, else keep repeatig loop. if (File.Exists(startFilePath) && !isBusy) { isBusy = true; string startFileText = File.ReadAllText(startFilePath); //File.Delete(startFilePath); //File.Move(startFilePath, "c:/clover/clover-request-sale-file.txt"); string[] startFileContent = startFileText.Split('\t'); switch (startFileContent[0].ToLower()) { case "SALE": case "Sale": case "sale": { //StartOrder(cloverConnector); StartSale(cloverConnector, startFileContent[1], Int32.Parse(startFileContent[2])); break; } case "mrefund": case "MREFUND": case "MRefund": case "refund": { StartRefund(cloverConnector, startFileContent[1], Int32.Parse(startFileContent[2])); break; } case "fdrefund": case "FDREFUND": case "FDRefund": { StartDirectRefund(cloverConnector, startFileContent[1], startFileContent[2]); break; } case "pdrefund": case "PDREFUND": case "PDRefund": { StartDirectRefund(cloverConnector, startFileContent[1], startFileContent[2], Int32.Parse(startFileContent[3])); break; } case "cancel": case "CANCEL": case "Cancel": { cloverConnector.ShowMessage("Transaction Canceled by the cashier."); Thread.Sleep(1500); cloverConnector.ShowWelcomeScreen(); break; } default: { cloverConnector.ShowMessage("Invalid Response"); Thread.Sleep(3000); cloverConnector.ShowWelcomeScreen(); break; } } //File.Delete(startFilePath); } Thread.Sleep(5000); }catch (Exception ex) { Console.WriteLine(ex.Message); log.WriteError(ex.Message); isBusy = false; Thread.Sleep(5000); } } } while (true); }