Skip to content

Commit

Permalink
Add ICommandHandler::ConnectorUpdateShort() as a way to distinguish…
Browse files Browse the repository at this point in the history
… using a short ID string vs. the full connector ID. Adds associated distinct class and a common base class for both ConnectorUpdate* derivatives.
  • Loading branch information
mpaperno committed Feb 18, 2022
1 parent 1f431d0 commit 0610921
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
14 changes: 14 additions & 0 deletions TouchPortalSDK/Clients/TouchPortalClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ bool ICommandHandler.ConnectorUpdate(string connectorId, int value)
return SendCommand(command);
}

/// <inheritdoc cref="ICommandHandler" />
bool ICommandHandler.ConnectorUpdateShort(string shortId, int value)
{
if (string.IsNullOrWhiteSpace(shortId))
return false;

if (value < 0 || value > 100)
return false;

var command = new ConnectorUpdateShortCommand(_eventHandler.PluginId, shortId, value);

return SendCommand(command);
}

public bool SendCommand<TCommand>(TCommand command, [CallerMemberName]string callerMemberName = "")
where TCommand : ITouchPortalMessage
{
Expand Down
17 changes: 16 additions & 1 deletion TouchPortalSDK/Interfaces/ICommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ public interface ICommandHandler
/// <returns></returns>
bool ShowNotification(string notificationId, string title, string message, NotificationOptions[] notificationOptions);

/// <summary>
/// Sends a connector value update to Touch Portal using the long form of the connector ID.
/// </summary>
/// <param name="connectorId">The long ID of the connector to update. The string "pc_{pluginId}_" is automatically prepended
/// before sending to TP. The total length must not exceed 200 chars.</param>
/// <param name="value">The value to send, must be between 0 and 100, inclusive.</param>
/// <returns>true on success, false otherwise.</returns>
bool ConnectorUpdate(string connectorId, int value);

/// <summary>
/// Sends a connector value update to Touch Portal using the short form of the connector ID.
/// </summary>
/// <param name="shortId">The short ID of the connector to update. This is obtained from a <see cref="ShortConnectorIdNotification"/> event.</param>
/// <param name="value">The value to send, must be between 0 and 100, inclusive.</param>
/// <returns>true on success, false otherwise.</returns>
bool ConnectorUpdateShort(string shortId, int value);
}
}
}
30 changes: 4 additions & 26 deletions TouchPortalSDK/Messages/Commands/ConnectorUpdateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,13 @@
using System;
using System.Linq;
using System.Text;
using TouchPortalSDK.Interfaces;
using TouchPortalSDK.Messages.Models;


namespace TouchPortalSDK.Messages.Commands
{
public class ConnectorUpdateCommand : ITouchPortalMessage
public class ConnectorUpdateCommand : ConnectorUpdateCommandBase
{
public string Type => "connectorUpdate";

public string ConnectorId { get; set; }

public int Value { get; set; }
public string ConnectorId { get { return Id; } set { Id = value; } }

public ConnectorUpdateCommand(string pluginId, string connectorId, int value)
public ConnectorUpdateCommand(string pluginId, string connectorId, int value) : base(pluginId, $"pc_{pluginId}_{connectorId}", value)
{
if (string.IsNullOrWhiteSpace(pluginId))
throw new ArgumentNullException(nameof(pluginId));

if (string.IsNullOrWhiteSpace(connectorId))
throw new ArgumentNullException(nameof(connectorId));

if (value < 0 || value > 100)
throw new ArgumentException("Value must be between 0 and 100", nameof(value));

ConnectorId = $"pc_{pluginId}_{connectorId}";
Value = value;
}

public Identifier GetIdentifier()
=> new Identifier(Type, ConnectorId, default);
}
}
35 changes: 35 additions & 0 deletions TouchPortalSDK/Messages/Commands/ConnectorUpdateCommandBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Linq;
using System.Text;
using TouchPortalSDK.Interfaces;
using TouchPortalSDK.Messages.Models;

namespace TouchPortalSDK.Messages.Commands
{
public abstract class ConnectorUpdateCommandBase : ITouchPortalMessage
{
public string Type => "connectorUpdate";

public string Id { get; set; }

public int Value { get; set; }

public ConnectorUpdateCommandBase(string pluginId, string id, int value)
{
if (string.IsNullOrWhiteSpace(pluginId))
throw new ArgumentNullException(nameof(pluginId));

if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));

if (value < 0 || value > 100)
throw new ArgumentException("Value must be between 0 and 100", nameof(value));

Id = id;
Value = value;
}

public Identifier GetIdentifier()
=> new Identifier(Type, Id, default);
}
}
12 changes: 12 additions & 0 deletions TouchPortalSDK/Messages/Commands/ConnectorUpdateShortCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace TouchPortalSDK.Messages.Commands
{
public class ConnectorUpdateShortCommand : ConnectorUpdateCommandBase
{
public string ShortId { get { return Id; } set { Id = value; } }

public ConnectorUpdateShortCommand(string pluginId, string shortId, int value) : base(pluginId, shortId, value) {

}
}
}

0 comments on commit 0610921

Please sign in to comment.