-
Notifications
You must be signed in to change notification settings - Fork 2
/
attr.go
125 lines (109 loc) · 2.52 KB
/
attr.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
package zcl
import "encoding/json"
type AttrID Zu16
func (i AttrID) MarshalZcl() ([]byte, error) { return Zu16(i).MarshalZcl() }
type UnknownAttr struct {
Type TypeID
AttrID AttrID
ClusterID ClusterID
AttrValue Val
}
func (a *UnknownAttr) UnmarshalJSON(b []byte) error {
s := &struct {
Type TypeID
Value json.RawMessage
}{}
if err := json.Unmarshal(b, s); err != nil {
return err
}
a.Type = s.Type
if len(s.Value) > 0 && string(s.Value) != "null" {
a.AttrValue = NewValue(uint8(s.Type))
return json.Unmarshal(s.Value, &a.AttrValue)
}
return nil
}
func (a UnknownAttr) MarshalJSON() ([]byte, error) {
s := &struct {
Type TypeID
Value json.RawMessage
}{}
s.Type = a.Type
if a.AttrValue == nil {
s.Value = json.RawMessage("null")
} else {
s.Value, _ = json.Marshal(a.AttrValue)
}
return json.Marshal(s)
}
func JsonAttr(a Attr) ([]byte, error) {
return json.Marshal(struct {
ID AttrID
Type TypeID
Value Val
}{
ID: a.ID(),
Type: a.TypeID(),
Value: a.Value(),
})
}
type TypeVal interface {
Val
TypeID() TypeID
}
type Argument interface {
TypeVal
Name() string
String() string
Value() Val
SetValue(v Val) error
}
type Attr interface {
Argument
ID() AttrID
Readable() bool
Writable() bool
Reportable() bool
SceneIndex() int
}
type AttrDef interface {
AttrID() AttrID
AttrType() TypeID
}
type AttrValue interface {
AttrDef
AttrValue() Val
}
type ClusterAttrImpl struct {
Attr
ClusterID ClusterID
}
func (c ClusterAttrImpl) Cluster() ClusterID {
return c.ClusterID
}
type ClusterAttr interface {
Attr
Cluster() ClusterID
}
func (a UnknownAttr) ID() AttrID { return a.AttrID }
func (a UnknownAttr) Name() string { return Sprintf("Unknown(0x%04X)", a.AttrID) }
func (a UnknownAttr) TypeID() TypeID { return a.Type }
func (a UnknownAttr) String() string { return Sprintf("%v", a.AttrValue) }
func (a UnknownAttr) Cluster() ClusterID { return a.ClusterID }
func (a UnknownAttr) Readable() bool { return true }
func (a UnknownAttr) Writable() bool { return false }
func (a UnknownAttr) Reportable() bool { return false }
func (a UnknownAttr) SceneIndex() int { return -1 }
func (a UnknownAttr) SetValue(v Val) error { a.AttrValue = v; return nil }
func (a *UnknownAttr) Value() Val {
if a.AttrValue == nil {
return nil
}
return a.AttrValue
}
func (a UnknownAttr) MarshalZcl() ([]byte, error) {
return a.AttrValue.MarshalZcl()
}
func (a *UnknownAttr) UnmarshalZcl(b []byte) ([]byte, error) {
return a.AttrValue.UnmarshalZcl(b)
}