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 ability to publish commands to devices by device ID via Meadow.Cloud #343

Merged
merged 1 commit into from
Aug 24, 2023
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
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
Loading