From 8be568bda5793ee2f1acdd1921f5137101d37eb5 Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 22 Nov 2023 11:28:35 -0800 Subject: [PATCH] Working --- .../test_cases/mqtt3_named_shadow_cfg.json | 6 +-- .../test_cases/mqtt5_named_shadow_cfg.json | 6 +-- servicetests/test_cases/mqtt5_shadow_cfg.json | 6 +-- servicetests/tests/shadow_update/index.ts | 43 ++++++++++++++++++- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/servicetests/test_cases/mqtt3_named_shadow_cfg.json b/servicetests/test_cases/mqtt3_named_shadow_cfg.json index 0c1836f2..eaaf271e 100644 --- a/servicetests/test_cases/mqtt3_named_shadow_cfg.json +++ b/servicetests/test_cases/mqtt3_named_shadow_cfg.json @@ -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", diff --git a/servicetests/test_cases/mqtt5_named_shadow_cfg.json b/servicetests/test_cases/mqtt5_named_shadow_cfg.json index ee49b01f..ee2b14e1 100644 --- a/servicetests/test_cases/mqtt5_named_shadow_cfg.json +++ b/servicetests/test_cases/mqtt5_named_shadow_cfg.json @@ -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", diff --git a/servicetests/test_cases/mqtt5_shadow_cfg.json b/servicetests/test_cases/mqtt5_shadow_cfg.json index 16c2c4e8..78430435 100644 --- a/servicetests/test_cases/mqtt5_shadow_cfg.json +++ b/servicetests/test_cases/mqtt5_shadow_cfg.json @@ -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", diff --git a/servicetests/tests/shadow_update/index.ts b/servicetests/tests/shadow_update/index.ts index 07c8a99f..64e94529 100644 --- a/servicetests/tests/shadow_update/index.ts +++ b/servicetests/tests/shadow_update/index.ts @@ -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"; @@ -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); @@ -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); @@ -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); }