Skip to content

Commit 313825e

Browse files
committed
restore gg_ipc sample
1 parent b474206 commit 313825e

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed

samples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This directory contains sample applications for [aws-iot-device-sdk-js-v2](../RE
66
* [Node Samples](#node-samples)
77
* [MQTT5 Client Samples](#mqtt5-client-samples)
88
* [Service Client Samples](#service-client-samples)
9+
* [Greengrass IPC Sample](./node/gg_ipc/README.md)
910
* [Instructions](#instructions)
1011
* [Enable Logging](#enable-logging-in-node-samples)
1112
* [Installing Via NPM](#installing-via-npm)

samples/node/gg_ipc/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Node: Greengrass IPC
2+
3+
[**Return to main sample list**](../../README.md)
4+
5+
This sample uses the AWS IoT [Greengrass IPC Client](https://aws.github.io/aws-iot-device-sdk-js-v2/node/modules/greengrasscoreipc) to defined a Greengrass component that subscribes to MQTT messages
6+
on a topic in AWS IoT Core and periodically publishes messages to that topic.
7+
8+
Note: For this sample to work, you must have configured and installed the AWS Greengrass software on the local host. The IAM role
9+
assumed by the Greengrass Nucleus must also include permissions to connect, subscribe, and publish to AWS IoT Core.
10+
11+
## Deploying the Component
12+
13+
First, go to the `./artifacts/com.amazon.RpcTest/1.0.0` folder and run the following command:
14+
15+
``` sh
16+
npm install
17+
```
18+
19+
Then, from this folder, run the following Greengrass CLI command:
20+
21+
Linux:
22+
``` sh
23+
sudo <path-to-greengrass-cli-executable> deployment create --recipeDir ./recipes --artifactDir ./artifacts --merge "com.amazon.RpcTest=1.0.0"
24+
```
25+
26+
Windows:
27+
``` sh
28+
<path-to-greengrass-cli-executable> deployment create --recipeDir ./recipes --artifactDir ./artifacts --merge "com.amazon.RpcTest=1.0.0"
29+
```
30+
31+
Check your nucleus logs and component log for details about the success of the deployment and component execution. See
32+
the [AWS Greengrass Documentation](https://docs.aws.amazon.com/greengrass/v2/developerguide) for more
33+
details about Greengrass components, Greengrass IPC, and the Greengrass CLI.
34+
35+
## ⚠️ Usage disclaimer
36+
37+
These code examples interact with services that may incur charges to your AWS account. For more information, see [AWS Pricing](https://aws.amazon.com/pricing/).
38+
39+
Additionally, example code might theoretically modify or delete existing AWS resources. As a matter of due diligence, do the following:
40+
41+
- Be aware of the resources that these examples create or delete.
42+
- Be aware of the costs that might be charged to your account as a result.
43+
- Back up your important data.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
const common_args = require('aws-iot-samples-util/cli_args');
15+
16+
yargs.command('*', false, (yargs: any) => {
17+
common_args.add_topic_message_arguments(yargs);
18+
}, main).parse();
19+
20+
async function main(argv: Args) {
21+
try {
22+
let client : greengrasscoreipc.Client = greengrasscoreipc.createClient();
23+
24+
await client.connect();
25+
26+
await client.subscribeToIoTCore({
27+
topicName: argv.topic,
28+
qos: greengrasscoreipc.model.QOS.AT_LEAST_ONCE
29+
}).on("message", (message: greengrasscoreipc.model.IoTCoreMessage) => {
30+
if (message.message) {
31+
console.log(`Message received on topic '${message.message.topicName}': '${toUtf8(new Uint8Array(message.message.payload as ArrayBuffer))}'`);
32+
}
33+
}).activate();
34+
35+
setInterval(async () => {
36+
await client.publishToIoTCore({
37+
topicName: argv.topic,
38+
payload: argv.message,
39+
qos : greengrasscoreipc.model.QOS.AT_LEAST_ONCE
40+
});
41+
}, 10000);
42+
43+
await once(client, greengrasscoreipc.Client.DISCONNECTION);
44+
45+
await client.close();
46+
} catch (err) {
47+
console.log("Aw shucks: " + (err as eventstream_rpc.RpcError) .toString());
48+
}
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "gg_ipc",
3+
"version": "1.0.0",
4+
"description": "NodeJS IoT SDK v2 GG IPC Sample",
5+
"homepage": "https://github.com/aws/aws-iot-device-sdk-js-v2",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/aws/aws-iot-device-sdk-js-v2.git"
9+
},
10+
"contributors": [
11+
"AWS SDK Common Runtime Team <[email protected]>"
12+
],
13+
"license": "Apache-2.0",
14+
"main": "./dist/index.js",
15+
"scripts": {
16+
"tsc": "tsc",
17+
"prepare": "npm run tsc"
18+
},
19+
"devDependencies": {
20+
"@types/node": "^10.17.50",
21+
"typescript": "^4.7.4"
22+
},
23+
"dependencies": {
24+
"@types/ws": "8.5.4",
25+
"aws-iot-device-sdk-v2": "^1.12.0",
26+
"@aws-sdk/util-utf8-browser": "^3.109.0",
27+
"yargs": "^16.2.0",
28+
"aws-iot-samples-util": "../../../../../util"
29+
}
30+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
// "lib": [], /* Specify library files to be included in the compilation. */
7+
// "allowJs": true, /* Allow javascript files to be compiled. */
8+
// "checkJs": true, /* Report errors in .js files. */
9+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
10+
"declaration": true, /* Generates corresponding '.d.ts' file. */
11+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
12+
"sourceMap": true, /* Generates corresponding '.map' file. */
13+
// "outFile": "./", /* Concatenate and emit output to single file. */
14+
"outDir": "./dist", /* Redirect output structure to the directory. */
15+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
16+
// "composite": true, /* Enable project compilation */
17+
// "removeComments": false, /* Do not emit comments to output. */
18+
// "noEmit": true, /* Do not emit outputs. */
19+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
20+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
21+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
22+
/* Strict Type-Checking Options */
23+
"strict": true, /* Enable all strict type-checking options. */
24+
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
25+
"strictNullChecks": true, /* Enable strict null checks. */
26+
"strictFunctionTypes": true, /* Enable strict checking of function types. */
27+
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
28+
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
29+
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
30+
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
31+
/* Additional Checks */
32+
"noUnusedLocals": true, /* Report errors on unused locals. */
33+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
34+
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
35+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
36+
/* Module Resolution Options */
37+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
38+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
39+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
40+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
41+
// "typeRoots": [], /* List of folders to include type definitions from. */
42+
// "types": [], /* Type declaration files to be included in compilation. */
43+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
44+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
45+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
46+
/* Source Map Options */
47+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
48+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
49+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
50+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
51+
/* Experimental Options */
52+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
53+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
54+
},
55+
"include": [
56+
"*.ts"
57+
],
58+
"exclude": [
59+
"node_modules",
60+
"dist"
61+
]
62+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"RecipeFormatVersion": "2020-01-25",
3+
"ComponentName": "com.amazon.RpcTest",
4+
"ComponentVersion": "1.0.0",
5+
"ComponentDescription": "A simple component demonstrating GG RPC in Javascript",
6+
"ComponentPublisher": "Amazon",
7+
"ComponentConfiguration": {
8+
"DefaultConfiguration": {
9+
"accessControl": {
10+
"aws.greengrass.ipc.mqttproxy": {
11+
"com.amazon.RcpTest:mqttproxy:1": {
12+
"policyDescription": "Allows access to everything",
13+
"operations": [
14+
"*"
15+
],
16+
"resources": [
17+
"*"
18+
]
19+
}
20+
}
21+
}
22+
}
23+
},
24+
"Manifests": [
25+
{
26+
"Platform": {
27+
"os": "linux"
28+
},
29+
"Lifecycle": {
30+
"Run": "node {artifacts:path}/dist/index.js"
31+
}
32+
},
33+
{
34+
"Platform": {
35+
"os": "windows"
36+
},
37+
"Lifecycle": {
38+
"Run": "node {artifacts:path}/dist/index.js"
39+
}
40+
}
41+
]
42+
}

0 commit comments

Comments
 (0)