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) { // Retrieve the arguments from the service ImagePath args = Environment.GetCommandLineArgs(); } 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 { 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, null, "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: { OpenCashDrawerRequest request = JsonUtils.deserialize <OpenCashDrawerRequest>(payload.ToString()); cloverConnector.OpenCashDrawer(request); 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: // deprecated. use ResetDevice case WebSocketMethod.ResetDevice: { 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; } case WebSocketMethod.StartCustomActivity: { CustomActivityRequest request = JsonUtils.deserialize <CustomActivityRequest>(payload.ToString()); cloverConnector.StartCustomActivity(request); break; } case WebSocketMethod.RetrieveDeviceStatus: { RetrieveDeviceStatusRequest request = JsonUtils.deserialize <RetrieveDeviceStatusRequest>(payload.ToString()); cloverConnector.RetrieveDeviceStatus(request); break; } case WebSocketMethod.SendMessageToActivity: { MessageToActivity mta = JsonUtils.deserialize <MessageToActivity>(payload.ToString()); cloverConnector.SendMessageToActivity(mta); break; } case WebSocketMethod.RetrievePaymentRequest: { RetrievePaymentRequest rpr = JsonUtils.deserialize <RetrievePaymentRequest>(payload.ToString()); cloverConnector.RetrievePayment(rpr); break; } case WebSocketMethod.RetrievePrintersRequest: { RetrievePrintersRequest rpr = JsonUtils.deserialize <RetrievePrintersRequest>(payload.ToString()); cloverConnector.RetrievePrinters(rpr); break; } case WebSocketMethod.PrintJobStatusRequest: { PrintJobStatusRequest req = JsonUtils.deserialize <PrintJobStatusRequest>(payload.ToString()); cloverConnector.RetrievePrintJobStatus(req); break; } case WebSocketMethod.PrintRequest: { PrintRequest64Message request = JsonUtils.deserialize <PrintRequest64Message>(payload.ToString()); PrintRequest printRequest = null; if (request.base64strings.Count > 0) { byte[] imgBytes = Convert.FromBase64String(request.base64strings[0]); MemoryStream ms = new MemoryStream(); ms.Write(imgBytes, 0, imgBytes.Length); Bitmap bp = new Bitmap(ms); ms.Close(); printRequest = new PrintRequest(bp, request.externalPrintJobId, request.printDeviceId); } else if (request.imageUrls.Count > 0) { printRequest = new PrintRequest(request.imageUrls[0], request.externalPrintJobId, request.printDeviceId); } else if (request.textLines.Count > 0) { printRequest = new PrintRequest(request.textLines, request.externalPrintJobId, request.printDeviceId); } cloverConnector.Print(printRequest); 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); }