Skip to content

Commit a3dd1ef

Browse files
committed
1.2.0
1 parent e15cb4f commit a3dd1ef

7 files changed

Lines changed: 340 additions & 192 deletions

lib/lib.device.base.upnp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = class UPNPDevice extends Device
1717

1818
close()
1919
{
20-
this.upnpClient.releaseEventingServer();
20+
this.upnpClient.releaseEventingServer(true);
2121
}
2222

2323
name()

lib/lib.discoverHostDevice.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
'use strict';
2+
var Logger = require('./lib.logger');
3+
var BaseManager = require('./lib.base.manager');
4+
var ManagerDisposer = require('./lib.managerDisposer');
5+
6+
var SsdpClient = require("node-ssdp").Client;
7+
8+
9+
module.exports = class DiscoverHostDevice extends BaseManager
10+
{
11+
constructor()
12+
{
13+
super();
14+
this.bonjourClient = null
15+
this.bonjourBrowser = null
16+
this.ssdpClient = null;
17+
}
18+
19+
20+
init()
21+
{
22+
var self = this
23+
24+
self.stopDiscover()
25+
//self.tinkerhubMDSNBrowser = null
26+
//self.tinkerhubMDSNBrowser = new TinkerhubMDSN.browser({type: '', protocol: 'udp'})
27+
//this.ssdpClient = new SsdpClient({customLogger : this.logUPNP(this), explicitSocketBind : true});
28+
//this.ssdpClient = new SsdpClient({explicitSocketBind : true})
29+
30+
self.createBonjourClient()
31+
//self.createSSDPClient();
32+
33+
}
34+
35+
createBonjourClient()
36+
{
37+
if(this.bonjourClient)
38+
{
39+
this.bonjourClient.destroy()
40+
this.bonjourClient = null
41+
}
42+
this.bonjourClient = require('bonjour')()
43+
this.createBonjourBrowser()
44+
}
45+
46+
createBonjourBrowser()
47+
{
48+
var self = this
49+
50+
if(this.bonjourBrowser)
51+
this.bonjourBrowser.stop()
52+
this.bonjourBrowser = this.bonjourClient.find({})
53+
54+
this.bonjourBrowser.on("up", function (_service) {
55+
if(_service.fqdn.startsWith("RaumfeldControl"))
56+
{
57+
self.deviceFound(_service.referer.address, _service.fqdn, _service)
58+
}
59+
})
60+
61+
this.bonjourBrowser.on("down", function (_service) {
62+
if(_service.fqdn.startsWith("RaumfeldControl"))
63+
{
64+
self.deviceLost(_service.referer.address, _service.fqdn, _service)
65+
}
66+
})
67+
}
68+
69+
70+
createSSDPClient()
71+
{
72+
var self = this
73+
74+
if(this.ssdpClient)
75+
this.ssdpClient.stop()
76+
this.ssdpClient = new SsdpClient({explicitSocketBind : true})
77+
78+
this.ssdpClient.on('response', function (_headers, _statusCode, _rinfo) {
79+
self.deviceFound(_headers.LOCATION, "", _headers)
80+
});
81+
82+
this.ssdpClient.on('advertise-alive', function (_headers) {
83+
self.deviceFound(_headers.LOCATION, "", _headers)
84+
});
85+
86+
this.ssdpClient.on('advertise-bye', function (_headers) {
87+
self.deviceLost(_headers.LOCATION, "", _headers)
88+
});
89+
90+
self.ssdpClient.search('urn:schemas-raumfeld-com:device:ConfigDevice:1');
91+
}
92+
93+
94+
deviceFound(_address, _name, _service)
95+
{
96+
this.emit("deviceFound", { "address" : _address, "name" : _name, origService: _service })
97+
}
98+
99+
deviceLost(_service)
100+
{
101+
this.emit("deviceLost", { "address" : _address, "name" : _name, origService: _service })
102+
}
103+
104+
105+
startDiscover()
106+
{
107+
if(this.bonjourBrowser)
108+
this.bonjourBrowser.start()
109+
if(this.ssdpClient)
110+
this.ssdpClient.start()
111+
}
112+
113+
114+
stopDiscover()
115+
{
116+
if(this.bonjourBrowser)
117+
this.bonjourBrowser.stop()
118+
if(this.ssdpClient)
119+
this.ssdpClient.stop();
120+
}
121+
122+
123+
updateDiscover()
124+
{
125+
this.createBonjourBrowser()
126+
this.bonjourBrowser.start()
127+
//this.createSSDPClient()
128+
//this.ssdpClient.start()
129+
}
130+
131+
}

lib/lib.external.upnp-device-client.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ DeviceClient.prototype.unsubscribe = function(serviceId, listener) {
323323

324324
req.on('error', function(err) {
325325
self.emit('error', err);
326+
// well this may occur if the client was removed and can not answer anymore, so we have to remove the subscription!
327+
clearTimeout(self.subscriptions[serviceId].timer);
328+
delete self.subscriptions[serviceId];
329+
self.releaseEventingServer();
326330
});
327331

328332
req.end();
@@ -369,6 +373,10 @@ DeviceClient.prototype.unsubscribeAll = function(serviceId) {
369373

370374
req.on('error', function(err) {
371375
self.emit('error', err);
376+
// well this may occur if the client was removed and can not answer anymore, so we have to remove the subscription!
377+
clearTimeout(self.subscriptions[serviceId].timer);
378+
delete self.subscriptions[serviceId];
379+
self.releaseEventingServer();
372380
});
373381

374382
req.end();
@@ -447,8 +455,8 @@ DeviceClient.prototype.ensureEventingServer = function(callback) {
447455
};
448456

449457

450-
DeviceClient.prototype.releaseEventingServer = function() {
451-
if(Object.keys(this.subscriptions).length === 0) {
458+
DeviceClient.prototype.releaseEventingServer = function(_force = false) {
459+
if(Object.keys(this.subscriptions).length === 0 || _force) {
452460
debug('shutdown eventing server');
453461
if(this.server)
454462
{

0 commit comments

Comments
 (0)