Skip to content

Commit

Permalink
Add new mobile device registration services
Browse files Browse the repository at this point in the history
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
FlorianSW committed May 6, 2017
1 parent ff57889 commit 6db1703
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 183 deletions.
111 changes: 111 additions & 0 deletions mobileregistrationservice/AndroidRegistrationService.js
@@ -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;
68 changes: 68 additions & 0 deletions mobileregistrationservice/AppleRegistrationService.js
@@ -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;
95 changes: 95 additions & 0 deletions mobileregistrationservice/GenericRegistrationService.js
@@ -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;
1 change: 0 additions & 1 deletion requesttracker/index.js
Expand Up @@ -44,7 +44,6 @@ RequestTracker.prototype.getAll = function () {
* @return {number}
*/
RequestTracker.prototype.acquireRequestId = function () {
console.log('ID acquired');
return this.requestCounter++;

};
Expand Down

0 comments on commit 6db1703

Please sign in to comment.