-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/SmartHomeDotNet.Package/SmartHome/Commands/CommandParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace SmartHomeDotNet.SmartHome.Commands | ||
{ | ||
/// <summary> | ||
/// A default parser for common commands | ||
/// </summary> | ||
public sealed class CommandParser : ICommandParser | ||
{ | ||
/// <summary> | ||
/// Singleton instance of the parser | ||
/// </summary> | ||
public static ICommandParser Default { get; } = new CommandParser(); | ||
|
||
private CommandParser() | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool TryParse(string value, out ICommand command) | ||
{ | ||
switch (value.ToLowerInvariant()) | ||
{ | ||
case "on": | ||
command = new TurnOn(); | ||
return true; | ||
|
||
case "off": | ||
command = new TurnOff(); | ||
return true; | ||
|
||
default: | ||
command = default; | ||
return false; | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/SmartHomeDotNet.Package/SmartHome/Commands/ICommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Linq; | ||
using SmartHomeDotNet.SmartHome.Devices; | ||
using SmartHomeDotNet.Utils; | ||
|
||
namespace SmartHomeDotNet.SmartHome.Commands | ||
{ | ||
/// <summary> | ||
/// Marker interface for commands on <see cref="IDevice"/> | ||
/// </summary> | ||
public interface ICommand | ||
{ | ||
} | ||
|
||
public static class CommandExtensions | ||
{ | ||
public static AsyncContextOperation TurnOn<T>(this IDevice<T> device) | ||
where T : ISupport<TurnOn> | ||
=> device.Host.Execute(new TurnOn(), device); | ||
|
||
public static AsyncContextOperation TurnOff<T>(this IDevice<T> device) | ||
where T : ISupport<TurnOff> | ||
=> device.Host.Execute(new TurnOff(), device); | ||
} | ||
|
||
public static class CommandParserExtensions | ||
{ | ||
public static ICommand ParseOrDefault(this ICommandParser parser, string command) | ||
=> parser.TryParse(command, out var c) ? c : default; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/SmartHomeDotNet.Package/SmartHome/Commands/ICommandParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace SmartHomeDotNet.SmartHome.Commands | ||
{ | ||
/// <summary> | ||
/// A parser capable to create an instance of an <see cref="ICommand"/> from its string representation | ||
/// </summary> | ||
public interface ICommandParser | ||
{ | ||
/// <summary> | ||
/// Try to parse a command from it string representation | ||
/// </summary> | ||
/// <param name="value">The source string to parse</param> | ||
/// <param name="command">The result command</param> | ||
/// <returns>A bool which indicates if the parsing was successful or not</returns> | ||
bool TryParse(string value, out ICommand command); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/SmartHomeDotNet.Package/SmartHome/Commands/ISupport.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Linq; | ||
using SmartHomeDotNet.SmartHome.Devices; | ||
|
||
namespace SmartHomeDotNet.SmartHome.Commands | ||
{ | ||
/// <summary> | ||
/// Marker interface for an <see cref="IDevice"/> to indicates the supported <see cref="ICommand"/> | ||
/// </summary> | ||
/// <typeparam name="T">Type of the command that is supported by this device</typeparam> | ||
public interface ISupport<T> | ||
where T : ICommand | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace SmartHomeDotNet.SmartHome.Commands | ||
{ | ||
/// <summary> | ||
/// A command to turn off a device | ||
/// </summary> | ||
public struct Toggle : ICommand | ||
{ | ||
/// <summary> | ||
/// The optional duration of the fade out for dimmable devices | ||
/// </summary> | ||
public TimeSpan? Duration { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace SmartHomeDotNet.SmartHome.Commands | ||
{ | ||
/// <summary> | ||
/// A command to turn off a device | ||
/// </summary> | ||
public struct TurnOff : ICommand | ||
{ | ||
/// <summary> | ||
/// The optional duration of the fade out for dimmable devices | ||
/// </summary> | ||
public TimeSpan? Duration { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Drawing; | ||
using System.Linq; | ||
|
||
namespace SmartHomeDotNet.SmartHome.Commands | ||
{ | ||
/// <summary> | ||
/// A command to turn on a device | ||
/// </summary> | ||
public struct TurnOn : ICommand | ||
{ | ||
/// <summary> | ||
/// The optional target color for light | ||
/// </summary> | ||
public Color? Color { get; set; } | ||
|
||
/// <summary> | ||
/// The optional target level for dimmable devices | ||
/// </summary> | ||
public double? Level { get; set; } | ||
|
||
/// <summary> | ||
/// The optional duration of the fade in for dimmable devices | ||
/// </summary> | ||
public TimeSpan? Duration { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters