diff --git a/src/udi/handlers/dt1t/dt1t.go b/src/udi/handlers/dt1t/dt1t.go index aae1c20..5074004 100644 --- a/src/udi/handlers/dt1t/dt1t.go +++ b/src/udi/handlers/dt1t/dt1t.go @@ -1,81 +1,75 @@ package dt1t import ( - "log" - "fmt" - "time" - "strconv" - "udi/handlers/handler" - "udi/database" - "udi/config" + "log" + "strconv" + "time" + "udi/config" + "udi/database" + "udi/handlers/handler" ) - type Dt1tHandler struct { - handler.CommonHandler - ready bool - label string - dbh *database.DatabaseHandle - application string - device string - + handler.CommonHandler + ready bool + label string + dbh *database.DatabaseHandle + application string + device string } func New(id string, config config.HandlerConfigT) handler.Handler { - t := &Dt1tHandler { - } + 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() - log.Printf("Handler DT1T %d initialized", id) - t.ready = true - return t + 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() + log.Printf("Handler DT1T %d initialized", id) + 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) + 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 + 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 + var measurement database.Measurement + measurement.Time = time.Now() + measurement.Application = self.application + measurement.Device = self.device - // log.Printf("Prepared measurement item: %s", measurement) - self.dbh.StoreMeasurement(&measurement) - self.S() + var variable database.VariableType + variable.Label = "Temperature" + variable.Variable = "" + variable.Unit = "°C" + variable.Value = 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() } - -