-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
203 lines (175 loc) · 6.78 KB
/
server.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
196
197
198
199
200
201
202
var config = require('config');
var bluetoothSerialPort = require('bluetooth-serial-port');
var express = require('express');
var btmac = config.get('BtClock.mac');
var btchannel = config.get('BtClock.channel');
var bttimeout = config.get('BtClock.timeout')*1;
var readonly = config.get('readonly');
var host = config.get('host');
var port = config.get('port');
var baseurl = config.get('baseurl');
for (var i=2;i<process.argv.length;i++) {
var param = process.argv[i];
switch(param) {
case '--host':
host = process.argv[++i];
break;
case '--port':
port = process.argv[++i];
break;
case '--baseurl':
baseurl = process.argv[++i];
break;
case '--mac':
btmac = process.argv[++i];
break;
case '--channel':
btchannel = process.argv[++i];
break;
case '--readonly':
readonly = true;
break;
case '-h':
case '--help':
default:
if (param != '-h' && param != '--help')
console.log('Unknown parameter: ' + param);
console.log('Usage:');
console.log(process.argv[0] + ' ' + process.argv[1] + ' -- [--host <host>] [--port <port>] [--baseurl <base url>] [--readonly] [--mac <bluetooth address>] [--channel <bluetooth channel>]');
console.log(' --host: host ip to bind to. Default: ' + config.get('host'));
console.log(' --port: port to bind to. Default: ' + config.get('port'));
console.log(' --baseurl: base URL under which the api endpoints will be served. Default: ' + config.get('baseurl'));
console.log(' --readonly: if given, prevents the web api from sending bluetooth requests which change eeprom values in the btclock. Default: ' + config.get('readonly'));
console.log(' --mac: bluetooth address of the btclock. Default: ' + config.get('BtClock.mac'));
console.log(' --channel: bluetooth channel of the btclock to connect to. Default: ' + config.get('BtClock.channel'));
console.log(' --timeout: Timeout in ms after which a bluetooth request will be considered as unanswered. Default: ' + config.get('BtClock.timeout'));
process.exit();
break;
}
}
var app = express();
var btClock = new (require('./bt-clock.js'))(btmac, btchannel, bttimeout);
function delay(value) {
return new Promise( (resolve, reject) => { setTimeout( () => resolve(value), 500 ); } );
}
function readonlyGuard() {
return new Promise( (resolve, reject) => {
if (readonly)
reject('readonly');
resolve();
});
}
app.get('/', function(req, res) {
res.redirect(baseurl + '/datetime');
});
app.get(baseurl, function(req, res) {
var response = {};
var errorResponse = null;
btClock.datetime.then( (v) => { response.datetime = v.datetime; } )
.then( (v) => delay(v) )
.then( (v) => btClock.blanktime.config ).then( (v) => { response.blanktime = v.blanktime; } )
.then( (v) => delay(v) )
.then( (v) => btClock.sequence.config ).then( (v) => { response.sequence = v.sequence; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline.config ).then( (v) => { response.specialline = v.specialline; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline.secondconfig ).then( (v) => { response.specialline.secondconfig = v.specialline.secondconfig; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline[1] ).then( (v) => { response.specialline[1] = v.specialline[1]; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline[2] ).then( (v) => { response.specialline[2] = v.specialline[2]; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline[3] ).then( (v) => { response.specialline[3] = v.specialline[3]; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline[4] ).then( (v) => { response.specialline[4] = v.specialline[4]; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline[5] ).then( (v) => { response.specialline[5] = v.specialline[5]; } )
.then( (v) => { res.type('json').send(JSON.stringify(response)); } )
.catch( (e) => { res.status(500).send(e) } );
});
app.get(baseurl + '/datetime', function(req, res) {
if (req.query.iso8601) {
readonlyGuard()
.then( () => btClock.setDateTime(new Date(req.query.iso8601)) )
.then( (value) => { res.send('OK') } )
.catch( (error) => { res.status(500).send(error) } )
} else {
btClock.datetime
.then( (value) => { res.type('json').json(value) } )
.catch( (error) => { res.status(500).send(error) } )
}
});
app.get(baseurl + '/sequence', function(req, res) {
if (req.query.config) {
readonlyGuard()
.then( () => btClock.setSequence(req.query.config) )
.then( (value) => { res.send('OK') } )
.catch( (error) => { res.status(500).send(error) } )
} else {
btClock.sequence.config
.then( (value) => { res.type('json').json(value) } )
.catch( (error) => { res.status(500).send(error) } )
}
});
app.get(baseurl + '/blanktime', function(req, res) {
if (req.query.config) {
readonlyGuard()
.then( () => btClock.setBlankTime(req.query.config) )
.then( (value) => { res.send('OK') } )
.catch( (error) => { res.status(500).send(error) } )
} else {
btClock.blanktime.config
.then( (value) => { res.type('json').json(value) } )
.catch( (error) => { res.status(500).send(error) } )
}
});
app.get(baseurl + '/specialline', function(req, res) {
var promise = null;
if (req.query.config || req.query.secondconfig)
promise = readonlyGuard();
if (req.query.config ) {
promise = promise
.then( () => btClock.setSpecialLineConfig(req.query.config) )
}
if (req.query.secondconfig) {
promise = promise.then( (v) => delay(v) )
.then( () => btClock.setSpecialLineSecondConfig(req.query.secondconfig) )
}
if (promise != null) {
promise
.then( (value) => { res.send('OK') } )
.catch( (error) => { res.status(500).send(error) } )
} else {
var response = {};
var errorResponse = null;
btClock.specialline.config.then( (v) => { response.specialline = v.specialline; } )
.then( (v) => delay(v) )
.then( (v) => btClock.specialline.secondconfig ).then( (v) => { response.specialline.secondconfig = v.specialline.secondconfig; } )
.then( (v) => { res.type('json').send(JSON.stringify(response)); } )
.catch( (e) => { res.status(500).send(e) } );
}
});
app.get(new RegExp(baseurl + "/specialline/0"), function(req, res) {
if (req.query.line) {
btClock.setSpecialLine(0, req.query.line)
.then( (value) => { res.send('OK') } )
.catch( (error) => { res.status(500).send(error) } )
} else {
res.status(500).send('');
}
});
app.get(new RegExp(baseurl + "/specialline/\([1-5]\)"), function(req, res) {
if (req.query.line) {
readonlyGuard()
.then( () => btClock.setSpecialLine(req.params[0], req.query.line) )
.then( (value) => { res.send('OK') } )
.catch( (error) => { res.status(500).send(error) } )
} else {
btClock.specialline[req.params[0]]
.then( (value) => { res.type('json').json(value) } )
.catch( (error) => { res.status(500).send(error) } )
}
});
app.listen(port, function() {
console.log('Listening on host ' + host + ', port ' + port);
});