Skip to content

Commit

Permalink
Expose and document scale parameter (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 2, 2020
1 parent 73753e1 commit ec5b258
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
13 changes: 11 additions & 2 deletions README.md
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions provider/config.go
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion provider/http.go
Expand Up @@ -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
}

Expand Down
38 changes: 19 additions & 19 deletions provider/mqtt.go
Expand Up @@ -105,25 +105,25 @@ 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)
return h.floatGetter
}

// 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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ec5b258

Please sign in to comment.