-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
195 lines (174 loc) · 4.89 KB
/
app.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* eslint-disable no-console */
const config = require("config");
const chalk = require("chalk");
const md5 = require("md5");
const Loki = require("lokijs");
const express = require("express");
const ioLib = require("socket.io");
const bodyParser = require("body-parser");
const fs = require("fs");
const jsonschema = require("jsonschema");
const EventEmitter = require("events");
const constants = require("./constants");
const eventCreators = require("./eventCreators");
const driverUtilsModule = require("./utils/driver");
const eventUtilsModule = require("./utils/event");
const deviceUtilsModule = require("./utils/device");
const jobsUtilsModule = require("./utils/jobs");
const commsLoader = require("./comms");
const socketApi = require("./socketApi");
const authenticateCtrlModule = require("./controllers/authenticate");
const eventCtrlModule = require("./controllers/event");
const driverCtrlModule = require("./controllers/driver");
const interfaceCtrlModule = require("./controllers/interface");
const schemasModule = require("./schemas");
const httpApi = require("./httpApi");
console.log(chalk.yellow("Starting Thinglator"));
const comms = commsLoader(fs, chalk);
process.on("SIGINT", async () => {
try {
await comms.disconnectAll();
} catch (err) {
console.log("ERROR2", err);
}
console.log(chalk.yellow("Stopping Thinglator"));
process.exit();
});
let driversCollection = null;
let devicesCollection = null;
let eventsCollection = null;
let db = null;
const setupDb = () =>
new Promise(resolve => {
const databaseInitialize = () => {
driversCollection = db.getCollection("drivers");
// console.log(driversCollection);
if (!driversCollection) {
driversCollection = db.addCollection("drivers", {
unique: ["driverId"]
});
}
devicesCollection = db.getCollection("devices");
if (!devicesCollection) {
devicesCollection = db.addCollection("devices", {
unique: ["deviceId"]
});
}
eventsCollection = db.getCollection("events");
if (!eventsCollection) {
eventsCollection = db.addCollection("events");
}
resolve();
};
db = new Loki(`${config.get("db.path")}/thinglator.db`, {
autoload: true,
autoloadCallback: databaseInitialize,
autosave: true,
autosaveInterval: 4000
});
});
const loadModule = moduleId => require(`${moduleId}`);
const launch = async () => {
const jsonValidator = new jsonschema.Validator();
console.log(chalk.blue("Setting up database.."));
await setupDb();
const schemas = schemasModule(constants);
const eventUtils = eventUtilsModule(
EventEmitter,
constants,
schemas,
jsonValidator,
eventsCollection
);
const deviceUtils = deviceUtilsModule(
md5,
devicesCollection,
schemas,
jsonValidator
);
const driverUtils = driverUtilsModule(
fs,
chalk,
driversCollection,
devicesCollection,
constants,
eventCreators,
eventUtils.eventEmitter,
schemas,
loadModule
);
const jobsUtils = jobsUtilsModule(eventsCollection);
console.log(chalk.blue("Database initialised!"));
jobsUtils.schedule();
console.log(chalk.blue("Jobs scheduled!"));
// get a list of all potential interfaces (one for each communication protocol)
const availableInterfaces = comms.installInterfaces();
// load the comms using the available interfaces and their configs
await comms.initialise(
availableInterfaces,
config.get("interfaces"),
eventUtils.eventEmitter
);
console.log(chalk.blue("All comms connected!"));
// Get the drivers and initialise them
const driverList = await driverUtils.load(comms);
console.log(chalk.blue("All drivers connected!"));
const driverCtrl = driverCtrlModule(
devicesCollection,
eventsCollection,
md5,
driverUtils,
deviceUtils,
driverList,
jsonValidator,
schemas,
comms
);
const authenticateCtrl = authenticateCtrlModule(
jsonValidator,
schemas.authentication,
driverUtils,
driverList
);
const eventCtrl = eventCtrlModule(eventsCollection);
const interfaceCtrl = interfaceCtrlModule(comms);
// load and initialise express
const app = express();
// setup the HTTP API
httpApi(
bodyParser,
authenticateCtrl,
eventCtrl,
driverCtrl,
interfaceCtrl,
app,
driverList
);
// Initialise the webserver
const httpServer = app.listen(config.get("port"), () => {
console.log(
chalk.blue(`REST API server listening on port ${config.get("port")}`)
);
});
// setup the websocket API
socketApi.initialise(
ioLib,
authenticateCtrl,
eventCtrl,
driverCtrl,
interfaceCtrl,
eventUtils,
httpServer,
driverList,
constants
);
console.log(
chalk.blue(`WebSocket server listening on port ${config.get("port")}`)
);
};
try {
launch();
} catch (err) {
console.log("ERROR", err);
process.emit("SIGINT");
}