amenzhinsky / iothub

Azure IoT Hub SDK for Golang
MIT License
51 stars 57 forks source link

Failing to update reported twin state #70

Closed aka-mj closed 1 year ago

aka-mj commented 1 year ago

I'm trying to update a field in my twin and I'm getting a 400 error back. I'm using MQTT.

"request failed with 400 response code"

The code:

    d, r, e := c.RetrieveTwinState(context.Background())
    if e != nil {
        log.Errorw("Failed to retrieve twin", "error", e)
    }
    fmt.Printf("DESIRED: %+v\nREPORTED: %+v\n", d, r)

    r["lastConnectionTime"] = time.Now().UTC().Format(time.RFC3339)

    if v, e := c.UpdateTwinState(context.Background(), r); e != nil {
        log.Errorw("Failed to update twin", "error", e)
    } else {
        log.Debugw("Successfully updated twin", "version", v)
    }

Output:

DESIRED: map[$version:1]
REPORTED: map[$version:106 deviceInfo:map[components:[map[hwpn:000-00000-00 hwrev:000 infoelem:0 nodeid:17 serialnum:0 swpn:081-10010-00 swrev:00.05.00 typeid:0]] timestamp:2022-10-20T18:13:38Z] lastConnectionTime:2022-10-20T17:32:46+00:00Z powerFailure:map[timestamp:2022-10-20T14:21:40Z] productType:245]
{"level":"error","ts":1666290220.208017,"logger":"main.heartbeat","caller":"azure-go-telem/heartbeat.go:22","msg":"Failed to update twin","error":"request failed with 400 response code","stacktrace":"main.heartbeat\n\t/home/mjohn/workspace/connectivity-module/apps/azure-go-telem/heartbeat.go:22"}
aka-mj commented 1 year ago

Looking at the response body:

{"errorCode":400004,"trackingId":"0345b491e59045b89ccdffb111968e83-G:0-TimeStamp:10/20/2022 19:10:06","message":"Invalid Digital Twin Interface Property name. Error in Property name '$version'.","timestampUtc":"2022-10-20T19:10:06.6270613Z"}
aka-mj commented 1 year ago

Able to get around this by removing the $version key.