@@ -243,7 +243,7 @@ class ThingDiscovery extends Stream<ThingDescription>
243
243
);
244
244
}
245
245
246
- Map <String , String > _parseTxtRecords (String txtRecords) {
246
+ Map <String , String > _parseMdnsTxtRecords (String txtRecords) {
247
247
final recordsList = txtRecords
248
248
.split ("\n " )
249
249
.map ((property) => property.split ("=" ))
@@ -253,6 +253,29 @@ class ThingDiscovery extends Stream<ThingDescription>
253
253
return Map .fromEntries (recordsList);
254
254
}
255
255
256
+ static String _trimTxtRecord (String txtRecord) {
257
+ final startIndex = txtRecord.startsWith ('"' ) ? 1 : 0 ;
258
+
259
+ final length = txtRecord.length;
260
+ final endIndex = txtRecord.endsWith ('"' ) ? length - 1 : length;
261
+
262
+ return txtRecord.substring (startIndex, endIndex);
263
+ }
264
+
265
+ Map <String , String > _parseTxtRecords (List <RRecord >? txtRecords) {
266
+ final entries = txtRecords
267
+ ? .map ((txtRecord) => txtRecord.data)
268
+ .map (_trimTxtRecord)
269
+ .map ((e) => e.split ("=" ))
270
+ .where ((element) => element.length == 2 )
271
+ .map ((txtRecord) {
272
+ return MapEntry (txtRecord[0 ], txtRecord[1 ]);
273
+ }) ??
274
+ [];
275
+
276
+ return Map .fromEntries (entries);
277
+ }
278
+
256
279
Future <Map <String , String >?> _lookupTxtRecords (
257
280
MDnsClient client,
258
281
String domainName,
@@ -267,7 +290,7 @@ class ThingDiscovery extends Stream<ThingDescription>
267
290
return null ;
268
291
}
269
292
270
- return _parseTxtRecords (firstTxtRecord);
293
+ return _parseMdnsTxtRecords (firstTxtRecord);
271
294
}
272
295
273
296
Stream <ThingDescription > _discoverUsingDnsSd (String name) async * {
@@ -309,20 +332,13 @@ class ThingDiscovery extends Stream<ThingDescription>
309
332
) ??
310
333
[];
311
334
312
- final txtRecord = txtRecords.firstOrNull;
313
-
314
- if (txtRecord == null ) {
315
- continue ;
316
- }
317
-
318
- // FIXME: Add parsing of multiple TXT records
319
- final parsedTxtRecord = _parseTxtRecords (txtRecord.data);
335
+ final parsedTxtRecords = _parseTxtRecords (txtRecords);
320
336
321
337
final uri = Uri (
322
338
host: target,
323
339
port: port,
324
- path: parsedTxtRecord ["td" ],
325
- scheme: parsedTxtRecord ["scheme" ] ?? defaultScheme,
340
+ path: parsedTxtRecords ["td" ],
341
+ scheme: parsedTxtRecords ["scheme" ] ?? defaultScheme,
326
342
);
327
343
328
344
final duplicate = ! discoveredUris.add (uri);
@@ -331,7 +347,7 @@ class ThingDiscovery extends Stream<ThingDescription>
331
347
continue ;
332
348
}
333
349
334
- final type = parsedTxtRecord ["type" ] ?? defaultType;
350
+ final type = parsedTxtRecords ["type" ] ?? defaultType;
335
351
336
352
switch (type) {
337
353
case "Thing" :
0 commit comments