Exemplo n.º 1
0
        private void PrintTextButton_Click(object sender, EventArgs e)
        {
            List <string> messages = new List <string>();

            messages.Add(PrintTextBox.Text);
            cloverConnector.PrintText(messages);
        }
        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);
        }