-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
80 lines (68 loc) · 1.79 KB
/
state.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
// Copyright (c) 2018 Bernhard Fluehmann. All rights reserved.
// Use of this source code is governed by ISC-style license
// that can be found in the LICENSE file.
//
// HomeMatic XML-API client library
//
// Implementation of state API-method
package homematic
import (
"net/url"
"encoding/xml"
"golang.org/x/net/html/charset"
"errors"
)
type State struct {
XMLName xml.Name `xml:"state"`
Device struct {
Name string `xml:"name,attr"`
IseID string `xml:"ise_id,attr"`
Unreach string `xml:"unreach,attr"`
StickyUnreach string `xml:"sticky_unreach,attr"`
ConfigPending string `xml:"config_pending,attr"`
Channel []struct {
Name string `xml:"name,attr"`
IseID string `xml:"ise_id,attr"`
Datapoint []struct {
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
IseID string `xml:"ise_id,attr"`
Value string `xml:"value,attr"`
Valuetype string `xml:"valuetype,attr"`
Valueunit string `xml:"valueunit,attr"`
Timestamp string `xml:"timestamp,attr"`
} `xml:"datapoint"`
} `xml:"channel"`
} `xml:"device"`
}
// Returns a list of devices
func (api *XmlApi) State(IseID string) (*State, error) {
if IseID == "" {
return nil, errors.New("Empty name")
}
var u *url.URL
u, err := url.Parse(api.apiURL)
if err != nil {
return nil, err
}
// Build URL
u.Path += "state.cgi"
// Set URL parameters
params := url.Values{}
params.Add("device_id", IseID)
u.RawQuery = params.Encode()
// HTTP GET
resp, err := api.client.Get(u.String())
if err != nil {
return nil, err
}
// Empty DeviceList object
var state State
decoder := xml.NewDecoder(resp.Body)
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(&state)
if err != nil {
return nil, err
}
return &state, nil
}