forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.go
132 lines (111 loc) Β· 2.59 KB
/
javascript.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package provider
import (
"strings"
"github.com/evcc-io/evcc/provider/javascript"
"github.com/evcc-io/evcc/util"
"github.com/robertkrimen/otto"
)
// Javascript implements Javascript request provider
type Javascript struct {
vm *otto.Otto
script string
}
func init() {
registry.Add("js", NewJavascriptProviderFromConfig)
}
// NewJavascriptProviderFromConfig creates a HTTP provider
func NewJavascriptProviderFromConfig(other map[string]interface{}) (IntProvider, error) {
var cc struct {
VM string
Script string
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
vm := javascript.RegisteredVM(strings.ToLower(cc.VM))
p := &Javascript{
vm: vm,
script: cc.Script,
}
return p, nil
}
// FloatGetter parses float from request
func (p *Javascript) FloatGetter() func() (float64, error) {
return func() (res float64, err error) {
v, err := p.vm.Eval(p.script)
if err == nil {
res, err = v.ToFloat()
}
return res, err
}
}
// IntGetter parses int64 from request
func (p *Javascript) IntGetter() func() (int64, error) {
return func() (res int64, err error) {
v, err := p.vm.Eval(p.script)
if err == nil {
res, err = v.ToInteger()
}
return res, err
}
}
// StringGetter sends string request
func (p *Javascript) StringGetter() func() (string, error) {
return func() (res string, err error) {
v, err := p.vm.Eval(p.script)
if err == nil {
res, err = v.ToString()
}
return res, err
}
}
// BoolGetter parses bool from request
func (p *Javascript) BoolGetter() func() (bool, error) {
return func() (res bool, err error) {
v, err := p.vm.Eval(p.script)
if err == nil {
res, err = v.ToBoolean()
}
return res, err
}
}
func (p *Javascript) setParam(param string, val interface{}) error {
err := p.vm.Set(param, val)
if err == nil {
err = p.vm.Set("param", param)
}
if err == nil {
err = p.vm.Set("val", val)
}
return err
}
// IntSetter sends int request
func (p *Javascript) IntSetter(param string) func(int64) error {
return func(val int64) error {
err := p.setParam(param, val)
if err == nil {
_, err = p.vm.Eval(p.script)
}
return err
}
}
// StringSetter sends string request
func (p *Javascript) StringSetter(param string) func(string) error {
return func(val string) error {
err := p.setParam(param, val)
if err == nil {
_, err = p.vm.Eval(p.script)
}
return err
}
}
// BoolSetter sends bool request
func (p *Javascript) BoolSetter(param string) func(bool) error {
return func(val bool) error {
err := p.setParam(param, val)
if err == nil {
_, err = p.vm.Eval(p.script)
}
return err
}
}