Skip to content

Commit

Permalink
Merge branch 'prod-lambda' of https://github.com/Fruittips/greendot i…
Browse files Browse the repository at this point in the history
…nto prod-lambda
  • Loading branch information
brycegoh committed Jan 4, 2024
2 parents 68416e9 + e732f7c commit 2158eff
Show file tree
Hide file tree
Showing 3 changed files with 1,478 additions and 41 deletions.
48 changes: 30 additions & 18 deletions fire-cloud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ async function connectAndSubscribe() {
const humidity = messageJson.humidity;
const airQualityPpm = messageJson.air;
const flameSensorValue = messageJson.flame;
const utcTimestamp = convertEpochToUTC(messageJson.timestamp);

const { data, error } = await supabase
.from("firecloud")
.insert({
node_id: messageJson.id,
timestamp: convertEpochToUTC(messageJson.timestamp),
timestamp: utcTimestamp,
temperature: temperature,
humidity: humidity,
air_quality_ppm: airQualityPpm,
Expand All @@ -110,7 +111,13 @@ async function connectAndSubscribe() {

//invoke lambda function to calculate fire probability and update database
const rowId = data[0].id;
const lambdaRes = await invokeAnalytics(nodeId, rowId, temperature, flameSensorValue);
const lambdaRes = await invokeAnalytics(
nodeId,
rowId,
temperature,
flameSensorValue,
utcTimestamp
);

const fireProbability = lambdaRes.fire_probability;

Expand All @@ -131,21 +138,25 @@ async function validateAndPublishFireMessage(nodeId, fireProbability) {
fireStatuses[nodeId] = 1;
}

//if fire is dying down
if (fireStatuses[nodeId] === 1 && fireProbability < fireProbabilityThreshold) {
if (noFireDurations[nodeId] === null) {
noFireDurations[nodeId] = new Date().getTime();
} else {
const currentTime = new Date().getTime();
const timeDiff = currentTime - noFireDurations[nodeId];
const timeDiffInMinutes = timeDiff / 60000;

// if status has been 0 for more than 5 minutes -> there is no more fire -> publish status 0
if (timeDiffInMinutes > 5) {
await updateAndPublishFireMessage(FLAME_PRESENCE_TOPIC, 0);
fireStatuses[nodeId] = 0;
noFireDurations[nodeId] = null;
//if fire is dying down aa
if (fireStatuses[nodeId] === 1) {
if (fireProbability < fireProbabilityThreshold) {
if (noFireDurations[nodeId] === null) {
noFireDurations[nodeId] = new Date().getTime();
} else {
const currentTime = new Date().getTime();
const timeDiff = currentTime - noFireDurations[nodeId];
const timeDiffInMinutes = timeDiff / 60000;

// if status has been 0 for more than 5 minutes -> there is no more fire -> publish status 0
if (timeDiffInMinutes > 5) {
await updateAndPublishFireMessage(FLAME_PRESENCE_TOPIC, 0);
fireStatuses[nodeId] = 0;
noFireDurations[nodeId] = null;
}
}
} else {
noFireDurations[nodeId] = null;
}
}
}
Expand Down Expand Up @@ -176,7 +187,7 @@ async function publishMessage(topic, message) {
}

// Function to invoke the Analytics lambda function to calculate the fire probability and update the database
async function invokeAnalytics(nodeId, rowId, temp, flameValue) {
async function invokeAnalytics(nodeId, rowId, temp, flameValue, utcDatetime) {
console.log("Invoking lambda function...");
const command = new InvokeCommand({
FunctionName: "greendot-analytics",
Expand All @@ -186,12 +197,13 @@ async function invokeAnalytics(nodeId, rowId, temp, flameValue) {
rowId: rowId,
temp: temp,
flame: flameValue,
utc_datetime_string: utcDatetime,
}),
});

const { Payload } = await lambdaClient.send(command);
let result = Buffer.from(Payload).toString();
result = JSON.parse(result)
result = JSON.parse(result);
return JSON.parse(result.body);
}

Expand Down
Loading

0 comments on commit 2158eff

Please sign in to comment.