|
| 1 | +/* |
| 2 | + * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +const Buffer = require('buffer').Buffer; |
| 6 | + |
| 7 | +const Lambda = require('./lambda'); |
| 8 | +const Util = require('./util'); |
| 9 | +const GreengrassCommon = require('aws-greengrass-common-js'); |
| 10 | + |
| 11 | +const envVars = GreengrassCommon.envVars; |
| 12 | +const MY_FUNCTION_ARN = envVars.MY_FUNCTION_ARN; |
| 13 | +const SHADOW_FUNCTION_ARN = envVars.SHADOW_FUNCTION_ARN; |
| 14 | +const ROUTER_FUNCTION_ARN = envVars.ROUTER_FUNCTION_ARN; |
| 15 | + |
| 16 | +class IotData { |
| 17 | + constructor() { |
| 18 | + this.lambda = new Lambda(); |
| 19 | + } |
| 20 | + |
| 21 | + getThingShadow(params, callback) { |
| 22 | + /* |
| 23 | + * Call shadow lambda to obtain current shadow state. |
| 24 | + * @param {object} params object contains parameters for the call |
| 25 | + * REQUIRED: 'thingName' the name of the thing |
| 26 | + */ |
| 27 | + const thingName = Util.getRequiredParameter(params, 'thingName'); |
| 28 | + if (thingName === undefined) { |
| 29 | + callback(new Error('"thingName" is a required parameter.'), null); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const payload = ''; |
| 34 | + this._shadowOperation('get', thingName, payload, callback); |
| 35 | + } |
| 36 | + |
| 37 | + updateThingShadow(params, callback) { |
| 38 | + /* |
| 39 | + * Call shadow lambda to update current shadow state. |
| 40 | + * @param {object} params object contains parameters for the call |
| 41 | + * REQUIRED: 'thingName' the name of the thing |
| 42 | + * 'payload' the state information in JSON format |
| 43 | + */ |
| 44 | + const thingName = Util.getRequiredParameter(params, 'thingName'); |
| 45 | + if (thingName === undefined) { |
| 46 | + callback(new Error('"thingName" is a required parameter.'), null); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const payload = Util.getRequiredParameter(params, 'payload'); |
| 51 | + if (payload === undefined) { |
| 52 | + callback(new Error('"payload" is a required parameter.'), null); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + this._shadowOperation('update', thingName, payload, callback); |
| 57 | + } |
| 58 | + |
| 59 | + deleteThingShadow(params, callback) { |
| 60 | + /* |
| 61 | + * Call shadow lambda to delete the shadow state. |
| 62 | + * @param {object} params object contains parameters for the call |
| 63 | + * REQUIRED: 'thingName' the name of the thing |
| 64 | + */ |
| 65 | + const thingName = Util.getRequiredParameter(params, 'thingName'); |
| 66 | + if (thingName === undefined) { |
| 67 | + callback(new Error('"thingName" is a required parameter.'), null); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const payload = ''; |
| 72 | + this._shadowOperation('delete', thingName, payload, callback); |
| 73 | + } |
| 74 | + |
| 75 | + publish(params, callback) { |
| 76 | + /* |
| 77 | + * Publishes state information. |
| 78 | + * @param {object} params object contains parameters for the call |
| 79 | + * REQUIRED: 'topic' the topic name to be published |
| 80 | + * 'payload' the state information in JSON format |
| 81 | + */ |
| 82 | + const topic = Util.getRequiredParameter(params, 'topic'); |
| 83 | + if (topic === undefined) { |
| 84 | + callback(new Error('"topic" is a required parameter'), null); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const payload = Util.getRequiredParameter(params, 'payload'); |
| 89 | + if (payload === undefined) { |
| 90 | + callback(new Error('"payload" is a required parameter'), null); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const context = { |
| 95 | + custom: { |
| 96 | + source: MY_FUNCTION_ARN, |
| 97 | + subject: topic, |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + const buff = Buffer.from(JSON.stringify(context)); |
| 102 | + const clientContext = buff.toString('base64'); |
| 103 | + |
| 104 | + const invokeParams = { |
| 105 | + FunctionName: ROUTER_FUNCTION_ARN, |
| 106 | + InvocationType: 'Event', |
| 107 | + ClientContext: clientContext, |
| 108 | + Payload: payload, |
| 109 | + }; |
| 110 | + |
| 111 | + console.log(`Publishing message on topic "${topic}" with Payload "${payload}"`); |
| 112 | + |
| 113 | + this.lambda.invoke(invokeParams, (err, data) => { |
| 114 | + if (err) { |
| 115 | + callback(err, null); // an error occurred |
| 116 | + } else { |
| 117 | + callback(null, data); // successful response |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + _shadowOperation(operation, thingName, payload, callback) { |
| 123 | + const topic = `$aws/things/${thingName}/shadow/${operation}`; |
| 124 | + const context = { |
| 125 | + custom: { |
| 126 | + subject: topic, |
| 127 | + }, |
| 128 | + }; |
| 129 | + |
| 130 | + const clientContext = Buffer.from(JSON.stringify(context)).toString('base64'); |
| 131 | + const invokeParams = { |
| 132 | + FunctionName: SHADOW_FUNCTION_ARN, |
| 133 | + ClientContext: clientContext, |
| 134 | + Payload: payload, |
| 135 | + }; |
| 136 | + |
| 137 | + console.log(`Calling shadow service on topic "${topic}" with payload "${payload}"`); |
| 138 | + this.lambda.invoke(invokeParams, (err, data) => { |
| 139 | + if (err) { |
| 140 | + callback(err, null); |
| 141 | + } else { |
| 142 | + callback(null, data); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +module.exports = IotData; |
0 commit comments