thin-edge / thin-edge.io

The open edge framework for lightweight IoT devices
https://thin-edge.io
Apache License 2.0
222 stars 55 forks source link

If the first message sent by a child device is ill-formed, the child device is never registered #2737

Closed didier-wenzek closed 6 months ago

didier-wenzek commented 9 months ago

Describe the bug

If the very first message sent by a child device is ill-formed, then:

To Reproduce

Prerequisite: a name for a new child device that is known neither by thin-edge nor Cumulocity. Here, I used ghost_child

Be ready to observe messages sent to Cumulocity:

$ tedge mqtt sub 'c8y/#' 
INFO: Connected

Send a first ill-formed measurement on -behalf of the new child device:

$ tedge mqtt pub te/device/ghost_child///m/ 'invalid measurement'

Observe, that as expected, nothing is sent to Cumulocity.

Now send a correct measurement on-behalf of the child device:

$ tedge mqtt pub te/device/ghost_child///m/ '{"value": 1}'

Observe, that the measurement is published to Cumulocity, but without any child device registration, making Cumulocity complain on c8y/error:

$ tedge mqtt sub 'c8y/#'                                                   
INFO: Connected

[c8y/measurement/measurements/create] {"externalSource":{"externalId":"demo-device-888:device:ghost_child","type":"c8y_Serial"},"value":{"value":{"value":1.0}},"time":"2024-02-22T17:22:22.655401232Z","type":"ThinEdgeMeasurement"}
[c8y/error] {"messageId":182,"error":"general/internalError","message":"Current device is not the owner of the given source null","info":"https://cumulocity.com/guides/reference/rest-implementation/#a-name-error-reporting-a-error-reporting"}

Expected behavior

A registration message must be sent for the child-device before is sent its first telemetry data or capability message. Cumulocity should not complain on c8y/error.

$ tedge mqtt sub 'c8y/#'                                                   
INFO: Connected

["c8y/s/us] 101,test-device:device:ghost_child,ghost_child,thin-edge.io-child
[c8y/measurement/measurements/create] {"externalSource":{"externalId":"demo-device-888:device:ghost_child","type":"c8y_Serial"},"value":{"value":{"value":1.0}

Screenshots

Environment (please complete the following information):

Additional context

This bug has been introduced by https://github.com/thin-edge/thin-edge.io/pull/2169 (my bad) and a unit test (which is currently ignored) is already here: https://github.com/thin-edge/thin-edge.io/blob/main/crates/extensions/c8y_mapper_ext/src/converter.rs#L2058

reubenmiller commented 6 months ago

Duplicated by #2889