-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
executable file
·358 lines (329 loc) · 10.5 KB
/
api.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httputil"
URL "net/url"
"strings"
)
var (
apiToken string
)
func commonHandler(handler func(path string, body any, params URL.Values, method string) any) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
if Config.Api_debug {
req.URL.Scheme = "http"
req.URL.Host = "localhost"
bytes, _ := httputil.DumpRequestOut(req, true)
log.Printf("API request:\n%s", bytes)
}
if req.URL.Path != "/accounts/login" && req.Header.Get("Ar.authtoken") != apiToken {
http.Error(w, "Invalid token", http.StatusBadRequest)
return
}
var p any
if req.Method != "GET" {
err := json.NewDecoder(req.Body).Decode(&p)
if err != nil {
log.Printf("Error while decoding body: %v", err)
}
}
w.Header().Set("Content-Type", "application/json")
result := handler(req.URL.Path, p, req.URL.Query(), req.Method)
json.NewEncoder(w).Encode(result)
if Config.Api_debug {
log.Printf("API result: %+v", result)
}
}
}
func velisPlants(path string, body any, params URL.Values, method string) any {
re := []any{}
for clId, c := range clientMap {
a := map[string]any{}
a["gw"] = clId
a["sn"] = c.birth["serial_number"]
a["fwVer"] = c.birth["firmware_version"]
for _, device := range Config.Devices {
if device.GwID == clId {
a["sys"] = device.Sys
a["wheType"] = device.WheType
a["wheModelType"] = device.WheModelType
a["name"] = device.Name
}
}
re = append(re, a)
}
return re
}
func remotePlants(path string, body any, params URL.Values, method string) any {
return []any{}
}
func defaultHandler(path string, body any, params URL.Values, method string) any {
log.Printf("no route for path %s", path)
return map[string]any{}
}
func login(path string, body any, params URL.Values, method string) any {
bmap, ok := body.(map[string]any)
if !ok {
return nil
}
if Config.Api_username != "" {
err := map[string]string{"error": "invalid username/password"}
usr := bmap["usr"].(string)
if usr != Config.Api_username {
return err
}
pwd := bmap["pwd"].(string)
if pwd != Config.Api_password {
return err
}
}
return map[string]any{"token": apiToken}
}
func velisPlantDataSet(clID, cat string, value int32) any {
b, err := putParams(cat, value)
if err == nil {
err = server.Publish("$EDC/ari/"+clID+"/ar1/PUT/Menu/Par", b, false, 0)
}
if err != nil {
log.Printf("error for %v %v %v: %v", clID, cat, value, err)
return nil
}
// remembering the new setting so it is returned correctly even if the API is called before the next polling happens
if c, ok := clientMap[clID]; ok && c.params != nil {
c.params[cat] = value
}
return map[string]bool{"success": true}
}
func busErrors(path string, body any, params URL.Values, method string) any {
if c, ok := clientMap[params.Get("gatewayId")]; ok && c.errors != nil {
return map[string]any{} // TODO: return errors
}
return map[string]any{}
}
func features(path string, body any, params URL.Values, method string) any {
v := strings.Split(path, "/")
clID := v[3]
if c, ok := clientMap[clID]; ok && c.errors != nil {
return map[string]any{"hasMetering": true}
}
return map[string]any{}
}
func consumption(path string, body any, params URL.Values, method string) any {
v := strings.Split(path, "/")
clID := v[3]
offset := 0
for _, device := range Config.Devices {
if device.GwID == clID {
offset = device.ConsumptionOffset
}
}
if c, ok := clientMap[clID]; ok && c.cWh != nil {
ret := []map[string]any{}
for _, consumptions := range c.cWh.Consumptions.Consumptions {
kwhs := make([]float32, len(consumptions.Wh))
for i, wh := range consumptions.Wh {
kwhs[i] = float32(wh) / 1000
}
re := map[string]any{}
re["k"] = consumptions.ConsumptionType + int32(offset)
re["p"] = consumptions.ConsumptionTimeInterval
re["v"] = kwhs
ret = append(ret, re)
}
return ret
}
return map[string]any{}
}
func medPlantData(path string, body any, params URL.Values, method string) any {
v := strings.Split(path, "/")
clID := v[3]
switch {
case len(v) == 4:
re := map[string]any{}
if c, ok := clientMap[clID]; ok && c.params != nil {
re["on"] = c.params["T_18.0.0"]
re["mode"] = c.params["T_18.0.1"]
re["eco"] = c.params["T_18.0.2"]
re["pwrOpt"] = c.params["T_18.0.3"]
re["reqTemp"] = c.params["T_18.1.0"] / 10
re["antiLeg"] = c.params["T_18.3.0"]
re["avShw"] = c.params["T_18.3.1"]
var hours int32 = 0
var minutes int32 = c.params["T_18.3.2"]
if minutes > 60 {
hours = minutes / 60
minutes -= hours * 60
}
re["rmTm"] = fmt.Sprintf("%d:%d:0", hours, minutes)
re["temp"] = float32(c.params["T_18.3.3"]) / 10
re["heatReq"] = c.params["T_18.3.5"]
re["procReqTemp"] = c.params["T_18.3.6"] / 10
re["gw"] = clID
}
return re
case len(v) == 5 && v[4] == "temperature":
bodyMap := body.(map[string]any)
newTemp := int32(bodyMap["new"].(float64)) * 10
return velisPlantDataSet(clID, "T_18.1.0", newTemp)
case len(v) == 5 && v[4] == "mode":
bodyMap := body.(map[string]any)
newMode := int32(bodyMap["new"].(float64))
return velisPlantDataSet(clID, "T_18.0.1", newMode)
case len(v) == 5 && v[4] == "switch":
newMode := 0
if body.(bool) {
newMode = 1
}
return velisPlantDataSet(clID, "T_18.0.0", int32(newMode))
case len(v) == 5 && v[4] == "switchEco":
newMode := 0
if body.(bool) {
newMode = 1
}
return velisPlantDataSet(clID, "T_18.0.2", int32(newMode))
case len(v) == 5 && v[4] == "plantSettings":
if method == "POST" {
return postMedPlantSettings(body, clID)
}
re := map[string]any{}
if c, ok := clientMap[clID]; ok && c.params != nil {
re["MedAntilegionellaOnOff"] = c.params["T_18.0.5"]
re["MedMaxSetpointTemperature"] = c.params["T_18.1.3"] / 10
re["MedMaxSetpointTemperatureMin"] = c.paramsLimits["T_18.1.3"].Min / 10
re["MedMaxSetpointTemperatureMax"] = c.paramsLimits["T_18.1.3"].Max / 10
}
return re
default:
log.Printf("no route for path %s", path)
return nil
}
}
func postMedPlantSettings(body any, clID string) any {
bodyMap := body.(map[string]any)
for key, value := range bodyMap {
if key == "MedMaxSetpointTemperature" {
valueMap := value.(map[string]any)
newTemp := valueMap["new"].(int) * 10
return velisPlantDataSet(clID, "T_18.1.3", int32(newTemp))
}
if key == "MedAntilegionellaOnOff" {
valueMap := value.(map[string]any)
newMode := int(valueMap["new"].(float64))
return velisPlantDataSet(clID, "T_18.0.5", int32(newMode))
}
}
return nil
}
func postSePlantSettings(body any, clID string) any {
bodyMap := body.(map[string]any)
for key, value := range bodyMap {
if key == "SeMaxSetpointTemperature" {
valueMap := value.(map[string]any)
newTemp := valueMap["new"].(int) * 10
return velisPlantDataSet(clID, "T_22.1.2", int32(newTemp))
}
if key == "SeAntiCoolingTemperature" {
valueMap := value.(map[string]any)
newTemp := valueMap["new"].(int) * 10
return velisPlantDataSet(clID, "T_22.1.4", int32(newTemp))
}
if key == "SeAntilegionellaOnOff" {
valueMap := value.(map[string]any)
newMode := int32(valueMap["new"].(float64))
return velisPlantDataSet(clID, "T_22.0.1", newMode)
}
if key == "SePermanentBoostOnOff" {
valueMap := value.(map[string]any)
newMode := int32(valueMap["new"].(float64))
return velisPlantDataSet(clID, "T_22.0.2", newMode)
}
if key == "SeNightModeOnOff" {
valueMap := value.(map[string]any)
newMode := int32(valueMap["new"].(float64))
return velisPlantDataSet(clID, "T_22.0.4", newMode)
}
if key == "SeAntiCoolingOnOff" {
valueMap := value.(map[string]any)
newMode := int32(valueMap["new"].(float64))
return velisPlantDataSet(clID, "T_22.0.5", newMode)
}
}
return nil
}
func sePlantData(path string, body any, params URL.Values, method string) any {
v := strings.Split(path, "/")
clID := v[3]
switch {
case len(v) == 4:
re := map[string]any{}
if c, ok := clientMap[clID]; ok && c.params != nil {
re["on"] = c.params["T_22.0.0"]
re["mode"] = c.params["T_22.0.3"]
re["boostReqTemp"] = c.params["T_22.1.0"] / 10
re["reqTemp"] = c.params["T_22.1.3"] / 10
re["heatReq"] = c.params["T_22.3.0"]
re["procReqTemp"] = c.params["T_22.3.1"] / 10
re["antiLeg"] = c.params["T_22.3.4"]
re["temp"] = float32(c.params["T_22.3.6"]) / 10
re["avShw"] = c.params["T_22.3.9"]
re["gw"] = clID
}
return re
case len(v) == 5 && v[4] == "temperature":
bodyMap := body.(map[string]any)
newTemp := int32(bodyMap["new"].(float64)) * 10
return velisPlantDataSet(clID, "T_22.1.3", newTemp)
case len(v) == 5 && v[4] == "mode":
bodyMap := body.(map[string]any)
newMode := int32(bodyMap["new"].(float64))
return velisPlantDataSet(clID, "T_22.0.3", newMode)
case len(v) == 5 && v[4] == "switch":
newMode := 0
if body.(bool) {
newMode = 1
}
return velisPlantDataSet(clID, "T_22.0.0", int32(newMode))
case len(v) == 5 && v[4] == "plantSettings":
if method == "POST" {
return postSePlantSettings(body, clID)
}
re := map[string]any{}
if c, ok := clientMap[clID]; ok && c.params != nil {
re["SeAntilegionellaOnOff"] = c.params["T_22.0.1"]
re["SePermanentBoostOnOff"] = c.params["T_22.0.2"]
re["SeNightModeOnOff"] = c.params["T_22.0.4"]
re["SeAntiCoolingOnOff"] = c.params["T_22.0.5"]
re["SeMaxSetpointTemperature"] = c.params["T_22.1.2"] / 10
re["SeMaxSetpointTemperatureMin"] = c.paramsLimits["T_22.1.2"].Min / 10
re["SeMaxSetpointTemperatureMax"] = c.paramsLimits["T_22.1.2"].Max / 10
re["SeAntiCoolingTemperature"] = c.params["T_22.1.4"] / 10
re["SeAntiCoolingTemperatureMin"] = c.paramsLimits["T_22.1.4"].Min / 10
re["SeAntiCoolingTemperatureMax"] = c.paramsLimits["T_22.1.4"].Max / 10
}
return re
default:
log.Printf("no route for path %s", path)
return nil
}
}
func apiLogic() {
apiToken = fmt.Sprintf("%v:%v", Config.Api_username, Config.Api_password)
http.HandleFunc("/accounts/login", commonHandler(login))
http.HandleFunc("/remote/plants", commonHandler(remotePlants))
http.HandleFunc("/velis/plants", commonHandler(velisPlants))
http.HandleFunc("/velis/medPlantData/", commonHandler(medPlantData))
http.HandleFunc("/velis/sePlantData/", commonHandler(sePlantData))
http.HandleFunc("/busErrors", commonHandler(busErrors))
http.HandleFunc("/remote/plants/", commonHandler(features))
http.HandleFunc("/remote/reports/", commonHandler(consumption))
http.HandleFunc("/", commonHandler(defaultHandler))
go func() {
log.Printf("API server listening on %v", Config.Api_listener)
if err := http.ListenAndServe(Config.Api_listener, nil); err != nil {
log.Fatal(err)
}
}()
}