Skip to content

Commit

Permalink
Converted Messages to Records
Browse files Browse the repository at this point in the history
- Untested
- Switched Protocol Core from netstandard2.1 to net5.0.
  Dropping .NET 3.1 support completely

#42
  • Loading branch information
tthiery committed Mar 6, 2021
1 parent 5ffa619 commit 30c0be0
Show file tree
Hide file tree
Showing 70 changed files with 741 additions and 1,049 deletions.
2 changes: 1 addition & 1 deletion src/SharpBrick.PoweredUp.Cli/Commands/DevicesList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task ExecuteAsync(SystemType knownSystemType)

await discoverPorts.ExecuteAsync();

await protocol.SendMessageReceiveResultAsync<HubActionMessage>(new HubActionMessage() { HubId = 0, Action = HubAction.SwitchOffHub }, result => result.Action == HubAction.HubWillSwitchOff);
await protocol.SendMessageReceiveResultAsync<HubActionMessage>(new HubActionMessage(HubAction.SwitchOffHub) { HubId = 0 }, result => result.Action == HubAction.HubWillSwitchOff);

Console.WriteLine(string.Empty);

Expand Down
19 changes: 4 additions & 15 deletions src/SharpBrick.PoweredUp.Cli/Commands/DumpStaticPortInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async Task ExecuteAsync(SystemType knownSystemType, byte portId, bool hea

await discoverPorts.ExecuteAsync(portId);

await protocol.SendMessageReceiveResultAsync<HubActionMessage>(new HubActionMessage() { HubId = 0, Action = HubAction.SwitchOffHub }, result => result.Action == HubAction.HubWillSwitchOff);
await protocol.SendMessageReceiveResultAsync<HubActionMessage>(new HubActionMessage(HubAction.SwitchOffHub) { HubId = 0 }, result => result.Action == HubAction.HubWillSwitchOff);

Console.WriteLine(string.Empty);

Expand Down Expand Up @@ -63,32 +63,21 @@ private byte[] CreateAttachedIOHeader(byte portId)

return MessageEncoder.Encode(portInfo switch
{
{ IsVirtual: false } => new HubAttachedIOForAttachedDeviceMessage()
{ IsVirtual: false } => new HubAttachedIOForAttachedDeviceMessage(portId, portInfo.IOTypeId, portInfo.HardwareRevision, portInfo.SoftwareRevision)
{
HubId = 0,
PortId = portId,
IOTypeId = portInfo.IOTypeId,
HardwareRevision = portInfo.HardwareRevision,
SoftwareRevision = portInfo.SoftwareRevision,
},
{ IsVirtual: true } => new HubAttachedIOForAttachedVirtualDeviceMessage()
{ IsVirtual: true } => new HubAttachedIOForAttachedVirtualDeviceMessage(portId, portInfo.IOTypeId, portInfo.PortAId, portInfo.PortBId)
{
HubId = 0,
PortId = portId,
IOTypeId = portInfo.IOTypeId,
PortAId = portInfo.PortAId,
PortBId = portInfo.PortBId,
}
}, protocol.Knowledge);
}

private byte[] CreateSystemTypeHeader(SystemType knownSystemType)
=> MessageEncoder.Encode(new HubPropertyMessage<SystemType>()
=> MessageEncoder.Encode(new HubPropertyMessage<SystemType>(HubProperty.SystemTypeId, HubPropertyOperation.Update, knownSystemType)
{
HubId = 0,
Property = HubProperty.SystemTypeId,
Operation = HubPropertyOperation.Update,
Payload = knownSystemType,
}, protocol.Knowledge);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net5.0-windows10.0.19041.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp3.1'" Include="Microsoft.Windows.SDK.Contracts" Version="10.0.19041.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SharpBrick.PoweredUp\SharpBrick.PoweredUp.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,9 @@ public Task WriteUpstreamAsync(params byte[] message)
public List<byte[]> DownstreamMessages { get; } = new List<byte[]>();

public Task AttachIO(DeviceType deviceType, byte hubId, byte portId, Version hw, Version sw)
=> WriteUpstreamAsync(MessageEncoder.Encode(new HubAttachedIOForAttachedDeviceMessage()
=> WriteUpstreamAsync(MessageEncoder.Encode(new HubAttachedIOForAttachedDeviceMessage(portId, deviceType, hw, sw)
{
HubId = hubId,
PortId = portId,
IOTypeId = deviceType,
HardwareRevision = hw,
SoftwareRevision = sw,
}, null));
}
}
36 changes: 17 additions & 19 deletions src/SharpBrick.PoweredUp/Devices/AbsoluteMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@ public async Task<PortFeedback> GotoPositionAsync(int absolutePosition, sbyte sp
AssertValidMaxPower(maxPower, nameof(maxPower));
AssertIsConnected();

var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandGotoAbsolutePositionMessage()
var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandGotoAbsolutePositionMessage(
_portId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
absolutePosition,
speed,
maxPower,
endState,
profile)
{
HubId = _hubId,
PortId = _portId,
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
AbsolutePosition = absolutePosition,
Speed = speed,
MaxPower = maxPower,
EndState = endState,
Profile = profile,
});

return response;
Expand Down Expand Up @@ -85,18 +84,17 @@ public async Task<PortFeedback> GotoPositionAsync(int absolutePosition1, int abs
AssertIsConnected();
AssertIsVirtualPort();

var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandGotoAbsolutePosition2Message()
var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandGotoAbsolutePosition2Message(
_portId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
absolutePosition1, absolutePosition2,
speed,
maxPower,
endState,
profile
)
{
HubId = _hubId,
PortId = _portId,
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
AbsolutePosition1 = absolutePosition1,
AbsolutePosition2 = absolutePosition2,
Speed = speed,
MaxPower = maxPower,
EndState = endState,
Profile = profile,
});

return response;
Expand Down
21 changes: 10 additions & 11 deletions src/SharpBrick.PoweredUp/Devices/BasicMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public async Task<PortFeedback> StartPowerAsync(sbyte power)
AssertValidPower(power, nameof(power));
AssertIsConnected();

var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPowerMessage()
var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPowerMessage(
_portId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
power
)
{
HubId = _hubId,
PortId = _portId,
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
Power = power,
});

return response;
Expand Down Expand Up @@ -86,14 +86,13 @@ public async Task<PortFeedback> StartPowerAsync(sbyte powerOnMotor1, sbyte power
AssertIsConnected();
AssertIsVirtualPort();

var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPower2Message()
var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPower2Message(
_portId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
powerOnMotor1, powerOnMotor2
)
{
HubId = _hubId,
PortId = _portId,
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
Power1 = powerOnMotor1,
Power2 = powerOnMotor2,
});

return response;
Expand Down
58 changes: 30 additions & 28 deletions src/SharpBrick.PoweredUp/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ public async Task SetupNotificationAsync(byte modeIndex, bool enabled, uint delt
deltaInterval = GetDefaultDeltaInterval(modeIndex);
}

await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage()
await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage(
_portId,
modeIndex,
deltaInterval,
enabled
)
{
HubId = _hubId,
PortId = _portId,
Mode = modeIndex,
DeltaInterval = deltaInterval,
NotificationEnabled = enabled,
});
}

Expand Down Expand Up @@ -123,31 +124,31 @@ public async Task<bool> TryLockDeviceForCombinedModeNotificationSetupAsync(param

if (combinationModeIndex <= 7) // spec chapter 3.18.1 max combination mode index
{
await _protocol.SendMessageAsync(new PortInputFormatSetupCombinedModeMessage()
await _protocol.SendMessageAsync(new PortInputFormatSetupCombinedModeMessage(
_portId,
PortInputFormatSetupCombinedSubCommand.LockDeviceForSetup
)
{
HubId = _hubId,
PortId = _portId,
SubCommand = PortInputFormatSetupCombinedSubCommand.LockDeviceForSetup,
});

// if this needs to be performed after the port formats, cache it for the unlock function
await _protocol.SendMessageAsync(new PortInputFormatSetupCombinedModeForSetModeDataSetMessage()
await _protocol.SendMessageAsync(new PortInputFormatSetupCombinedModeForSetModeDataSetMessage(
_portId,
combinationModeIndex,
modeIndices
.Select(m => _protocol.Knowledge.PortMode(_hubId, _portId, m))
.SelectMany(mode => Enumerable
.Range(0, mode.NumberOfDatasets)
.Select(dataSetPosition => new PortInputFormatSetupCombinedModeModeDataSet()
{
Mode = mode.ModeIndex,
DataSet = (byte)dataSetPosition,
}))
.ToArray() // manage DataSet for device which has (A) multiple modes and (B) returns for a mode more than one data set (e.g. R, G, B for color).
)
{
HubId = _hubId,
PortId = _portId,
SubCommand = PortInputFormatSetupCombinedSubCommand.SetModeAndDataSetCombination,

CombinationIndex = combinationModeIndex,
ModeDataSets = modeIndices
.Select(m => _protocol.Knowledge.PortMode(_hubId, _portId, m))
.SelectMany(mode => Enumerable
.Range(0, mode.NumberOfDatasets)
.Select(dataSetPosition => new PortInputFormatSetupCombinedModeModeDataSet()
{
Mode = mode.ModeIndex,
DataSet = (byte)dataSetPosition,
}))
.ToArray(), // manage DataSet for device which has (A) multiple modes and (B) returns for a mode more than one data set (e.g. R, G, B for color).
});

result = true;
Expand All @@ -160,13 +161,14 @@ public async Task UnlockFromCombinedModeNotificationSetupAsync(bool enableUpdate
{
AssertIsConnected();

await _protocol.SendMessageAsync(new PortInputFormatSetupCombinedModeMessage()
await _protocol.SendMessageAsync(new PortInputFormatSetupCombinedModeMessage(
_portId,
enableUpdates
? PortInputFormatSetupCombinedSubCommand.UnlockAndStartWithMultiUpdateEnabled
: PortInputFormatSetupCombinedSubCommand.UnlockAndStartWithMultiUpdateDisabled
)
{
HubId = _hubId,
PortId = _portId,
SubCommand = enableUpdates
? PortInputFormatSetupCombinedSubCommand.UnlockAndStartWithMultiUpdateEnabled
: PortInputFormatSetupCombinedSubCommand.UnlockAndStartWithMultiUpdateDisabled,
});
}

Expand Down
21 changes: 10 additions & 11 deletions src/SharpBrick.PoweredUp/Devices/DuploTrainBaseMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public async Task<PortFeedback> StartPowerAsync(sbyte power)
AssertValidPower(power, nameof(power));
AssertIsConnected();

var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPowerMessage()
var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPowerMessage(
_portId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
power
)
{
HubId = _hubId,
PortId = _portId,
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
Power = power,
});

return response;
Expand Down Expand Up @@ -100,14 +100,13 @@ public async Task<PortFeedback> StartPowerAsync(sbyte powerOnMotor1, sbyte power
AssertIsConnected();
AssertIsVirtualPort();

var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPower2Message()
var response = await _protocol.SendPortOutputCommandAsync(new PortOutputCommandStartPower2Message(
_portId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
powerOnMotor1, powerOnMotor2
)
{
HubId = _hubId,
PortId = _portId,
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
Power1 = powerOnMotor1,
Power2 = powerOnMotor2,
});

return response;
Expand Down
16 changes: 6 additions & 10 deletions src/SharpBrick.PoweredUp/Devices/Mode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public async Task<PortFeedback> WriteDirectModeDataAsync(params byte[] data)
throw new InvalidOperationException("The protocol knowledge declares that this mode cannot be written to (IsOutput = false)");
}

var response = await _protocol.SendPortOutputCommandAsync(new GenericWriteDirectModeDataMessage(_modeInfo.ModeIndex)
var response = await _protocol.SendPortOutputCommandAsync(new GenericWriteDirectModeDataMessage(
_modeInfo.PortId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
_modeInfo.ModeIndex,
data)
{
HubId = _modeInfo.HubId,
PortId = _modeInfo.PortId,
StartupInformation = PortOutputCommandStartupInformation.ExecuteImmediately,
CompletionInformation = PortOutputCommandCompletionInformation.CommandFeedback,
Data = data,
});

return response;
Expand Down Expand Up @@ -99,13 +99,9 @@ public async Task SetupNotificationAsync(bool enabled, uint deltaInterval = 5)
throw new InvalidOperationException("The protocol knowledge declares that this mode cannot be read (IsInput = false)");
}

await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage()
await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage(_modeInfo.PortId, _modeInfo.ModeIndex, deltaInterval, enabled)
{
HubId = _modeInfo.HubId,
PortId = _modeInfo.PortId,
Mode = _modeInfo.ModeIndex,
DeltaInterval = deltaInterval,
NotificationEnabled = enabled,
});
}

Expand Down
Loading

0 comments on commit 30c0be0

Please sign in to comment.