|
| 1 | +#include <EdisonAWSImplementations.h> |
| 2 | +#include "AWShelperFunctions.h" |
| 3 | +#include "HardwareFunctions.h" |
| 4 | +#include "keys.h" |
| 5 | +#include <Utils.h> |
| 6 | + |
| 7 | +AmazonSNSClient snsClient; |
| 8 | +AmazonDynamoDBClient ddbClient; |
| 9 | +AmazonKinesisClient kClient; |
| 10 | + |
| 11 | +/* Device independent implementations required for AmazonDynamoDBClient to |
| 12 | + * function. */ |
| 13 | +EdisonHttpClient httpClient; |
| 14 | +//kinesis |
| 15 | +EdisonHttpCurlClient httpCurlClient; |
| 16 | +EdisonDateTimeProvider dateTimeProvider; |
| 17 | + |
| 18 | +PublishInput publishInput; |
| 19 | +GetItemInput getItemInput; |
| 20 | +PutItemInput putItemInput; |
| 21 | +AttributeValue hashKey; |
| 22 | +AttributeValue rangeKey; |
| 23 | +ActionError actionError; |
| 24 | + |
| 25 | +void SNSClient_Setup() { |
| 26 | + snsClient.setAWSRegion(AWS_REGION); |
| 27 | + snsClient.setAWSEndpoint(AWS_ENDPOINT); |
| 28 | + snsClient.setAWSSecretKey(awsSecKey); |
| 29 | + snsClient.setAWSKeyID(awsKeyID); |
| 30 | + snsClient.setHttpClient(&httpClient); |
| 31 | + snsClient.setDateTimeProvider(&dateTimeProvider); |
| 32 | +} |
| 33 | + |
| 34 | +void DynamoBDClient_Setup() { |
| 35 | + ddbClient.setAWSRegion(AWS_REGION); |
| 36 | + ddbClient.setAWSEndpoint(AWS_ENDPOINT); |
| 37 | + ddbClient.setAWSSecretKey(awsSecKey); |
| 38 | + ddbClient.setAWSKeyID(awsKeyID); |
| 39 | + ddbClient.setHttpClient(&httpClient); |
| 40 | + ddbClient.setDateTimeProvider(&dateTimeProvider); |
| 41 | +} |
| 42 | + |
| 43 | +void KinesisClient_Setup() { |
| 44 | + kClient.setAWSRegion(AWS_REGION); |
| 45 | + kClient.setAWSEndpoint(AWS_ENDPOINT); |
| 46 | + kClient.setAWSSecretKey(awsSecKey); |
| 47 | + kClient.setAWSKeyID(awsKeyID); |
| 48 | + kClient.setHttpClient(&httpCurlClient); |
| 49 | + kClient.setDateTimeProvider(&dateTimeProvider); |
| 50 | +} |
| 51 | + |
| 52 | +void putDynamoDb() { |
| 53 | + // Put device & datestamp record in DynamoDB table |
| 54 | + // Create an Item. |
| 55 | + AttributeValue deviceIdValue; |
| 56 | + deviceIdValue.setS(HASH_KEY_VALUE); |
| 57 | + AttributeValue timeValue; |
| 58 | + // Getting current time for Time attribute. |
| 59 | + timeValue.setN(dateTimeProvider.getDateTime()); |
| 60 | + AttributeValue deviceValue; |
| 61 | + deviceValue.setS(ATTRIBUTE_VALUE); |
| 62 | + |
| 63 | + MinimalKeyValuePair<MinimalString, AttributeValue> att1(HASH_KEY_NAME, deviceIdValue); |
| 64 | + MinimalKeyValuePair<MinimalString, AttributeValue> att2(RANGE_KEY_NAME, timeValue); |
| 65 | + MinimalKeyValuePair<MinimalString, AttributeValue> att3(ATTRIBUTE_NAME, deviceValue); |
| 66 | + MinimalKeyValuePair<MinimalString, AttributeValue> itemArray[] = { att1, |
| 67 | + att2, att3 }; |
| 68 | + |
| 69 | + // Set values for putItemInput. |
| 70 | + putItemInput.setItem(MinimalMap<AttributeValue>(itemArray, 3)); |
| 71 | + putItemInput.setTableName(TABLE_NAME); |
| 72 | + |
| 73 | + // perform putItem and check for errors. |
| 74 | + PutItemOutput putItemOutput = ddbClient.putItem(putItemInput, actionError); |
| 75 | + switch (actionError) { |
| 76 | + case NONE_ACTIONERROR: |
| 77 | + Serial.println("DynamoDB PutItem succeeded!"); |
| 78 | + break; |
| 79 | + case INVALID_REQUEST_ACTIONERROR: |
| 80 | + Serial.print("ERROR: "); |
| 81 | + Serial.println(putItemOutput.getErrorMessage().getCStr()); |
| 82 | + break; |
| 83 | + case MISSING_REQUIRED_ARGS_ACTIONERROR: |
| 84 | + Serial.println( |
| 85 | + "ERROR: Required arguments were not set for PutItemInput"); |
| 86 | + break; |
| 87 | + case RESPONSE_PARSING_ACTIONERROR: |
| 88 | + Serial.println("ERROR: Problem parsing http response of PutItem"); |
| 89 | + break; |
| 90 | + case CONNECTION_ACTIONERROR: |
| 91 | + Serial.println("ERROR: Connection problem"); |
| 92 | + break; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void putKinesis(double realAccelX, double realAccelY, double realAccelZ) { |
| 97 | +// light the grove LED to indicate posting to AWS service |
| 98 | + |
| 99 | + PutRecordInput putRecordInput; |
| 100 | + putRecordInput.setStreamName(streamName); |
| 101 | + |
| 102 | + char buffer[255]; |
| 103 | + String dataSource; |
| 104 | + dataSource = String("{\"device_id\":\""); |
| 105 | + dataSource += HASH_KEY_VALUE; |
| 106 | + dataSource += String("\",\"time\":"); |
| 107 | + dataSource += dateTimeProvider.getDateTime(); |
| 108 | + dataSource += String(",\"device\":\""); |
| 109 | + dataSource += ATTRIBUTE_VALUE; |
| 110 | + dataSource += String("\",\"sensors\":[{\"telemetryData\": {\"xval\":"); |
| 111 | + sprintf(buffer, "%f", realAccelX); |
| 112 | + dataSource += buffer; |
| 113 | + dataSource += String(",\"yval\":"); |
| 114 | + sprintf(buffer, "%f", realAccelY); |
| 115 | + dataSource += buffer; |
| 116 | + dataSource += String(",\"zval\":"); |
| 117 | + sprintf(buffer, "%f", realAccelZ); |
| 118 | + dataSource += buffer; |
| 119 | + dataSource += String("}}]}"); |
| 120 | + dataSource.toCharArray(buffer, 255); |
| 121 | + Serial.println(buffer); |
| 122 | + Serial.println(dateTimeProvider.getDateTime()); |
| 123 | + char* data = base64Encode(buffer); |
| 124 | + putRecordInput.setData(data); |
| 125 | + delete[] data; |
| 126 | + putRecordInput.setPartitionKey(partitionKey); |
| 127 | + |
| 128 | +// simple blind putRecord, no way to read output of System call for now |
| 129 | + kClient.putRecord(putRecordInput, actionError); |
| 130 | + Serial.println("\nKinesis record posted to Stream"); |
| 131 | +// switch off the Grove LED again - message posted |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +void putSns() { |
| 136 | + |
| 137 | +// set datetime as the message to TARGET_ARN |
| 138 | + MinimalString dateTime(dateTimeProvider.getDateTime()); |
| 139 | + dateTimeProvider.sync(dateTime.getCStr()); |
| 140 | + |
| 141 | +// Delivery attempt to Amazon SNS. |
| 142 | + publishInput.setMessage(dateTime); |
| 143 | + publishInput.setTargetArn(TARGET_ARN); |
| 144 | + PublishOutput publishOutput = snsClient.publish(publishInput, actionError); |
| 145 | + Serial.print("Sent message ID: "); |
| 146 | + Serial.println(publishOutput.getMessageId().getCStr()); |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +void indicateServiceThroughLED_blink_Buzzer(AWS_Service_ID service) { |
| 152 | + |
| 153 | + int blinkCount = 0; |
| 154 | + |
| 155 | + switch (service) { |
| 156 | + |
| 157 | + case KINESIS: |
| 158 | + blinkCount = 1; |
| 159 | + break; |
| 160 | + |
| 161 | + case DYNAMO_DB: |
| 162 | + blinkCount = 2; |
| 163 | + break; |
| 164 | + |
| 165 | + case SNS: |
| 166 | + blinkCount = 3; |
| 167 | + break; |
| 168 | + |
| 169 | + default: |
| 170 | + blinkCount = 10; |
| 171 | + break; |
| 172 | + } |
| 173 | + blinkGroveLed(blinkCount); |
| 174 | + |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +AWS_Service_ID changeService(AWS_Service_ID currentServiceId) { |
| 179 | + AWS_Service_ID nextServiceId; |
| 180 | + |
| 181 | + Serial.print("Button pressed - changing mode. New mode: "); |
| 182 | + |
| 183 | + nextServiceId = (AWS_Service_ID)(currentServiceId + 1); |
| 184 | + if (nextServiceId >= END_NOSERVICE) { |
| 185 | + nextServiceId = KINESIS; |
| 186 | + } |
| 187 | + |
| 188 | + indicateServiceThroughLED_blink_Buzzer(nextServiceId); |
| 189 | + |
| 190 | + switch (nextServiceId) { |
| 191 | + case 0: |
| 192 | + Serial.println("Kinesis"); |
| 193 | + break; |
| 194 | + case 1: |
| 195 | + Serial.println("DynamoDB"); |
| 196 | + break; |
| 197 | + case 2: |
| 198 | + Serial.println("SNS"); |
| 199 | + break; |
| 200 | + } |
| 201 | + return nextServiceId; |
| 202 | +} |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | + |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | + |
| 268 | + |
0 commit comments