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+ }
0 commit comments