amenzhinsky / iothub

Azure IoT Hub SDK for Golang
MIT License
53 stars 58 forks source link

Subscribe to C2D events throws error while receiving the events on device side #80

Open PremSahooESL opened 1 year ago

PremSahooESL commented 1 year ago

Go Version: go 1.19

ERROR message parse error: invalid semicolon separator in query

      c, err := iotdevice.NewFromConnectionString(
        iotmqtt.New(), "xxxxxxxxxxxxxxxxxxxx",
    )
    if err != nil {
        log.Fatal(err)
    }

    // connect to the iothub
    if err = c.Connect(context.Background()); err != nil {
        log.Fatal(err)
    }

    // receive a cloud-to-device message
    eventSub, err := c.SubscribeEvents(context.Background())

    for msg := range eventSub.C() {
        fmt.Println(msg.Payload)
    }

    if err := eventSub.Err(); err != nil {
        log.Fatal(err)
    }
amenzhinsky commented 1 year ago

Something must be wrong with the connection url

PremSahooESL commented 1 year ago

Connection has been established. It also gets triggered upon events arrival. But with the below error

ERROR message parse error: invalid semicolon separator in query

Below is the sample connection URL I used:

HostName=xxxxxxx.azure-devices.net;SharedAccessKeyName=device;DeviceId=MY_GO_TEST;SharedAccessKey=xxxxxxxxx

vishal210893 commented 1 year ago

Hi, @amenzhinsky is there any update?

PremSahooESL commented 1 year ago

@amenzhinsky Do you have any concerns on above connection string format? If so then how come the client could get authorized with above connection string format. But could not parse the data and throws parse error.

Is this SDK able to receive data in your case?

amenzhinsky commented 1 year ago

@PremSahooESL it seems parseCloudToDeviceTopic fails to parse the topic name of the incoming message, could you log it in your app to investigate what causes the problem?

PremSahooESL commented 1 year ago

Thanks for the response. Sure let me check..

PremSahooESL commented 1 year ago

Below is the Topic Name What I received from: This could not be parsed by parseQuery() function on url.go

$.to=/devices/PREM_GO_TEST/messages/deviceBound&$.ct=text/plain; charset=UTF-8&$.ce=

PremSahooESL commented 1 year ago

Is it something different or unexpected which comes from C2D by IotHub?

amenzhinsky commented 1 year ago

That's odd, there's a preparation of this type of queries for the following parsing.

https://github.com/amenzhinsky/iothub/blob/master/iotdevice/transport/mqtt/mqtt.go#L280

Could you make sure that parseCloudToDeviceTopic throws the error?

PremSahooESL commented 1 year ago

Below code throws the error: parseCloudToDeviceTopic() -> ParseQuery() -> parseQuery()

func parseCloudToDeviceTopic(s string) (map[string]string, error) {
    s, err := url.QueryUnescape(s)
    if err != nil {
        return nil, err
    }

    // attributes prefixed with $.,
    // e.g. `messageId` becomes `$.mid`, `to` becomes `$.to`, etc.
    i := strings.Index(s, "$.")
    if i == -1 {
        return nil, errors.New("malformed cloud-to-device topic name")
    }
    q, err := url.ParseQuery(s[i:])
    if err != nil {
        return nil, err
    }

    p := make(map[string]string, len(q))
    for k, v := range q {
        if len(v) != 1 {
            return nil, fmt.Errorf("unexpected number of property values: %d", len(q))
        }
        p[k] = v[0]
    }
    return p, nil
}
PremSahooESL commented 1 year ago

I could see below line is missing from above code:

https://github.com/amenzhinsky/iothub/blob/master/iotdevice/transport/mqtt/mqtt.go#L280

amenzhinsky commented 1 year ago

https://github.com/amenzhinsky/iothub/commit/7eb906f6251679227fb62b5f05c5c1e3b7316b4f

Could you update to the latest version?

PremSahooESL commented 1 year ago

it works now. Thanks.

PremSahooESL commented 1 year ago

I will keep posting here if I face other issues. Infact this is the only Github repo for Go Lang SDK for Azure IoT Hub. Hope it supports all basic C2D functions.

PremSahooESL commented 1 year ago

i am just trying to send a C-2-D message.

I am trying to use below imports statement. But my application could not start up. It keeps throwing beolow error.

"github.com/amenzhinsky/iothub/iotservice"

image

PremSahooESL commented 1 year ago

Looks.... latest Eventhub client.go in this repo is not using latest amqp 1.0.1 lib

image

PremSahooESL commented 1 year ago

Below is the send() of latest amqp 1.0.1 lib code....

image