-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
173 lines (158 loc) · 6.28 KB
/
main.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
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const { WebServer } = require('@iobroker/webserver');
const RestAPI = require('./lib/rest-api.js');
const adapterName = require('./package.json').name.split('.').pop();
let webServer = null;
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: adapterName,
stateChange: (id, state) => {
webServer && webServer.api && webServer.api.stateChange(id, state);
},
objectChange: (id, obj) => webServer && webServer.api && webServer.api.objectChange(id, obj),
unload: callback => {
try {
adapter.setState('info.connection', false, true);
adapter.log.info(`terminating http${adapter.config.secure ? 's' : ''} server on port ${adapter.config.port}`);
if (webServer && webServer.api) {
webServer.api.unload()
.then(() => {
try {
if (webServer.server) {
webServer.server.close();
webServer.server = null;
}
} catch (error) {
// ignore
console.error('Cannot close server: ' + error);
}
webServer = null;
callback();
});
} else {
if (webServer && webServer.server) {
webServer.server.close();
webServer.server = null;
}
webServer = null;
callback();
}
} catch (error) {
console.error('Cannot close server: ' + error);
callback();
}
},
ready: main
});
adapter = new utils.Adapter(options);
return adapter;
}
function main() {
if (adapter.config.webInstance) {
console.log('Adapter runs as a part of web service');
adapter.log.warn('Adapter runs as a part of web service');
adapter.setForeignState(`system.adapter.${adapter.namespace}.alive`, false, true, () =>
setTimeout(() => process.exit(utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION), 1000));
} else {
if (adapter.config.secure) {
// Load certificates
adapter.getCertificates((err, certificates, leConfig) => {
adapter.config.certificates = certificates;
adapter.config.leConfig = leConfig;
initWebServer(adapter.config, server =>
webServer = server);
});
} else {
initWebServer(adapter.config, server =>
webServer = server);
}
}
}
//settings: {
// "port": 8080,
// "auth": false,
// "secure": false,
// "bind": "0.0.0.0", // "::"
// "cache": false
//}
function initWebServer(settings, callback) {
settings.port = parseInt(settings.port, 10);
const server = {
app: null,
server: null,
api: null,
io: null,
settings
};
server.api = new RestAPI(server.server, settings, adapter, null, null, async app => {
if (settings.port) {
if (settings.secure && !adapter.config.certificates) {
return null;
}
try {
const webserver = new WebServer({
app,
adapter,
secure: adapter.config.secure
});
server.server = await webserver.init();
} catch (err) {
adapter.log.error(`Cannot create webserver: ${err}`);
adapter.terminate ? adapter.terminate(1) : process.exit(1);
return;
}
server.server.__server = server;
} else {
adapter.log.error('port missing');
process.exit(1);
}
if (server.server) {
let serverListening = false;
let serverPort = settings.port;
server.server.on('error', e => {
if (e.toString().includes('EACCES') && serverPort <= 1024) {
adapter.log.error(`node.js process has no rights to start server on the port ${serverPort}.\n` +
`Do you know that on linux you need special permissions for ports under 1024?\n` +
`You can call in shell following scrip to allow it for node.js: "iobroker fix"`
);
} else {
adapter.log.error(`Cannot start server on ${settings.bind || '0.0.0.0'}:${serverPort}: ${e}`);
}
if (!serverListening) {
callback && callback(server);
adapter.terminate ? adapter.terminate(1) : process.exit(1);
}
});
adapter.getPort(settings.port, (!settings.bind || settings.bind === '0.0.0.0') ? undefined : settings.bind || undefined, port => {
if (port !== settings.port && !adapter.config.findNextPort) {
adapter.log.error(`port ${settings.port} already in use`);
process.exit(1);
}
serverPort = port;
server.server.listen(port, (!settings.bind || settings.bind === '0.0.0.0') ? undefined : settings.bind || undefined, async () => {
await adapter.setStateAsync('info.connection', true, true);
adapter.log.info(`http${settings.secure ? 's' : ''} server listening on port ${port}`);
serverListening = true
});
callback && callback(server);
callback = null;
});
} else if (callback) {
callback(server);
callback = null;
}
});
}
// If started as allInOne mode => return function to create instance
if (module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}