|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +import {eventstream_rpc, greengrasscoreipc} from 'aws-iot-device-sdk-v2'; |
| 7 | +import {once} from "events"; |
| 8 | +import {toUtf8} from "@aws-sdk/util-utf8-browser"; |
| 9 | + |
| 10 | +type Args = { [index: string]: any }; |
| 11 | + |
| 12 | +const yargs = require('yargs'); |
| 13 | + |
| 14 | +yargs.command('*', false, (yargs: any) => { |
| 15 | +}, main).parse(); |
| 16 | + |
| 17 | +async function main(argv: Args) { |
| 18 | + try { |
| 19 | + let client : greengrasscoreipc.Client = greengrasscoreipc.createClient(); |
| 20 | + |
| 21 | + await client.connect(); |
| 22 | + |
| 23 | + await client.subscribeToIoTCore({ |
| 24 | + topicName: "hello/world", |
| 25 | + qos: greengrasscoreipc.model.QOS.AT_LEAST_ONCE |
| 26 | + }).on("message", (message: greengrasscoreipc.model.IoTCoreMessage) => { |
| 27 | + if (message.message) { |
| 28 | + console.log(`Message received on topic '${message.message.topicName}': '${toUtf8(new Uint8Array(message.message.payload as ArrayBuffer))}'`); |
| 29 | + } |
| 30 | + }).activate(); |
| 31 | + |
| 32 | + setInterval(async () => { |
| 33 | + await client.publishToIoTCore({ |
| 34 | + topicName: "hello/world", |
| 35 | + payload: "Hello from a component!", |
| 36 | + qos : greengrasscoreipc.model.QOS.AT_LEAST_ONCE |
| 37 | + }); |
| 38 | + }, 10000); |
| 39 | + |
| 40 | + await once(client, greengrasscoreipc.Client.DISCONNECTION); |
| 41 | + |
| 42 | + await client.close(); |
| 43 | + } catch (err) { |
| 44 | + console.log("Aw shucks: " + (err as eventstream_rpc.RpcError) .toString()); |
| 45 | + } |
| 46 | +} |
0 commit comments