Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new mobile device registration services
These services are used to register a mobile device to the currently logged in user account. By this, the methods are decoupled from the routes.
- Loading branch information
Showing
5 changed files
with
281 additions
and
183 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
mobileregistrationservice/AndroidRegistrationService.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
var inherits = require('util').inherits, | ||
GenericRegistrationService = require('./GenericRegistrationService'), | ||
UserDevice = require('../models/userdevice'); | ||
|
||
function AndroidRegistrationService () { | ||
GenericRegistrationService.apply(this, arguments); | ||
} | ||
|
||
inherits(AndroidRegistrationService, GenericRegistrationService); | ||
|
||
/** | ||
* Registers the Android device of the request to the logged in user, if it is not already registered, | ||
* otherwise it will be updated. | ||
* | ||
* @param req | ||
* @param res | ||
*/ | ||
AndroidRegistrationService.prototype.register = function (req, res) { | ||
var self = this; | ||
|
||
if (!this.validateRequest(req, res)) { | ||
return; | ||
} | ||
|
||
// Try to find user device by device Id | ||
UserDevice.findOne({ | ||
owner: req.user.id, | ||
deviceType: 'android', | ||
deviceId: this.getDeviceId() | ||
}, function (error, userDevice) { | ||
if (error) { | ||
self.getLogger().warn('openHAB-cloud: Error looking up device: ' + error); | ||
res.send(500, 'Internal server error'); | ||
return; | ||
} | ||
|
||
if (userDevice) { | ||
// If found, update the changed registration id | ||
self.getLogger().info('openHAB-cloud: Found an Android device for user ' + req.user.username + ', updating'); | ||
userDevice.androidRegistration = self.getRegistrationId(); | ||
userDevice.lastUpdate = new Date(); | ||
userDevice.save(function (error) { | ||
if (error) { | ||
self.getLogger().error('openHAB-cloud: Error saving user device: ' + error); | ||
} | ||
}); | ||
res.send(200, 'Updated'); | ||
} else { | ||
// If not found, try to find device by registration id. Sometimes android devices change their | ||
// ids dynamically, while google play services continue to return the same registration id | ||
// so this is still the same device and we don't want any duplicates | ||
self.findAndroidDeviceByRegistrationId(req, self.getRegistrationId(), res, self.getDeviceId(), self.getDeviceModel()); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* Tries to find an android device using the registration ID and sets the given deviceId to this UserDevice. | ||
* | ||
* @param req | ||
* @param registrationId | ||
* @param res | ||
* @param deviceId | ||
* @param deviceModel | ||
*/ | ||
AndroidRegistrationService.prototype.findAndroidDeviceByRegistrationId = function (req, registrationId, res, deviceId, deviceModel) { | ||
var self = this; | ||
|
||
UserDevice.findOne({ | ||
owner: req.user.id, | ||
deviceType: 'android', | ||
androidRegistration: registrationId | ||
}, | ||
function (error, userDevice) { | ||
if (error) { | ||
self.getLogger().warn('openHAB-cloud: Error looking up device: ' + error); | ||
res.send(500, 'Internal server error'); | ||
return; | ||
} | ||
if (userDevice) { | ||
// If found, update the changed device id | ||
userDevice.deviceId = deviceId; | ||
userDevice.lastUpdate = new Date(); | ||
userDevice.save(function (error) { | ||
if (error) { | ||
self.getLogger().error('openHAB-cloud: Error saving user device: ' + error); | ||
} | ||
}); | ||
res.send(200, 'Updated'); | ||
} else { | ||
// If not found, finally register a new one | ||
userDevice = new UserDevice({ | ||
owner: req.user.id, | ||
deviceType: 'android', | ||
deviceId: deviceId, | ||
androidRegistration: registrationId, | ||
deviceModel: deviceModel, | ||
lastUpdate: new Date(), | ||
registered: new Date() | ||
}); | ||
userDevice.save(function (error) { | ||
if (error) { | ||
self.getLogger().error('openHAB-cloud: Error saving user device: ' + error); | ||
} | ||
}); | ||
res.send(200, 'Added'); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = AndroidRegistrationService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var inherits = require('util').inherits, | ||
GenericRegistrationService = require('./GenericRegistrationService'), | ||
UserDevice = require('../models/userdevice'); | ||
|
||
function AppleRegistrationService () { | ||
GenericRegistrationService.apply(this, arguments); | ||
} | ||
|
||
inherits(AppleRegistrationService, GenericRegistrationService); | ||
|
||
/** | ||
* 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 | ||
*/ | ||
AppleRegistrationService.prototype.register = function (req, res) { | ||
var self = this; | ||
|
||
if (!this.validateRequest(req, res)) { | ||
return; | ||
} | ||
|
||
UserDevice.findOne({ | ||
owner: req.user.id, | ||
deviceType: 'ios', | ||
deviceId: deviceId | ||
}, function (error, userDevice) { | ||
if (error) { | ||
self.getLogger().warn('openHAB-cloud: Error looking up device: ' + error); | ||
res.send(500, 'Internal server error'); | ||
return; | ||
} | ||
if (userDevice) { | ||
// If found, update device token and save | ||
self.logger.info('openHAB-cloud: Found iOS device for user ' + req.user.username + ', updating'); | ||
userDevice.iosDeviceToken = self.getRegistrationId(); | ||
userDevice.lastUpdate = new Date(); | ||
userDevice.save(function (error) { | ||
if (error) { | ||
self.getLogger().error('openHAB-cloud: Error saving user device: ' + error); | ||
} | ||
}); | ||
res.send(200, 'Updated'); | ||
} else { | ||
// If not found, add new device registration | ||
self.getLogger().info('openHAB-cloud: Registering new iOS device for user ' + req.user.username); | ||
userDevice = new UserDevice({ | ||
owner: req.user.id, | ||
deviceType: 'ios', | ||
deviceId: deviceId, | ||
iosDeviceToken: self.getRegistrationId(), | ||
deviceModel: self.getDeviceModel(), | ||
lastUpdate: new Date(), | ||
registered: new Date() | ||
}); | ||
userDevice.save(function (error) { | ||
if (error) { | ||
self.getLogger().error('openHAB-cloud: Error saving user device: ' + error); | ||
} | ||
}); | ||
res.send(200, 'Added'); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = AppleRegistrationService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
var GenericRegistrationService = function (logger) { | ||
this.registrationId = 0; | ||
this.deviceId = 'unknown'; | ||
this.deviceModel = 'unknown'; | ||
this.log = logger; | ||
}; | ||
|
||
/** | ||
* Validates the given request and, if the validation fails, sets the passed response accordingly. | ||
* | ||
* @param req | ||
* @param res | ||
* @return {boolean} True, if the validation succeeded, false otherwise. | ||
*/ | ||
GenericRegistrationService.prototype.validateRequest = function (req, res) { | ||
if (!req.query.hasOwnProperty('regId')) { | ||
res.send(404, 'Parameters missing'); | ||
return false; | ||
} | ||
this.setRegistrationId(req.query['regId']); | ||
if (req.query.hasOwnProperty('deviceId')) { | ||
this.setDeviceId(req.query['deviceId']); | ||
} | ||
|
||
if (req.query.hasOwnProperty('deviceModel')) { | ||
this.setDeviceModel(req.query['deviceModel']); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Returns the logger for this service. | ||
* | ||
* @return {logger} | ||
*/ | ||
GenericRegistrationService.prototype.getLogger = function () { | ||
return this.log; | ||
}; | ||
|
||
/** | ||
* Sets the registration ID, which will later be used to register the device. | ||
* | ||
* @param regId | ||
*/ | ||
GenericRegistrationService.prototype.setRegistrationId = function (regId) { | ||
this.registrationId = regId; | ||
}; | ||
|
||
/** | ||
* Returns the set registration ID from the request. | ||
* | ||
* @return {number|*} | ||
*/ | ||
GenericRegistrationService.prototype.getRegistrationId = function () { | ||
return this.registrationId; | ||
}; | ||
|
||
/** | ||
* Sets the device ID. | ||
* | ||
* @param deviceId | ||
*/ | ||
GenericRegistrationService.prototype.setDeviceId = function (deviceId) { | ||
this.deviceId = deviceId; | ||
}; | ||
|
||
/** | ||
* Returns the device ID, if it was set before, otherwise this function returns 'unknown'. | ||
* | ||
* @return {string} | ||
*/ | ||
GenericRegistrationService.prototype.getDeviceId = function () { | ||
return this.deviceId; | ||
}; | ||
|
||
/** | ||
* Sets the device model. | ||
* | ||
* @param deviceModel | ||
*/ | ||
GenericRegistrationService.prototype.setDeviceModel = function (deviceModel) { | ||
this.deviceModel = deviceModel; | ||
}; | ||
|
||
/** | ||
* Returns the device model, if it was set before, otherwise 'unknown' will be returned. | ||
* | ||
* @return {string} | ||
*/ | ||
GenericRegistrationService.prototype.getDeviceModel = function () { | ||
return this.deviceModel; | ||
}; | ||
|
||
module.exports = GenericRegistrationService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.