-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathincomfort.go
184 lines (160 loc) · 4.23 KB
/
incomfort.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
173
174
175
176
177
178
179
180
181
182
183
184
// Control thermostat via the incomfort LAN2RF gateway
package incomfort
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
var displayCodes = map[int]string{
85: "sensortest",
170: "service",
204: "tapwater",
51: "tapwater int.",
240: "boiler int.",
15: "boiler ext.",
153: "postrun boiler",
102: "central heating",
0: "opentherm",
255: "buffer",
24: "frost",
231: "postrun ch",
126: "standby",
37: "central heating rf",
}
func lsbMsb(lsb, msb int) float32 {
return float32(lsb+msb*256) / 100
}
// Response of the gateway to a heaterlist request
type heaterListData struct {
HeaterList []*string `json:"heaterlist"`
}
// Response of the gateway to a heater request
type heaterData struct {
NodeNr int `json:"nodenr"`
ChTempLsb int `json:"ch_temp_lsb"`
ChTempMsb int `json:"ch_temp_msb"`
TapTempLsb int `json:"tap_temp_lsb"`
TapTempMsb int `json:"tap_temp_msb"`
ChPressureLsb int `json:"ch_pressure_lsb"`
ChPressureMsb int `json:"ch_pressure_msb"`
RoomTemp1Lsb int `json:"room_temp_1_lsb"`
RoomTemp1Msb int `json:"room_temp_1_msb"`
RoomTempSet1Lsb int `json:"room_temp_set_1_lsb"`
RoomTempSet1Msb int `json:"room_temp_set_1_msb"`
RoomTemp2Lsb int `json:"room_temp_2_lsb"`
RoomTemp2Msb int `json:"room_temp_2_msb"`
RoomTempSet2Lsb int `json:"room_temp_set_2_lsb"`
RoomTempSet2Msb int `json:"room_temp_set_2_msb"`
DisplCode int `json:"displ_code"`
IO int `json:"IO"`
SerialYear int `json:"serial_year"`
SerialMonth int `json:"serial_month"`
SerialLine int `json:"serial_line"`
SerialSn1 int `json:"serial_sn1"`
SerialSn2 int `json:"serial_sn2"`
SerialSn3 int `json:"serial_sn3"`
RoomSetOvr1Msb int `json:"room_set_ovr_1_msb"`
RoomSetOvr1Lsb int `json:"room_set_ovr_1_lsb"`
RoomSetOvr2Msb int `json:"room_set_ovr_2_msb"`
RoomSetOvr2Lsb int `json:"room_set_ovr_2_lsb"`
RfMessageRssi int `json:"rf_message_rssi"`
RfStatusCntr int `json:"rfstatus_cntr"`
}
// Represent the incomfort LAN2RF gateway
type Gateway struct {
Host string
}
// Represent a heater
type Heater struct {
gw *Gateway
Id int
Name string
Pressure float32
HeaterTemp float32
TapTemp float32
RoomTemp float32
Setpoint float32
SetpointOverride float32
DisplayCode string
IsBurning bool
IsLockout bool
IsPumping bool
IsTapping bool
}
func NewGateway(host string) *Gateway {
return &Gateway{host}
}
func (g *Gateway) doGet(path string, data interface{}) error {
resp, err := http.Get("http://" + g.Host + path)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if data == nil {
return nil
}
return json.Unmarshal(body, data)
}
// Returns the list of heaters
func (g *Gateway) Heaters() (heaters []Heater, err error) {
heaters = []Heater{}
var data heaterListData
err2 := g.doGet("/heaterlist.json", &data)
if err2 != nil {
return nil, err2
}
for i, h := range data.HeaterList {
if h == nil {
continue
}
heater := Heater{
gw: g,
Id: i,
Name: *h,
}
heater.Update()
heaters = append(heaters, heater)
}
return
}
// Updates the data
func (h *Heater) Update() error {
var data heaterData
err := h.gw.doGet(fmt.Sprintf("/data.json?heater=%d", h.Id), &data)
if err != nil {
return err
}
h.Pressure = lsbMsb(data.ChPressureLsb, data.ChPressureMsb)
h.HeaterTemp = lsbMsb(data.ChTempLsb, data.ChTempMsb)
h.TapTemp = lsbMsb(data.TapTempLsb, data.TapTempMsb)
h.RoomTemp = lsbMsb(data.RoomTemp1Lsb, data.RoomTemp1Msb)
h.Setpoint = lsbMsb(data.RoomTempSet1Lsb, data.RoomTempSet1Msb)
h.SetpointOverride = lsbMsb(data.RoomSetOvr1Lsb, data.RoomSetOvr1Msb)
if dc, ok := displayCodes[data.DisplCode]; ok {
h.DisplayCode = dc
} else {
h.DisplayCode = fmt.Sprintf("unknown: %d", data.DisplCode)
}
h.IsBurning = data.IO&8 != 0
h.IsLockout = data.IO&1 != 0
h.IsPumping = data.IO&2 != 0
h.IsTapping = data.IO&4 != 0
return nil
}
// Set temperature
func (h *Heater) Set(temp float32) error {
if temp < 5 {
temp = 5
}
if temp > 30 {
temp = 30
}
return h.gw.doGet(fmt.Sprintf(
"/data.json?heater=%d&thermostat=0&setpoint=%d",
h.Id, int((temp-5)*10)), nil)
}