Compare commits

...

2 Commits

Author SHA1 Message Date
0f6590c720 float fix 4
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2026-03-06 21:49:51 +01:00
42ff8b51ed float fix 3
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2026-03-06 21:35:32 +01:00
2 changed files with 62 additions and 67 deletions

View File

@@ -34,20 +34,20 @@ type CarHandler struct {
*/ */
type CarValue struct { type CarValue struct {
Status string `unit:"" json:"status"` Status string `unit:"" json:"status"`
Timestamp string `unit:"" json:"timestamp"` Timestamp string `unit:"" json:"timestamp"`
VoltageL1 float32 `unit:"V" json:"voltageL1"` VoltageL1 float32 `unit:"V" json:"voltageL1"`
VoltageL2 float32 `unit:"V" json:"voltageL2"` VoltageL2 float32 `unit:"V" json:"voltageL2"`
VoltageL3 float32 `unit:"V" json:"voltageL3"` VoltageL3 float32 `unit:"V" json:"voltageL3"`
CurrentL1 float32 `unit:"A" json:"currentL1"` CurrentL1 float32 `unit:"A" json:"currentL1"`
CurrentL2 float32 `unit:"A" json:"currentL2"` CurrentL2 float32 `unit:"A" json:"currentL2"`
CurrentL3 float32 `unit:"A" json:"currentL3"` CurrentL3 float32 `unit:"A" json:"currentL3"`
PowerL1 float32 `unit:"W" json:"powerL1"` PowerL1 float32 `unit:"W" json:"powerL1"`
PowerL2 float32 `unit:"W" json:"powerL2"` PowerL2 float32 `unit:"W" json:"powerL2"`
PowerL3 float32 `unit:"W" json:"powerL3"` PowerL3 float32 `unit:"W" json:"powerL3"`
TotalImportEnergy float32 `unit:"Wh" json:"totalImportEnergy"` TotalImportEnergy float32 `unit:"Wh" json:"totalImportEnergy"`
TotalExportEnergy float32 `unit:"Wh" json:"totalExportEnergy"` TotalExportEnergy float32 `unit:"Wh" json:"totalExportEnergy"`
Cnt int `unit:"" json:"cnt"` Cnt int `unit:"" json:"cnt"`
} }
func New(id string, config config.HandlerConfigT) handler.Handler { func New(id string, config config.HandlerConfigT) handler.Handler {

View File

@@ -1,79 +1,74 @@
package prepared package prepared
import ( import (
"time" "encoding/json"
"log" "log"
"encoding/json" "time"
"udi/config" "udi/config"
"udi/handlers/handler" "udi/database"
"udi/database" "udi/handlers/handler"
) )
type PreparedHandler struct { type PreparedHandler struct {
handler.CommonHandler handler.CommonHandler
dbh *database.DatabaseHandle dbh *database.DatabaseHandle
} }
type endpoint_t struct { type endpoint_t struct {
Label string `json:"label"` Label string `json:"label"`
Variable string `json:"variable"` Variable string `json:"variable"`
Value string `json:"value"` Value interface{} `json:"value"`
Unit string `json:"unit"` Unit string `json:"unit"`
Status string `json:"status"` Status string `json:"status"`
} }
type observation_t struct { type observation_t struct {
Device string `json:"device"` Device string `json:"device"`
Label string `json:"label"` Label string `json:"label"`
Variables map[string]endpoint_t `json:"variables"` Variables map[string]endpoint_t `json:"variables"`
} }
func New(id string, config config.HandlerConfigT) handler.Handler { func New(id string, config config.HandlerConfigT) handler.Handler {
t := &PreparedHandler { t := &PreparedHandler{}
} t.Id = id
t.Id = id t.dbh = database.NewDatabaseHandle()
t.dbh = database.NewDatabaseHandle() log.Printf("Handler Prepared %d initialized", id)
log.Printf("Handler Prepared %d initialized", id) return t
return t
} }
func (self *PreparedHandler) Handle(message handler.MessageT) { func (self *PreparedHandler) Handle(message handler.MessageT) {
log.Printf("Handler Prepared %d processing %s -> %s", self.Id, message.Topic, message.Payload) log.Printf("Handler Prepared %d processing %s -> %s", self.Id, message.Topic, message.Payload)
var observation observation_t var observation observation_t
err := json.Unmarshal([]byte(message.Payload), &observation) err := json.Unmarshal([]byte(message.Payload), &observation)
if err != nil { if err != nil {
self.Lost("Unable to parse payload into Observation struct", err, message) self.Lost("Unable to parse payload into Observation struct", err, message)
return return
} }
var measurement database.Measurement var measurement database.Measurement
measurement.Time = time.Now() measurement.Time = time.Now()
measurement.Application = self.Id measurement.Application = self.Id
measurement.Device = observation.Device measurement.Device = observation.Device
measurement.Attributes = map[string]interface{} { measurement.Attributes = map[string]interface{}{
"Label": observation.Label, "Label": observation.Label,
} }
measurement.Values = make(map[string]database.VariableType) measurement.Values = make(map[string]database.VariableType)
for k, v := range observation.Variables { for k, v := range observation.Variables {
measurement.Values[k] = database.VariableType { measurement.Values[k] = database.VariableType{
Label: v.Label, Label: v.Label,
Variable: v.Variable, Variable: v.Variable,
Unit: v.Unit, Unit: v.Unit,
Value: v.Value, Value: v.Value,
Status: v.Status, Status: v.Status,
} }
} }
log.Printf("Prepared measurement item: %s", measurement) log.Printf("Prepared measurement item: %s", measurement)
self.dbh.StoreMeasurement(&measurement) self.dbh.StoreMeasurement(&measurement)
self.S() self.S()
} }