Пример #1
0
        public async Task Write(byte[] dataToWrite)
        {
            try
            {
                //string temp = Encoding.UTF8.GetString(dataToWrite, 0, dataToWrite.Length);
                //Debug.WriteLine(">" + dataToWrite.Length + temp + "<");

                if (dataToWrite.Length < 5)
                {
                    return;
                }

                //TcpClnt.WriteStream.Write(dataToWrite, 0, dataToWrite.Length);
                //await TcpClnt.WriteStream.FlushAsync();

                OnSendToPumpController(this, new SendToPumpControllerEventArgs(dataToWrite));
                await Task.Delay(500);
            }
            catch (Exception ex)
            {
                string dataBuffer = Encoding.UTF8.GetString(dataToWrite, 0, dataToWrite.Length);
                SystemLogs.LogError("WriteToSocket Exception", "Error Message: " + ex.Message,
                                    "Buffer:" + dataBuffer);
            }
        }
Пример #2
0
        public async Task ProcessDataReceived(byte[] data)
        {
            string dataInString = "INIT";

            try
            {
                dataInString = Encoding.UTF8.GetString(data, 0, data.Length);
                if (dataInString.Trim() == "")
                {
                    return;
                }

                MessageType messageType = MessageType.Unknown;

                if (dataInString.Contains("$"))
                {
                    messageType = MessageType.AuthorizationInfo;
                }
                else
                {
                    string messageTypeIdentifier = Encoding.UTF8.GetString(data, 1, 1);
                    Enum.TryParse <MessageType>(messageTypeIdentifier, out messageType);
                }

                if (messageType == MessageType.PumpStatus)
                {
                    int minPumpStatusData = 30;
                    if (data.Length > minPumpStatusData)
                    {
                        dataInString = dataInString.Substring(0, 68);
                        await updatePumpStatus(dataInString);

                        for (int i = 0; i < PiePumps.TotalOfPump; i++)
                        {
                            if (PiePumps.Pumps[i].CurrentStatus == PumpStatus.DispenseCompleted)
                            {
                                if (PiePumps.Pumps[i].Request != RequestPumpStatus.RequestDispenseCompleted)
                                {
                                    await processUnexectedPumpDispenseCompleted(i);

                                    continue;
                                }
                            }
                            else
                            {
                                if (PiePumps.Pumps[i].Request == RequestPumpStatus.RequestReserve)
                                {
                                    processReserve(i);
                                    continue;
                                }
                                else
                                if (PiePumps.Pumps[i].Request == RequestPumpStatus.RequestAutorise)
                                {
                                    processAuthorise(i);
                                    continue;
                                }
                                else if (PiePumps.Pumps[i].Request == RequestPumpStatus.RequestStartedDispense)
                                {
                                    processRunning(i);
                                    continue;
                                }
                                else if (PiePumps.Pumps[i].Request == RequestPumpStatus.RequestDispenseCompleted)
                                {
                                    await processDispenseCompleted(i);

                                    continue;
                                }
                                else if (PiePumps.Pumps[i].Request == RequestPumpStatus.RequestCancel)
                                {
                                    processCancel(i, PiePumps.Pumps[i].ZapOrderId);
                                    continue;
                                }
                            }
                        }

                        if (DEBUG_MODE)
                        {
                            for (int i = 0; i < PiePumps.TotalOfPump; i++)
                            {
                                //if (i > 2)
                                //return;

                                if (PiePumps.Pumps[i].Request > (RequestPumpStatus)2)
                                {
                                    var status = "|" + PiePumps.Pumps[i].PumpId + "*" +
                                                 PiePumps.Pumps[i].ZapOrderId + "*" +
                                                 PiePumps.Pumps[i].Request.ToString("G") + "*" +
                                                 PiePumps.Pumps[i].CurrentStatus.ToString("G") + "*" +
                                                 PiePumps.Pumps[i].RequestTriedCount + "|";
                                    Debug.WriteLine(status);
                                }
                            }
                        }
                    }
                }
                else if (messageType == MessageType.AuthorizationInfo)
                {
                    GetAuthorization(dataInString);
                    SystemLogs.LogBankServer(dataInString + 1, "POS", "Dispense Info");
                }
                //else if (messageType == MessageType.ReserveStatus)
                //{
                //    GetReserveStatus(dataInString);
                //}
                else
                // Sometime it is not trigger properly the reserve status
                // below is the workaround
                if (Encoding.UTF8.GetString(data, 1, 1) == "M")
                {
                    GetReserveStatus(dataInString);
                }

                // Log every received msj to DB. BS is BankServer
                // SystemLogs.LogBankServer(dataInString, "PI", messageType.ToString("G"));

                if (DEBUG_MODE)
                {
                    if (messageType != MessageType.PumpStatus)
                    {
                        Debug.WriteLine("Received from Pie: " + dataInString + Environment.NewLine);
                    }
                    else
                    {
                        if (DEBUG_MODE2)
                        {
                            Debug.WriteLine("Received from Pie: " + dataInString + Environment.NewLine);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SystemLogs.LogError("ProcessDataReceived Exception", "Error Message: " + ex.Message,
                                    "Received Data: " + dataInString);
                Debug.WriteLineIf(ex.Message != "", ex.Message);
            }
        }
Пример #3
0
        public async Task SendDataToPie(MessageCommand messageCommand, string ZapOrderId,
                                        int pumpId             = 0, decimal authoriseLimit = 0,
                                        string transactionId   = "", string webhook_start  = "",
                                        string webhook_success = "", string webhook_error  = "")
        {
            var dataByte = new byte[1];

            byte[] buffer = dataByte;
            string dataBuffer;

            try
            {
                switch (messageCommand)
                {
                case MessageCommand.RequestPumpStatus:
                    //return;
                    buffer = new PumpStatusRequest().GetBytes();
                    break;

                case MessageCommand.Reserve:
                    if (PiePumps.Pumps[pumpId - 1].CurrentStatus == PumpStatus.Idle)
                    {
                        PiePumps.Pumps[pumpId - 1].Request           = RequestPumpStatus.RequestReserve;
                        PiePumps.Pumps[pumpId - 1].ZapOrderId        = ZapOrderId;
                        PiePumps.Pumps[pumpId - 1].RequestTriedCount = 0;

                        PiePumps.Pumps[pumpId].UnexectedPumpDispenseCompletedFlag = false;

                        buffer = new Reserve(pumpId, ZapOrderId).GetBytes();
                    }
                    else
                    {
                        // straight away to reject it
                        ReservePump(pumpId, false);
                    }
                    break;

                case MessageCommand.Authorize:
                    if (PiePumps.Pumps[pumpId - 1].Request != RequestPumpStatus.RequestAutorise)
                    {
                        Debug.WriteLine("Failed. Pump is not in RequestAutorise state");
                        AuthorizePump(pumpId, false);
                        break;
                    }

                    PiePumps.Pumps[pumpId - 1].RequestTriedCount        = 0;
                    PiePumps.Pumps[pumpId - 1].BosTransactionId         = transactionId;
                    PiePumps.Pumps[pumpId - 1].WebhookStartDispense     = webhook_start;
                    PiePumps.Pumps[pumpId - 1].WebhookCompletedDispense = webhook_success;
                    PiePumps.Pumps[pumpId - 1].WebhookError             = webhook_error;

                    buffer = new Authorise(pumpId, authoriseLimit).GetBytes();
                    break;

                case MessageCommand.GetFinalization:
                    PiePumps.Pumps[pumpId - 1].Request           = RequestPumpStatus.RequestFinalize;
                    PiePumps.Pumps[pumpId - 1].RequestTriedCount = 0;

                    buffer = new GetFinalization(pumpId).GetBytes();
                    break;

                case MessageCommand.PumpCancel2:
                    PiePumps.Pumps[pumpId - 1].PreviousRequest = PiePumps.Pumps[pumpId - 1].Request;        // probably cancel request is failed

                    if (PiePumps.Pumps[pumpId - 1].CurrentStatus == PumpStatus.Reserved ||
                        PiePumps.Pumps[pumpId - 1].CurrentStatus == PumpStatus.Authorized)
                    {
                        bool okProceed = true;
                        if (ZapOrderId != "")
                        {
                            if (PiePumps.Pumps[pumpId - 1].ZapOrderId != ZapOrderId)                                        // make sure the same order id)
                            {
                                okProceed = false;
                            }
                        }

                        if (okProceed)
                        {
                            PiePumps.Pumps[pumpId - 1].RequestTriedCount = 0;
                            PiePumps.Pumps[pumpId - 1].Request           = RequestPumpStatus.RequestCancel;
                            buffer = new CancelPump(pumpId).GetBytes();

                            break;
                        }
                    }

                    PiePumps.Pumps[pumpId - 1].RequestTriedCount = 9999;                                   // terus cancel
                    processCancel(pumpId - 1, ZapOrderId);
                    return;
                }

                if (messageCommand != MessageCommand.RequestPumpStatus)
                {
                    dataBuffer = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                    SystemLogs.LogBankServer(dataBuffer, "POS", messageCommand.ToString("G"));
                }
                await Write(buffer);

                if (DEBUG_MODE)
                {
                    if (messageCommand != MessageCommand.RequestPumpStatus)
                    {
                        Debug.WriteLine("--------> Request to Pie for " + messageCommand.ToString("G") + " Pump No:" + pumpId +
                                        " Data:" + Encoding.UTF8.GetString(buffer, 0, buffer.Length));
                    }
                }
            }
            catch (Exception ex)
            {
                dataBuffer = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                SystemLogs.LogError("SendDataToBankServer Exception", "Error Message: " + ex.Message,
                                    "Buffer:" + dataBuffer);
            }
        }