Ejemplo n.º 1
0
        // Write To Card
        public static CISAResult CISACheck(KafkaMessage kmessage, NFC NFCDevice)
        {
            /* CISA DLL Testing  */
            CISAResult result = new CISAResult();
            short      rc     = 0;

            byte[] bufCard         = new byte[400]; // 221
            byte[] bufCardWavemode = new byte[400]; // 228

            Csemks32.csdate csdateNow;
            Csemks32.csdate csdateEnd;

            Csemks32.cstime cstimeNow;
            Csemks32.cstime cstimeEnd;

            int secNow;

            csdateNow.day_Renamed   = 0;
            csdateNow.month_Renamed = 0;
            csdateNow.year_Renamed  = 0;

            cstimeNow.hours   = 0;
            cstimeNow.minutes = 0;

            string serialnumberC2B = string.Format("{0,7}", ""); // 8 Chars fixed length string

            serialnumberC2B = vbHelper.Hex2Bin(CardUID + "C15A13FF");

            /* Calling CSEReadDateTime */
            rc = CSEReadDateTime(ref csdateNow, ref cstimeNow, out secNow);

            if (rc == Csemks32.CSE_SUCCESS)
            {
                Console.WriteLine("CSEReadDateTime OK");
                Console.WriteLine("Now is {0}/{1}/{2} {3}:{4}:{5}", csdateNow.day_Renamed.ToString("D2"), csdateNow.month_Renamed.ToString("D2"), csdateNow.year_Renamed.YearCisa(), cstimeNow.hours.ToString("D2"), cstimeNow.minutes.ToString("D2"), secNow.ToString("D2"));
            }
            else
            {
                result.rc        = CSELoadErrNo();
                result.errordesc = "\"CSEReadDateTime - ErrNo " + result.rc + "\"";
                Console.WriteLine("CSEReadDateTime Failed");
                Console.WriteLine("ErrNo: " + result.rc.ToString());

                return(result);
            }

            /* Calling CSECard2Buffer */
            // 6 Chars fixed length strings
            string accesstname1 = string.Format("{0,6}", (kmessage.payload.room != null) ? kmessage.payload.room : kmessage.payload.zone);                          // room/zone - first access target name
            string accesstname2 = string.Format("{0,6}", (kmessage.payload.spaces != null && kmessage.payload.spaces.Count > 0) ? kmessage.payload.spaces[0] : ""); // spaces - second access target name
            string accesstname3 = string.Format("{0,6}", (kmessage.payload.spaces != null && kmessage.payload.spaces.Count > 1) ? kmessage.payload.spaces[1] : ""); // spaces - third access target name
            string accesstname4 = string.Format("{0,6}", (kmessage.payload.spaces != null && kmessage.payload.spaces.Count > 2) ? kmessage.payload.spaces[2] : ""); // spaces - fourth access target name
            string accesstname5 = string.Format("{0,6}", (kmessage.payload.groups != null && kmessage.payload.groups.Count > 0) ? kmessage.payload.groups[0] : ""); // groups - category name

            // string accesstname5 = string.Format("{0,6}", (kmessage.payload.groups != null && kmessage.payload.groups.Count > 0) ? "" : ""); // groups - category name // Pass "group" field value empty for testing purposes

            Csemks32.card guestcard = new Csemks32.card();

            if (kmessage.payload.room != null)
            {
                guestcard.cardtype = 0; // 0 - Guest Card
            }
            else
            {
                guestcard.cardtype = 4; // 4 - Staff Card
            }

            // Provisional data for testing

            /*
             * csdateEnd.year_Renamed = csdateNow.year_Renamed;
             * csdateEnd.month_Renamed = csdateNow.month_Renamed;
             * csdateEnd.day_Renamed = 30;
             */
            DateTime endDate     = Helpers.epoch2date(double.Parse(kmessage.payload.checkoutHours));
            int      endDateYear = Helpers.YearPC2Cisa(endDate.Year);

            csdateEnd.year_Renamed  = Convert.ToByte(sbyte.Parse(endDateYear.ToString().Substring(endDateYear.ToString().Length - 2)));
            csdateEnd.month_Renamed = Convert.ToByte(sbyte.Parse(endDate.Month.ToString()));
            csdateEnd.day_Renamed   = Convert.ToByte(sbyte.Parse(endDate.Day.ToString()));


            guestcard.accessid = kmessage.payload.accessId;                                                         // Guest/Employee name
            guestcard.accesstime_Renamed.dateStart       = csdateNow;                                               // Checkin date
            guestcard.accesstime_Renamed.dateEnd         = csdateEnd;                                               // Checkout date
            guestcard.accesstime_Renamed.timeStart       = cstimeNow;                                               // checkin time
            guestcard.accesstime_Renamed.timeEnd.hours   = Convert.ToByte(sbyte.Parse(endDate.Hour.ToString()));    // checkout hour
            guestcard.accesstime_Renamed.timeEnd.minutes = Convert.ToByte(sbyte.Parse(endDate.Minute.ToString()));; // checkout minutes
            guestcard.credits = 0;                                                                                  // credits

            // Force null group
            guestcard.accesstarget5.bed = 0;
            guestcard.accesstarget5.id  = 0;

            guestcard.cardinfo_Renamed.icopy = 1;
            guestcard.cardinfo_Renamed.ncopy = 1;
            if (guestcard.cardtype == 4) // Staff
            {
                guestcard.fOvrPrivacy = 0;
            }
            else // Guest
            {
                guestcard.fOvrPrivacy = 1;
            }
            guestcard.fOffice      = 0;
            guestcard.fCardHistory = 0;
            guestcard.nhoursvalid  = 0;



            if (guestcard.cardtype == 4)
            { // Staff's Card
              /*
               * guestcard.accesstime_Renamed.Initialize();
               *
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[0] = new Csemks32.mtimeshift();
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[1] = new Csemks32.mtimeshift();
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[2] = new Csemks32.mtimeshift();
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[3] = new Csemks32.mtimeshift();
               *
               *
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[0].start1 = 0;
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[0].start2 = 0;
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[0].end1 = 95;
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[0].end2 = 0;
               * guestcard.accesstime_Renamed.mtimeshift_Renamed[0].days = 255;
               */
            }

            rc = CSECard2Buffer(ref guestcard,
                                String.Format(accesstname1, "!@@@@@@"),
                                String.Format(accesstname2, "!@@@@@@"),
                                String.Format(accesstname3, "!@@@@@@"),
                                String.Format(accesstname4, "!@@@@@@"),
                                String.Format(accesstname5, "!@@@@@@"),
                                bufCard);

            if (rc == Csemks32.CSE_SUCCESS)
            {
                Console.WriteLine("CSECard2Buffer OK");
                Console.WriteLine(vbHelper.Ascii2Hex(bufCard));
            }
            else
            {
                result.rc        = CSELoadErrNo();
                result.errordesc = "\"CSECard2Buffer - ErrNo " + result.rc + "\"";
                Console.WriteLine("CSECard2Buffer Failed");
                Console.WriteLine("ErrNo: " + result.rc.ToString());

                return(result);
            }

            /* Calling CSEWaveModeEncode */

            const byte IRversion = 1;

            rc = CSEWaveModeEncode(bufCard, serialnumberC2B, IRversion, bufCardWavemode);

            if (rc == Csemks32.CSE_SUCCESS)
            {
                Console.WriteLine("CSEWaveModeEncode OK");
                Console.WriteLine("* bufCardWavemode Content: " + bufCardWavemode);
                Console.WriteLine("* bufCardWavemode Lenght: " + bufCardWavemode.Length);
                Console.WriteLine("* HOTEL File Lenght: " + vbHelper.MidVB(vbHelper.Ascii2Hex(bufCardWavemode), 1, 32 * 2).Length);
                Console.WriteLine("* HOTEL FILE: " + vbHelper.MidVB(vbHelper.Ascii2Hex(bufCardWavemode), 1, 32 * 2));
                Console.WriteLine("* HOTEL FILE: " + Helpers.Mid(bufCardWavemode.Ascii2Hex(), 1, 32 * 2));
                Console.WriteLine("* KEYPLAN File Lenght: " + vbHelper.MidVB(vbHelper.Ascii2Hex(bufCardWavemode), 32 * 2 + 1, 188 * 2).Length);
                Console.WriteLine("* KEYPLAN FILE: " + vbHelper.MidVB(vbHelper.Ascii2Hex(bufCardWavemode), 32 * 2 + 1, 188 * 2));
                Console.WriteLine("* KEYPLAN FILE: " + Helpers.Mid(bufCardWavemode.Ascii2Hex(), 32 * 2 + 1, 188 * 2));

                if (NFCDevice.connectCard())// establish connection to the card: you've declared this from previous post
                {
                    Console.WriteLine("Writing to Card ...");
                    NFCDevice.writeHotelFile(vbHelper.MidVB(vbHelper.Ascii2Hex(bufCardWavemode), 1, 36 * 2));             // Write Hotel File - 36 bytes
                    NFCDevice.writeKeyplanFile(vbHelper.MidVB(vbHelper.Ascii2Hex(bufCardWavemode), 36 * 2 + 1, 192 * 2)); // Write Keyplan File - 192 bytes
                    NFCDevice.Close();
                }
                else
                {
                    Console.WriteLine("Card not availale.");
                    result.rc        = -1;
                    result.errordesc = "\"Writing card failed - Card not availale.\"";

                    return(result);
                }
            }
            else
            {
                result.rc        = CSELoadErrNo();
                result.errordesc = "\"CSEWaveModeEncode Failed - ErrNo " + result.rc + "\"";
                Console.WriteLine("CSEWaveModeEncode Failed");
                Console.WriteLine("ErrNo: " + result.rc.ToString());

                return(result);
            }

            return(result);
        }
Ejemplo n.º 2
0
        // Read From Card
        public static CardData CISAReadCard(NFC NFCDevice)
        {
            /* CISA DLL Testing  */
            CardData cardData = new CardData();
            string   hotelFile, keyplanFile;

            short  rc              = 0;
            string serialnumberC2B = string.Format("{0,7}", ""); // 8 Chars fixed length string

            serialnumberC2B = vbHelper.Hex2Bin(CardUID + "C15A13FF");

            byte[] bufCard         = new byte[400];               // 221
            byte[] bufCardWavemode = new byte[400];               // 228

            string accesstname1Read = string.Format("{0,6}", ""); // room/zone - first access target name
            string accesstname2Read = string.Format("{0,6}", ""); // spaces - second access target name
            string accesstname3Read = string.Format("{0,6}", ""); // spaces - third access target name
            string accesstname4Read = string.Format("{0,6}", ""); // spaces - fourth access target name
            string accesstname5Read = string.Format("{0,6}", ""); // groups - category name
            string warning          = "";

            Csemks32.card guestcard = new Csemks32.card();

            if (NFCDevice.connectCard())// establish connection to the card
            {
                Console.WriteLine("Reading from Card ...");
                hotelFile   = NFCDevice.readHotelFile();   // Read Hotel File - 36 bytes
                keyplanFile = NFCDevice.readKeyplanFile(); // Read Keyplan File - 192 bytes
                NFCDevice.Close();
                // Console.WriteLine("* HOTEL FILE: " + vbHelper.Ascii2Hex(Encoding.ASCII.GetBytes(hotelFile)));
                // Console.WriteLine("* KEYPLAN FILE: " + vbHelper.Ascii2Hex(Encoding.ASCII.GetBytes(keyplanFile)));
                Console.WriteLine("* HOTEL FILE: " + hotelFile);
                Console.WriteLine("* KEYPLAN FILE: " + keyplanFile);

                string bufCardWavemodeStr = hotelFile + keyplanFile;

                bufCardWavemode = (byte[])Helpers.Hex2Byte(bufCardWavemodeStr);

                rc = CSEWaveModeDecode(bufCardWavemode, serialnumberC2B, bufCard); // We are getting bufCardWavemode value reading the card via NFC

                if (rc == Csemks32.CSE_SUCCESS)
                {
                    rc = CSEBuffer2Card(bufCard, accesstname1Read, accesstname2Read, accesstname3Read, accesstname4Read, accesstname5Read, ref guestcard, warning);

                    if (rc == Csemks32.CSE_SUCCESS)
                    {
                        Console.WriteLine("Card readed correctly");
                        cardData.result.rc = 0; // Reading Result Successful = Csemks32.CSE_SUCCESS

                        // checkoutHours: get param from guestcard.accesstime_Renamed variable
                        string   codateYear    = DateTime.Now.Year.ToString().Substring(0, 2) + Helpers.YearCisa(guestcard.accesstime_Renamed.dateEnd.year_Renamed); // Checkout date (year)
                        int      codateMonth   = (int)guestcard.accesstime_Renamed.dateEnd.month_Renamed;                                                            // Checkout date (month)
                        int      codateDay     = (int)guestcard.accesstime_Renamed.dateEnd.day_Renamed;                                                              // Checkout date (day)
                        int      codateHour    = (int)guestcard.accesstime_Renamed.timeEnd.hours;                                                                    // Checkout date (hour)
                        int      codateMinutes = (int)guestcard.accesstime_Renamed.timeEnd.minutes;                                                                  // Checkout date (minutes)
                        DateTime checkoutDate  = DateTime.ParseExact(codateYear + "-" + codateMonth.ToString("00") + "-" + codateDay.ToString("00") + " " + codateHour.ToString("00") + ":" + codateMinutes.ToString("00") + ":00", "yyyy-MM-dd HH:mm:ss",
                                                                     System.Globalization.CultureInfo.InvariantCulture);
                        cardData.message.payload.checkoutHours = Helpers.date2epoch(checkoutDate);


                        // Read room/zone
                        if (guestcard.cardtype > 0)                                                                // Staff
                        {
                            cardData.message.payload.zone     = CISAReadAccessTargetZone(guestcard.accesstarget1); // Read zone
                            cardData.message.payload.room     = null;
                            cardData.message.payload.accessId = "Staff name";                                      // The Staff name is not physically written on the card, so it cannot be returned.
                        }
                        else // Guest
                        {
                            cardData.message.payload.room     = CISAReadAccessTargetLock(guestcard.accesstarget1); // Read room
                            cardData.message.payload.zone     = null;
                            cardData.message.payload.accessId = "Guest name";                                      // The Guest name is not physically written on the card, so it cannot be returned.
                        }

                        // Try to Get "Guest/Staff member" name
                        // nCard parameter -> guestcard.cardinfo_Renamed.ncard;
                        // Function to call -> rc = CSESearchCard(ref guestcardOp, ref guestcardRead, ref cardSearch);
                        Csemks32.card       guestcardRead = new Csemks32.card();
                        Csemks32.card       guestcardOp   = new Csemks32.card();
                        Csemks32.cardsearch cardSearch    = new Csemks32.cardsearch();

                        cardSearch.ncard = guestcard.cardinfo_Renamed.ncard;

                        //byte[] bufCardop = new byte[600];  // 221
                        //byte[] bufCard = new byte[600];  // 221


                        rc = CSESearchCard(ref guestcardOp, ref guestcardRead, ref cardSearch);
                        if (rc == Csemks32.CSE_SUCCESS)
                        {
                            string name = guestcardRead.accessid;
                            cardData.message.payload.accessId = name; // Set "guest/staff member" name
                            // DateTime dtStart = Helpers.csdt2Dt(guestcardRead.accesstime_Renamed.dateStart, guestcardRead.accesstime_Renamed.timeStart);
                            // DateTime dtEnd = Helpers.csdt2Dt(guestcardRead.accesstime_Renamed.dateEnd, guestcardRead.accesstime_Renamed.timeEnd);
                            //Console.WriteLine("Card info: " + string.Format("ncard:{0} name:{1}  start:{2}  End:{3}\n", guestcardRead.cardinfo_Renamed.ncard, name, dtStart, dtEnd));
                        }
                        else
                        {
                            cardData.result.rc        = CSELoadErrNo();
                            cardData.result.errordesc = "\"CSESearchCard - ErrNo " + cardData.result.rc + "\"";
                            Console.WriteLine("CSESearchCard Failed");
                            Console.WriteLine("ErrNo: " + cardData.result.rc.ToString());
                        }
                        // End Get "Guest/Staff member" name


                        // Read extra spaces
                        List <string> spaces = new List <string>();
                        if (guestcard.accesstarget2.bed != 0) // Check if extraSpace is not empty (bed = 0)
                        {
                            string space1 = CISAReadAccessTargetLock(guestcard.accesstarget2);
                            spaces.Add(space1);
                        }
                        if (guestcard.accesstarget3.bed != 0) // Check if extraSpace is not empty (bed = 0)
                        {
                            string space2 = CISAReadAccessTargetLock(guestcard.accesstarget3);
                            spaces.Add(space2);
                        }
                        if (guestcard.accesstarget4.bed != 0) // Check if extraSpace is not empty (bed = 0)
                        {
                            string space3 = CISAReadAccessTargetLock(guestcard.accesstarget4);
                            spaces.Add(space3);
                        }
                        cardData.message.payload.spaces = spaces; // new List<string>(new string[] { "espace1", "espace2" });

                        // Read groups
                        List <string> groups = new List <string>();
                        if (guestcard.accesstarget5.bed != 0) // Check if group is not empty (bed = 0)
                        {
                            string group1 = CISAReadAccessTargetLock(guestcard.accesstarget5);
                            groups.Add(group1);
                        }

                        cardData.message.payload.groups = groups; // new List<string>(new string[] { "group1", "group2" });
                    }
                    else
                    {
                        cardData.result.rc        = CSELoadErrNo();
                        cardData.result.errordesc = "\"CSEBuffer2Card - ErrNo " + cardData.result.rc + "\"";
                        Console.WriteLine("CSEBuffer2Card Failed");
                        Console.WriteLine("ErrNo: " + cardData.result.rc.ToString());
                    }
                }
                else
                {
                    cardData.result.rc        = CSELoadErrNo();
                    cardData.result.errordesc = "\"CSEWaveModeDecode Failed - ErrNo " + cardData.result.rc + "\"";
                    Console.WriteLine("CSEWaveModeDecode Failed");
                    Console.WriteLine("ErrNo: " + cardData.result.rc.ToString());
                }
            }
            else
            {
                Console.WriteLine("Card not availale.");
                cardData.result.rc        = -1;
                cardData.result.errordesc = "\"Reading card failed - Card not availale.\"";
            }



            return(cardData);
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            string encoderID = Properties.Settings.Default.EncoderId;

            Console.WriteLine("Sarting up application.");
            Console.WriteLine("EncoderID: " + encoderID);
            Helpers.WriteLog("Starting up application.");

            /* Reading App Configuration Values */
            string bootstrapServers = Properties.Settings.Default.BootstrapServers;
            int    autoOffsetReset  = Properties.Settings.Default.AutoOffsetReset;

            var jsonsettings = new JsonSerializerSettings
            {
                NullValueHandling     = NullValueHandling.Ignore,
                MissingMemberHandling = MissingMemberHandling.Ignore,
                DefaultValueHandling  = DefaultValueHandling.Ignore
            };

            // Preparing NFC Device
            NFC NFCDevice = new NFC();

            NFCDevice.SelectDevice();
            NFCDevice.establishContext();
            if (NFCDevice.connectCard())
            {
                //Console.WriteLine("Card UID bytes lenght: " + System.Text.ASCIIEncoding.ASCII.GetByteCount(NFCDevice.getcardUID());
                CardUID = NFCDevice.getcardUID();
                //CardUID = "C15A13FF" + NFCDevice.getcardUID(); // Mifare Classic cards have a 4-byte UID, which must be padded with 0xC15A13FF to reach an 8-byte length
                Console.WriteLine("Card UID: " + CardUID);
            }


            // Keep the console window open in debug mode.
            //Console.WriteLine("Press any key to exit.");
            //Console.ReadKey();


            //Kafka Consumer
            Random rand   = new Random();
            int    rvalue = rand.Next(1000);

            kafkaTopics = new List <string>()
            {
                "write_request", "read_request", "card_readers_request"
            };
            KafkaMessage kmessage;

            Action <DeliveryReport <Null, string> > handler = r =>
                                                              Console.WriteLine(!r.Error.IsError
                    ? $"Delivered message to {r.TopicPartitionOffset}"
                    : $"Delivery Error: {r.Error.Reason}");

            var confC = new ConsumerConfig
            {
                GroupId          = "testingCISA" + rvalue.ToString("000"),
                BootstrapServers = bootstrapServers,
                AutoOffsetReset  = AutoOffsetReset.Latest // By default 1 == AutoOffsetResetType.Earliest
                                                          //SecurityProtocol = SecurityProtocolType.Sasl_Plaintext,
                                                          //SaslMechanism = SaslMechanismType.ScramSha256,
                                                          //SaslPassword = "******",
                                                          //SaslUsername = "******",
                                                          //
                                                          // Note: The AutoOffsetReset property determines the start offset in the event
                                                          // there are not yet any committed offsets for the consumer group for the
                                                          // topic/partitions of interest. By default, offsets are committed
                                                          // automatically, so in this example, consumption will only start from the
                                                          // earliest message in the topic 'write_request' the first time you run the program.
            };

            using (var c = new ConsumerBuilder <Ignore, string>(confC).Build())
            {
                c.Subscribe(kafkaTopics);

                bool consuming = true;
                // The client will automatically recover from non-fatal errors. You typically
                // don't need to take any action unless an error is marked as fatal.
                //c.OnError += (_, e) => consuming = !e.IsFatal;

                // Raised on critical errors, e.g. connection failures or all brokers down.
                CancellationTokenSource cts = new CancellationTokenSource();
                Console.CancelKeyPress += (_, e) => {
                    e.Cancel = true; // prevent the process from terminating.
                    cts.Cancel();
                };

                /*
                 * c.OnError += (_, error)
                 * =>
                 * {
                 *  Console.WriteLine($"Kafka error occured: {error.Reason}");
                 *  Helpers.WriteLog($"Kafka error occured: {error.Reason}");
                 *  consuming = !error.IsFatal;
                 * };*/

                while (consuming)
                {
                    try
                    {
                        var    cr            = c.Consume();
                        var    p             = new ProducerBuilder <Null, string>(confC).Build();
                        string resultmessage = "";
                        var    Topic         = cr.Topic;

                        if (cr.Value.IsValidJSON())
                        {
                            if (cr.Topic == "write_request")
                            { // Write Request
                                kmessage = JsonConvert.DeserializeObject <KafkaMessage>(cr.Value, jsonsettings);
                                if (kmessage.payload.accessType == "CISA" && kmessage.payload.deviceId == encoderID)
                                { // Only Kafka messages tagged with "CISA" accessType are processed
                                    string spaces = (kmessage.payload.spaces != null ? string.Join(",", kmessage.payload.spaces) : null);
                                    string groups = (kmessage.payload.groups != null ? string.Join(",", kmessage.payload.groups) : null);

                                    Console.WriteLine($"Code: '{kmessage.code}' Guest/Staff: '{kmessage.payload.accessId}' Room: '{kmessage.payload.room}' Zone: '{kmessage.payload.zone}' spaces: '{spaces}' Groups: '{groups}' at: '{cr.TopicPartitionOffset}'.");
                                    Helpers.WriteLog($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");

                                    CISAResult result = CISACheck(kmessage, NFCDevice);
                                    if (result.rc == Csemks32.CSE_SUCCESS)
                                    {
                                        resultmessage = "{\"code\":\"write_result\",\"payload\":{\"id\":" + kmessage.payload.id + "}}";
                                    }
                                    else
                                    {
                                        resultmessage = "{\"code\":\"write_result\",\"payload\":{\"id\":" + kmessage.payload.id + ",\"error\":" + result.errordesc.ToString() + "}}";
                                    }

                                    p.Produce("write_result", new Message <Null, string> {
                                        Value = resultmessage
                                    }, handler);
                                    p.Flush(TimeSpan.FromSeconds(1));

                                    Helpers.WriteLog($"Response '{resultmessage}' sent to write_result topic.");
                                    Console.WriteLine($"Response '{resultmessage}' sent to write_result topic.");
                                }
                            }

                            if (cr.Topic == "read_request")
                            { // Read Request
                                kmessage = JsonConvert.DeserializeObject <KafkaMessage>(cr.Value, jsonsettings);
                                if (kmessage.payload.accessType == "CISA" && kmessage.payload.deviceId == encoderID)
                                { // Only Kafka messages tagged with "CISA" accessType are processed
                                    CardData cardData = CISAReadCard(NFCDevice);
                                    if (cardData.result.rc == Csemks32.CSE_SUCCESS)
                                    {
                                        string spaces = null;
                                        string groups = null;
                                        if (cardData.message.payload.spaces != null)
                                        {
                                            for (var i = 0; i < cardData.message.payload.spaces.Count; i++)
                                            {
                                                spaces = spaces + "\"" + cardData.message.payload.spaces[i] + "\"";
                                                if (i != cardData.message.payload.spaces.Count - 1)
                                                {
                                                    spaces = spaces + ","; // separator
                                                }
                                            }
                                        }
                                        if (cardData.message.payload.groups != null)
                                        {
                                            for (var i = 0; i < cardData.message.payload.groups.Count; i++)
                                            {
                                                groups = groups + "\"" + cardData.message.payload.groups[i] + "\"";
                                                if (i != cardData.message.payload.groups.Count - 1)
                                                {
                                                    groups = groups + ","; // separator
                                                }
                                            }
                                        }
                                        resultmessage = "{\"code\":\"read_card_result\",\"payload\":{\"id\":" + kmessage.payload.id + ",\"accessType\":\"CISA\",\"deviceId\":\"" + kmessage.payload.deviceId + "\",\"guestName\":\"" + cardData.message.payload.accessId + "\",\"addressType\":null,\"checkoutHours\":" + (!String.IsNullOrEmpty(cardData.message.payload.checkoutHours) ? "\"" + cardData.message.payload.checkoutHours + "\"" : "null") + ",\"spaces\":" + (spaces != null ? "[" + spaces + "]" : "null") + ",\"groups\":" + ((groups) != null ? "[" + groups + "]" : "null") + ",\"hotelName\":null,\"mac\":null,\"room\":" + (!String.IsNullOrEmpty(cardData.message.payload.room) ? "\"" + cardData.message.payload.room + "\"" : "null") + ",\"zone\":" + (cardData.message.payload.zone != null ? "\"" + cardData.message.payload.zone + "\"" : "null") + "}}";
                                    }
                                    else
                                    {
                                        resultmessage = "{\"code\":\"read_card_result\",\"payload\":{\"id\":" + kmessage.payload.id + ",\"error\":" + cardData.result.errordesc.ToString() + "}}";
                                    }

                                    p.Produce("read_card_result", new Message <Null, string> {
                                        Value = resultmessage
                                    }, handler);
                                    p.Flush(TimeSpan.FromSeconds(1));

                                    Helpers.WriteLog($"Response '{resultmessage}' sent to read_card_result topic.");
                                    Console.WriteLine($"Response '{resultmessage}' sent to read_card_result topic.");
                                }
                            }

                            if (cr.Topic == "card_readers_request")
                            { // Card Reader Request
                                kmessage = JsonConvert.DeserializeObject <KafkaMessage>(cr.Value, jsonsettings);
                                if (kmessage.payload.id != null)
                                {
                                    resultmessage = "{\"code\":\"card_readers_result\",\"payload\":{\"id\":" + kmessage.payload.id + ",\"deviceId\":\"" + encoderID + "\",\"type\":\"CISA\"}}";
                                    p.Produce("card_readers_result", new Message <Null, string> {
                                        Value = resultmessage
                                    }, handler);
                                    p.Flush(TimeSpan.FromSeconds(1));

                                    Helpers.WriteLog($"Response '{resultmessage}' sent to card_readers_result topic.");
                                    Console.WriteLine($"Response '{resultmessage}' sent to card_readers_result topic.");
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine($"ERROR Invalid JSON format - Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                            Helpers.WriteLog($"ERROR Invalid JSON format - Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                        }
                    }
                    catch (ConsumeException e)
                    {
                        Console.WriteLine($"Error occured: {e.Error.Reason}");
                        Helpers.WriteLog($"Error occured: {e.Error.Reason}");
                    }
                }

                // Ensure the consumer leaves the group cleanly and final offsets are committed.
                c.Close();
            }
            //KAFKA
        }