-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOSTMAN_REST_API_VALIDATION.js
72 lines (68 loc) · 2.92 KB
/
POSTMAN_REST_API_VALIDATION.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Created on 13th, Feb 2020
@author: Akram Sheriff
AWS IOT Cloud Architecture Diagram with different components in IOT Cloud Data lake implementation".
SMART CITY PARKING IOT USECASE
"""
console.log('Loading function');
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB.DocumentClient();
var table = "iotCatalog";
//var table = "Parking_State";
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
var params = {
TableName:table,
Key:{
"serialNumber": event.serialNumber,
"clientId": event.clientId,
// "location": event.location,
// "timestamp": event.timestamp,
// "Occupancy_state": event.Occupancy_state,
// "activationCode": event.activationCode
}
};
console.log("Gettings Parking IoT device details & current occupancy state of Sensor");
dynamo.get(params, function(err, data) {
if (err) {
console.error("Unable to get Parking Sensor device details from DB. Error JSON:", JSON.stringify(err, null, 2));
context.fail();
} else {
console.log("Parking Sensor data:", JSON.stringify(data, null, 2));
// Logic for Identifying the closest Parking Sensor to the Car & also to check if that Parking Sensor is Occupied or not currently
console.log(data.Item.activationCode);
//var distance =
console.log(data.Item.Occupancy_state);
console.log("event code location is " , JSON.stringify(event.car_location));
if (data.Item.activationCode == event.activationCode && (data.Item.Occupancy_state != false ) ){
console.log("This Parking spot is un-occupied, Proceed to assigner Car owner e-mail address and update the Parking space allotment slot");
console.log('Activate the parking sensor and send email to the car owner about this allotment');
var params = {
TableName:table,
Key:{
"serialNumber": event.serialNumber,
"clientId": event.clientId,
},
UpdateExpression: "set email = :val1, activated = :val2",
ExpressionAttributeValues:{
":val1": event.email,
":val2": "true"
},
ReturnValues:"UPDATED\_NEW"
};
dynamo.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
context.fail();
} else {
console.log("Parking Sensor Device is now active!", JSON.stringify(data, null, 2));
context.succeed("Car is alloted with the Parking Sensor at Location: !");
}
});
} else {
context.fail("Activation Code Invalid");
}
}
});
}