Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
f63c22912a
|
|||
|
c37420a993
|
@@ -105,6 +105,7 @@
|
|||||||
"devicePart": "3",
|
"devicePart": "3",
|
||||||
"valueFrom": "payload",
|
"valueFrom": "payload",
|
||||||
"valuePart": "1",
|
"valuePart": "1",
|
||||||
|
"valueType": "float",
|
||||||
"unitFrom": "payload",
|
"unitFrom": "payload",
|
||||||
"unitPart": "3"
|
"unitPart": "3"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,13 @@ package dt1t
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"udi/handlers/handler"
|
"time"
|
||||||
"udi/database"
|
|
||||||
"udi/config"
|
"udi/config"
|
||||||
|
"udi/database"
|
||||||
|
"udi/handlers/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Dt1tHandler struct {
|
type Dt1tHandler struct {
|
||||||
handler.CommonHandler
|
handler.CommonHandler
|
||||||
ready bool
|
ready bool
|
||||||
@@ -18,12 +16,10 @@ type Dt1tHandler struct {
|
|||||||
dbh *database.DatabaseHandle
|
dbh *database.DatabaseHandle
|
||||||
application string
|
application string
|
||||||
device string
|
device string
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
t := &Dt1tHandler {
|
t := &Dt1tHandler{}
|
||||||
}
|
|
||||||
|
|
||||||
if config.Attributes["Application"] == "" {
|
if config.Attributes["Application"] == "" {
|
||||||
log.Println("Error: application not configured")
|
log.Println("Error: application not configured")
|
||||||
@@ -69,7 +65,7 @@ func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
|||||||
variable.Label = "Temperature"
|
variable.Label = "Temperature"
|
||||||
variable.Variable = ""
|
variable.Variable = ""
|
||||||
variable.Unit = "°C"
|
variable.Unit = "°C"
|
||||||
variable.Value = fmt.Sprintf("%f", temperatureF)
|
variable.Value = temperatureF
|
||||||
measurement.Values = make(map[string]database.VariableType)
|
measurement.Values = make(map[string]database.VariableType)
|
||||||
measurement.Values["Value"] = variable
|
measurement.Values["Value"] = variable
|
||||||
|
|
||||||
@@ -77,5 +73,3 @@ func (self *Dt1tHandler) Handle(message handler.MessageT) {
|
|||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
self.S()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,16 @@
|
|||||||
package sver
|
package sver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"log"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"regexp"
|
"time"
|
||||||
"log"
|
|
||||||
"udi/config"
|
"udi/config"
|
||||||
"udi/handlers/handler"
|
|
||||||
"udi/database"
|
"udi/database"
|
||||||
|
"udi/handlers/handler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type SingleValueExtractorRegexHandler struct {
|
type SingleValueExtractorRegexHandler struct {
|
||||||
handler.CommonHandler
|
handler.CommonHandler
|
||||||
ready bool
|
ready bool
|
||||||
@@ -32,12 +31,12 @@ type localConfig struct {
|
|||||||
device string
|
device string
|
||||||
valueFrom string
|
valueFrom string
|
||||||
valuePart int
|
valuePart int
|
||||||
|
valueType string
|
||||||
unitFrom string
|
unitFrom string
|
||||||
unitPart int
|
unitPart int
|
||||||
unit string
|
unit string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func New(id string, config config.HandlerConfigT) handler.Handler {
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
||||||
t := &SingleValueExtractorRegexHandler{
|
t := &SingleValueExtractorRegexHandler{
|
||||||
ready: false,
|
ready: false,
|
||||||
@@ -88,6 +87,12 @@ func New(id string, config config.HandlerConfigT) handler.Handler {
|
|||||||
localConfig.valuePart = valuePart
|
localConfig.valuePart = valuePart
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.Attributes["valueType"] != "float" && config.Attributes["valueType"] != "string" {
|
||||||
|
log.Printf("Error: invalid value %s for valueType", config.Attributes["valueType"])
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
localConfig.valueType = config.Attributes["valueType"]
|
||||||
|
|
||||||
if config.Attributes["unitFrom"] != PAYLOAD_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL {
|
if config.Attributes["unitFrom"] != PAYLOAD_SEL && config.Attributes["unitFrom"] != CONSTANT_SEL {
|
||||||
log.Printf("Error: invalid value %s for unitFrom", config.Attributes["unitFrom"])
|
log.Printf("Error: invalid value %s for unitFrom", config.Attributes["unitFrom"])
|
||||||
return t
|
return t
|
||||||
@@ -161,6 +166,7 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
variable.Label = ""
|
variable.Label = ""
|
||||||
variable.Variable = ""
|
variable.Variable = ""
|
||||||
|
|
||||||
|
var value string
|
||||||
switch self.config.valueFrom {
|
switch self.config.valueFrom {
|
||||||
case PAYLOAD_SEL:
|
case PAYLOAD_SEL:
|
||||||
if self.payloadRegex == nil {
|
if self.payloadRegex == nil {
|
||||||
@@ -171,9 +177,19 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
self.Lost("valuePart out of range", nil, message)
|
self.Lost("valuePart out of range", nil, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
variable.Value = payloadMatches[self.config.valuePart]
|
value = payloadMatches[self.config.valuePart]
|
||||||
case PAYLOAD_FULL_SEL:
|
case PAYLOAD_FULL_SEL:
|
||||||
variable.Value = message.Payload
|
value = message.Payload
|
||||||
|
}
|
||||||
|
if self.config.valueType == "float" {
|
||||||
|
fValue, err := strconv.ParseFloat(value, 64)
|
||||||
|
if err != nil {
|
||||||
|
self.Lost("Unable to convert value to float", err, message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
variable.Value = fValue
|
||||||
|
} else {
|
||||||
|
variable.Value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
switch self.config.unitFrom {
|
switch self.config.unitFrom {
|
||||||
@@ -197,4 +213,3 @@ func (self *SingleValueExtractorRegexHandler) Handle(message handler.MessageT) {
|
|||||||
self.dbh.StoreMeasurement(&measurement)
|
self.dbh.StoreMeasurement(&measurement)
|
||||||
self.S()
|
self.S()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user