|
1 | 1 | using CliFx.Attributes;
|
2 |
| -using CliFx.Exceptions; |
3 | 2 | using Meadow.Cloud;
|
4 | 3 | using Meadow.Cloud.Identity;
|
5 | 4 | using Microsoft.Extensions.Logging;
|
6 |
| -using System.Text.Json; |
7 | 5 |
|
8 | 6 | namespace Meadow.CLI.Commands.DeviceManagement;
|
9 | 7 |
|
10 |
| -[Command("cloud command publish", Description = "Publish a command to Meadow devices via the Meadow Service")] |
11 |
| -public class CloudCommandPublishCommand : BaseCloudCommand<CloudCommandPublishCommand> |
12 |
| -{ |
13 |
| - public const string DefaultHost = "https://www.meadowcloud.co"; |
14 |
| - |
15 |
| - [CommandParameter(0, Description = "The name of the command", IsRequired = true, Name = "COMMAND_NAME")] |
16 |
| - public string CommandName { get; set; } |
17 |
| - |
18 |
| - [CommandOption("collectionId", 'c', Description = "The target collection for publishing the command")] |
19 |
| - public string? CollectionId { get; set; } |
20 |
| - |
21 |
| - [CommandOption("deviceIds", 'd', Description = "The target devices for publishing the command")] |
22 |
| - public string[]? DeviceIds { get; set; } |
23 |
| - |
24 |
| - [CommandOption("args", 'a', Description = "The arguments for the command as a JSON string", Converter = typeof(JsonDocumentBindingConverter))] |
25 |
| - public JsonDocument? Arguments { get; set; } |
26 |
| - |
27 |
| - [CommandOption("qos", 'q', Description = "The MQTT-defined quality of service for the command")] |
28 |
| - public QualityOfService QualityOfService { get; set; } = QualityOfService.AtLeastOnce; |
29 |
| - |
30 |
| - [CommandOption("host", Description = "Optionally set a host (default is https://www.meadowcloud.co)")] |
31 |
| - public string? Host { get; set; } |
32 |
| - |
33 |
| - private CommandService CommandService { get; } |
34 |
| - |
35 |
| - public CloudCommandPublishCommand( |
36 |
| - IdentityManager identityManager, |
37 |
| - UserService userService, |
38 |
| - DeviceService deviceService, |
39 |
| - CollectionService collectionService, |
40 |
| - CommandService commandService, |
41 |
| - ILoggerFactory? loggerFactory) |
42 |
| - : base(identityManager, userService, deviceService, collectionService, loggerFactory) |
43 |
| - { |
44 |
| - CommandService = commandService; |
45 |
| - } |
46 |
| - |
47 |
| - protected override async ValueTask ExecuteCommand(CancellationToken? cancellationToken) |
48 |
| - { |
49 |
| - if (string.IsNullOrWhiteSpace(CollectionId) && (DeviceIds == null || DeviceIds.Length == 0)) |
50 |
| - { |
51 |
| - throw new CommandException("Either a collection ID (-c|--collectionId) or a list of device IDs (-d|--deviceIds) must be specified.", showHelp: true); |
52 |
| - } |
53 |
| - |
54 |
| - if (!string.IsNullOrWhiteSpace(CollectionId) && (DeviceIds != null && DeviceIds.Length > 0)) |
55 |
| - { |
56 |
| - throw new CommandException("Cannot specify both a collection ID (-c|--collectionId) and list of device IDs (-d|--deviceIds). Only one is allowed.", showHelp: true); |
57 |
| - } |
58 |
| - |
59 |
| - if (Host == null) Host = DefaultHost; |
60 |
| - |
61 |
| - var token = await IdentityManager.GetAccessToken(cancellationToken); |
62 |
| - if (string.IsNullOrWhiteSpace(token)) |
63 |
| - { |
64 |
| - throw new CommandException("You must be signed into Meadow.Cloud to execute this command. Run 'meadow cloud login' to do so."); |
65 |
| - } |
66 |
| - |
67 |
| - try |
68 |
| - { |
69 |
| - Logger?.LogInformation($"Publishing '{CommandName}' command to Meadow.Cloud. Please wait..."); |
70 |
| - if (!string.IsNullOrWhiteSpace(CollectionId)) |
71 |
| - { |
72 |
| - await CommandService.PublishCommandForCollection(CollectionId, CommandName, Arguments, (int)QualityOfService, Host, cancellationToken); |
73 |
| - } |
74 |
| - else if (DeviceIds.Any()) |
75 |
| - { |
76 |
| - await CommandService.PublishCommandForDevices(DeviceIds, CommandName, Arguments, (int)QualityOfService, Host, cancellationToken); |
77 |
| - } |
78 |
| - else |
79 |
| - { |
80 |
| - throw new CommandException("Cannot specify both a collection ID (-c|--collectionId) and list of device IDs (-d|--deviceIds). Only one is allowed."); |
81 |
| - } |
82 |
| - Logger?.LogInformation("Publish command successful."); |
83 |
| - } |
84 |
| - catch (MeadowCloudAuthException ex) |
85 |
| - { |
86 |
| - throw new CommandException("You must be signed in to execute this command.", innerException: ex); |
87 |
| - } |
88 |
| - catch (MeadowCloudException ex) |
89 |
| - { |
90 |
| - throw new CommandException($"Publish command failed: {ex.Message}", innerException: ex); |
91 |
| - } |
92 |
| - } |
93 |
| -} |
94 |
| - |
95 | 8 | [Command("cloud collection list", Description = "List Meadow Collections")]
|
96 | 9 | public class CloudCollectionListCommand : BaseCloudCommand<CloudCollectionListCommand>
|
97 | 10 | {
|
|
0 commit comments