Skip to content

Commit 14522a8

Browse files
committed
Use Unmarshal in fetch
Also fix some lint issues
1 parent 30eaaa9 commit 14522a8

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

main.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type bridgeCfg struct {
6363
Client MQTT.Client
6464
WriteChannel chan writeEvent
6565
RefreshRoomChannel chan string
66-
HeatingUrl string
66+
HeatingURL string
6767
Polling int
6868
Topic string
6969
LastNumberOfDevices int
@@ -78,7 +78,7 @@ func setupCloseHandler(bridge *bridgeCfg) {
7878
}()
7979
}
8080

81-
func generateXml(values []string, prefix string) string {
81+
func generateXML(values []string, prefix string) string {
8282
xmlValue := "<content>"
8383
for _, v := range values {
8484
xmlValue += "<field>"
@@ -95,25 +95,35 @@ func generateXml(values []string, prefix string) string {
9595
return xmlValue
9696
}
9797

98-
func fetch(ip string, values []string, prefix string) []byte {
98+
func fetch(ip string, values []string, prefix string) content {
9999
url := "http://" + ip + "/cgi-bin/ILRReadValues.cgi"
100-
xmlValue := generateXml(values, prefix)
100+
xmlValue := generateXML(values, prefix)
101+
var c content
101102

102103
resp, err := http.Post(url, "text/xml", bytes.NewBuffer([]byte(xmlValue)))
103-
if err == nil {
104-
defer resp.Body.Close()
105-
body, err := ioutil.ReadAll(resp.Body)
106-
if err == nil {
107-
return body
108-
}
104+
if err != nil {
105+
log.Print(err)
106+
return c
107+
}
108+
109+
defer resp.Body.Close()
110+
body, err := ioutil.ReadAll(resp.Body)
111+
if err != nil {
112+
log.Print(err)
113+
return c
109114
}
110115

111-
log.Print(err)
112-
return []byte("")
116+
err = xml.Unmarshal(body, &c)
117+
if err != nil {
118+
log.Print(err)
119+
return c
120+
}
121+
122+
return c
113123
}
114124

115125
func propagate(bridge *bridgeCfg, name string, value string, prefix string) bool {
116-
url := "http://" + bridge.HeatingUrl + "/cgi-bin/writeVal.cgi?" + prefix + "." + name + "=" + value
126+
url := "http://" + bridge.HeatingURL + "/cgi-bin/writeVal.cgi?" + prefix + "." + name + "=" + value
117127

118128
resp, err := http.Get(url)
119129
if err == nil {
@@ -144,10 +154,7 @@ func refreshSystemInformation(bridge *bridgeCfg) int {
144154
"VPI.href", "VPI.state",
145155
"CD.uname", "CD.upass", "CD.ureg"}
146156

147-
body := fetch(bridge.HeatingUrl, fields, "")
148-
149-
var c content
150-
xml.Unmarshal(body, &c)
157+
c := fetch(bridge.HeatingURL, fields, "")
151158

152159
totalNumberOfDevices := 0
153160
for i := 0; i < len(c.Entries); i++ {
@@ -172,10 +179,7 @@ func refreshRoomInformation(bridge *bridgeCfg, number string) {
172179
"RaumTemp", "SollTemp", "TempSIUnit", "WeekProg", "WeekProgEna",
173180
"SollTempStepVal", "SollTempMinVal", "SollTempMaxVal"}
174181

175-
body := fetch(bridge.HeatingUrl, fields, number)
176-
177-
var c content
178-
xml.Unmarshal(body, &c)
182+
c := fetch(bridge.HeatingURL, fields, number)
179183

180184
for i := 0; i < len(c.Entries); i++ {
181185
splitted := strings.SplitN(c.Entries[i].Name, ".", 2)
@@ -319,7 +323,7 @@ func main() {
319323
KeepRunning: make(chan bool),
320324
WriteChannel: make(chan writeEvent, 50),
321325
RefreshRoomChannel: make(chan string, 50),
322-
HeatingUrl: *heating,
326+
HeatingURL: *heating,
323327
Polling: *polling,
324328
Topic: *topic,
325329
LastNumberOfDevices: 0,

0 commit comments

Comments
 (0)