Add logic to only log meter readings on change. Otherwise, insert NULL.
All checks were successful
Build Docker image / build (push) Successful in 1m17s
All checks were successful
Build Docker image / build (push) Successful in 1m17s
This commit is contained in:
parent
8673fabccd
commit
66ad959ada
1 changed files with 23 additions and 5 deletions
28
main.go
28
main.go
|
@ -15,15 +15,15 @@ import (
|
||||||
|
|
||||||
type Payload struct {
|
type Payload struct {
|
||||||
T string `json:"t"`
|
T string `json:"t"`
|
||||||
Dt1 int `json:"dt1"`
|
Dt1 *int `json:"dt1"`
|
||||||
Dt2 int `json:"dt2"`
|
Dt2 *int `json:"dt2"`
|
||||||
Rt1 int `json:"rt1"`
|
Rt1 *int `json:"rt1"`
|
||||||
Rt2 int `json:"rt2"`
|
Rt2 *int `json:"rt2"`
|
||||||
D int `json:"d"`
|
D int `json:"d"`
|
||||||
R int `json:"r"`
|
R int `json:"r"`
|
||||||
F int `json:"f"`
|
F int `json:"f"`
|
||||||
Fl int `json:"fl"`
|
Fl int `json:"fl"`
|
||||||
G int `json:"g"`
|
G *int `json:"g"`
|
||||||
V1 int `json:"v1"`
|
V1 int `json:"v1"`
|
||||||
V2 int `json:"v2"`
|
V2 int `json:"v2"`
|
||||||
V3 int `json:"v3"`
|
V3 int `json:"v3"`
|
||||||
|
@ -88,6 +88,18 @@ func main() {
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var prevDt1, prevDt2, prevRt1, prevRt2, prevG 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 mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
|
func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
|
||||||
// Parse JSON payload
|
// Parse JSON payload
|
||||||
var payload Payload
|
var payload Payload
|
||||||
|
@ -104,6 +116,12 @@ func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payload.Dt1 = updateFieldIfChanged(payload.Dt1, &prevDt1)
|
||||||
|
payload.Dt2 = updateFieldIfChanged(payload.Dt2, &prevDt2)
|
||||||
|
payload.Rt1 = updateFieldIfChanged(payload.Rt1, &prevRt1)
|
||||||
|
payload.Rt2 = updateFieldIfChanged(payload.Rt2, &prevRt2)
|
||||||
|
payload.G = updateFieldIfChanged(payload.G, &prevG)
|
||||||
|
|
||||||
// Insert data into PostgreSQL
|
// Insert data into PostgreSQL
|
||||||
err = insertData(timestamp, payload)
|
err = insertData(timestamp, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue