Add debugging code for update only on change.
All checks were successful
Build Docker image / build (push) Successful in 1m14s

This commit is contained in:
Pieter Hollander 2024-02-18 17:56:06 +01:00
parent 666bf5700e
commit cfbd22d259
Signed by: pieter
SSH key fingerprint: SHA256:HbX+9cBXsop9SuvL+mELd29sK+7DehFfdVweFVDtMSg

52
main.go
View file

@ -119,14 +119,24 @@ func main() {
var prevDt1, prevDt2, prevRt1, prevRt2, prevG int var prevDt1, prevDt2, prevRt1, prevRt2, prevG int
func updateFieldIfChanged(currentValue *int, previousValue *int) *int { // func updateFieldIfChanged(currentValue *int, previousValue *int) *int {
// if currentValue != nil && *currentValue == *previousValue {
// return nil // No change, so we prepare to insert a NULL
// } else if currentValue != nil {
// *previousValue = *currentValue // Update the previous value to the current one
// return currentValue
// }
// return currentValue // Return the original value if it's nil
// }
func updateFieldIfChanged(currentValue *int, previousValue *int) (*int, bool) {
if currentValue != nil && *currentValue == *previousValue { if currentValue != nil && *currentValue == *previousValue {
return nil // No change, so we prepare to insert a NULL return nil, false // No change
} else if currentValue != nil { } else if currentValue != nil {
*previousValue = *currentValue // Update the previous value to the current one *previousValue = *currentValue // Update the previous value to the current one
return currentValue return currentValue, true // Change occurred
} }
return currentValue // Return the original value if it's nil return currentValue, false // Return the original value if it's nil, indicating no change
} }
func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) { func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
@ -145,12 +155,36 @@ func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
return return
} }
payload.Dt1 = updateFieldIfChanged(payload.Dt1, &prevDt1) // payload.Dt1 = updateFieldIfChanged(payload.Dt1, &prevDt1)
payload.Dt2 = updateFieldIfChanged(payload.Dt2, &prevDt2) // payload.Dt2 = updateFieldIfChanged(payload.Dt2, &prevDt2)
payload.Rt1 = updateFieldIfChanged(payload.Rt1, &prevRt1) // payload.Rt1 = updateFieldIfChanged(payload.Rt1, &prevRt1)
payload.Rt2 = updateFieldIfChanged(payload.Rt2, &prevRt2) // payload.Rt2 = updateFieldIfChanged(payload.Rt2, &prevRt2)
payload.G = updateFieldIfChanged(payload.G, &prevG) // payload.G = updateFieldIfChanged(payload.G, &prevG)
var changed bool // Flag to track if any value changed
// Update each field, directly updating `changed` if any change is detected
var tempChanged bool // Used to capture the change status for each field
payload.Dt1, tempChanged = updateFieldIfChanged(payload.Dt1, &prevDt1)
changed = changed || tempChanged
payload.Dt2, tempChanged = updateFieldIfChanged(payload.Dt2, &prevDt2)
changed = changed || tempChanged
payload.Rt1, tempChanged = updateFieldIfChanged(payload.Rt1, &prevRt1)
changed = changed || tempChanged
payload.Rt2, tempChanged = updateFieldIfChanged(payload.Rt2, &prevRt2)
changed = changed || tempChanged
payload.G, tempChanged = updateFieldIfChanged(payload.G, &prevG)
changed = changed || tempChanged
// 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)
}
// Insert data into PostgreSQL // Insert data into PostgreSQL
err = insertData(timestamp, payload) err = insertData(timestamp, payload)
if err != nil { if err != nil {