81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package dt1t
|
|
|
|
import (
|
|
"log"
|
|
"fmt"
|
|
"time"
|
|
"strconv"
|
|
"udi/handlers/handler"
|
|
"udi/database"
|
|
"udi/config"
|
|
)
|
|
|
|
|
|
type Dt1tHandler struct {
|
|
handler.CommonHandler
|
|
ready bool
|
|
label string
|
|
dbh *database.DatabaseHandle
|
|
application string
|
|
device string
|
|
|
|
}
|
|
|
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
t := &Dt1tHandler {
|
|
}
|
|
|
|
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"]
|
|
t.Id = id
|
|
|
|
t.dbh = database.NewDatabaseHandle()
|
|
t.ready = true
|
|
return t
|
|
}
|
|
|
|
func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
|
if ! self.ready {
|
|
self.Lost("Handler is not marked as ready", nil, 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 {
|
|
self.Lost("Invalid raw value", err, message)
|
|
return
|
|
}
|
|
if temperature & 0x8000 != 0{
|
|
temperature = ((temperature - 1) ^ 0xffff) * -1
|
|
}
|
|
temperatureF := float32(temperature) / 10.0
|
|
|
|
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)
|
|
self.S()
|
|
}
|
|
|
|
|