Пример #1
0
        private IotDevice Decompress(CompressionBuffer buffer, IotDevice lastDevice)
        {
            try
            {
                var    uptime  = (lastDevice?.UptimeInSeconds ?? 0) + (ulong)(buffer.Decompress().Value * 100.0);
                string message = lastDevice?.StatusMessage;
                if (lastDevice == null || buffer.Decompress().Value < 0.1)
                {
                    message = buffer.DecompressString();
                }

                return(new IotDevice(
                           lastDevice.Name,
                           lastDevice.Id,
                           message,
                           buffer.DecompressBoolean(),
                           buffer.DecompressBoolean(),
                           uptime,
                           new PhysicalValue(buffer.Decompress().Value * 5.0, "bar"),
                           new PhysicalValue(buffer.Decompress().Value * 100.0, "°C"),
                           new PhysicalValue(buffer.Decompress().Value * 1000.0, "m")));
            }
            catch (InvalidOperationException ioe)
            {
                if (_debug)
                {
                    Console.WriteLine("Decompression Error" + ioe.ToString());
                }
                return(null);
            }
        }
Пример #2
0
 private void Compress(CompressionBuffer buffer, IotDevice lastDevice, IotDevice device)
 {
     buffer.CompressAndFold(4 + ((lastDevice == null || lastDevice.StatusMessage == null || lastDevice.StatusMessage != device.StatusMessage) ? 10 : 0));
     buffer.CompressAndFold(device.Pressure.Value, 5);
     buffer.CompressAndFold(device.Temperature.Value, 100);
     buffer.CompressAndFold(device.Distance.Value, 1000);
 }
Пример #3
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);
            }
        }
Пример #4
0
 public EntropyMaximizingCodec(bool debug = false)
 {
     _debug          = debug;
     _encodingBuffer = new CompressionBuffer(debug);
     _decodingBuffer = new CompressionBuffer(debug);
 }