protected override void ReadImplementation(IResponseReader responseReader, ushort size) { if (size == 3) { Value = (Capabilities) new Capabilities { MaxRxFrame = (byte)responseReader.ReadByte(), MaxTxFrame = (byte)responseReader.ReadByte(), MinInterframe = (byte)responseReader.ReadByte() }; } else if (size == 2) { Value = (Capabilities) new Capabilities(); for (int i = 0; i < 2; i++) { Value.Code0x01[i] = (byte)responseReader.ReadByte(); } } else if (size == 11) { Value = (Capabilities) new Capabilities(); for (int i = 0; i < 11; i++) { Value.Code0x02[i] = (byte)responseReader.ReadByte(); } } Value = Value; }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { byte hour = (byte)responseReader.ReadByte(); byte minute = (byte)responseReader.ReadByte(); byte second = (byte)responseReader.ReadByte(); Value = (TimeSpan?)new TimeSpan(hour, minute, second); }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { Value = new UserInfo { Weight = responseReader.ReadUShort(), Units = (UnitType)responseReader.ReadByte(), Age = responseReader.ReadByte(), Gender = (Gender)responseReader.ReadByte() }; }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { Value = (ProductVersion?)new ProductVersion { ManufacturerId = (byte)responseReader.ReadByte(), CID = (byte)responseReader.ReadByte(), Model = (byte)responseReader.ReadByte(), HardwareVersion = (ushort)responseReader.ReadUShort(), SoftwareVersion = (ushort)responseReader.ReadUShort() }; }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { Value = (StrokeStatistics?)new StrokeStatistics() { Distance = (ushort)responseReader.ReadUShort(), DriveTime = (byte)responseReader.ReadByte(), RecoveryTime = (ushort)responseReader.ReadUShort(), Length = (byte)responseReader.ReadByte(), Count = (ushort)responseReader.ReadUShort(), Peak = (ushort)responseReader.ReadUShort(), ImpulseForce = (ushort)responseReader.ReadUShort(), AverageForce = (ushort)responseReader.ReadUShort(), WorkPerStroke = (ushort)responseReader.ReadUShort() }; }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { uint wholeTime = responseReader.ReadUInt(); uint fractionalTime = (byte)responseReader.ReadByte(); Value = (decimal?)Convert.ToDecimal(wholeTime + (fractionalTime * 0.01)); }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { byte[] serialBytes = new byte[9]; for (int i = 0; i < 9; i++) { serialBytes[i] = (byte)responseReader.ReadByte(); } Value = (string?)System.Text.Encoding.ASCII.GetString(serialBytes); }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { byte size = (byte)responseReader.ReadByte(); List <ushort> heartbeats = new List <ushort>(); for (int i = 0; i < size; i += 2) { heartbeats.Add((ushort)responseReader.ReadUShort()); } Value = (ushort[]?)heartbeats.ToArray(); }
public void Read(IResponseReader responseReader) { if (ResponseSize == 0) { return; } ushort size = (ushort)responseReader.ReadByte(); if (size == ResponseSize) { ReadImplementation(responseReader, size); } else { throw new InvalidOperationException($"Unexpected size. Encountered size [{size}] and expected [{ResponseSize}]."); } }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { Value = (DisplayUnitsType?)responseReader.ReadByte(); }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { Value = (OperationalState?)responseReader.ReadByte(); }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { Value = (ushort?)responseReader.ReadUShort(); responseReader.ReadByte(); // TODO: Units }
protected override void ReadImplementation(IResponseReader responseReader, ushort _) { Value = (IntervalType?)responseReader.ReadByte(); }
// TODO: Refactor read into something smaller /// <inheritdoc /> public bool Read(IResponseReader reader) { if (IsOpen) { throw new InvalidOperationException("Attempting to read set before it has been prepared."); } if (reader.Count == 0) { throw new InvalidOperationException("Invalid response. Response was completely empty."); } uint reportId = reader.ReadByte(); if (reader.Count < 1) { throw new InvalidOperationException("No data was returned"); } uint startFlag = reader.ReadByte(); uint destination; uint source; if (startFlag == (byte)FrameCommands.EXTENDED_START_FLAG) { if (reader.Count < 4) { throw new InvalidOperationException("Improperly formatted frame."); } destination = reader.ReadByte(); source = reader.ReadByte(); } else if (startFlag != (byte)FrameCommands.STANDARD_START_FLAG) { throw new InvalidOperationException("No start flag was found"); } int stopFrameFlagIndex = reader.Position; uint checksum; stopFrameFlagIndex = ((List <uint>)reader).IndexOf((byte)FrameCommands.STOP_FRAME_FLAG, stopFrameFlagIndex + 1); if (stopFrameFlagIndex == -1) { InvalidOperationException exception = new ("Improperly formatted frame. No stop flag was found."); _logger.LogError(exception, "No stop flag found."); throw exception; } // Unstuff bytes for (int index = reader.Position; index < stopFrameFlagIndex - 1; index++) { if (reader[index] == 0xF3) { reader.RemoveAt(index); stopFrameFlagIndex--; reader[index] += 0xF0; } } checksum = reader.Skip(reader.Position).Take(stopFrameFlagIndex - reader.Position - 1).Aggregate <uint, uint>(0, _calculateChecksum); if (checksum != reader[stopFrameFlagIndex - 1]) { InvalidOperationException exception = new("Improperly formatted frame. Checksum did not match."); _logger.LogError(exception, "Checksum did not match."); throw exception; } // Remove checksum and end flag reader.Truncate(stopFrameFlagIndex - 1); if (reader.Position == reader.Size) { return(true); } uint status = reader.ReadByte(); int listHighWaterMark = 0; do { uint commandCode = reader.ReadByte(); if (_wrapperCommands.Contains(commandCode)) { uint size = reader.ReadByte(); int startIndex = reader.Position; while (reader.Position < startIndex + size) { uint proprietaryCommandCode = reader.ReadByte(); ICommand wrappedCommand = this.Skip(listHighWaterMark).First(command => command.Code == proprietaryCommandCode && command.Wrapper == commandCode); listHighWaterMark = IndexOf(wrappedCommand) + 1; if (wrappedCommand is GetCommand) { try { wrappedCommand.Read(reader); } catch (Exception e) { _logger.LogError(e, "Exception occurred while reading command [{CommandName}]", wrappedCommand.Name); throw; } } } continue; } ICommand command = this.Skip(listHighWaterMark).First(command => command.Code == commandCode && command.Wrapper == null); listHighWaterMark = IndexOf(command) + 1; if (command is GetCommand) { try { command.Read(reader); } catch (Exception e) { _logger.LogError(e, "Exception occurred while reading command [{CommandName}]", command.Name); throw; } } } while (reader.Position < reader.Size); // Ensure whole response has been read bool success = reader.Position == reader.Size; return(success); }