1
0
mirror of https://github.com/elisspace/FxLifeSheet.git synced 2026-08-29 15:44:00 +00:00
Files
FxLifeSheet/web.ts
2019-04-30 12:56:30 +02:00

59 lines
1.4 KiB
TypeScript

// Third party dependencies
var http = require("http");
const moment = require("moment");
// Internal dependencies
let google_sheets = require("./classes/google_sheets.js");
// State
var lastMoodData = null; // used for the moods API
let currentMood = null;
let rawDataSheetRef = null;
google_sheets.setupGoogleSheets(initMoodAPI);
function initMoodAPI(rawDataSheet, lastRunSheet) {
rawDataSheetRef = rawDataSheet;
console.log("Launching up API web server...");
// Periodically refresh the value
setInterval(loadCurrentMood, 5 * 60 * 1000);
loadCurrentMood();
http
.createServer(function(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.write(JSON.stringify(lastMoodData));
return res.end();
})
.listen(process.env.PORT);
}
function loadCurrentMood() {
console.log("Refreshing latest moood entry...");
currentMood = rawDataSheetRef.getRows(
{
offset: 0,
limit: 1,
orderby: "timestamp",
reverse: true,
query: "key=mood"
},
function(error, rows) {
if (error) {
console.error(error);
}
let lastMoodRow = rows[0];
// `lastMoodRow` is null if we haven't tracked a mood yet
if (lastMoodRow != null) {
lastMoodData = {
time: moment(Number(lastMoodRow.timestamp)).format(),
value: Number(lastMoodRow.value)
};
}
}
);
}
export {};