示例#1
0
        private void RaiseDataReceived(ReceivedDataArgs args)
        {
            if (DataReceivedEvent != null)
            {
                DataReceivedEvent(this, args);
            }

            //DataReceivedEvent?.Invoke(this, args);
        }
示例#2
0
        private void Server_DataReceivedEvent(object sender, ReceivedDataArgs args)
        {
            if (!Program.isFirebaseTriggered)
            {
                client = new FireSharp.FirebaseClient(config);

                if (client != null)
                {
                    Program.isFirebaseTriggered = true;
                }

                getDataFromFirebase();
            }

            //TODO: put this processing in separate function maybe.
            dataBlocks.Clear();
            data = "";
            //Get byte and convert to hex values
            for (int i = 0; i <= args.ReceivedBytes.Length - 1; i++)
            {
                rawData += args.ReceivedBytes[i].ToString("X2") + " ";

                data += args.ReceivedBytes[i].ToString("X2");
                if ((i + 1) % 4 == 0)
                {
                    dataBlocks.Add(data);
                    counter++;
                    data = "";
                }
            }

            long newKey       = Convert.ToInt64(dataBlocks[1], 16);
            long lanCodeToInt = Convert.ToInt64(lanCode, 16);

            newKey = newKey ^ lanCodeToInt;

            decoded = dataBlocks[0].ToString() + dataBlocks[1].ToString();
            var decoded_part = "";

            for (int i = 2; i <= dataBlocks.Count - 1; i++)
            {
                decoded_part = "";
                long value = Convert.ToInt64(dataBlocks[i], 16);
                value = value ^ newKey;
                //alarmID += Encoding.Default.GetString(BitConverter.GetBytes(value).Reverse().ToArray());
                decoded_part = value.ToString("X");
                if (decoded_part.Length != 8)
                {
                    var numOfZeros = 8 - decoded_part.Length;
                    for (int j = 0; j < numOfZeros; j++)
                    {
                        decoded += "0";
                    }
                }
                decoded += decoded_part;
            }

            //Console.WriteLine(alarmID);

            Console.WriteLine("------------------------------------------------------------------------------------");
            Console.WriteLine("RawData: " + rawData);

            var decodedForPrinting = "";

            for (int i = 0; i <= decoded.Length - 1; i++)
            {
                if (i == 0 || i % 2 == 1)
                {
                    decodedForPrinting += decoded[i].ToString();
                }
                else
                {
                    decodedForPrinting += " " + decoded[i].ToString();
                }
            }

            Console.WriteLine("Decode:  " + decodedForPrinting);

            var hexaNumbToText = "";

            for (int i = 16; i <= decoded.Length - 1; i++)
            {
                hexaNumbToText = "";
                byte[] dataBytes = FromHex(decoded[i].ToString() + decoded[i + 1].ToString());
                hexaNumbToText = Encoding.ASCII.GetString(dataBytes);
                if (hexaNumbToText.All(c => Char.IsLetterOrDigit(c) || c.Equals('.')))
                {
                    finalText += Encoding.ASCII.GetString(dataBytes);
                }
                else
                {
                    finalText += "<" + decoded[i].ToString() + decoded[i + 1].ToString() + ">";
                }
                i++;
            }

            Console.WriteLine("Text:    " + finalText);
            //TODO: put this in try or do the checks there.
            DataProcessing(finalText);
            LoggDataToDB(finalText);

            //INFO : printing in blocks
            //var dataBlocks_string = "";
            //for (int i = 0; i <= dataBlocks.Count - 1; i++)
            //{
            //    dataBlocks_string += dataBlocks[i].ToString() + " ";
            //}
            //Console.WriteLine(dataBlocks_string);

            rawData   = "";
            counter   = 0;
            alarmID   = "";
            decoded   = "";
            finalText = "";
        }