dt1t
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-12-20 17:15:44 +01:00
parent 3779547a95
commit 99d678b4b1
2 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,91 @@
package dt1t
import (
"log"
"fmt"
"time"
"strconv"
"udi/handlers/handler"
"udi/database"
"udi/config"
)
var idSeq int = 0
type Dt1tHandler struct {
id int
ready bool
dbh *database.DatabaseHandle
application string
device string
}
func NewDt1tHandler(config config.HandlerConfigT) handler.Handler {
t := &Dt1tHandler {
id: idSeq,
}
if config.Attributes["Application"] == "" {
log.Println("Error: application not configured")
return t
}
t.application = config.Attributes["Application"]
if config.Attributes["Device"] == "" {
log.Println("Error: device not configured")
return t
}
t.device = config.Attributes["Device"]
idSeq += 1
t.dbh = database.NewDatabaseHandle()
t.ready = true
return t
}
func (self *Dt1tHandler) GetId() string {
return fmt.Sprintf("DT1T%d", self.id)
}
func lost(msg string, message handler.MessageT) {
log.Printf("Error: %s, message %s is lost", msg, message)
}
func (self *Dt1tHandler) Handle(message handler.MessageT) {
if ! self.ready {
log.Println("Handler is not marked as ready, message %s is lost", message)
return
}
log.Printf("Handler DT1T %d processing %s -> %s", self.id, message.Topic, message.Payload)
temperature, err := strconv.Atoi(message.Payload)
if err != nil {
lost(fmt.Sprintf("Invalid raw value: %s", err), message)
return
}
if temperature & 0x8000 != 0{
temperature = ((temperature - 1) ^ 0xffff) * -1
}
temperatureF := float32(temperature) / 10.0
log.Printf("TemperatureF: %f", temperatureF)
var measurement database.Measurement
measurement.Time = time.Now()
measurement.Application = self.application
measurement.Device = self.device
var variable database.VariableType
variable.Label = "Temperature"
variable.Variable = ""
variable.Unit = "°C"
variable.Value = fmt.Sprintf("%f", temperatureF)
measurement.Values = make(map[string]database.VariableType)
measurement.Values["Value"] = variable
log.Printf("Prepared measurement item: %s", measurement)
self.dbh.StoreMeasurement(&measurement)
}