forked from LUJUNQUAN/hap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
float.go
81 lines (66 loc) · 1.79 KB
/
float.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
package characteristic
import (
"github.com/twxstar/hap/log"
"net/http"
)
type Float struct {
*C
}
func NewFloat(t string) *Float {
c := New()
c.Type = t
return &Float{c}
}
// SetValue sets a value
func (c *Float) SetValue(v float64) {
c.setValue(v, nil)
}
func (c *Float) SetMinValue(v float64) {
c.MinVal = v
}
func (c *Float) SetMaxValue(v float64) {
c.MaxVal = v
}
func (c *Float) SetStepValue(v float64) {
c.StepVal = v
}
// Value returns the value of c as float64.
func (c *Float) Value() float64 {
return c.C.value().(float64)
}
func (c *Float) MinValue() float64 {
return c.MinVal.(float64)
}
func (c *Float) MaxValue() float64 {
return c.MaxVal.(float64)
}
func (c *Float) StepValue() float64 {
return c.StepVal.(float64)
}
// OnSetRemoteValue set c.SetValueRequestFunc and calls fn.
// If the function returns an error, the code -70402 is
// included in the HTTP response.
func (c *Float) OnSetRemoteValue(fn func(v float64) error) {
c.SetValueRequestFunc = func(v interface{}, r *http.Request) (interface{}, int) {
if err := fn(v.(float64)); err != nil {
log.Debug.Println(err)
return nil, -70402
}
return nil, 0
}
}
// OnValueRemoteUpdate calls fn when the value of the characteristic was updated.
// If the provided http request is not nil, the value was updated by a paired controller (ex. iOS device).
func (c *Float) OnValueUpdate(fn func(new, old float64, r *http.Request)) {
c.OnCValueUpdate(func(c *C, new, old interface{}, r *http.Request) {
fn(new.(float64), old.(float64), r)
})
}
// OnValueRemoteUpdate calls fn when the value of the C was updated by a paired controller (ex. iOS device).
func (c *Float) OnValueRemoteUpdate(fn func(v float64)) {
c.OnCValueUpdate(func(c *C, new, old interface{}, r *http.Request) {
if r != nil {
fn(new.(float64))
}
})
}