Fix printing pointer instead of value for changed values.
All checks were successful
Build Docker image / build (push) Successful in 1m6s
All checks were successful
Build Docker image / build (push) Successful in 1m6s
This commit is contained in:
parent
cfbd22d259
commit
ddfc896960
1 changed files with 15 additions and 2 deletions
17
main.go
17
main.go
|
@ -4,6 +4,7 @@ package main
|
|||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
@ -139,6 +140,16 @@ func updateFieldIfChanged(currentValue *int, previousValue *int) (*int, bool) {
|
|||
return currentValue, false // Return the original value if it's nil, indicating no change
|
||||
}
|
||||
|
||||
// safeDerefInt handles potential nil pointers to avoid a runtime panic
|
||||
// log messages will display the actual integer values if the pointers are not nil,
|
||||
// or "nil" if the pointers are nil.
|
||||
func safeDerefInt(ptr *int) string {
|
||||
if ptr != nil {
|
||||
return fmt.Sprintf("%d", *ptr) // Dereference the pointer to get the value
|
||||
}
|
||||
return "nil" // Return a string indicating the value is nil
|
||||
}
|
||||
|
||||
func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
|
||||
// Parse JSON payload
|
||||
var payload Payload
|
||||
|
@ -182,8 +193,10 @@ func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
|
|||
|
||||
// If any value has changed, log all the relevant values
|
||||
if changed {
|
||||
log.Printf("Values changed: dt1=%v, dt2=%v, rt1=%v, rt2=%v, g=%v\n",
|
||||
payload.Dt1, payload.Dt2, payload.Rt1, payload.Rt2, payload.G)
|
||||
log.Printf("Values changed: dt1=%s, dt2=%s, rt1=%s, rt2=%s, g=%s\n",
|
||||
safeDerefInt(payload.Dt1), safeDerefInt(payload.Dt2),
|
||||
safeDerefInt(payload.Rt1), safeDerefInt(payload.Rt2),
|
||||
safeDerefInt(payload.G))
|
||||
}
|
||||
// Insert data into PostgreSQL
|
||||
err = insertData(timestamp, payload)
|
||||
|
|
Loading…
Reference in a new issue