forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
215 deletions.
There are no files selected for viewing
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
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,131 @@ | ||
const system = require('./system'); | ||
|
||
/** | ||
* This module maintains XMPP connection to FCM to receive messages from Android | ||
* app. | ||
*/ | ||
|
||
const UserDevice = require('./models/userdevice'), | ||
UserDeviceLocationHistory = require('./models/userdevicelocationhistory'), | ||
xmpp = require('node-xmpp-client'), | ||
Firebase = require('firebase-messaging'), | ||
firebase = require('./notificationsender/firebase'), | ||
logger = require('./logger.js'), | ||
xmppOptions = { | ||
type: 'client', | ||
jid: system.getGcmJid(), | ||
password: system.getGcmPassword(), | ||
port: 5235, | ||
host: 'fcm-xmpp.googleapis.com', | ||
legacySSL: true, | ||
}; | ||
|
||
let xmppClient; | ||
|
||
const ACK_MESSAGE_TYPE = 'ack'; | ||
const NACK_MESSAGE_TYPE = 'nack'; | ||
|
||
logger.info('openHAB-cloud: Initializing XMPP connection to GCM'); | ||
xmppClient = new xmpp.Client(xmppOptions); | ||
|
||
xmppClient.on('online', function() { | ||
logger.info('openHAB-cloud: GCM XMPP connection is online'); | ||
}); | ||
|
||
function updateLocationOfDevice(messageData) { | ||
logger.info('openHAB-cloud: This is a location message'); | ||
UserDevice.findOne({androidRegistration: messageData.from}, function (error, userDevice) { | ||
let newLocation; | ||
|
||
if (error) { | ||
logger.warn('openHAB-cloud: Error finding user device: ' + error); | ||
return; | ||
} | ||
if (!userDevice) { | ||
logger.warn('openHAB-cloud: Unable to find user device with reg id = ' + messageData.from); | ||
return; | ||
} | ||
|
||
userDevice.globalLocation = [messageData.data.latitude, messageData.data.longitude]; | ||
userDevice.globalAccuracy = messageData.data.accuracy; | ||
userDevice.globalAltitude = messageData.data.altitude; | ||
userDevice.lastGlobalLocation = new Date(messageData.data.timestamp); | ||
userDevice.save(); | ||
newLocation = new UserDeviceLocationHistory({userDevice: userDevice.id}); | ||
newLocation.globalLocation = [messageData.data.latitude, messageData.data.longitude]; | ||
newLocation.when = new Date(messageData.data.timestamp); | ||
newLocation.globalAltitude = messageData.data.altitude; | ||
newLocation.globalAccuracy = messageData.data.accuracy; | ||
newLocation.save(); | ||
}); | ||
} | ||
|
||
function hideNotificationInfo(messageData) { | ||
logger.info('openHAB-cloud: This is hideNotification message'); | ||
UserDevice.findOne({androidRegistration: messageData.from}, function (error, userDevice) { | ||
if (error) { | ||
logger.warn('openHAB-cloud: Error finding user device: ' + error); | ||
return; | ||
} | ||
|
||
if (!userDevice) { | ||
logger.warn('openHAB-cloud: Unable to find user device with reg id = ' + messageData.from); | ||
return; | ||
} | ||
|
||
UserDevice.find({owner: userDevice.owner}, function (error, userDevices) { | ||
// TODO: now send hideNotification data to all devices except the source one | ||
const registrationIds = []; | ||
for (let i = 0; i < userDevices.length; i++) { | ||
const uDevice = userDevices[i]; | ||
// Skip the device which sent notification hide itself | ||
if (uDevice.androidRegistration !== userDevice.androidRegistration) { | ||
registrationIds.push(uDevice.androidRegistration); | ||
} | ||
} | ||
if (registrationIds.length < 0) { | ||
return; | ||
} | ||
|
||
firebase.hideNotification(registrationIds, messageData.data.notificationId); | ||
}); | ||
}); | ||
} | ||
|
||
xmppClient.on('stanza', function(stanza) { | ||
if (!stanza.is('message') || stanza.attrs.type === 'error') { | ||
return; | ||
} | ||
|
||
logger.info('openHAB-cloud: GCM XMPP received message'); | ||
|
||
const messageData = JSON.parse(stanza.getChildText('gcm')); | ||
if (messageData && messageData.message_type === ACK_MESSAGE_TYPE || messageData.message_type === NACK_MESSAGE_TYPE) { | ||
return; | ||
} | ||
|
||
const ackMsg = new xmpp.Element('message') | ||
.c('gcm', { xmlns: 'google:mobile:data' }) | ||
.t(JSON.stringify({ | ||
'to': messageData.from, | ||
'message_id': messageData.message_id, | ||
'message_type': ACK_MESSAGE_TYPE | ||
})); | ||
|
||
xmppClient.send(ackMsg); | ||
|
||
logger.info('openHAB-cloud: GCM XMPP ack sent'); | ||
if (messageData.data.type === 'location') { | ||
updateLocationOfDevice(messageData); | ||
} | ||
|
||
if (messageData.data.type === 'hideNotification') { | ||
hideNotificationInfo(messageData); | ||
} | ||
}); | ||
|
||
xmppClient.on('error', function (error) { | ||
logger.warn('openHAB-cloud: GCM XMPP error: ' + error); | ||
}); | ||
|
||
module.exports = xmppClient; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.