@@ -43,41 +43,56 @@ export class MdnsScanner extends EventEmitter {
4343 this . scanner = mdns ( ) ;
4444 this . scanner . on ( 'response' , async ( response : ResponsePacket ) => {
4545 let port = 0 ;
46- let gen = 0 ;
46+ let gen = 1 ;
47+ if ( debug ) this . log . debug ( `***--- start ---` ) ;
48+ // if (debug && response.answers.length === 0 && response.additionals.length === 0) this.log.debug('ResponsePacket:\n', response);
49+ if ( debug ) this . log . debug ( `--- response.answers ---` ) ;
4750 for ( const a of response . answers ) {
4851 if ( debug && a . type === 'PTR' ) {
4952 this . log . debug ( `[${ idn } ${ a . type } ${ rs } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ typeof a . data === 'string' ? a . data : debugStringify ( a . data ) } ` ) ;
5053 }
54+ if ( debug && a . type === 'TXT' ) {
55+ // this.log.debug(`[${idn}${a.type}${rs}${db}] Name: ${CYAN}${a.name}${db} data: ${typeof a.data === 'string' ? a.data : debugStringify(a.data)}`);
56+ }
5157 if ( debug && a . type === 'PTR' && a . name === '_http._tcp.local' ) {
5258 this . log . debug ( `[${ BLUE } ${ a . type } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ typeof a . data === 'string' ? a . data : debugStringify ( a . data ) } ` ) ;
5359 }
54- if ( debug && a . type === 'A' && a . name . startsWith ( 'shelly' ) ) {
60+ if ( debug && a . type === 'A' && ( a . name . startsWith ( 'shelly' ) || a . name . startsWith ( 'Shelly' ) ) ) {
5561 this . log . debug ( `[${ BLUE } ${ a . type } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ typeof a . data === 'string' ? a . data : debugStringify ( a . data ) } ` ) ;
5662 }
57- if ( debug && a . type === 'NSEC' && a . name . startsWith ( 'shelly' ) ) {
63+ if ( debug && a . type === 'NSEC' && ( a . name . startsWith ( 'shelly' ) || a . name . startsWith ( 'Shelly' ) ) ) {
5864 this . log . debug ( `[${ BLUE } ${ a . type } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ typeof a . data === 'string' ? a . data : debugStringify ( a . data ) } ` ) ;
5965 }
60- if ( debug && a . type === 'SRV' && a . name . startsWith ( 'shelly' ) ) {
66+ if ( debug && a . type === 'SRV' && ( a . name . startsWith ( 'shelly' ) || a . name . startsWith ( 'Shelly' ) ) ) {
6167 this . log . debug ( `[${ BLUE } ${ a . type } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ typeof a . data === 'string' ? a . data : debugStringify ( a . data ) } ` ) ;
6268 }
63- if ( debug && a . type === 'TXT' && a . name . startsWith ( 'shelly' ) ) {
69+ if ( debug && a . type === 'TXT' && ( a . name . startsWith ( 'shelly' ) || a . name . startsWith ( 'Shelly' ) ) ) {
6470 this . log . debug ( `[${ BLUE } ${ a . type } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ a . data } ` ) ;
6571 }
66- if ( a . type === 'SRV' && a . name . startsWith ( 'shelly' ) ) {
72+ if ( a . type === 'SRV' && ( a . name . startsWith ( 'shelly' ) || a . name . startsWith ( 'Shelly' ) ) ) {
6773 port = a . data . port ;
6874 }
69- if ( a . type === 'A' && a . name . startsWith ( 'shelly' ) ) {
70- if ( ! this . discoveredDevices . has ( a . name . replace ( '.local' , '' ) ) ) {
71- this . log . info ( `Discovered shelly gen: ${ CYAN } 1${ nf } device id: ${ hk } ${ a . name . replace ( '.local' , '' ) } ${ nf } host: ${ zb } ${ a . data } ${ nf } port: ${ zb } ${ port } ${ nf } ` ) ;
72- this . discoveredDevices . set ( a . name . replace ( '.local' , '' ) , { id : a . name . replace ( '.local' , '' ) , host : a . data , port, gen : 1 } ) ;
73- this . emit ( 'discovered' , { id : a . name . replace ( '.local' , '' ) , host : a . data , port, gen : 1 } ) ;
75+ if ( a . type === 'TXT' && a . name . startsWith ( 'Shelly' ) && a . data . toString ( ) === 'gen=2' ) {
76+ gen = 2 ;
77+ }
78+ if ( a . type === 'A' && ( a . name . startsWith ( 'shelly' ) || a . name . startsWith ( 'Shelly' ) ) ) {
79+ const [ name , mac ] = a . name . replace ( '.local' , '' ) . split ( '-' ) ;
80+ const deviceId = name . toLowerCase ( ) + '-' + mac . toUpperCase ( ) ;
81+ if ( ! this . discoveredDevices . has ( deviceId ) ) {
82+ this . log . info ( `Discovered shelly gen: ${ CYAN } ${ gen } ${ nf } device id: ${ hk } ${ deviceId } ${ nf } host: ${ zb } ${ a . data } ${ nf } port: ${ zb } ${ port } ${ nf } ` ) ;
83+ this . discoveredDevices . set ( deviceId , { id : deviceId , host : a . data , port, gen } ) ;
84+ this . emit ( 'discovered' , { id : deviceId , host : a . data , port, gen } ) ;
7485 }
7586 }
7687 }
88+ if ( debug ) this . log . debug ( `--- response.additionals ---` ) ;
7789 for ( const a of response . additionals ) {
7890 if ( debug && a . type === 'PTR' ) {
7991 this . log . debug ( `[${ idn } ${ a . type } ${ rs } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ typeof a . data === 'string' ? a . data : debugStringify ( a . data ) } ` ) ;
8092 }
93+ if ( debug && a . type === 'TXT' ) {
94+ // this.log.debug(`[${idn}${a.type}${rs}${db}] Name: ${CYAN}${a.name}${db} data: ${typeof a.data === 'string' ? a.data : debugStringify(a.data)}`);
95+ }
8196 if ( debug && a . type === 'PTR' && a . name === '_http._tcp.local' ) {
8297 this . log . debug ( `[${ BLUE } ${ a . type } ${ db } ] Name: ${ CYAN } ${ a . name } ${ db } data: ${ typeof a . data === 'string' ? a . data : debugStringify ( a . data ) } ` ) ;
8398 }
@@ -100,15 +115,16 @@ export class MdnsScanner extends EventEmitter {
100115 gen = parseInt ( a . data . toString ( ) . replace ( 'gen=' , '' ) ) ;
101116 }
102117 if ( a . type === 'A' && a . name . startsWith ( 'Shelly' ) ) {
103- if ( ! this . discoveredDevices . has ( a . name . replace ( '.local' , '' ) . toLowerCase ( ) ) ) {
104- this . log . info (
105- `Discovered shelly gen: ${ CYAN } ${ gen } ${ nf } device id: ${ hk } ${ a . name . replace ( '.local' , '' ) . toLowerCase ( ) } ${ nf } host: ${ zb } ${ a . data } ${ nf } port: ${ zb } ${ port } ${ nf } ` ,
106- ) ;
107- this . discoveredDevices . set ( a . name . replace ( '.local' , '' ) . toLowerCase ( ) , { id : a . name . replace ( '.local' , '' ) . toLowerCase ( ) , host : a . data , port, gen } ) ;
108- this . emit ( 'discovered' , { id : a . name . replace ( '.local' , '' ) . toLowerCase ( ) , host : a . data , port, gen } ) ;
118+ const [ name , mac ] = a . name . replace ( '.local' , '' ) . split ( '-' ) ;
119+ const deviceId = name . toLowerCase ( ) + '-' + mac . toUpperCase ( ) ;
120+ if ( ! this . discoveredDevices . has ( deviceId ) ) {
121+ this . log . info ( `Discovered shelly gen: ${ CYAN } ${ gen } ${ nf } device id: ${ hk } ${ deviceId } ${ nf } host: ${ zb } ${ a . data } ${ nf } port: ${ zb } ${ port } ${ nf } ` ) ;
122+ this . discoveredDevices . set ( deviceId , { id : deviceId , host : a . data , port, gen } ) ;
123+ this . emit ( 'discovered' , { id : deviceId , host : a . data , port, gen } ) ;
109124 }
110125 }
111126 }
127+ if ( debug ) this . log . debug ( `--- end ---` ) ;
112128 } ) ;
113129
114130 this . sendQuery ( ) ;
@@ -150,8 +166,9 @@ export class MdnsScanner extends EventEmitter {
150166}
151167
152168/*
169+ // node dist/mdnsScanner.js mdnsScanner
153170if (process.argv.includes('mdnsScanner')) {
154- const mdnsScanner = new MdnsScanner();
171+ const mdnsScanner = new MdnsScanner(true );
155172 mdnsScanner.start(undefined, true);
156173
157174 process.on('SIGINT', async function () {
0 commit comments