chkr1011 / CoAPnet

CoAPnet is a high performance .NET library for CoAP based communication. It provides a CoAP client and a CoAP server. It also has DTLS support out of the box.
MIT License
70 stars 15 forks source link

Identity Example #6

Closed gkapellmann closed 3 years ago

gkapellmann commented 3 years ago

Following this note: "Please note that the identity and key must be generated first using the security token on the back of the device."

Doing so with a python lib is easy, but is there a way to do this with CoAPnet?

If so, could you point me in the right direction to do so?

Thank you!

chkr1011 commented 3 years ago

Hi, I made this with a simple test Console App where I put all the required calls and noted the response afterwards. You can surely do everything required with COAPnet.

You basically need to follow this instructions: https://github.com/home-assistant/core/issues/10252

Instead of using the coap-client application you can send the same requests with COAPnet.

Best regards Christian

gkapellmann commented 3 years ago

OK, so can you help me understand this, its literally this command the one I am having trouble to interpret:

coap-client -m post -u "Client_identity" -k "SECURITY_CODE" -e '{"9090":"IDENTITY"}' "coaps://IP_ADDRESS:5684/15011/9063"

It clearly is a POST type, but I am assuming this has to happen before this:

var connectOptions = new CoapClientConnectOptionsBuilder() .WithHost(address) .WithPort(5684) .WithDtlsTransportLayer(o => o.WithPreSharedKey(myID, secretKey)) .Build(); await coapClient.ConnectAsync(connectOptions, CancellationToken.None);

so my problem here is how to put in the POST the address and the "Client_identity", I am clearly miss understanding something, so my POST command so far is like this:

var request = new CoapRequestBuilder() .WithMethod(CoapRequestMethod.Post) .WithPath("15011/9063") .WithQuery(GatewayCode) .WithPayload("{\"9090\":\"" + Identity + "\"}") .Build(); var response = await theClient.RequestAsync(request, CancellationToken.None).ConfigureAwait(false);

What am I missing?

Thank you in advance!

gkapellmann commented 3 years ago

Hello @chkr1011 , I was wondering if you could give me some guidance here, I am still stuck with it.

chkr1011 commented 3 years ago

Your code looks fine to me. I also used it to communicate with TRADFRI devices. My code is here: https://github.com/chkr1011/CoAPnet/blob/34339382f94008482b0fd5de3c1c1453744c3fa3/Source/CoAP.TestClient/Program.cs#L59

gkapellmann commented 3 years ago

Right! I was locating the Query in the wrong place.

Thank you for your help!