Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Command concept #12

Merged
merged 1 commit into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
}
13 changes: 13 additions & 0 deletions src/SmartHomeDotNet.Package/SmartHome/Commands/ICommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Linq;
using SmartHomeDotNet.SmartHome.Devices;

namespace SmartHomeDotNet.SmartHome.Commands
{
/// <summary>
/// Marker interface for commands on <see cref="IDevice"/>
/// </summary>
public interface ICommand
{
}
}
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