forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appleRegistrationService.js
61 lines (59 loc) · 2.12 KB
/
appleRegistrationService.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
var UserDevice = require('../models/userdevice');
var logger = require('../logger');
/**
* registers the apple device, which is request by this request, to the logged in user, or upgrades it,
* if it is already registered.
*
* @param req
* @param res
*/
module.exports = function (req, res) {
if (!req.query.hasOwnProperty('regId') || !req.query.hasOwnProperty('deviceId')) {
res.send(404, 'Parameters missing');
return;
}
var regId = req.query['regId'];
var deviceId = req.query['deviceId'];
var deviceModel = req.query['deviceModel'];
UserDevice.findOne({
owner: req.user.id,
deviceType: 'ios',
deviceId: deviceId
}, function (error, userDevice) {
if (error) {
logger.warn('openHAB-cloud: Error looking up device: ' + error);
res.send(500, 'Internal server error');
return;
}
if (userDevice) {
// If found, update device token and save
logger.info('openHAB-cloud: Found iOS device for user ' + req.user.username + ', updating');
userDevice.iosDeviceToken = regId;
userDevice.lastUpdate = new Date();
userDevice.save(function (error) {
if (error) {
logger.error('openHAB-cloud: Error saving user device: ' + error);
}
});
res.send(200, 'Updated');
} else {
// If not found, add new device registration
logger.info('openHAB-cloud: Registering new iOS device for user ' + req.user.username);
userDevice = new UserDevice({
owner: req.user.id,
deviceType: 'ios',
deviceId: deviceId,
iosDeviceToken: regId,
deviceModel: deviceModel,
lastUpdate: new Date(),
registered: new Date()
});
userDevice.save(function (error) {
if (error) {
logger.error('openHAB-cloud: Error saving user device: ' + error);
}
});
res.send(200, 'Added');
}
});
};