Skip to content

Commit

Permalink
Add ability to publish commands by device ID via Meadow.Cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenkuhn committed Aug 24, 2023
1 parent e65cc13 commit 7bf211a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
32 changes: 32 additions & 0 deletions Meadow.CLI.Core/CloudServices/CommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,37 @@ public async Task PublishCommandForCollection(
throw new MeadowCloudException(message);
}
}

public async Task PublishCommandForDevices(
string[] deviceIds,
string commandName,
JsonDocument? arguments = null,
int qualityOfService = 0,
string? host = null,
CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(host))
{
host = _config[Constants.MEADOW_CLOUD_HOST_CONFIG_NAME];
}

var httpClient = await GetAuthenticatedHttpClient(cancellationToken);

var payload = new
{
deviceIds,
commandName,
args = arguments,
qos = qualityOfService
};
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync($"{host}/api/devices/commands", content, cancellationToken);

if (!response.IsSuccessStatusCode)
{
var message = await response.Content.ReadAsStringAsync();
throw new MeadowCloudException(message);
}
}
}
}
29 changes: 27 additions & 2 deletions Meadow.CLI/Commands/Cloud/Command/PublishCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Meadow.CLI.Core.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

Expand Down Expand Up @@ -38,9 +39,12 @@ public PublishCommand(ILoggerFactory loggerFactory, CommandService commandServic
[CommandParameter(0, Description = "The name of the command", IsRequired = true, Name = "COMMAND_NAME")]
public string CommandName { get; set; }

[CommandOption("collectionId", 'c', Description = "The target collection for publishing the command", IsRequired = true)]
[CommandOption("collectionId", 'c', Description = "The target collection for publishing the command")]
public string CollectionId { get; set; }

[CommandOption("deviceIds", 'd', Description = "The target devices for publishing the command")]
public string[] DeviceIds { get; set; }

[CommandOption("args", 'a', Description = "The arguments for the command as a JSON string", Converter = typeof(JsonDocumentBindingConverter))]
public JsonDocument Arguments { get; set; }

Expand All @@ -52,6 +56,16 @@ public PublishCommand(ILoggerFactory loggerFactory, CommandService commandServic

public async ValueTask ExecuteAsync(IConsole console)
{
if (string.IsNullOrWhiteSpace(CollectionId) && (DeviceIds == null || DeviceIds.Length == 0))
{
throw new CommandException("Either a collection ID (-c|--collectionId) or a list of device IDs (-d|--deviceIds) must be specified.", showHelp: true);
}

if (!string.IsNullOrWhiteSpace(CollectionId) && (DeviceIds != null && DeviceIds.Length > 0))
{
throw new CommandException("Cannot specify both a collection ID (-c|--collectionId) and list of device IDs (-d|--deviceIds). Only one is allowed.", showHelp: true);
}

var cancellationToken = console.RegisterCancellationHandler();

await Task.Yield();
Expand All @@ -65,7 +79,18 @@ public async ValueTask ExecuteAsync(IConsole console)
try
{
_logger.LogInformation($"Publishing '{CommandName}' command to Meadow.Cloud. Please wait...");
await _commandService.PublishCommandForCollection(CollectionId, CommandName, Arguments, (int)QualityOfService, Host, cancellationToken);
if (!string.IsNullOrWhiteSpace(CollectionId))
{
await _commandService.PublishCommandForCollection(CollectionId, CommandName, Arguments, (int)QualityOfService, Host, cancellationToken);
}
else if (DeviceIds.Any())
{
await _commandService.PublishCommandForDevices(DeviceIds, CommandName, Arguments, (int)QualityOfService, Host, cancellationToken);
}
else
{
throw new CommandException("Cannot specify both a collection ID (-c|--collectionId) and list of device IDs (-d|--deviceIds). Only one is allowed.");
}
_logger.LogInformation("Publish command successful.");
}
catch (MeadowCloudAuthException ex)
Expand Down

0 comments on commit 7bf211a

Please sign in to comment.