Skip to content

Commit d04eae0

Browse files
committed
feat(onvif-events): Add eventsError event
1 parent f1823d2 commit d04eae0

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

Diff for: .github/workflows/npm.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Node.js Package
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: actions/setup-node@v4
13+
with:
14+
node-version: 20
15+
- run: npm ci
16+
- run: npm test
17+
18+
publish-npm:
19+
needs: build
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v4
23+
- uses: actions/setup-node@v4
24+
with:
25+
node-version: 20
26+
registry-url: https://registry.npmjs.org/
27+
- run: npm ci
28+
- run: npm publish
29+
env:
30+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

Diff for: .github/workflows/publish.yml

-18
This file was deleted.

Diff for: lib/events.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,13 @@ module.exports = function(Cam) {
146146

147147
/**
148148
* Renew pull-point subscription
149-
* @param {options} callback
150-
* @param {function} callback
149+
* @param {Object|Function} [options]
150+
* @param {Function} callback
151151
*/
152-
153152
Cam.prototype.renew = function(options, callback) {
153+
if (!callback) {
154+
callback = options;
155+
}
154156
let urlAddress = null;
155157
let subscriptionId = null;
156158
try {
@@ -335,9 +337,7 @@ module.exports = function(Cam) {
335337
* @private
336338
*/
337339
function _terminationTime(response) {
338-
let result = new Date(Date.now() - response.currentTime.getTime() + response.terminationTime.getTime());
339-
// console.log("Events: Termination Time is " + result);
340-
return result;
340+
return new Date(Date.now() - response.currentTime.getTime() + response.terminationTime.getTime());
341341
}
342342

343343
/**
@@ -351,12 +351,15 @@ module.exports = function(Cam) {
351351
// if there is no pull-point subscription or it has expired, create new subscription
352352
this.createPullPointSubscription(function(error) {
353353
if (!error) {
354+
delete this._eventReconnectms;
354355
this._eventPull();
355-
} else if (typeof error === 'object' && retryErrorCodes.includes( error.code)) {
356-
// connection reset on creation - restart Event loop for pullMessages request
357-
this._restartEventRequest();
356+
} else {
357+
this.emit('eventsError', error);
358+
if (typeof error === 'object' && retryErrorCodes.includes(error.code)) {
359+
// connection reset on creation - restart Event loop for pullMessages request
360+
this._restartEventRequest();
361+
}
358362
}
359-
console.log("Error: " + error.code);
360363
}.bind(this));
361364
} else {
362365
this._eventPull();
@@ -376,10 +379,10 @@ module.exports = function(Cam) {
376379
if (this.listeners('event').length && this.events.subscription) { // check for event listeners, if zero, or no subscription then stop pulling
377380
this.pullMessages({
378381
messageLimit: this.events.messageLimit
379-
}, function(err, data, xml) {
380-
if (!err) {
382+
}, function(error, data, xml) {
383+
if (!error) {
384+
delete this._eventReconnectms;
381385
if (data.notificationMessage) {
382-
this._eventReconnectms = 0;
383386
if (!Array.isArray(data.notificationMessage)) {
384387
data.notificationMessage = [data.notificationMessage];
385388
}
@@ -395,14 +398,15 @@ module.exports = function(Cam) {
395398
this.events.terminationTime = _terminationTime(data); // Axis does not increment the termination time. Use RENEW. Vista returns a termination time with the time now (ie we have expired) even if there was still time left over. Use RENEW
396399

397400
// Axis cameras require us to Rewew the Pull Point Subscription
398-
this.renew({},function(err,data) {
399-
if (!err) {
401+
this.renew({},function(error, data) {
402+
if (!error) {
400403
this.events.terminationTime = _terminationTime(data);
401404
}
402405
this._eventRequest(); // go around the loop again, once the RENEW has completed (and terminationTime updated)
403406
});
404407
} else {
405-
if (typeof err === 'object' && err.code === 'ECONNRESET') {
408+
this.emit('eventsError', error);
409+
if (typeof error === 'object' && retryErrorCodes.includes(error.code)) {
406410
// connection reset - restart Event loop for pullMessages request
407411
this._restartEventRequest();
408412
} else {

Diff for: test/events.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ describe('Events', () => {
9999
assert.ok(gotMessage === 11 || gotMessage === 12);
100100
assert.ok(pullMessagesCallCount > gotMessage && pullMessagesCallCount > 20);
101101
cam.removeListener('event', onEvent);
102-
done();
102+
cam.unsubscribe(done);
103103
}, 1.5 * 1000);
104104
});
105+
it('should return an error when calling renew without subscription', (done) => {
106+
cam.renew({}, (err) => {
107+
assert.ok(err instanceof Error);
108+
done();
109+
});
110+
});
105111
});

0 commit comments

Comments
 (0)