mirror of
https://github.com/Lomanic/presence-button-web
synced 2024-11-26 07:27:30 +00:00
🍀🚘 Checkpoint
./server.js:933874/30256
This commit is contained in:
parent
4fcf7d3979
commit
5b893c58a2
29
server.js
29
server.js
@ -1,6 +1,6 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const app = express();
|
const app = express();
|
||||||
const https = require('https');
|
const request = require("request");
|
||||||
|
|
||||||
var fuzIsOpen = false;
|
var fuzIsOpen = false;
|
||||||
var lastSeen = new Date("1970-01-01");
|
var lastSeen = new Date("1970-01-01");
|
||||||
@ -75,21 +75,40 @@ const listener = app.listen(process.env.PORT, function() {
|
|||||||
console.log("Your app is listening on port " + listener.address().port);
|
console.log("Your app is listening on port " + listener.address().port);
|
||||||
});
|
});
|
||||||
|
|
||||||
const loop = () => {
|
request
|
||||||
|
.post({
|
||||||
|
url:
|
||||||
|
"https://" +
|
||||||
|
process.env.MATRIXUSERNAME.substring(
|
||||||
|
process.env.MATRIXUSERNAME.indexOf(":") + 1
|
||||||
|
) +
|
||||||
|
"/_matrix/client/r0/login",
|
||||||
|
body: JSON.stringify({ type: "m.login.password", user: }),
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.on("response", function(response) {
|
||||||
|
const loop = () => {
|
||||||
console.log("loop", lastClosed);
|
console.log("loop", lastClosed);
|
||||||
if (lastSeen < new Date() - 2 * 60 * 1000 && lastClosed < lastSeen) {
|
if (lastSeen < new Date() - 2 * 60 * 1000 && lastClosed < lastSeen) {
|
||||||
// the Fuz is newly closed, notify on matrix and write file to survive reboot
|
// the Fuz is newly closed, notify on matrix and write file to survive reboot
|
||||||
lastClosed = new Date();
|
lastClosed = new Date();
|
||||||
//lastNofified = new Date();
|
//lastNofified = new Date();
|
||||||
//https.post ... send message to Fuz process.env.MATRIXROOM
|
//https.post ... send message to Fuz process.env.MATRIXROOM
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fs.writeFileSync(db, JSON.stringify({ fuzIsOpen, lastSeen, lastClosed }));
|
fs.writeFileSync(
|
||||||
|
db,
|
||||||
|
JSON.stringify({ fuzIsOpen, lastSeen, lastClosed })
|
||||||
|
);
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(loop, 10 * 1000);
|
setTimeout(loop, 10 * 1000);
|
||||||
};
|
};
|
||||||
setTimeout(loop, 1 * 1000); // give some time for presence button to show up (1 min)
|
setTimeout(loop, 1 * 1000); // give some time for presence button to show up (1 min)
|
||||||
|
});
|
||||||
|
|
||||||
if (process.env.PROJECT_DOMAIN != "") {
|
if (process.env.PROJECT_DOMAIN != "") {
|
||||||
process.on("SIGTERM", function() {
|
process.on("SIGTERM", function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user