Ejemplo n.º 1
0
        private byte[] SerializeType(IotDevice obj)
        {
            var bitWriter = new BitWriter();

            var id            = Guid.Parse(obj.Id);
            var statusMessage = obj.StatusMessage.Replace(" ", string.Empty);

            bitWriter.WriteBit(string.Equals(statusMessage, _statusMessage, StringComparison.Ordinal));
            bitWriter.WriteBit(IsEqual(_pressure, obj.Pressure.Value));
            bitWriter.WriteBit(IsEqual(_temperature, obj.Temperature.Value));
            bitWriter.WriteBit(IsEqual(_distance, obj.Distance.Value));

            if (!string.Equals(obj.Name, _name, StringComparison.Ordinal))
            {
                SerializeType(bitWriter, obj.Name);
                _name = obj.Name;
            }

            if (id != _id)
            {
                SerializeType(bitWriter, id);
                _id = id;
            }

            if (!string.Equals(statusMessage, _statusMessage, StringComparison.Ordinal))
            {
                SerializeType(bitWriter, statusMessage);
                _statusMessage = statusMessage;
            }

            SerializeType(bitWriter, obj.SelfCheckPassed);

            SerializeType(bitWriter, obj.ServiceModeEnabled);

            SerializeType(bitWriter, obj.UptimeInSeconds - _uptimeInSeconds, bitLength: 7);
            _uptimeInSeconds = obj.UptimeInSeconds;

            if (!IsEqual(_pressure, obj.Pressure.Value))
            {
                SerializeType(bitWriter, obj.Pressure.Value);
                _pressure = obj.Pressure.Value;
            }

            if (!IsEqual(_temperature, obj.Temperature.Value))
            {
                SerializeType(bitWriter, obj.Temperature.Value);
                _temperature = obj.Temperature.Value;
            }

            if (!IsEqual(_distance, obj.Distance.Value))
            {
                SerializeType(bitWriter, obj.Distance.Value);
                _distance = obj.Distance.Value;
            }

            var bytes = bitWriter.ToArray();

            return(bytes);
        }
        private void SerializeType(BitWriter bitWriter, string value)
        {
            var bytes = Encoding.UTF8.GetBytes(value);

            bitWriter.WriteBytes(bytes);

            var x = new BitReader(bitWriter.ToArray());
            var y = x.ReadBytes(bytes.Length);
            var z = Encoding.UTF8.GetString(y);

            // null terminator
            bitWriter.WriteByte((byte)0);
        }