forked from openhab/openhab-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcm-xmpp.js
131 lines (113 loc) · 5.49 KB
/
gcm-xmpp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var system = require('./system');
/*
This module maintains XMPP connection to GCM to receive messages from Android
app.
*/
var UserDevice = require('./models/userdevice'),
UserDeviceLocationHistory = require('./models/userdevicelocationhistory'),
xmpp = require('node-xmpp'),
logger = require('./logger.js'),
xmppOptions = {
type: 'client',
jid: system.getGcmJid(),
password: system.getGcmPassword(),
port: 5235,
host: 'gcm.googleapis.com',
legacySSL: true,
preferredSaslMechanism : 'PLAIN'
},
xmppClient, gcmSender, gcm;
logger.info('openHAB-cloud: Initializing XMPP connection to GCM');
xmppClient = new xmpp.Client(xmppOptions);
gcmSender = require('./gcmsender.js');
gcm = require('node-gcm');
xmppClient.on('online', function() {
logger.info('openHAB-cloud: GCM XMPP connection is online');
});
xmppClient.on('stanza', function(stanza) {
if (stanza.is('message') && stanza.attrs.type !== 'error') {
var messageData = JSON.parse(stanza.getChildText('gcm'));
logger.info('openHAB-cloud: GCM XMPP received message');
if (messageData && messageData.message_type !== 'ack' && messageData.message_type !== 'nack') {
var 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'
}));
xmppClient.send(ackMsg);
logger.info('openHAB-cloud: GCM XMPP ack sent');
if (messageData.data.type === 'location') {
logger.info('openHAB-cloud: This is a location message');
UserDevice.findOne({androidRegistration: messageData.from}, function (error, userDevice) {
var 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();
});
} else if (messageData.data.type === 'hideNotification') {
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) {
var gcmMessage;
// TODO: now send hideNotification message to all devices except the source one
var registrationIds = [];
for (var i=0; i<userDevices.length; i++) {
var 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;
}
gcmMessage = new gcm.Message({
delayWhileIdle: false,
data: {
type: 'hideNotification',
notificationId: messageData.data.notificationId
}
});
gcmSender.send(gcmMessage, registrationIds, 4, function (err, result) {
if (err) {
logger.error('mopenHAB-cloud: GCM send error: ' + err);
}
});
});
});
}
} else {
logger.info('openHAB-cloud: GCM XMPP message is ack or nack, ignoring');
}
}
});
xmppClient.on('error', function (error) {
logger.warn('openHAB-cloud: GCM XMPP error: ' + error);
});
module.exports = xmppClient;