-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
697 lines (638 loc) · 22.1 KB
/
client.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package zont
import (
"bytes"
"encoding/json"
"io"
"net/http"
"time"
"github.com/hashicorp/go-retryablehttp"
)
const (
baseUrl = "https://zont-online.ru/api/"
)
type Client struct {
httpClient *http.Client
httpTimeout time.Duration
debug bool
AuthTokenResponse *AuthTokenResponse
clientName string
xZontClient string
login string
password string
}
type AuthTokenResponse struct {
Token string `json:"token"`
IsGuest bool `json:"is_guest"`
Permissions []interface{} `json:"permissions"`
IsStaff bool `json:"is_staff"`
IsSuperuser bool `json:"is_superuser"`
AllowTerminal bool `json:"allow_terminal"`
ID int `json:"id"`
Username string `json:"username"`
Fullname string `json:"fullname"`
Phone interface{} `json:"phone"`
Email string `json:"email"`
EmailConfirmed bool `json:"email_confirmed"`
IsDemo bool `json:"is_demo"`
GuestPasswordSet bool `json:"guest_password_set"`
ShowNetworkEvents bool `json:"show_network_events"`
AutoscanSystem interface{} `json:"autoscan_system"`
PublicAPIAllowed bool `json:"public_api_allowed"`
ZthDashboard struct {
Show bool `json:"show"`
EnableModes bool `json:"enable_modes"`
EnableZones bool `json:"enable_zones"`
EnableTemperature bool `json:"enable_temperature"`
EnableAlert bool `json:"enable_alert"`
} `json:"zth_dashboard"`
Sims []struct {
SimCard struct {
ID struct {
Operator string `json:"operator"`
ID string `json:"id"`
} `json:"id"`
Iccid string `json:"iccid"`
Msisdn string `json:"msisdn"`
} `json:"sim_card"`
Active bool `json:"active"`
UserID int `json:"user_id"`
PaidUntil int `json:"paid_until"`
AutoProlong bool `json:"auto_prolong"`
Terminated bool `json:"terminated"`
FirstActivate interface{} `json:"first_activate"`
Limit string `json:"limit"`
Tariff string `json:"tariff"`
} `json:"sims"`
ForeignSims []interface{} `json:"foreign_sims"`
DateLastNewsReading int `json:"date_last_news_reading"`
WebuiBeta bool `json:"webui_beta"`
AllowNewUI bool `json:"allow_new_ui"`
UseNewUI bool `json:"use_new_ui"`
Ok bool `json:"ok"`
}
type DevicesResponse struct {
Devices []Device `json:"devices"`
DeviceTree []struct {
ZontID int `json:"ZontId"`
} `json:"device_tree"`
Ok bool `json:"ok"`
}
type Device struct {
Access []interface{} `json:"access,omitempty"`
Capabilities []string `json:"capabilities,omitempty"`
DeviceType struct {
Code string `json:"code"`
Name string `json:"name"`
} `json:"device_type,omitempty"`
WidgetType string `json:"widget_type,omitempty"`
HardwareType struct {
Code string `json:"code"`
Name string `json:"name"`
} `json:"hardware_type,omitempty"`
ID int `json:"id,omitempty"`
IP string `json:"ip,omitempty"`
IsActive bool `json:"is_active,omitempty"`
Online bool `json:"online,omitempty"`
OwnerUsername string `json:"owner_username,omitempty"`
UserID int `json:"user_id,omitempty"`
LastReceiveTime int `json:"last_receive_time,omitempty"`
LastReceiveTimeRelative int `json:"last_receive_time_relative,omitempty"`
Name string `json:"name,omitempty"`
Color string `json:"color,omitempty"`
Notes string `json:"notes,omitempty"`
Serial string `json:"serial,omitempty"`
VisibleDeviceType interface{} `json:"visible_device_type,omitempty"`
FirmwareVersion []int `json:"firmware_version,omitempty"`
IsConfiguredByWizard interface{} `json:"is_configured_by_wizard,omitempty"`
IsConfiguredByZontWizard interface{} `json:"is_configured_by_zont_wizard,omitempty"`
SuggestFirmwareUpgrade bool `json:"suggest_firmware_upgrade,omitempty"`
GraphsConfig struct {
Blocks []struct {
Sources []struct {
Class string `json:"class"`
Params struct {
ZoneNo int `json:"zone_no"`
} `json:"params,omitempty"`
Params0 struct {
ThermID string `json:"therm_id"`
} `json:"params,omitempty"`
} `json:"sources"`
Height interface{} `json:"height"`
HeightMobile string `json:"height_mobile"`
} `json:"blocks"`
} `json:"graphs_config,omitempty"`
CamsShow bool `json:"cams_show,omitempty"`
ShowHeatingTab bool `json:"show_heating_tab,omitempty"`
ServerNotifications struct {
Events struct {
Enabled bool `json:"enabled"`
} `json:"events"`
Offline struct {
Enabled bool `json:"enabled"`
Timeout int `json:"timeout"`
} `json:"offline"`
} `json:"server_notifications,omitempty"`
DebugTextMessagesRegexp interface{} `json:"debug_text_messages_regexp,omitempty"`
StationaryLocation struct {
Loc []float64 `json:"loc"`
} `json:"stationary_location,omitempty"`
Cams []interface{} `json:"cams,omitempty"`
SpecialistInfo interface{} `json:"specialist_info,omitempty"`
InstallationState interface{} `json:"installation_state,omitempty"`
Maintenances interface{} `json:"maintenances,omitempty"`
WorkState interface{} `json:"work_state,omitempty"`
IndividualDeviceName string `json:"individual_device_name,omitempty"`
IndividualDeviceNote string `json:"individual_device_note,omitempty"`
UISettings struct {
HeatTab struct {
} `json:"heat_tab"`
Icons struct {
} `json:"icons"`
} `json:"ui_settings,omitempty"`
Timezone int `json:"timezone,omitempty"`
LastGuardEvent interface{} `json:"last_guard_event,omitempty"`
ThermostatErrorInputPolarity string `json:"thermostat_error_input_polarity,omitempty"`
ThermostatInputconfig struct {
Num1 string `json:"1"`
Num2 string `json:"2"`
} `json:"thermostat_inputconfig,omitempty"`
ThermostatEnableGuard bool `json:"thermostat_enable_guard,omitempty"`
BoilerInfo struct {
Model string `json:"model"`
Vendor string `json:"vendor"`
} `json:"boiler_info,omitempty"`
ThermostatExtMode int `json:"thermostat_ext_mode,omitempty"`
ThermostatMode string `json:"thermostat_mode,omitempty"`
ThermostatGate bool `json:"thermostat_gate,omitempty"`
Tempschedule struct {
Day []float64 `json:"day"`
Week struct {
Num0 []float64 `json:"0"`
Num1 []float64 `json:"1"`
Num2 []float64 `json:"2"`
Num3 []float64 `json:"3"`
Num4 []float64 `json:"4"`
Num5 []float64 `json:"5"`
Num6 []float64 `json:"6"`
} `json:"week"`
} `json:"tempschedule,omitempty"`
Tempstep int `json:"tempstep,omitempty"`
Notifications struct {
Alarm struct {
Numbers string `json:"numbers"`
PowerOff string `json:"power-off"`
PowerOn string `json:"power-on"`
Blackout string `json:"blackout"`
Doors string `json:"doors"`
DriverCall string `json:"driver_call"`
Ignition string `json:"ignition"`
Moving string `json:"moving"`
Shock string `json:"shock"`
Tilt string `json:"tilt"`
TrunkHood string `json:"trunk-hood"`
} `json:"alarm"`
Guard struct {
Numbers string `json:"numbers"`
Off string `json:"off"`
On string `json:"on"`
} `json:"guard"`
Info struct {
Balance string `json:"balance"`
EcuError string `json:"ecu_error"`
Numbers string `json:"numbers"`
FobBattery string `json:"fob_battery"`
} `json:"info"`
Thermostat struct {
BoilerFail string `json:"boiler_fail"`
TempHigh string `json:"temp_high"`
TempLow string `json:"temp_low"`
ThermMalf string `json:"therm_malf"`
} `json:"thermostat"`
Autoignition struct {
Breakdown string `json:"breakdown"`
Fail string `json:"fail"`
Numbers string `json:"numbers"`
Success string `json:"success"`
} `json:"autoignition"`
} `json:"notifications,omitempty"`
ThermostatHysteresis float64 `json:"thermostat_hysteresis,omitempty"`
ThermostatTempsLimits struct {
Num0 struct {
Max interface{} `json:"max"`
Min interface{} `json:"min"`
} `json:"0"`
Num1 struct {
Max interface{} `json:"max"`
Min interface{} `json:"min"`
} `json:"1"`
} `json:"thermostat_temps_limits,omitempty"`
TemperatureAlarm struct {
High int `json:"high"`
Low int `json:"low"`
} `json:"temperature_alarm,omitempty"`
SimInDevice struct {
SimType string `json:"sim_type"`
SimID struct {
Operator string `json:"operator"`
ID string `json:"id"`
} `json:"sim_id"`
ForeignMsisdn interface{} `json:"foreign_msisdn"`
} `json:"sim_in_device,omitempty"`
Balance struct {
Limit int `json:"limit"`
Ussd string `json:"ussd"`
Warning bool `json:"warning"`
} `json:"balance,omitempty"`
TrustedPhones string `json:"trusted_phones,omitempty"`
GsmRoaming interface{} `json:"gsm_roaming,omitempty"`
Imei string `json:"imei,omitempty"`
Iccid interface{} `json:"iccid,omitempty"`
OtEnabled bool `json:"ot_enabled,omitempty"`
OtSaveParams []string `json:"ot_save_params,omitempty"`
OtMinSetpoint float64 `json:"ot_min_setpoint,omitempty"`
OtMaxSetpoint float64 `json:"ot_max_setpoint,omitempty"`
OtMaxMl float64 `json:"ot_max_ml,omitempty"`
OtDhwSetpoint float64 `json:"ot_dhw_setpoint,omitempty"`
OtMinWp float64 `json:"ot_min_wp,omitempty"`
OtConfig []string `json:"ot_config,omitempty"`
OtMode string `json:"ot_mode,omitempty"`
OtBoilerType string `json:"ot_boiler_type,omitempty"`
OtShowDhwControl bool `json:"ot_show_dhw_control,omitempty"`
RfStatus interface{} `json:"rf_status,omitempty"`
BoilerDelay struct {
Off int `json:"off"`
On int `json:"on"`
} `json:"boiler_delay,omitempty"`
Pza struct {
Enabled bool `json:"enabled"`
Curve int `json:"curve"`
} `json:"pza,omitempty"`
PzaMaxDelta struct {
Enabled bool `json:"enabled"`
} `json:"pza_max_delta,omitempty"`
ThermostatExtModesConfig struct {
Num0 struct {
Active bool `json:"active"`
Name string `json:"name"`
ScheduleNumber interface{} `json:"schedule_number"`
ZoneSensors struct {
Num1 interface{} `json:"1"`
} `json:"zone_sensors"`
ZoneTemp struct {
Num1 float64 `json:"1"`
} `json:"zone_temp"`
} `json:"0"`
Num1 struct {
Active bool `json:"active"`
Name string `json:"name"`
ScheduleNumber interface{} `json:"schedule_number"`
ZoneSensors struct {
Num1 interface{} `json:"1"`
} `json:"zone_sensors"`
ZoneTemp struct {
Num1 float64 `json:"1"`
} `json:"zone_temp"`
} `json:"1"`
Num2 struct {
Active bool `json:"active"`
Name string `json:"name"`
ScheduleNumber int `json:"schedule_number"`
ZoneSensors struct {
} `json:"zone_sensors"`
ZoneTemp struct {
} `json:"zone_temp"`
} `json:"2"`
Num3 struct {
Active bool `json:"active"`
Name string `json:"name"`
ScheduleNumber interface{} `json:"schedule_number"`
ZoneSensors struct {
Num1 interface{} `json:"1"`
} `json:"zone_sensors"`
ZoneTemp struct {
Num1 float64 `json:"1"`
} `json:"zone_temp"`
} `json:"3"`
Num4 struct {
Active bool `json:"active"`
Name string `json:"name"`
ZoneTemp struct {
Num1 float64 `json:"1"`
} `json:"zone_temp"`
ScheduleNumber interface{} `json:"schedule_number"`
ZoneSensors struct {
Num1 int `json:"1"`
} `json:"zone_sensors"`
} `json:"4"`
Num5 struct {
Active bool `json:"active"`
Name string `json:"name"`
} `json:"5"`
Num6 struct {
Active bool `json:"active"`
Name string `json:"name"`
} `json:"6"`
Num7 struct {
Active bool `json:"active"`
Name string `json:"name"`
} `json:"7"`
Num8 struct {
Active bool `json:"active"`
Name string `json:"name"`
} `json:"8"`
Num9 struct {
Active bool `json:"active"`
Name string `json:"name"`
} `json:"9"`
} `json:"thermostat_ext_modes_config,omitempty"`
ThermostatTargetTemps *map[string]ThermostatTargetTemps `json:"thermostat_target_temps,omitempty"`
ThermostatExtModesAdvanced bool `json:"thermostat_ext_modes_advanced,omitempty"`
ThermostatRelayMode string `json:"thermostat_relay_mode,omitempty"`
OtGateEnabled bool `json:"ot_gate_enabled,omitempty"`
UseInternetWeatherForPza bool `json:"use_internet_weather_for_pza,omitempty"`
Thermometers []struct {
IsAssignedToSlot bool `json:"is_assigned_to_slot"`
Slot int `json:"slot"`
UUID string `json:"uuid"`
Serial string `json:"serial"`
Type string `json:"type"`
Name string `json:"name"`
Color string `json:"color"`
Limits struct {
Low int `json:"low"`
High int `json:"high"`
} `json:"limits"`
Function string `json:"function"`
Functions []struct {
F string `json:"f"`
Zone int `json:"zone"`
} `json:"functions"`
Sort int `json:"sort"`
LastState string `json:"last_state"`
LastValue float64 `json:"last_value"`
LastValueTime int `json:"last_value_time"`
} `json:"thermometers,omitempty"`
Filetransfers []interface{} `json:"filetransfers,omitempty"`
InternetWeather float64 `json:"internet_weather,omitempty"`
AspBilling struct {
InService bool `json:"in_service"`
AllowedForUser bool `json:"allowed_for_user"`
} `json:"asp_billing,omitempty"`
}
type ThermostatTargetTemps struct {
Manual bool `json:"manual"`
Temp float64 `json:"temp"`
}
type LoadDataResponse struct {
Ok bool `json:"ok,omitempty"`
Responses []struct {
DeviceID int `json:"device_id,omitempty"`
Ok bool `json:"ok,omitempty"`
TimeTruncated bool `json:"time_truncated,omitempty"`
Temperature map[string]struct {
Name string `json:"name,omitempty"`
Color string `json:"color,omitempty"`
Sort int `json:"sort,omitempty"`
Temperature [][]float64 `json:"temperature,omitempty"`
} `json:"temperature,omitempty"`
Timings struct {
Temperature struct {
Wall float64 `json:"wall,omitempty"`
Proc float64 `json:"proc,omitempty"`
} `json:"temperature,omitempty"`
} `json:"timings,omitempty"`
} `json:"responses,omitempty"`
}
// https://zont-online.ru/api/docs/#thermostat_work
type LoadDataThermostatWorkResponse struct {
Ok bool `json:"ok"`
Responses []struct {
DeviceID int `json:"device_id"`
Ok bool `json:"ok"`
TimeTruncated bool `json:"time_truncated"`
ThermostatWork struct {
ThermostatMode [][]int `json:"thermostat_mode"`
DhwT [][]float64 `json:"dhw_t"`
Power [][]any `json:"power"`
Fail [][]any `json:"fail"`
Gate [][]any `json:"gate"`
Ot struct {
Cs [][]int `json:"cs"`
Bt [][]int `json:"bt"`
Dt [][]int `json:"dt"`
Rwt [][]int `json:"rwt"`
Rml [][]int `json:"rml"`
Wp [][]float64 `json:"wp"`
S [][]any `json:"s"`
} `json:"ot"`
Zones struct {
Num1 struct {
TargetTemp [][]int `json:"target_temp"`
Worktime [][]int `json:"worktime"`
} `json:"1"`
} `json:"zones"`
BoilerWorkTime [][]int `json:"boiler_work_time"`
TargetTemp [][]int `json:"target_temp"`
} `json:"thermostat_work"`
Timings struct {
ThermostatWork struct {
Wall float64 `json:"wall"`
Proc float64 `json:"proc"`
} `json:"thermostat_work"`
} `json:"timings"`
} `json:"responses"`
}
// NewClient return new client
func NewClient(clientName, xZontClient, login, password string) *Client {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
standardClient := retryClient.StandardClient() // *http.Client
httpTimeout := 20 * time.Second
return &Client{
standardClient,
httpTimeout,
false,
nil,
clientName,
xZontClient,
login,
password,
}
}
// PostRequestHandler handle all post requests
func (cl *Client) PostRequestHandler(data interface{}, uri string, basic bool) []byte {
jsonValue, err := json.Marshal(data)
if err != nil {
ContextLogger.Error(err)
}
b := bytes.NewBuffer(jsonValue)
req, err := http.NewRequest("POST", uri, b)
if err != nil {
ContextLogger.Error(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-ZONT-Client", cl.xZontClient)
if basic {
req.SetBasicAuth(cl.login, cl.password)
} else {
req.Header.Set("X-ZONT-Token", cl.AuthTokenResponse.Token)
}
res, err := cl.httpClient.Do(req)
if err != nil {
ContextLogger.Error(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
ContextLogger.Error(err)
}
defer res.Body.Close()
return body
}
// GetAuthToken return token base on login and password
func (cl *Client) GetAuthToken() {
method := "get_authtoken"
uri := baseUrl + method
values := map[string]string{"client_name": cl.clientName}
body := cl.PostRequestHandler(values, uri, true)
err := json.Unmarshal(body, &cl.AuthTokenResponse)
if err != nil {
ContextLogger.Error(err)
}
}
// GetDevices return device info in DevicesResponse format
func (cl *Client) GetDevices() *DevicesResponse {
if len(cl.AuthTokenResponse.Token) < 1 {
ContextLogger.Infoln("AuthToken not exist!")
return nil
}
method := "devices"
uri := baseUrl + method
values := map[string]string{"client_name": cl.clientName}
body := cl.PostRequestHandler(values, uri, false)
devices := DevicesResponse{}
err := json.Unmarshal(body, &devices)
if err != nil {
ContextLogger.Error(err)
}
return &devices
}
// UpdateDevice send update request based on data interface
func (cl *Client) UpdateDevice(data interface{}) error {
if len(cl.AuthTokenResponse.Token) < 1 {
ContextLogger.Infoln("AuthToken not exist!")
return nil
}
method := "update_device"
uri := baseUrl + method
body := cl.PostRequestHandler(data, uri, false)
devices := DevicesResponse{}
err := json.Unmarshal(body, &devices)
if err != nil {
ContextLogger.Error(err)
return err
}
return nil
}
// LoadData return device information and metrics based on data interface
func (cl *Client) LoadData(data interface{}) *LoadDataResponse {
if len(cl.AuthTokenResponse.Token) < 1 {
ContextLogger.Infoln("AuthToken not exist!")
return nil
}
method := "load_data"
uri := baseUrl + method
body := cl.PostRequestHandler(data, uri, false)
loadData := LoadDataResponse{}
err := json.Unmarshal(body, &loadData)
if err != nil {
ContextLogger.Error(err)
}
return &loadData
}
// LoadDataThermostatWork return device information and metrics based on data interface
func (cl *Client) LoadDataThermostatWork(data interface{}) *LoadDataThermostatWorkResponse {
if len(cl.AuthTokenResponse.Token) < 1 {
ContextLogger.Infoln("AuthToken not exist!")
return nil
}
method := "load_data"
uri := baseUrl + method
body := cl.PostRequestHandler(data, uri, false)
loadData := LoadDataThermostatWorkResponse{}
err := json.Unmarshal(body, &loadData)
if err != nil {
ContextLogger.Error(err)
}
return &loadData
}
// GetCurrentTemp return current temperature from first thermometer on device with deviceId
func (cl *Client) GetCurrentTemp(deviceId int) (temp float64) {
dataRequest := struct {
DeviceID int `json:"device_id"`
DataTypes []string `json:"data_types"`
MinTime int64 `json:"mintime"`
MaxTime int64 `json:"maxtime"`
}{}
dataRequest.DeviceID = deviceId
dataRequest.DataTypes = append(dataRequest.DataTypes, "temperature")
dataRequest.MaxTime = time.Now().Unix()
dataRequest.MinTime = time.Now().Add(-180 * time.Second).Unix()
dataLoad := struct {
Requests []struct {
DeviceID int `json:"device_id"`
DataTypes []string `json:"data_types"`
MinTime int64 `json:"mintime"`
MaxTime int64 `json:"maxtime"`
} `json:"requests"`
}{}
dataLoad.Requests = append(dataLoad.Requests, dataRequest)
loadResp := &LoadDataResponse{}
loadResp = cl.LoadData(dataLoad)
for k := range loadResp.Responses[0].Temperature {
return loadResp.Responses[0].Temperature[k].Temperature[0][1]
}
return temp
}
// GetCurrentHotWaterTemp return current hot water temperature
func (cl *Client) GetCurrentHotWaterTemp(deviceId int) (temp float64) {
dataRequest := struct {
DeviceID int `json:"device_id"`
DataTypes []string `json:"data_types"`
MinTime int64 `json:"mintime"`
MaxTime int64 `json:"maxtime"`
}{}
dataRequest.DeviceID = deviceId
dataRequest.DataTypes = append(dataRequest.DataTypes, "thermostat_work")
dataRequest.MaxTime = time.Now().Unix()
dataRequest.MinTime = time.Now().Add(-180 * time.Second).Unix()
dataLoad := struct {
Requests []struct {
DeviceID int `json:"device_id"`
DataTypes []string `json:"data_types"`
MinTime int64 `json:"mintime"`
MaxTime int64 `json:"maxtime"`
} `json:"requests"`
}{}
dataLoad.Requests = append(dataLoad.Requests, dataRequest)
loadResp := cl.LoadDataThermostatWork(dataLoad)
return loadResp.Responses[0].ThermostatWork.DhwT[0][1]
}
type ThermostatData struct {
DeviceID int `json:"device_id"`
ThermostatTargetTemps map[string]ThermostatTargetTemps `json:"thermostat_target_temps"`
}
// SetTargetTemp send temperature update request based on deviceId and targetTemp
func (cl *Client) SetTargetTemp(deviceId int, termostatid string, targetTemp float64) error {
// Update data
data := ThermostatData{}
data.DeviceID = deviceId
data.ThermostatTargetTemps = map[string]ThermostatTargetTemps{
termostatid: {Manual: true, Temp: targetTemp},
}
err := cl.UpdateDevice(data)
if err != nil {
ContextLogger.Error(err)
return err
}
return nil
}