-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfetchdata.go
85 lines (71 loc) · 2.4 KB
/
fetchdata.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
package main
import "github.com/go-openapi/runtime"
import "github.com/go-openapi/swag"
import "github.com/nougad/velux-cli/client"
import "github.com/nougad/velux-cli/client/operations"
import "github.com/nougad/velux-cli/models"
import apiclient "github.com/nougad/velux-cli/client"
import httptransport "github.com/go-openapi/runtime/client"
type State struct {
HomeId string
BridgeId string
Api *client.VeluxActiveWithNetatmo
Auth runtime.ClientAuthInfoWriter
NameForRoom map[string]string
RoomForName map[string]string
RoomForModule map[string]string
ModulesForRoom map[string][]string
NameForModule map[string]string
ModuleForName map[string]string
ModuleStatus map[string]*models.ModuleStatus
RoomStatus map[string]*models.RoomStatus
}
func fetchData(tokenFile string) *State {
token := refreshToken(tokenFile)
cfg := apiclient.DefaultTransportConfig()
t := httptransport.New(cfg.Host, cfg.BasePath, cfg.Schemes)
t.SetDebug(Debug)
client := apiclient.New(t, nil)
var state = &State{
Api: client,
Auth: httptransport.BearerToken(token.AccessToken),
NameForRoom: make(map[string]string),
RoomForName: make(map[string]string),
RoomForModule: make(map[string]string),
ModulesForRoom: make(map[string][]string),
NameForModule: make(map[string]string),
ModuleForName: make(map[string]string),
ModuleStatus: make(map[string]*models.ModuleStatus),
RoomStatus: make(map[string]*models.RoomStatus),
}
r, err := state.Api.Operations.HomesData(operations.NewHomesDataParams(), state.Auth)
if err != nil {
panic(err)
}
state.HomeId = r.Payload.Body.Homes[0].ID
for _, r := range r.Payload.Body.Homes[0].Rooms {
state.NameForRoom[r.ID] = r.Name
state.NameForRoom[r.Name] = r.ID
for _, m := range r.Modules {
state.RoomForModule[m] = r.ID
}
state.ModulesForRoom[r.ID] = r.Modules
}
for _, m := range r.Payload.Body.Homes[0].Modules {
state.NameForModule[m.ID] = m.Name
state.ModuleForName[m.Name] = m.ID
}
param := operations.NewHomeStatusParams()
param.WithBody(operations.HomeStatusBody{HomeID: swag.String(state.HomeId)})
r2, err := state.Api.Operations.HomeStatus(param, state.Auth)
if err != nil {
panic(err)
}
for _, m := range r2.Payload.Body.Home.Modules {
state.ModuleStatus[m.ID] = m
}
for _, r := range r2.Payload.Body.Home.Rooms {
state.RoomStatus[r.ID] = r
}
return state
}