-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from WildernessLabs/cloud-command-publish
This adds the ability to publish commands to a collection of devices via Meadow.Cloud using Meadow.Cloud's new Command & Control publish command feature.
- Loading branch information
Showing
12 changed files
with
252 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Meadow.CLI.Core.Exceptions; | ||
using Meadow.CLI.Core.Identity; | ||
using Microsoft.Extensions.Configuration; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Meadow.CLI.Core.CloudServices | ||
{ | ||
public class CommandService : CloudServiceBase | ||
{ | ||
readonly IConfiguration _config; | ||
readonly IdentityManager _identityManager; | ||
|
||
public CommandService(IConfiguration config, IdentityManager identityManager) : base(identityManager) | ||
{ | ||
_config = config; | ||
_identityManager = identityManager; | ||
} | ||
|
||
public async Task PublishCommandForCollection( | ||
string collectionId, | ||
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 | ||
{ | ||
commandName, | ||
args = arguments, | ||
qos = qualityOfService | ||
}; | ||
var content = new StringContent(JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json"); | ||
var response = await httpClient.PostAsync($"{host}/api/collections/{collectionId}/commands", content, cancellationToken); | ||
|
||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var message = await response.Content.ReadAsStringAsync(); | ||
throw new MeadowCloudException(message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.