forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_timeout.go
72 lines (61 loc) · 1.45 KB
/
mqtt_timeout.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
package provider
import "encoding/json"
// TimeoutHandler is a wrapper for a Getter that times out after a given duration
type TimeoutHandler struct {
ticker func() (string, error)
}
func NewTimeoutHandler(ticker func() (string, error)) *TimeoutHandler {
return &TimeoutHandler{ticker}
}
func (h *TimeoutHandler) BoolGetter(p BoolProvider) (func() (bool, error), error) {
g, err := p.BoolGetter()
if err != nil {
return nil, err
}
return func() (val bool, err error) {
if val, err = g(); err == nil {
_, err = h.ticker()
}
return val, err
}, nil
}
func (h *TimeoutHandler) FloatGetter(p FloatProvider) (func() (float64, error), error) {
g, err := p.FloatGetter()
if err != nil {
return nil, err
}
return func() (val float64, err error) {
if val, err = g(); err == nil {
_, err = h.ticker()
}
return val, err
}, nil
}
func (h *TimeoutHandler) StringGetter(p StringProvider) (func() (string, error), error) {
g, err := p.StringGetter()
if err != nil {
return nil, err
}
return func() (val string, err error) {
if val, err = g(); err == nil {
_, err = h.ticker()
}
return val, err
}, nil
}
func (h *TimeoutHandler) JsonGetter(p StringProvider) (func(any) error, error) {
g, err := p.StringGetter()
if err != nil {
return nil, err
}
return func(res any) error {
val, err := g()
if err != nil {
return err
}
if _, err := h.ticker(); err != nil {
return err
}
return json.Unmarshal([]byte(val), res)
}, nil
}