Exemplo n.º 1
0
        public static CanPlaySuccessResponse DeserializeASCII(byte[][] buffer)
        {
            // how many tokens
            int tokensCount = buffer.GetLength(0);

            // concentrator id
            char[] p1 = tokensCount >= 1 ?
                        buffer[0]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // controller id
            char[] p2 = tokensCount >= 2 ?
                        buffer[1]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // card id
            char[] p3 = tokensCount >= 3 ?
                        buffer[2]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // transaction
            char[] p4 = tokensCount >= 4 ?
                        buffer[3]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // game id
            char[] p5 = tokensCount >= 5 ?
                        buffer[4]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // permission
            char[] p6 = tokensCount >= 6 ?
                        buffer[5]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // relay type
            char[] p7 = tokensCount >= 7 ?
                        buffer[6]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // relay pulse
            char[] p8 = tokensCount >= 8 ?
                        buffer[7]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // relay on time
            char[] p9 = tokensCount >= 9 ?
                        buffer[8]
                        .Where(b => b != 0x1F && b != 0x1E)
                        .Select(b => (char)b)
                        .ToArray() : null;
            // relay off time
            char[] p10 = tokensCount >= 10 ?
                         buffer[9]
                         .Where(b => b != 0x1F && b != 0x1E)
                         .Select(b => (char)b)
                         .ToArray() : null;
            // relay display time
            char[] p11 = tokensCount >= 11 ?
                         buffer[10]
                         .Where(b => b != 0x1F && b != 0x1E)
                         .Select(b => (char)b)
                         .ToArray() : null;
            // message line1
            char[] p12 = tokensCount >= 12 ?
                         buffer[11]
                         .Where(b => b != 0x1F && b != 0x1E)
                         .Select(b => (char)b)
                         .ToArray() : null;

            // message line2
            char[] p13 = tokensCount >= 13 ?
                         buffer[12]
                         .Where(b => b != 0x1F && b != 0x1E)
                         .Select(b => (char)b)
                         .ToArray() : null;

            CanPlaySuccessResponse response = new CanPlaySuccessResponse
            {
                ConcentratorId   = p1 != null ? new string(p1) : string.Empty,
                GameControllerId = p2 != null ? new string(p2) : string.Empty,
                CardId           = p3 != null ? new string(p3) : string.Empty,
                TransactionId    = p4 != null && p4.Count() > 0 ? int.Parse(new string(p4)) : 0,
                GameId           = p5 != null ? new string(p5) : string.Empty,
                Permission       = p6 != null && p6.Count() > 0 && p6[0] == '1', //  0x30 '0' char(48)
                RelayType        = p7 != null ? new string(p7) : string.Empty,
                RelayPulse       = p8 != null && p8.Count() > 0 ? int.Parse(new string(p8)) : 0,
                RelayOnTime      = p9 != null && p9.Count() > 0 ? int.Parse(new string(p9)) : 0,
                RelayOffTime     = p10 != null && p10.Count() > 0 ? int.Parse(new string(p10)) : 0,
                RelayDisplayTime = p11 != null && p11.Count() > 0 ? int.Parse(new string(p11)) : 0,
                MessageLine1     = p12 != null ? new string(p12) : string.Empty,
                MessageLine2     = p13 != null ? new string(p13) : string.Empty
            };

            return(response);
        }
Exemplo n.º 2
0
        public void VerifyCards(CancellationToken token)
        {
            Console.WriteLine($"Controller {GameControllerId} is Connecting ...");
            using (var client = new TcpClient())
            {
                try
                {
                    //client.Connect("192.168.1.198", 8877);
                    client.Connect("192.168.1.198", 502);
                    Console.WriteLine($"Controller {GameControllerId} Connected");

                    var indx          = 0;
                    var transactionId = 1;
                    for (; ;)
                    {
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        if (indx >= Cards.Count)
                        {
                            indx = 0;
                        }

                        if (transactionId > 255)
                        {
                            transactionId = 1;
                        }

                        var request = new CanPlayRequest
                        {
                            ConcentratorId   = ConcentratorId,
                            GameControllerId = GameControllerId,
                            CardType         = Cards[indx].CardType,
                            CardId           = Cards[indx].CardId,
                            ShoulPay         = false,
                            TransactionId    = transactionId,
                            EndpointRssi     = EndpointRssi,
                            ConcentratorRssi = ConcentratorRssi
                        };
                        byte[] outputBuffer = request.SerializeASCII();

                        // Get a client stream for reading and writing.
                        NetworkStream stream = client.GetStream();

                        // Send the message to the connected TcpServer.
                        stream.Write(outputBuffer, 0, outputBuffer.Length);
                        stream.Flush();

                        Console.WriteLine("Controller {0} Sent: {1}", GameControllerId, BitConverter.ToString(outputBuffer));

                        // Buffer to store the response bytes.
                        byte[]      inputBuffer       = new byte[32];
                        List <byte> receivedBytes     = new List <byte>();
                        int         numberOfBytesRead = 0;
                        do
                        {
                            if (stream.CanRead)
                            {
                                numberOfBytesRead = stream.Read(inputBuffer, 0, inputBuffer.Length);
                            }
                            else
                            {
                                break;
                            }

                            receivedBytes.AddRange(inputBuffer.Take(numberOfBytesRead));
                        }while (stream.DataAvailable);

                        //извличане на получените данни
                        byte[][] splittedMessage = SplitByteArray(receivedBytes, 0x1F).ToArray();

                        if (splittedMessage.Count() <= 1)
                        {
                            Console.WriteLine("Controller {0} Received: {1}",
                                              GameControllerId,
                                              new string(
                                                  receivedBytes
                                                  .Select(b => (char)b)
                                                  .ToArray()));
                        }
                        else
                        {
                            var response = CanPlaySuccessResponse.DeserializeASCII(splittedMessage);
                            Console.WriteLine("Controller {0} Received:\n\tconcentrator {1}\n\tcontroller {2}\n\tcard {3}\n\ttransaction {4}\n\tgame {5}\n\tpermission {6}\n\trelay type {7}\n\trelay pulse {8}\n\trelay on time {9}\n\trelay off time {10}\n\trelay display time {11}\n\tline1 {12}\n\tline2 {13}",
                                              response.GameControllerId,
                                              response.ConcentratorId,
                                              response.GameControllerId,
                                              response.CardId,
                                              response.TransactionId,
                                              response.GameId,
                                              response.Permission,
                                              response.RelayType,
                                              response.RelayPulse,
                                              response.RelayOnTime,
                                              response.RelayOffTime,
                                              response.RelayDisplayTime,
                                              response.MessageLine1,
                                              response.MessageLine2);
                        }

                        indx++;
                        transactionId++;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            Console.WriteLine($"Controller {GameControllerId} disconnected");
        }