-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (110 loc) · 3.4 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
process.env.DEBUG = "collector";
process.title = "collector-microservice";
const debug = require("debug")("collector");
import MongoDB from "./lib/MongoDB";
import MQTT from "./lib/MQTT";
import Thermostat from "./devices/Thermostat";
import Weather from "./devices/Weather";
import Autelis from "./devices/Autelis";
import LGTV from "./devices/LGTV";
import Bravia from "./devices/Bravia";
import Denon from "./devices/Denon";
import Sensor from "./devices/Sensor";
import SmartThings from "./devices/SmartThings";
//const MongoClient = require("mongodb").MongoClient,
// url = "mongodb://robodomo:27017";
const express = require("express"),
app = express();
app.set("port", 4000);
const nop = async () => {
setInterval(() => {
// do nothing
}, 60000);
};
const main = async () => {
await nop();
const monitors = [];
MQTT.connect(async () => {
const db = new MongoDB("settings");
await db.use("config");
const Config = await db.config.findOne({ _id: "config" });
if (Config) {
if (Config.nest && Config.nest.thermostats) {
for (const thermostat of Config.nest.thermostats) {
monitors.push(new Thermostat(Config.mqtt.nest, thermostat.device));
}
}
if (Config.weather && Config.weather.locations) {
for (const location of Config.weather.locations) {
monitors.push(new Weather(Config.mqtt.weather, location.device));
}
}
if (Config.autelis) {
monitors.push(
new Autelis(Config.mqtt.autelis, null, Config.autelis.deviceMap)
);
}
if (Config.lgtv && Config.lgtv.tvs) {
for (const tv of Config.lgtv.tvs) {
monitors.push(new LGTV(Config.mqtt.lgtv, tv.device));
}
}
if (Config.bravia && Config.bravia.tvs) {
for (const tv of Config.bravia.tvs) {
monitors.push(new Bravia(Config.mqtt.bravia, tv.device));
}
}
if (Config.denon && Config.denon.receivers) {
for (const receiver of Config.denon.receivers) {
monitors.push(new Denon(Config.mqtt.denon, receiver.device));
}
}
if (Config.sensors) {
for (const sensor of Config.sensors) {
monitors.push(
new Sensor(
Config.mqtt.smartthings,
sensor.type,
sensor.device || sensor.name
)
);
}
}
if (Config.smartthings && Config.smartthings.things) {
for (const thing of Config.smartthings.things) {
monitors.push(
new SmartThings(
Config.mqtt.smartthings,
thing.type,
thing.device || thing.name
)
);
}
}
}
app.get("/", (req, res) => {
res.json({ message: "Hello, world" });
});
app.get("/collections", async (req, res) => {
const db = new MongoDB("robodomo");
res.json(await db.listCollections("robodomo"));
});
app.get("/config", async (req, res) => {
res.json(await db.config.findOne({ _id: "config" }));
});
app.get("/macros", async (req, res) => {
res.json(await db.config.findOne({ _id: "macros" }));
});
// 404 handler
app.use((req, res) => {
res
.status(404)
.json({ status: 404, message: `Can't ${req.method} ${req.url}` });
});
app.listen(4000, () => {
debug("listening on port 4000");
});
});
};
//
main();