From a3f5fa9ee640314722a34de536f5a845fc68115e Mon Sep 17 00:00:00 2001 From: Dan Cunningham Date: Tue, 16 Oct 2018 11:45:11 -0700 Subject: [PATCH] Fixes #184 (#217) Signed-off-by: digitaldan --- .../AndroidRegistrationService.js | 111 ------------------ .../AppleRegistrationService.js | 68 ----------- .../GenericRegistrationService.js | 95 --------------- models/userdevice.js | 6 +- routes/androidRegistrationService.js | 104 ++++++++++++++++ routes/appleRegistrationService.js | 61 ++++++++++ routes/index.js | 11 +- 7 files changed, 172 insertions(+), 284 deletions(-) delete mode 100644 mobileregistrationservice/AndroidRegistrationService.js delete mode 100644 mobileregistrationservice/AppleRegistrationService.js delete mode 100644 mobileregistrationservice/GenericRegistrationService.js create mode 100644 routes/androidRegistrationService.js create mode 100644 routes/appleRegistrationService.js diff --git a/mobileregistrationservice/AndroidRegistrationService.js b/mobileregistrationservice/AndroidRegistrationService.js deleted file mode 100644 index 086cddb..0000000 --- a/mobileregistrationservice/AndroidRegistrationService.js +++ /dev/null @@ -1,111 +0,0 @@ -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; \ No newline at end of file diff --git a/mobileregistrationservice/AppleRegistrationService.js b/mobileregistrationservice/AppleRegistrationService.js deleted file mode 100644 index a75be5d..0000000 --- a/mobileregistrationservice/AppleRegistrationService.js +++ /dev/null @@ -1,68 +0,0 @@ -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: 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 device token and save - self.getLogger().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: self.getDeviceId(), - 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; \ No newline at end of file diff --git a/mobileregistrationservice/GenericRegistrationService.js b/mobileregistrationservice/GenericRegistrationService.js deleted file mode 100644 index 0f4df34..0000000 --- a/mobileregistrationservice/GenericRegistrationService.js +++ /dev/null @@ -1,95 +0,0 @@ -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; \ No newline at end of file diff --git a/models/userdevice.js b/models/userdevice.js index e3654d4..242a5ca 100644 --- a/models/userdevice.js +++ b/models/userdevice.js @@ -7,11 +7,11 @@ var mongoose = require('mongoose'), var UserDeviceSchema = new Schema({ owner: {type: ObjectId, required: true}, - androidRegistration: {type: String, unique: true}, - iosDeviceToken: {type: String, unique: true}, + androidRegistration: {type: String}, + iosDeviceToken: {type: String}, deviceType: {type: String}, deviceModel: {type: String}, - deviceId: {type: String, unique: true}, + deviceId: {type: String}, globalLocation: {type: [Number], index: '2d'}, globalAltitude: {type: Number}, globalAccuracy: {type: Number}, diff --git a/routes/androidRegistrationService.js b/routes/androidRegistrationService.js new file mode 100644 index 0000000..3c71f8c --- /dev/null +++ b/routes/androidRegistrationService.js @@ -0,0 +1,104 @@ +var UserDevice = require('../models/userdevice'); +var logger = require('../logger'); + +/** + * 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 + */ +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']; + + // Try to find user device by device Id + UserDevice.findOne({ + owner: req.user.id, + deviceType: 'android', + 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 the changed registration id + logger.info('openHAB-cloud: Found an Android device for user ' + req.user.username + ', updating'); + userDevice.androidRegistration = 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, 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 + findAndroidDeviceByRegistrationId(req, regId, res, deviceId, deviceModel); + } + }); + + /** + * 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 + */ + var 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) { + logger.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) { + logger.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) { + logger.error('openHAB-cloud: Error saving user device: ' + error); + } + }); + res.send(200, 'Added'); + } + }); + }; +}; \ No newline at end of file diff --git a/routes/appleRegistrationService.js b/routes/appleRegistrationService.js new file mode 100644 index 0000000..4f47e61 --- /dev/null +++ b/routes/appleRegistrationService.js @@ -0,0 +1,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'); + } + }); +}; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 613e133..b26884e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -14,8 +14,8 @@ var system = require('../system'), api_routes = require('./api'), oauth2 = require('./oauth2'), setSessionTimezone = require('./setTimezone'), - AndroidRegistrationService = require('../mobileregistrationservice/AndroidRegistrationService'), - AppleRegistrationService = require('../mobileregistrationservice/AppleRegistrationService'); + androidRegistrationService = require('./androidRegistrationService'), + appleRegistrationService = require('./appleRegistrationService'); ifttt_routes = require('./ifttt'); /** @@ -232,16 +232,13 @@ Routes.prototype.setupProxyRoutes = function (app) { }; Routes.prototype.setupAppRoutes = function (app) { - var appleRegistration = new AppleRegistrationService(this.logger), - androidRegistration = new AndroidRegistrationService(this.logger); - // myOH API for mobile apps app.all('/api/v1/notifications*', this.ensureRestAuthenticated, this.preassembleBody, this.setOpenhab, api_routes.notificationsget); app.all('/api/v1/settings/notifications', this.ensureRestAuthenticated, this.preassembleBody, this.setOpenhab, api_routes.notificationssettingsget); // Android app registration - app.all('/addAndroidRegistration*', this.ensureRestAuthenticated, this.preassembleBody, this.setOpenhab, androidRegistration.register.bind(androidRegistration)); - app.all('/addAppleRegistration*', this.ensureRestAuthenticated, this.preassembleBody, this.setOpenhab, appleRegistration.register.bind(appleRegistration)); + app.all('/addAndroidRegistration*', this.ensureRestAuthenticated, this.preassembleBody, this.setOpenhab, androidRegistrationService); + app.all('/addAppleRegistration*', this.ensureRestAuthenticated, this.preassembleBody, this.setOpenhab, appleRegistrationService); }; // Ensure user is authenticated for web requests