Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add javascript support to the http plugin #1944

Merged
merged 10 commits into from
Nov 28, 2021
124 changes: 123 additions & 1 deletion provider/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
"encoding/hex"
"fmt"
"io"
"math"
Expand All @@ -9,12 +10,15 @@ import (
"strings"
"time"

"github.com/evcc-io/evcc/provider/javascript"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/jq"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/itchyny/gojq"
"github.com/jpfielding/go-http-digest/pkg/digest"
"github.com/robertkrimen/otto"
"github.com/volkszaehler/mbmd/meters/rs485"
)

// HTTP implements HTTP request provider
Expand All @@ -25,6 +29,10 @@ type HTTP struct {
body string
re *regexp.Regexp
jq *gojq.Query
unpack string
decode string
vm *otto.Otto
script string
scale float64
cache time.Duration
updated time.Time
Expand All @@ -49,6 +57,10 @@ func NewHTTPProviderFromConfig(other map[string]interface{}) (IntProvider, error
Body string
Regex string
Jq string
Unpack string
Decode string
VM string
Script string
Scale float64
Insecure bool
Auth Auth
Expand Down Expand Up @@ -83,6 +95,18 @@ func NewHTTPProviderFromConfig(other map[string]interface{}) (IntProvider, error
_, err = http.WithJq(cc.Jq)
}

if err == nil && cc.Unpack != "" {
_, err = http.WithUnpack(cc.Unpack)
}

if err == nil && cc.Decode != "" {
_, err = http.WithDecode(cc.Decode)
}

if err == nil && cc.Script != "" {
_, err = http.WithScript(cc.VM, cc.Script)
}

if err == nil && cc.Auth.Type != "" {
_, err = http.WithAuth(cc.Auth.Type, cc.Auth.User, cc.Auth.Password)
}
Expand Down Expand Up @@ -149,6 +173,30 @@ func (p *HTTP) WithJq(jq string) (*HTTP, error) {
return p, nil
}

// WithUnpack adds data unpacking
func (p *HTTP) WithUnpack(unpack string) (*HTTP, error) {
p.unpack = strings.ToLower(unpack)

return p, nil
}

// WithDecode adds data decoding
func (p *HTTP) WithDecode(decode string) (*HTTP, error) {
p.decode = strings.ToLower(decode)

return p, nil
}

// WithScript adds a javascript script to process the response
func (p *HTTP) WithScript(vm, script string) (*HTTP, error) {
regvm := javascript.RegisteredVM(strings.ToLower(vm))

p.vm = regvm
p.script = script

return p, nil
}

// WithAuth adds authorized transport
func (p *HTTP) WithAuth(typ, user, password string) (*HTTP, error) {
switch strings.ToLower(typ) {
Expand Down Expand Up @@ -184,6 +232,47 @@ func (p *HTTP) request(body ...string) ([]byte, error) {
return p.val, p.err
}

func (p *HTTP) unpackValue(value []byte) (string, error) {
switch p.unpack {
case "hex":
b, err := hex.DecodeString(string(value))
if err != nil {
return "", err
}
return string(b), nil
}

return "", fmt.Errorf("invalid unpack: %s", p.unpack)
}

// decode a hex string to a proper value
func (p *HTTP) decodeValue(value []byte) (interface{}, error) {
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
switch p.decode {
case "float32", "ieee754":
return rs485.RTUIeee754ToFloat64(value), nil
case "float32s", "ieee754s":
return rs485.RTUIeee754ToFloat64Swapped(value), nil
case "float64":
return rs485.RTUUint64ToFloat64(value), nil
case "uint16":
return rs485.RTUUint16ToFloat64(value), nil
case "uint32":
return rs485.RTUUint32ToFloat64(value), nil
case "uint32s":
return rs485.RTUUint32ToFloat64Swapped(value), nil
case "uint64":
return rs485.RTUUint64ToFloat64(value), nil
case "int16":
return rs485.RTUInt16ToFloat64(value), nil
case "int32":
return rs485.RTUInt32ToFloat64(value), nil
case "int32s":
return rs485.RTUInt32ToFloat64Swapped(value), nil
}

return nil, fmt.Errorf("invalid decoding: %s", p.decode)
}

// FloatGetter parses float from request
func (p *HTTP) FloatGetter() func() (float64, error) {
g := p.StringGetter()
Expand Down Expand Up @@ -230,7 +319,40 @@ func (p *HTTP) StringGetter() func() (string, error) {

if p.jq != nil {
v, err := jq.Query(p.jq, b)
return fmt.Sprintf("%v", v), err
if err != nil {
return string(b), err
}
b = []byte(fmt.Sprintf("%v", v))
}

if p.unpack != "" {
v, err := p.unpackValue(b)
if err != nil {
return string(b), err
}
b = []byte(fmt.Sprintf("%v", v))
}

if p.decode != "" {
v, err := p.decodeValue(b)
if err != nil {
return string(b), err
}
b = []byte(fmt.Sprintf("%v", v))
}

if p.vm != nil {
inputScript := fmt.Sprintf("var input = '" + string(b) + "';\n")
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
script := inputScript + p.script
v, err := p.vm.Eval(script)
if err != nil {
fmt.Println(err)
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
return string(b), err
}

s, err := v.ToString()
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println(err)
DerAndereAndi marked this conversation as resolved.
Show resolved Hide resolved
return string(s), err
}

return string(b), err
Expand Down