forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
93 lines (78 loc) · 2.37 KB
/
types.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
package homematic
import (
"encoding/xml"
"fmt"
)
// Homematic CCU XML-RPC types
// https://homematic-ip.com/sites/default/files/downloads/HM_XmlRpc_API.pdf
// https://homematic-ip.com/sites/default/files/downloads/HMIP_XmlRpc_API_Addendum.pdf
type MethodCall struct {
XMLName xml.Name `xml:"methodCall"`
MethodName string `xml:"methodName"`
Params []Param `xml:"params>param,omitempty"`
}
type Param struct {
CCUBool string `xml:"value>boolean,omitempty"`
CCUFloat float64 `xml:"value>double,omitempty"`
CCUInt int64 `xml:"value>i4,omitempty"`
CCUString string `xml:"value>string,omitempty"`
}
type Member struct {
Name string `xml:"name,omitempty"`
Value Value `xml:"value,omitempty"`
}
type MethodResponse struct {
XMLName xml.Name `xml:"methodResponse"`
CCUBool string `xml:"params>param>value>boolean,omitempty"`
CCUFloat float64 `xml:"params>param>value>double,omitempty"`
CCUInt int64 `xml:"params>param>value>i4,omitempty"`
CCUString string `xml:"params>param>value>string,omitempty"`
Member []Member `xml:"params>param>value>struct>member,omitempty"`
Fault []Member `xml:"fault>value>struct>member,omitempty"`
}
// FloatValue selects a float value of a CCU API response member
func (res *MethodResponse) FloatValue(val string) float64 {
for _, m := range res.Member {
if m.Name == val {
return m.Value.CCUFloat
}
}
return 0
}
// BoolValue selects a float value of a CCU API response member
func (res *MethodResponse) BoolValue(val string) bool {
for _, m := range res.Member {
if m.Name == val {
return m.Value.CCUBool
}
}
return false
}
// Error checks on Homematic CCU error codes
// Refer to page 30 of https://homematic-ip.com/sites/default/files/downloads/HM_XmlRpc_API.pdf
func (res *MethodResponse) Error() error {
var faultCode int64
var faultString string
for _, f := range res.Fault {
if f.Name == "faultCode" {
faultCode = f.Value.CCUInt
}
if f.Name == "faultString" {
faultString = f.Value.CCUString
}
}
if faultCode != 0 {
if faultString == "" {
faultString = "unknown api error"
}
return fmt.Errorf("%s (%v)", faultString, faultCode)
}
return nil
}
type Value struct {
XMLName xml.Name `xml:"value"`
CCUString string `xml:",chardata"`
CCUInt int64 `xml:"i4,omitempty"`
CCUBool bool `xml:"boolean,omitempty"`
CCUFloat float64 `xml:"double,omitempty"`
}