-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathstring.go
More file actions
59 lines (49 loc) · 1.46 KB
/
string.go
File metadata and controls
59 lines (49 loc) · 1.46 KB
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
package characteristic
import (
"github.com/brutella/hap/log"
"net/http"
)
type String struct {
*C
}
func NewString(typ string) *String {
c := New()
c.Type = typ
c.Format = FormatString
return &String{c}
}
// SetValue sets the value of c to v.
func (c *String) SetValue(v string) {
c.setValue(v, nil)
}
// Value returns the value of c as string.
func (c *String) Value() string {
return c.C.Value().(string)
}
// OnSetRemoteValue set c.SetValueRequestFunc and calls fn.
// If the function returns an error, the code -70402 is
// included in the HTTP response.
func (c *String) OnSetRemoteValue(fn func(v string) error) {
c.SetValueRequestFunc = func(v interface{}, r *http.Request) (interface{}, int) {
if err := fn(v.(string)); 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 *String) OnValueUpdate(fn func(new, old string, r *http.Request)) {
c.OnCValueUpdate(func(c *C, new, old interface{}, r *http.Request) {
fn(new.(string), old.(string), r)
})
}
// OnValueRemoteUpdate calls fn when the value of the C was updated by a paired controller (ex. iOS device).
func (c *String) OnValueRemoteUpdate(fn func(v string)) {
c.OnCValueUpdate(func(c *C, new, old interface{}, r *http.Request) {
if r != nil {
fn(new.(string))
}
})
}