Ejemplo n.º 1
0
        public long Run(IDataSource source, IEncoder encoder, IDecoder decoder, long msgCount, bool debug)
        {
            long totalBytesTransmitted = 0;

            for (long i = 0; i < msgCount; i++)
            {
                var sourceData = source.GetNextDataPoint();

                var encodedData = encoder.Encode(sourceData);

                // This is where the data is normally transmitted via network
                totalBytesTransmitted += encodedData.LongLength;

                var decodedData = decoder.Decode(encodedData);

                var areEqual = IotDevice.AreEquals(sourceData, decodedData, false);

                if (!areEqual || debug)
                {
                    Console.WriteLine();
                    Console.WriteLine("##########################################################");
                    Console.WriteLine($"Message number: {i+1}");
                    Console.WriteLine();

                    Console.WriteLine("source data:");
                    Console.WriteLine(sourceData);
                    Console.WriteLine();

                    Console.WriteLine("encoded data:");
                    Console.WriteLine(BitConverter.ToString(encodedData).Replace("-", ""));
                    Console.WriteLine();

                    Console.WriteLine("decoded data:");
                    Console.WriteLine(decodedData);
                    Console.WriteLine();

                    if (!areEqual)
                    {
                        // run this again to get debug outputs
                        IotDevice.AreEquals(sourceData, decodedData, true);
                    }

                    Console.WriteLine($"Encoding/Decoding successful: {areEqual}");
                }

                if (debug)
                {
                    Console.WriteLine("Press any key to send the next message.");
                    Console.ReadKey();
                }
            }

            return(totalBytesTransmitted);
        }
Ejemplo n.º 2
0
        public byte[] Encode(IotDevice device)
        {
            var clone = _encodingBuffer.Clone();

            Compress(_encodingBuffer, _lastEncodedDevice, device);

            var x = Decompress(clone, _lastEncodedDevice);

            _lastEncodedDevice = device;
            // Wenn beim Komprimieren Rundungsfehler auftreten, dann nehmen wir einfach den Stateless Codec
            if (x != null && IotDevice.AreEquals(device, x, _debug))
            {
                _encodingBuffer = clone;
                return(new byte[0]);
            }
            else
            {
                var bytes = _encoder.Encode(device, sent ? _lastEncodedDevice : null);
                sent = true;
                return(bytes);
            }
        }