itead / Sonoff_Devices_DIY_Tools

BSD 3-Clause "New" or "Revised" License
553 stars 167 forks source link

mDNS data decoding #32

Closed alexbk66 closed 5 years ago

alexbk66 commented 5 years ago

Using Mono.Zeroconf discovery when service is found - 'data1' gets split into 11 records, instead of single string:

for (int i = 0; i < record.Count; i++)
{
    Console.WriteLine($"{i}: {record.GetItemAt(i)}");
}

0: data1 = {"switch":"on" 1: "startup" = 2: :"stay" = 3: "pulse" = 4: :"off" = 5: "sledOnline" = 6: :"on" = 7: "pulseWidth" = 8: :500 = 9: "rssi" = 10: :-57} = 11: seq = 43 12: apivers = 1 13: type = diy_plug 14: id = 100086f61b 15: txtvers = 1

ZZLinvec commented 5 years ago

This looks interesting. Can you tell me what this is doing?

alexbk66 commented 5 years ago

This prints discovered service.TxtRecord

alexbk66 commented 5 years ago

Only when service is discovered first time, after mDNS works

ZZLinvec commented 5 years ago

It looks like the = on lines 1 through 10 was added by accident. This is C#?

alexbk66 commented 5 years ago

The "=" is just from ToString() function:

public override string ToString()
{
    return String.Format("{0} = {1}", Key, ValueString);
}

Which means that record["data1"] = {"switch":"on" And the rest of json string goes to record keys, not values Yes, it's C#

alexbk66 commented 5 years ago

What's the record separator in json string? It should be comma, but it looks like you have something else, i.e. new line.
BTW, I noticed in your code sometimes ^M which means you are editing in Windows which appends ^L^M and on Linux new line is just ^L

ZZLinvec commented 5 years ago

data1 = {"switch":"on","startup":"stay", "pulse" :"off","sledOnline" :"on" ,"pulseWidth" :500 ,"rssi" :-57} seq = 43 apivers = 1 type = diy_plug id = 100086f61b txtvers = 1

ZZLinvec commented 5 years ago

@alexbk66 data1 values json-formatted objects,You have to explain it again?

alexbk66 commented 5 years ago

I know, that's the problem - it should be json comma separated, but it's obviously not - since Mono.Zeroconf returns them split in separate records.

I'm trying to help you - testing your code! It's not really nice to outsource testing to your cutomers why actually pay for your devices. It's not opensource project....

ZZLinvec commented 5 years ago

I mean our current 3.3.0 device code is using standard json, TxtRecord parsing error.

alexbk66 commented 5 years ago

I'm debugging now

alexbk66 commented 5 years ago

I found that Mono.Zeroconf.Providers.AvahiDBus.TxtRecord parsing uses the following pattern, which obviously splits the record on comma. No idea why. I contacted the author, see if he replies.

For now I just commented out this code - and it works....

static Regex item_regex = new Regex (@"""[^""]*""|[^,]+", RegexOptions.IgnorePatternWhitespace);
foreach (Match item_match in item_regex.Matches (encoding.GetString (raw_item)))
{