Expose and document scale parameter (#90)
This commit is contained in:
parent
73753e1212
commit
ec5b258e39
4 changed files with 36 additions and 27 deletions
13
README.md
13
README.md
|
|
@ -305,8 +305,16 @@ Sample configuration:
|
|||
```yaml
|
||||
type: mqtt
|
||||
topic: mbmd/sdm1-1/Power
|
||||
timeout: 30s
|
||||
payload: ${var:%.2f}
|
||||
timeout: 30s # don't accept values older than timeout
|
||||
scale: 0.001 # floating point factor applied to result, e.g. for kW to W conversion
|
||||
```
|
||||
|
||||
Sample write configuration:
|
||||
|
||||
```yaml
|
||||
type: mqtt
|
||||
topic: mbmd/charger/maxcurrent
|
||||
payload: ${var:%d}
|
||||
```
|
||||
|
||||
For write access, the data is provided using the `payload` attribute. If `payload` is missing, the value will be written in default format.
|
||||
|
|
@ -344,6 +352,7 @@ method: GET # default HTTP method
|
|||
headers:
|
||||
- content-type: application/json
|
||||
jq: .data.tuples[0][1] # parse response json
|
||||
scale: 0.001 # floating point factor applied to result, e.g. for kW to W conversion
|
||||
```
|
||||
|
||||
Sample write configuration:
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ type Config struct {
|
|||
// mqttConfig is the specific mqtt getter/setter configuration
|
||||
type mqttConfig struct {
|
||||
Topic, Payload string // Payload only applies to setters
|
||||
Multiplier float64
|
||||
Scale float64
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
|
|
@ -42,8 +42,8 @@ func mqttFromConfig(log *util.Logger, other map[string]interface{}) mqttConfig {
|
|||
var pc mqttConfig
|
||||
util.DecodeOther(log, other, &pc)
|
||||
|
||||
if pc.Multiplier == 0 {
|
||||
pc.Multiplier = 1
|
||||
if pc.Scale == 0 {
|
||||
pc.Scale = 1
|
||||
}
|
||||
|
||||
return pc
|
||||
|
|
@ -67,7 +67,7 @@ func NewFloatGetterFromConfig(log *util.Logger, config Config) (res FloatGetter)
|
|||
res = NewHTTPProviderFromConfig(log, config.Other).FloatGetter
|
||||
case "mqtt":
|
||||
pc := mqttFromConfig(log, config.Other)
|
||||
res = MQTT.FloatGetter(pc.Topic, pc.Multiplier, pc.Timeout)
|
||||
res = MQTT.FloatGetter(pc.Topic, pc.Scale, pc.Timeout)
|
||||
case "script":
|
||||
pc := scriptFromConfig(log, config.Other)
|
||||
res = NewScriptProvider(pc.Timeout).FloatGetter(pc.Cmd)
|
||||
|
|
@ -90,7 +90,7 @@ func NewIntGetterFromConfig(log *util.Logger, config Config) (res IntGetter) {
|
|||
res = NewHTTPProviderFromConfig(log, config.Other).IntGetter
|
||||
case "mqtt":
|
||||
pc := mqttFromConfig(log, config.Other)
|
||||
res = MQTT.IntGetter(pc.Topic, int64(pc.Multiplier), pc.Timeout)
|
||||
res = MQTT.IntGetter(pc.Topic, int64(pc.Scale), pc.Timeout)
|
||||
case "script":
|
||||
pc := scriptFromConfig(log, config.Other)
|
||||
res = NewScriptProvider(pc.Timeout).IntGetter(pc.Cmd)
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ func (p *HTTP) FloatGetter() (float64, error) {
|
|||
}
|
||||
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
if err == nil && p.scale > 0 {
|
||||
if err == nil && p.scale != 0 {
|
||||
f *= p.scale
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -105,12 +105,12 @@ func (m *MqttClient) listen(topic string, callback func(string)) {
|
|||
}
|
||||
|
||||
// FloatGetter creates handler for float64 from MQTT topic that returns cached value
|
||||
func (m *MqttClient) FloatGetter(topic string, multiplier float64, timeout time.Duration) FloatGetter {
|
||||
func (m *MqttClient) FloatGetter(topic string, scale float64, timeout time.Duration) FloatGetter {
|
||||
h := &msgHandler{
|
||||
log: m.log,
|
||||
topic: topic,
|
||||
multiplier: multiplier,
|
||||
timeout: timeout,
|
||||
log: m.log,
|
||||
topic: topic,
|
||||
scale: scale,
|
||||
timeout: timeout,
|
||||
}
|
||||
|
||||
m.Listen(topic, h.Receive)
|
||||
|
|
@ -118,12 +118,12 @@ func (m *MqttClient) FloatGetter(topic string, multiplier float64, timeout time.
|
|||
}
|
||||
|
||||
// IntGetter creates handler for int64 from MQTT topic that returns cached value
|
||||
func (m *MqttClient) IntGetter(topic string, multiplier int64, timeout time.Duration) IntGetter {
|
||||
func (m *MqttClient) IntGetter(topic string, scale int64, timeout time.Duration) IntGetter {
|
||||
h := &msgHandler{
|
||||
log: m.log,
|
||||
topic: topic,
|
||||
multiplier: float64(multiplier),
|
||||
timeout: timeout,
|
||||
log: m.log,
|
||||
topic: topic,
|
||||
scale: float64(scale),
|
||||
timeout: timeout,
|
||||
}
|
||||
|
||||
m.Listen(topic, h.Receive)
|
||||
|
|
@ -213,14 +213,14 @@ func (m *MqttClient) WaitForToken(token mqtt.Token) {
|
|||
}
|
||||
|
||||
type msgHandler struct {
|
||||
log *util.Logger
|
||||
once sync.Once
|
||||
mux sync.Mutex
|
||||
updated time.Time
|
||||
timeout time.Duration
|
||||
multiplier float64
|
||||
topic string
|
||||
payload string
|
||||
log *util.Logger
|
||||
once sync.Once
|
||||
mux sync.Mutex
|
||||
updated time.Time
|
||||
timeout time.Duration
|
||||
scale float64
|
||||
topic string
|
||||
payload string
|
||||
}
|
||||
|
||||
func (h *msgHandler) Receive(payload string) {
|
||||
|
|
@ -263,7 +263,7 @@ func (h *msgHandler) floatGetter() (float64, error) {
|
|||
return 0, fmt.Errorf("%s invalid: '%s'", h.topic, h.payload)
|
||||
}
|
||||
|
||||
return h.multiplier * val, nil
|
||||
return h.scale * val, nil
|
||||
}
|
||||
|
||||
func (h *msgHandler) intGetter() (int64, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue