forked from akx/ruuvidriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (46 loc) · 1.1 KB
/
index.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
const tagDatas = {};
let initialized = false;
const ruuvi = require("node-ruuvitag");
function init() {
if (initialized) {
return false;
}
ruuvi.on("found", tag => {
console.log(`Found tag ${tag.id}`);
tag.on("updated", data => {
tagDatas[tag.id] = { ...data, ts: +new Date() };
});
});
return true;
}
function getRuuvi() {
return ruuvi;
}
function stop() {
if (ruuvi) {
ruuvi = null;
initialized = false;
}
}
function createApp() {
const express = require("express");
const app = express();
app.get("/tags", (req, res) => res.json(tagDatas));
app.get("/tag/:id", (req, res) => res.json(tagDatas[req.params.id]));
return app;
}
module.exports.init = init;
module.exports.stop = stop;
module.exports.tagDatas = tagDatas;
module.exports.createApp = createApp;
module.exports.getRuuvi = getRuuvi;
// Being run from the command line?
if (module.parent === null) {
require("dotenv").config();
const port = parseInt(process.env.RUUVI_PORT || "52020", 10);
const app = createApp();
init();
app.listen(port, () => {
console.log(`Listening on ${port}`);
});
}