velux-cli/fetchdata.go

86 lines
2.4 KiB
Go
Raw Permalink Normal View History

package main
2019-08-11 20:47:16 +02:00
import "github.com/go-openapi/runtime"
2019-08-11 22:14:06 +02:00
import "github.com/go-openapi/swag"
2019-08-11 21:13:44 +02:00
import "github.com/nougad/velux-cli/client"
import "github.com/nougad/velux-cli/client/operations"
2019-08-11 22:14:06 +02:00
import "github.com/nougad/velux-cli/models"
2019-08-11 21:13:44 +02:00
import apiclient "github.com/nougad/velux-cli/client"
2019-08-11 20:47:16 +02:00
import httptransport "github.com/go-openapi/runtime/client"
type State struct {
HomeId string
BridgeId string
2019-08-11 20:47:16 +02:00
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
2019-08-11 20:47:16 +02:00
ModuleStatus map[string]*models.ModuleStatus
RoomStatus map[string]*models.RoomStatus
}
func fetchData(tokenFile string) *State {
token := refreshToken(tokenFile)
2019-08-11 20:47:16 +02:00
cfg := apiclient.DefaultTransportConfig()
t := httptransport.New(cfg.Host, cfg.BasePath, cfg.Schemes)
2019-08-11 22:13:49 +02:00
t.SetDebug(Debug)
2019-08-11 20:47:16 +02:00
client := apiclient.New(t, nil)
var state = &State{
2019-08-11 20:47:16 +02:00
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),
2019-08-11 20:47:16 +02:00
ModuleStatus: make(map[string]*models.ModuleStatus),
RoomStatus: make(map[string]*models.RoomStatus),
}
2019-08-11 20:47:16 +02:00
r, err := state.Api.Operations.HomesData(operations.NewHomesDataParams(), state.Auth)
if err != nil {
panic(err)
}
2019-08-11 20:47:16 +02:00
state.HomeId = r.Payload.Body.Homes[0].ID
2019-08-11 20:47:16 +02:00
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 {
2019-08-11 20:47:16 +02:00
state.RoomForModule[m] = r.ID
}
2019-08-11 20:47:16 +02:00
state.ModulesForRoom[r.ID] = r.Modules
}
2019-08-11 20:47:16 +02:00
for _, m := range r.Payload.Body.Homes[0].Modules {
state.NameForModule[m.ID] = m.Name
state.ModuleForName[m.Name] = m.ID
}
2019-08-11 20:47:16 +02:00
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)
}
2019-08-11 20:47:16 +02:00
for _, m := range r2.Payload.Body.Home.Modules {
state.ModuleStatus[m.ID] = m
}
2019-08-11 20:47:16 +02:00
for _, r := range r2.Payload.Body.Home.Rooms {
state.RoomStatus[r.ID] = r
}
return state
}