/
device.go
172 lines (150 loc) · 5.59 KB
/
device.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package hmlib
import (
"encoding/xml"
"fmt"
"github.com/tommi2day/gomodules/common"
log "github.com/sirupsen/logrus"
)
// DeviceListEndpoint is the endpoint for the device list
const DeviceListEndpoint = "/addons/xmlapi/devicelist.cgi"
// DeviceTypeListEndpoint is the endpoint for the device type list
const DeviceTypeListEndpoint = "/addons/xmlapi/devicetypelist.cgi"
// DeviceAddressMap is a list of devices by name
var DeviceAddressMap = map[string]DeviceListEntry{}
// DeviceIDMap is a list of devices by id
var DeviceIDMap = map[string]DeviceListEntry{}
// DeviceTypes is a list of device types per name
var DeviceTypes = map[string]DeviceTypeEntry{}
// DeviceListResponse is a list of devices returned by API
type DeviceListResponse struct {
XMLName xml.Name `xml:"deviceList"`
DeviceListEntries []DeviceListEntry `xml:"device"`
}
// DeviceListEntry is a single device
type DeviceListEntry struct {
XMLName xml.Name `xml:"device"`
Name string `xml:"name,attr"`
Type string `xml:"device_type,attr"`
Address string `xml:"address,attr"`
Interface string `xml:"interface,attr"`
IseID string `xml:"ise_id,attr"`
ReadyConfig string `xml:"ready_config,attr"`
Channels []DeviceChannel `xml:"channel"`
}
// DeviceChannel is a single channel of a device
type DeviceChannel struct {
XMLName xml.Name `xml:"channel"`
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
Address string `xml:"address,attr"`
IseID string `xml:"ise_id,attr"`
ParentDevice string `xml:"parent_device,attr"`
Index string `xml:"index,attr"`
Direction string `xml:"direction,attr"`
GroupPartner string `xml:"group_partner,attr"`
AesAvailable string `xml:"aes_available,attr"`
TransmissionMode string `xml:"transmission_mode,attr"`
Visible string `xml:"visible,attr"`
ReadyConfig string `xml:"ready_config,attr"`
Operate string `xml:"operate,attr"`
}
// DeviceTypeListResponse is a list of device types returned by API
type DeviceTypeListResponse struct {
XMLName xml.Name `xml:"deviceTypeList"`
DeviceTypeListEntries []DeviceTypeEntry `xml:"deviceType"`
}
// DeviceTypeEntry is a single device type
type DeviceTypeEntry struct {
XMLName xml.Name `xml:"deviceType"`
Name string `xml:"name,attr"`
Description string `xml:"description,attr"`
ThumbnailPath string `xml:"thumbnailPath,attr"`
ImagePath string `xml:"imagePath,attr"`
Forms []DeviceTypeForm `xml:"form"`
}
// DeviceTypeForm is a single form of a device type
type DeviceTypeForm struct {
XMLName xml.Name `xml:"form"`
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
}
// GetDeviceList returns the list of devices
func GetDeviceList(deviceIDs string, internal bool) (result DeviceListResponse, err error) {
result = DeviceListResponse{}
log.Debug("devicelist called")
var parameter = map[string]string{}
if len(deviceIDs) > 0 {
parameter["device_id"] = common.RemoveSpace(deviceIDs)
}
if internal {
parameter["show_internal"] = "1"
}
// reset maps
AllIDs = map[string]IDMapEntry{}
DeviceAddressMap = map[string]DeviceListEntry{}
DeviceIDMap = map[string]DeviceListEntry{}
// query
err = QueryAPI(DeviceListEndpoint, &result, parameter)
if err != nil {
log.Errorf("devicelist returned error: %s", err)
return
}
for _, e := range result.DeviceListEntries {
DeviceIDMap[e.IseID] = e
DeviceAddressMap[e.Address] = e
AllIDs[e.IseID] = IDMapEntry{e.IseID, e.Name, "Device", e}
for _, c := range e.Channels {
AllIDs[c.IseID] = IDMapEntry{c.IseID, c.Name, "Channel", c}
}
}
log.Debugf("devicelist returned %d devices", len(result.DeviceListEntries))
return
}
// GetDeviceTypeList returns the list of device types
func GetDeviceTypeList() (result DeviceTypeListResponse, err error) {
result = DeviceTypeListResponse{}
DeviceTypes = map[string]DeviceTypeEntry{}
log.Debug("devicetypelist called")
err = QueryAPI(DeviceTypeListEndpoint, &result, nil)
if err != nil {
log.Errorf("devicetypelist returned error: %s", err)
return
}
log.Debugf("devicetypelist returned %d devices", len(result.DeviceTypeListEntries))
for _, e := range result.DeviceTypeListEntries {
DeviceTypes[e.Name] = e
}
return
}
// String returns a string representation of a device list
func (e DeviceListResponse) String() string {
var s string
for _, e := range e.DeviceListEntries {
s += fmt.Sprintf("%s\n", e)
}
return s
}
// String returns a string representation of a device
func (e DeviceListEntry) String() string {
return fmt.Sprintf("ID:%s, Name: %s, Address: %s, Type: %s", e.IseID, e.Name, e.Address, e.Type)
}
// String returns a string representation of a channel
func (e DeviceChannel) String() string {
return fmt.Sprintf(" C_ID:%s, Name: %s, Address: %s, Type: %s, Parent: %s ", e.IseID, e.Name, e.Address, e.Type, e.ParentDevice)
}
// String returns a string representation of a device type list
func (e DeviceTypeListResponse) String() string {
var s string
for _, e := range e.DeviceTypeListEntries {
s += fmt.Sprintf("%s\n", e)
}
return s
}
// String returns a string representation of a device type
func (e DeviceTypeEntry) String() string {
return fmt.Sprintf("Name: %s, Description: %s, %d forms", e.Name, e.Description, len(e.Forms))
}
// String returns a string representation of a device type form
func (e DeviceTypeForm) String() string {
return fmt.Sprintf("Name: %s, Type: %s", e.Name, e.Type)
}