diff --git a/lib/modules/device.js b/lib/modules/device.js index b0fe712..4fb12db 100644 --- a/lib/modules/device.js +++ b/lib/modules/device.js @@ -37,14 +37,16 @@ function OnvifDevice(params) { this.xaddr = ''; this.user = ''; this.pass = ''; + this.keepAddr = false; if(('xaddr' in params) && typeof(params['xaddr']) === 'string') { this.xaddr = params['xaddr']; let ourl = mUrl.parse(this.xaddr); this.address = ourl.hostname; } else if(('address' in params) && typeof(params['address']) === 'string') { + this.keepAddr = true; this.address = params['address']; - this.xaddr = 'http://' + this.address +':80/onvif/device_service'; + this.xaddr = 'http://' + this.address +'/onvif/device_service'; } else { throw new Error('The parameter was invalid.'); } @@ -400,7 +402,7 @@ OnvifDevice.prototype._getCapabilities = function() { let events = c['Events']; if(events && events['XAddr']) { this.services.events = new mOnvifServiceEvents({ - 'xaddr' : events['XAddr'], + 'xaddr' : this._getXaddr(events['XAddr']), 'time_diff': this.time_diff, 'user' : this.user, 'pass' : this.pass @@ -420,7 +422,7 @@ OnvifDevice.prototype._getCapabilities = function() { let media = c['Media']; if(media && media['XAddr']) { this.services.media = new mOnvifServiceMedia({ - 'xaddr': media['XAddr'], + 'xaddr' : this._getXaddr(media['XAddr']), 'time_diff': this.time_diff, 'user' : this.user, 'pass' : this.pass @@ -429,7 +431,7 @@ OnvifDevice.prototype._getCapabilities = function() { let ptz = c['PTZ']; if(ptz && ptz['XAddr']) { this.services.ptz = new mOnvifServicePtz({ - 'xaddr': ptz['XAddr'], + 'xaddr' : this._getXaddr(ptz['XAddr']), 'time_diff': this.time_diff, 'user' : this.user, 'pass' : this.pass @@ -601,6 +603,7 @@ OnvifDevice.prototype._mediaGetStreamURI = function() { this.services.media.getStreamUri(params, (error, result) => { if(!error) { let uri = result['data']['GetStreamUriResponse']['MediaUri']['Uri']; + uri = this._getUri(uri); this.profile_list[profile_index]['stream'][protocol.toLowerCase()] = uri; } protocol_index ++; @@ -632,8 +635,10 @@ OnvifDevice.prototype._mediaGetSnapshotUri = function() { this.services.media.getSnapshotUri(params, (error, result) => { if(!error) { try { - profile['snapshot'] = result['data']['GetSnapshotUriResponse']['MediaUri']['Uri']; - } catch(e) {} + let snapshotUri = result['data']['GetSnapshotUriResponse']['MediaUri']['Uri']; + snapshotUri = this._getSnapshotUri(snapshotUri); + profile['snapshot'] = snapshotUri; + } catch(e) { console.log(e);} } profile_index ++; getSnapshotUri(); @@ -647,4 +652,35 @@ OnvifDevice.prototype._mediaGetSnapshotUri = function() { return promise; }; +OnvifDevice.prototype._getXaddr = function(directXaddr) { + if (!this.keepAddr) return directXaddr; + const path = mUrl.parse(directXaddr).path; + return 'http://' + this.address + path; +} + +OnvifDevice.prototype._getUri = function(directUri) { + if (!this.keepAddr) return directUri; + const base = mUrl.parse('http://' + this.address); + const parts = mUrl.parse(directUri); + const newParts = { + host: base.host, + pathname: base.pathname + parts.pathname + }; + const newUri = mUrl.format(newParts); + return newUri; +} + +OnvifDevice.prototype._getSnapshotUri = function(directUri) { + if (!this.keepAddr) return directUri; + const base = mUrl.parse('http://' + this.address); + const parts = mUrl.parse(directUri); + const newParts = { + protocol: parts.protocol, + host: base.host, + pathname: base.pathname + parts.pathname + }; + const newUri = mUrl.format(newParts); + return newUri; +} + module.exports = OnvifDevice;