Skip to content

Commit

Permalink
Moved Message stringification to Messages
Browse files Browse the repository at this point in the history
- Fixed ModeIndex parameter

Closes #10
  • Loading branch information
tthiery committed Jun 14, 2020
1 parent 97ad6bf commit b65fb24
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 44 deletions.
43 changes: 6 additions & 37 deletions src/SharpBrick.PoweredUp/Functions/TraceMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,45 +49,14 @@ void TraceMessage(PoweredUpMessage message)

private static string MessageToString(PoweredUpMessage message)
{
string messageAsString = message switch
var result = message.ToString();

if (result.Contains(message.GetType().Name))
{
HubPropertyMessage<Version> msg => $"Hub Property - {msg.Property}: {msg.Payload}",
HubPropertyMessage<string> msg => $"Hub Property - {msg.Property}: {msg.Payload}",
HubPropertyMessage<bool> msg => $"Hub Property - {msg.Property}: {msg.Payload}",
HubPropertyMessage<sbyte> msg => $"Hub Property - {msg.Property}: {msg.Payload}",
HubPropertyMessage<byte> msg => $"Hub Property - {msg.Property}: {msg.Payload}",
HubPropertyMessage<byte[]> msg => $"Hub Property - {msg.Property}: {BytesStringUtil.DataToString(msg.Payload)}",
HubActionMessage msg => $"Hub Action - {msg.Action}",
HubAttachedIOForAttachedDeviceMessage msg => $"Attached IO - Port {msg.PortId} of type {msg.IOTypeId} (HW: {msg.HardwareRevision} / SW: {msg.SoftwareRevision})",
HubAttachedIOForAttachedVirtualDeviceMessage msg => $"Attached Virtual IO - Port {msg.PortId} with A {msg.PortAId} / B {msg.PortBId} of type {msg.IOTypeId}",
HubAttachedIOForDetachedDeviceMessage msg => $"Dettached IO - Port {msg.PortId}",
GenericErrorMessage msg => $"Error - {msg.ErrorCode} from {(MessageType)msg.CommandType}",
PortInformationForModeInfoMessage msg => $"Port Information - Port {msg.PortId} Total Modes {msg.TotalModeCount} / Capabilities Output:{msg.OutputCapability}, Input:{msg.InputCapability}, LogicalCombinable:{msg.LogicalCombinableCapability}, LogicalSynchronizable:{msg.LogicalSynchronizableCapability} / InputModes: {msg.InputModes:X}, OutputModes: {msg.InputModes:X}",
PortInformationForPossibleModeCombinationsMessage msg => $"Port Information (Combinations) - Port {msg.PortId} Combinations: {string.Join(",", msg.ModeCombinations.Select(x => x.ToString("X")))}",
PortValueSingleMessage msg => "Port Values - " + string.Join(";", msg.Data.Select(d => d switch
{
PortValueData<sbyte> dd => $"Port {dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<short> dd => $"Port {dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<int> dd => $"Port {dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<float> dd => $"Port {dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
_ => "Undefined Data Type",
})),
PortValueCombinedModeMessage msg => $"Port Value (Combined Mode) - Port {msg.PortId} " + string.Join(";", msg.Data.Select(d => d switch
{
PortValueData<sbyte> dd => $"Mode {dd.ModeIndex}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<short> dd => $"Mode {dd.ModeIndex}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<int> dd => $"Mode {dd.ModeIndex}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<float> dd => $"Mode {dd.ModeIndex}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
_ => "Undefined Data Type",
})),
PortInputFormatSingleMessage msg => $"Port Input Format (Single) - Port {msg.PortId}, Mode {msg.Mode}, Threshold {msg.DeltaInterval}, Notification {msg.NotificationEnabled}",
PortInputFormatCombinedModeMessage msg => $"Port Input Format (Combined Mode) - Port {msg.PortId} UsedCombinationIndex {msg.UsedCombinationIndex} Enabled {msg.MultiUpdateEnabled} Configured Modes {string.Join(",", msg.ConfiguredModeDataSetIndex)}",
PortOutputCommandFeedbackMessage msg => $"Port Command Feedback - " + string.Join(",", msg.Feedbacks.Select(f => $"Port {f.PortId} -> {f.Feedback}")),
UnknownMessage msg => $"Unknown Message Type: {(MessageType)msg.MessageType} Length: {msg.Length} Content: {BytesStringUtil.DataToString(msg.Data)}",
var unknown => $"{unknown.MessageType} (not yet formatted)",
};
result = $"{message.MessageType} in {message} (not yet formatted)";
}

return messageAsString;
return result;
}
}
}
4 changes: 2 additions & 2 deletions src/SharpBrick.PoweredUp/Knowledge/KnowledgeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public static Task ApplyDynamicProtocolKnowledge(PoweredUpMessage message, Proto

case PortInputFormatSingleMessage msg:
port = knowledge.Port(msg.PortId);
mode = knowledge.PortMode(msg.PortId, msg.Mode);
mode = knowledge.PortMode(msg.PortId, msg.ModeIndex);

port.LastFormattedPortMode = msg.Mode;
port.LastFormattedPortMode = msg.ModeIndex;

mode.DeltaInterval = msg.DeltaInterval;
mode.NotificationEnabled = msg.NotificationEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public PoweredUpMessage Decode(in Span<byte> data)
=> new PortInputFormatSingleMessage()
{
PortId = data[0],
Mode = data[1],
ModeIndex = data[1],
DeltaInterval = BitConverter.ToUInt32(data.Slice(2, 4)),
NotificationEnabled = (data[6] == 0x01),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ public class GenericErrorMessage : PoweredUpMessage
{
public byte CommandType { get; set; }
public ErrorCode ErrorCode { get; set; }

public override string ToString()
=> $"Error - {ErrorCode} from {(MessageType)CommandType}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ namespace SharpBrick.PoweredUp.Protocol.Messages
public class HubActionMessage : PoweredUpMessage
{
public HubAction Action { get; set; }

public override string ToString()
=> $"Hub Action - {this.Action}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class HubAttachedIOForAttachedDeviceMessage : HubAttachedIOMessage
public DeviceType IOTypeId { get; set; }
public Version HardwareRevision { get; set; }
public Version SoftwareRevision { get; set; }

public override string ToString()
=> $"Attached IO - Port {HubId}/{PortId} of device type {IOTypeId} (HW: {HardwareRevision} / SW: {SoftwareRevision})";
}

// spec chapter: 3.8.1
Expand All @@ -23,9 +26,16 @@ public class HubAttachedIOForAttachedVirtualDeviceMessage : HubAttachedIOMessage
public DeviceType IOTypeId { get; set; }
public byte PortAId { get; set; }
public byte PortBId { get; set; }

public override string ToString()
=> $"Attached Virtual IO - Port {HubId}/{PortId} using ports {PortAId} + {PortBId} of device type {IOTypeId}";
}

// spec chapter: 3.8.1
public class HubAttachedIOForDetachedDeviceMessage : HubAttachedIOMessage
{ }
{

public override string ToString()
=> $"Dettached IO - Port {HubId}{PortId}";
}
}
10 changes: 10 additions & 0 deletions src/SharpBrick.PoweredUp/Protocol/Messages/HubPropertyMessage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using SharpBrick.PoweredUp.Utils;

namespace SharpBrick.PoweredUp.Protocol.Messages
{
// spec chapter: 3.5.2
Expand All @@ -10,5 +13,12 @@ public class HubPropertyMessage : PoweredUpMessage
public class HubPropertyMessage<TPayload> : HubPropertyMessage
{
public TPayload Payload { get; set; }

public override string ToString()
=> this switch
{
HubPropertyMessage<byte[]> msg => $"Hub Property - {msg.Property}: {BytesStringUtil.DataToString(msg.Payload)}",
_ => $"Hub Property - {this.Property}: {this.Payload}",
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SharpBrick.PoweredUp.Protocol.Messages
using System.Linq;

namespace SharpBrick.PoweredUp.Protocol.Messages
{
// spec chapter: 3.19.1
public abstract class PortInformationMessage : PoweredUpMessage
Expand All @@ -18,11 +20,17 @@ public class PortInformationForModeInfoMessage : PortInformationMessage
public byte TotalModeCount { get; set; }
public ushort InputModes { get; set; }
public ushort OutputModes { get; set; }

public override string ToString()
=> $"Port Information - Port {HubId}/{PortId} Total Modes {TotalModeCount} / Capabilities Output:{OutputCapability}, Input:{InputCapability}, LogicalCombinable:{LogicalCombinableCapability}, LogicalSynchronizable:{LogicalSynchronizableCapability} / InputModes: {InputModes:X}, OutputModes: {InputModes:X}";
}

// spec chapter: 3.19.1
public class PortInformationForPossibleModeCombinationsMessage : PortInformationMessage
{
public ushort[] ModeCombinations { get; set; }

public override string ToString()
=> $"Port Information (Combinations) - Port {HubId}/{PortId} Combinations: {string.Join(",", ModeCombinations.Select(x => x.ToString("X")))}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ public class PortInputFormatCombinedModeMessage : PoweredUpMessage
public byte UsedCombinationIndex { get; set; }
public bool MultiUpdateEnabled { get; set; }
public int[] ConfiguredModeDataSetIndex { get; set; }

public override string ToString()
=> $"Port Input Format (Combined Mode) - Port {HubId}/{PortId} UsedCombinationIndex {UsedCombinationIndex} Enabled {MultiUpdateEnabled} Configured Modes {string.Join(",", ConfiguredModeDataSetIndex)}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
public class PortInputFormatSingleMessage : PoweredUpMessage
{
public byte PortId { get; set; }
public byte Mode { get; set; }
public byte ModeIndex { get; set; }
public uint DeltaInterval { get; set; }
public bool NotificationEnabled { get; set; }

public override string ToString()
=> $"Port Input Format (Single) - Mode {HubId}/{PortId}/{ModeIndex}: Threshold {DeltaInterval}, Notification {NotificationEnabled}";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System.Linq;

namespace SharpBrick.PoweredUp.Protocol.Messages
{
public class PortOutputCommandFeedbackMessage : PoweredUpMessage
{
public PortOutputCommandFeedback[] Feedbacks { get; set; }

public override string ToString()
=> $"Port Output Command Feedback - " + string.Join(",", this.Feedbacks.Select(f => $"Port {this.HubId}/{f.PortId} -> {f.Feedback}"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ public class PortValueCombinedModeMessage : PoweredUpMessage
public byte PortId { get; set; }

public PortValueData[] Data { get; set; }

public override string ToString()
=> $"Port Values (Combined Mode) - " + PortValueSingleMessage.FormatPortValueDataArray(HubId, Data);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
using System.Linq;

namespace SharpBrick.PoweredUp.Protocol.Messages
{
public class PortValueSingleMessage : PoweredUpMessage
{
public PortValueData[] Data { get; set; }

public static string FormatPortValueDataArray(byte hubId, in PortValueData[] data)
=> string.Join(";", data.Select(d => d switch
{
PortValueData<sbyte> dd => $"Mode {hubId}/{dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<short> dd => $"Mode {hubId}/{dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<int> dd => $"Mode {hubId}/{dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
PortValueData<float> dd => $"Mode {hubId}/{dd.PortId}/{dd.ModeIndex}: {string.Join(",", dd.InputValues)} ({dd.DataType})",
_ => "Undefined Data Type",
}));

public override string ToString()
=> "Port Values - " + FormatPortValueDataArray(HubId, Data);
}
}
5 changes: 5 additions & 0 deletions src/SharpBrick.PoweredUp/Protocol/Messages/UnknownMessage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using SharpBrick.PoweredUp.Utils;

namespace SharpBrick.PoweredUp.Protocol.Messages
{
public class UnknownMessage : PoweredUpMessage
{
public byte[] Data { get; set; }

public override string ToString()
=> $"Unknown Message Type: {(MessageType)this.MessageType} Length: {this.Length} Content: {BytesStringUtil.DataToString(this.Data)}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void PortInputFormatSingleEncoder_Decode(string dataAsString, byte expect

// assert
Assert.Equal(expectedPortId, message.PortId);
Assert.Equal(expectedMode, message.Mode);
Assert.Equal(expectedMode, message.ModeIndex);
Assert.Equal(expectedDeltaInterval, message.DeltaInterval);
Assert.Equal(expectedNotificationEnabled, message.NotificationEnabled);
}
Expand Down

0 comments on commit b65fb24

Please sign in to comment.