Skip to content

Commit 19afd72

Browse files
committed
Support for properies of type "timespan" and "string".
Issue #91
1 parent 3e96708 commit 19afd72

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

StreamDeckSimHub.Plugin/SimHub/PropertyParser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class PropertyParser
3232
"integer" => PropertyType.Integer,
3333
"long" => PropertyType.Long,
3434
"double" => PropertyType.Double,
35+
"timespan" => PropertyType.TimeSpan,
36+
"string" => PropertyType.String,
3537
"object" => PropertyType.Object,
3638
_ => PropertyType.Double // Should not happen. But best guess should always be "double".
3739
};

StreamDeckSimHub.Plugin/SimHub/PropertyType.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public enum PropertyType
1414
Integer,
1515
Long,
1616
Double,
17+
TimeSpan,
18+
String,
1719
Object
1820
}
1921

@@ -55,6 +57,15 @@ public static class PropertyTypeEx
5557
var result = double.TryParse(propertyValue, NumberStyles.Any, CultureInfo.InvariantCulture, out var doubleResult);
5658
return result ? doubleResult : 0.0d;
5759
}
60+
case PropertyType.TimeSpan:
61+
{
62+
var result = TimeSpan.TryParse(propertyValue, CultureInfo.InvariantCulture, out var timeSpanResult);
63+
return result ? timeSpanResult : null;
64+
}
65+
case PropertyType.String:
66+
{
67+
return propertyValue;
68+
}
5869
case PropertyType.Object:
5970
{
6071
// Try to parse as double.

StreamDeckSimHub.PluginTests/SimHub/PropertyParserTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ public void ParseDouble()
3131
Assert.That(result2.Value.value, Is.InRange(18.33, 18.34));
3232
}
3333

34+
[Test]
35+
public void ParseTimeSpan()
36+
{
37+
var result1 = _parser.ParseLine("Property dcp.gd.SessionTimeLeft timespan (null)");
38+
Assert.That(result1, Is.Not.Null);
39+
Assert.That(result1.Value.name, Is.EqualTo("dcp.gd.SessionTimeLeft"));
40+
Assert.That(result1.Value.type, Is.EqualTo(PropertyType.TimeSpan));
41+
Assert.That(result1.Value.value, Is.Null);
42+
43+
44+
var result2 = _parser.ParseLine("Property dcp.gd.SessionTimeLeft timespan 00:04:10.8570000");
45+
Assert.That(result2, Is.Not.Null);
46+
Assert.That(result2.Value.name, Is.EqualTo("dcp.gd.SessionTimeLeft"));
47+
Assert.That(result2.Value.type, Is.EqualTo(PropertyType.TimeSpan));
48+
Assert.That(result2.Value.value, Is.EqualTo(TimeSpan.FromSeconds(4 * 60 + 10) + TimeSpan.FromMilliseconds(857)));
49+
}
50+
51+
[Test]
52+
public void ParseString()
53+
{
54+
var result1 = _parser.ParseLine("Property dcp.gd.Gear string (null)");
55+
Assert.That(result1, Is.Not.Null);
56+
Assert.That(result1.Value.name, Is.EqualTo("dcp.gd.Gear"));
57+
Assert.That(result1.Value.type, Is.EqualTo(PropertyType.String));
58+
Assert.That(result1.Value.value, Is.Null);
59+
60+
var result2 = _parser.ParseLine("Property dcp.gd.Gear string N");
61+
Assert.That(result2, Is.Not.Null);
62+
Assert.That(result2.Value.name, Is.EqualTo("dcp.gd.Gear"));
63+
Assert.That(result2.Value.type, Is.EqualTo(PropertyType.String));
64+
Assert.That(result2.Value.value, Is.EqualTo("N"));
65+
}
66+
3467
[Test]
3568
public void ParseWhitespace()
3669
{

doc/hotkey/Hotkey.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ Technically, the above syntax is the inner part of a C# format string. For detai
228228
* https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=net-6.0#the-format-item
229229
* https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
230230
* https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
231+
* https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings
232+
* https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings
231233

232234
Here are a few examples for the "Display Format" that should already cover a large number of cases. `_` is used in these examples to indicate a space in the generated output:
233235

0 commit comments

Comments
 (0)