-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
79 lines (64 loc) · 2.43 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
/**
*
* nuki server functions
*
*
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
import {nukiState, setLockState} from './device';
import {adapter} from './nuki';
function initServer(_ip, _port) {
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
// routes will go here
app.get('/api/:key', function(req, res) {
res.send(`Hello ${req.params.key} ;-)`);
});
// POST parameters sent with
app.post(`/api/nuki.${adapter.instance}`, function(req, res) {
let nukiId = req.body.nukiId;
let deviceType = 1;
if (req.body.hasOwnProperty('deviceType')) {
deviceType = req.body.deviceType
} else {
// default to Nuki lock
deviceType = 0;
}
nukiState.state = req.body.state;
nukiState.stateName = req.body.stateName;
nukiState.batteryCritical = req.body.batteryCritical;
if (req.body.hasOwnProperty('mode')) {
nukiState.mode = req.body.mode;
} else {
// default to door mode
nukiState.mode = '2';
}
if (req.body.hasOwnProperty('batteryCharging')) {
nukiState.batteryCharging = req.body.batteryCharging;
}
if (req.body.hasOwnProperty('batteryChargeState')) {
nukiState.batteryChargeState = req.body.batteryChargeState;
}
if (req.body.hasOwnProperty('keypadBatteryCritical')) {
nukiState.keypadBatteryCritical = req.body.keypadBatteryCritical;
}
if (req.body.hasOwnProperty('doorsensorState')) {
nukiState.doorsensorState = req.body.doorsensorState;
nukiState.doorsensorStateName = req.body.doorsensorStateName;
}
if (req.body.hasOwnProperty('ringactionState')) {
nukiState.ringactionState = req.body.ringactionState;
nukiState.ringactionTimestamp = req.body.ringactionTimestamp;
}
try {
adapter.log.info(`status change received for NukiID ${nukiId}: ${nukiState.stateName}`);
adapter.log.info(`battery status received for NukiID ${nukiId}: ${nukiState.batteryCritical}`);
setLockState(nukiId, deviceType, nukiState);
res.sendStatus(200);
} catch (e) {
res.sendStatus(500);
adapter.log.warn(e.message);
}
});
}