Skip to content

Commit

Permalink
Add the Command concept
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Jun 15, 2019
1 parent a51bc74 commit 419dd8d
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/SmartHomeDotNet.Package/SmartHome/Commands/CommandParser.cs
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 src/SmartHomeDotNet.Package/SmartHome/Commands/ICommand.cs
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 src/SmartHomeDotNet.Package/SmartHome/Commands/ICommandParser.cs
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 src/SmartHomeDotNet.Package/SmartHome/Commands/ISupport.cs
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
{
}
}
16 changes: 16 additions & 0 deletions src/SmartHomeDotNet.Package/SmartHome/Commands/Toggle.cs
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; }
}
}
16 changes: 16 additions & 0 deletions src/SmartHomeDotNet.Package/SmartHome/Commands/TurnOff.cs
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; }
}
}
27 changes: 27 additions & 0 deletions src/SmartHomeDotNet.Package/SmartHome/Commands/TurnOn.cs
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; }
}
}
1 change: 1 addition & 0 deletions src/SmartHomeDotNet.Package/SmartHomeDotNet.Package.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<RootNamespace>SmartHomeDotNet</RootNamespace>
<PackageId>smarthomedotnet</PackageId>
<GeneratePackageOnBuild Condition="$(Configuration)=='Release'">true</GeneratePackageOnBuild>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 419dd8d

Please sign in to comment.