Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Nov 22, 2023
1 parent d2d573e commit 8be568b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
6 changes: 3 additions & 3 deletions servicetests/test_cases/mqtt3_named_shadow_cfg.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"language": "Javascript",
"runnable_file": "tests/ShadowUpdate",
"runnable_file": "./servicetests/tests/shadow_update",
"runnable_region": "us-east-1",
"runnable_main_class": "shadowUpdate.ShadowUpdate",
"runnable_main_class": "",
"arguments": [
{
"name": "--mqtt_version",
"data": 3
"data": "3"
},
{
"name": "--endpoint",
Expand Down
6 changes: 3 additions & 3 deletions servicetests/test_cases/mqtt5_named_shadow_cfg.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"language": "Javascript",
"runnable_file": "tests/ShadowUpdate",
"runnable_file": "./servicetests/tests/shadow_update",
"runnable_region": "us-east-1",
"runnable_main_class": "shadowUpdate.ShadowUpdate",
"runnable_main_class": "",
"arguments": [
{
"name": "--mqtt_version",
"data": 5
"data": "5"
},
{
"name": "--endpoint",
Expand Down
6 changes: 3 additions & 3 deletions servicetests/test_cases/mqtt5_shadow_cfg.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"language": "Javascript",
"runnable_file": "tests/ShadowUpdate",
"runnable_file": "./servicetests/tests/shadow_update",
"runnable_region": "us-east-1",
"runnable_main_class": "shadowUpdate.ShadowUpdate",
"runnable_main_class": "",
"arguments": [
{
"name": "--mqtt_version",
"data": 5
"data": "5"
},
{
"name": "--endpoint",
Expand Down
43 changes: 41 additions & 2 deletions servicetests/tests/shadow_update/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

import { mqtt, iotshadow } from 'aws-iot-device-sdk-v2';
import {once} from "events";

Expand Down Expand Up @@ -45,6 +50,34 @@ function change_shadow_value(shadow: iotshadow.IotShadowClient, argv: Args, new_
});
}

function change_named_shadow_value(shadow: iotshadow.IotShadowClient, argv: Args, new_value?: object) {
return new Promise(async (resolve, reject) => {
try {
if (typeof new_value !== 'undefined') {
var updateNamedShadow: iotshadow.model.UpdateNamedShadowRequest = {
state: {
desired: new_value,
reported: new_value
},
thingName: argv.thing_name,
shadowName: argv.shadow_name
};

await shadow.publishUpdateNamedShadow(
updateNamedShadow,
mqtt.QoS.AtLeastOnce)

console.log("Update request published.");
}
}
catch (error) {
console.log("Failed to publish update request.")
reject(error);
}
resolve(true)
});
}

async function main(argv: Args) {
common_args.apply_sample_arguments(argv);

Expand All @@ -55,7 +88,7 @@ async function main(argv: Args) {
var shadow;

console.log("Connecting...");
if (argv.mqtt5) { // Build the mqtt5 client
if (argv.mqtt_version == 5) { // Build the mqtt5 client
client5 = common_args.build_mqtt5_client_from_cli_args(argv);
shadow = iotshadow.IotShadowClient.newFromMqtt5Client(client5);

Expand All @@ -74,7 +107,13 @@ async function main(argv: Args) {
try {
let data_to_send: any = {}
data_to_send[shadow_property] = "on"
await change_shadow_value(shadow, argv, data_to_send);
if (argv.shadow_name) {
console.log("Use named shadow")
await change_named_shadow_value(shadow, argv, data_to_send);
} else {
console.log("Use classic shadow")
await change_shadow_value(shadow, argv, data_to_send);
}
} catch (error) {
console.log(error);
}
Expand Down

0 comments on commit 8be568b

Please sign in to comment.