Skip to content

Commit 7ba9a59

Browse files
Add emit for all messages to support heartbeat / keep alive and get uploadmedia events (#31)
* Clean-up * Added emit for all messages * Added emit for all messages * Added loginAndLeepAlive forever Co-authored-by: Geoff <[email protected]>
1 parent df69d07 commit 7ba9a59

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/Arlo.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ class Arlo extends EventEmitter {
8787
}.bind(this)
8888
);
8989
}
90+
91+
keepAlive = true
92+
async loginAndKeepAlive(user, password, callback, heartbeat = 30000) {
93+
let msLastResponse = 0;
94+
this.addListener(Constants.EVENT_MESSAGE, () =>
95+
msLastResponse = new Date());
96+
while (this.keepAlive) {
97+
if (new Date() - msLastResponse > heartbeat)
98+
this.login(user, password, callback);
99+
else if (this.token)
100+
this.getDevices();
101+
await new Promise(resolve => setTimeout(resolve, heartbeat));
102+
}
103+
}
90104

91105
subscribe(callback) {
92106
let reCamera = /cameras\/(.+)$/;
@@ -113,7 +127,7 @@ class Arlo extends EventEmitter {
113127
}
114128

115129
data = msg.data;
116-
130+
this.emit(Constants.EVENT_MESSAGE,data);
117131
switch (data.resource) {
118132
case Constants.RESOURCE_CAMERAS:
119133
for (let i = 0; i < msg.data.properties.length; i++) {
@@ -342,6 +356,7 @@ Arlo.CHARGING = Constants.EVENT_CHARGING;
342356
Arlo.FOUND = Constants.EVENT_FOUND;
343357
Arlo.MOTION = Constants.EVENT_MOTION;
344358
Arlo.UPDATE = Constants.EVENT_UPDATE;
359+
Arlo.MESSAGE = Constants.EVENT_MESSAGE;
345360

346361
Arlo.FF_SNAPSHOT = Constants.FF_SNAPSHOT_AVAILABLE;
347362

lib/ArloConstants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
EVENT_FOUND : 'found',
2121
EVENT_MOTION : 'motionDetected',
2222
EVENT_UPDATE : 'update',
23+
EVENT_MESSAGE : 'message',
2324

2425
EVENT_LOGGED_IN : 'logged_in',
2526
EVENT_GOT_DEVICES : 'got_devices',

0 commit comments

Comments
 (0)