Migate to go-swagger

This commit is contained in:
Florian Eitel 2019-08-11 20:47:16 +02:00
parent 1990634373
commit a9ac0e8827
Signed by: flo
GPG Key ID: 9987EAFEF6F686BB
5 changed files with 79 additions and 63 deletions

View File

@ -5,29 +5,29 @@ import "fmt"
import "io/ioutil" import "io/ioutil"
type Status struct { type Status struct {
ShutterStatus map[string]int32 ShutterStatus map[string]int64
AirQuality map[string]int32 AirQuality map[string]int64
Co2 map[string]int32 Co2 map[string]int64
Temperature map[string]int32 Temperature map[string]int64
Humidity map[string]int32 Humidity map[string]int64
Lux map[string]int32 Lux map[string]int64
BatteryPercent map[string]int32 BatteryPercent map[string]int64
RfStrength map[string]int32 RfStrength map[string]int64
} }
func DumpJSON(state *State, outfile string) { func DumpJSON(state *State, outfile string) {
var status = new(Status) var status = new(Status)
status.AirQuality = make(map[string]int32) status.AirQuality = make(map[string]int64)
status.Co2 = make(map[string]int32) status.Co2 = make(map[string]int64)
status.Temperature = make(map[string]int32) status.Temperature = make(map[string]int64)
status.Humidity = make(map[string]int32) status.Humidity = make(map[string]int64)
status.Lux = make(map[string]int32) status.Lux = make(map[string]int64)
status.ShutterStatus = make(map[string]int32) status.ShutterStatus = make(map[string]int64)
status.BatteryPercent = make(map[string]int32) status.BatteryPercent = make(map[string]int64)
status.RfStrength = make(map[string]int32) status.RfStrength = make(map[string]int64)
for _, r := range state.RoomStatus { for _, r := range state.RoomStatus {
roomName := state.NameForRoom[r.Id] roomName := state.NameForRoom[r.ID]
if r.Temperature != 0 { if r.Temperature != 0 {
status.AirQuality[roomName] = r.AirQuality status.AirQuality[roomName] = r.AirQuality
status.Co2[roomName] = r.Co2 status.Co2[roomName] = r.Co2
@ -38,10 +38,10 @@ func DumpJSON(state *State, outfile string) {
} }
for _, m := range state.ModuleStatus { for _, m := range state.ModuleStatus {
moduleName := state.NameForModule[m.Id] moduleName := state.NameForModule[m.ID]
if m.Type_ == "NXO" { if m.Type == "NXO" {
status.ShutterStatus[moduleName] = m.TargetPosition status.ShutterStatus[moduleName] = m.TargetPosition
} else if m.Type_ == "NXG" { } else if m.Type == "NXG" {
// bridge -- noop // bridge -- noop
} else { } else {
status.BatteryPercent[moduleName] = m.BatteryPercent status.BatteryPercent[moduleName] = m.BatteryPercent

View File

@ -1,72 +1,85 @@
package main package main
import "context" import "velux-cli/models"
import sw "./go-client" import "github.com/go-openapi/runtime"
import "velux-cli/client"
import "velux-cli/client/operations"
import "github.com/go-openapi/swag"
import apiclient "velux-cli/client"
import httptransport "github.com/go-openapi/runtime/client"
type State struct { type State struct {
HomeId string HomeId string
BridgeId string BridgeId string
Api *sw.DefaultApiService Api *client.VeluxActiveWithNetatmo
Auth context.Context Auth runtime.ClientAuthInfoWriter
NameForRoom map[string]string NameForRoom map[string]string
RoomForName map[string]string RoomForName map[string]string
RoomForModule map[string]string RoomForModule map[string]string
ModulesForRoom map[string][]string ModulesForRoom map[string][]string
NameForModule map[string]string NameForModule map[string]string
ModuleForName map[string]string ModuleForName map[string]string
ModuleStatus map[string]sw.ModuleStatus ModuleStatus map[string]*models.ModuleStatus
RoomStatus map[string]sw.RoomStatus RoomStatus map[string]*models.RoomStatus
} }
func fetchData(tokenFile string) *State { func fetchData(tokenFile string) *State {
token := refreshToken(tokenFile) token := refreshToken(tokenFile)
cfg := apiclient.DefaultTransportConfig()
t := httptransport.New(cfg.Host, cfg.BasePath, cfg.Schemes)
t.SetDebug(true)
client := apiclient.New(t, nil)
var state = &State{ var state = &State{
Api: sw.NewAPIClient(sw.NewConfiguration()).DefaultApi, Api: client,
BridgeId: BridgeId, BridgeId: BridgeId,
Auth: context.WithValue(context.Background(), sw.ContextAccessToken, token.AccessToken), Auth: httptransport.BearerToken(token.AccessToken),
NameForRoom: make(map[string]string), NameForRoom: make(map[string]string),
RoomForName: make(map[string]string), RoomForName: make(map[string]string),
RoomForModule: make(map[string]string), RoomForModule: make(map[string]string),
ModulesForRoom: make(map[string][]string), ModulesForRoom: make(map[string][]string),
NameForModule: make(map[string]string), NameForModule: make(map[string]string),
ModuleForName: make(map[string]string), ModuleForName: make(map[string]string),
ModuleStatus: make(map[string]sw.ModuleStatus), ModuleStatus: make(map[string]*models.ModuleStatus),
RoomStatus: make(map[string]sw.RoomStatus), RoomStatus: make(map[string]*models.RoomStatus),
} }
r, _, err := state.Api.HomesData(state.Auth) r, err := state.Api.Operations.HomesData(operations.NewHomesDataParams(), state.Auth)
if err != nil { if err != nil {
panic(err) panic(err)
} }
state.HomeId = r.Body.Homes[0].Id state.HomeId = r.Payload.Body.Homes[0].ID
for _, r := range r.Body.Homes[0].Rooms { for _, r := range r.Payload.Body.Homes[0].Rooms {
state.NameForRoom[r.Id] = r.Name state.NameForRoom[r.ID] = r.Name
state.NameForRoom[r.Name] = r.Id state.NameForRoom[r.Name] = r.ID
for _, m := range r.Modules { for _, m := range r.Modules {
state.RoomForModule[m] = r.Id state.RoomForModule[m] = r.ID
} }
state.ModulesForRoom[r.Id] = r.Modules state.ModulesForRoom[r.ID] = r.Modules
} }
for _, m := range r.Body.Homes[0].Modules { for _, m := range r.Payload.Body.Homes[0].Modules {
state.NameForModule[m.Id] = m.Name state.NameForModule[m.ID] = m.Name
state.ModuleForName[m.Name] = m.Id state.ModuleForName[m.Name] = m.ID
} }
r2, _, err := state.Api.HomeStatus(state.Auth, sw.Body{HomeId: state.HomeId}) param := operations.NewHomeStatusParams()
param.WithBody(operations.HomeStatusBody{HomeID: swag.String(state.HomeId)})
r2, err := state.Api.Operations.HomeStatus(param, state.Auth)
if err != nil { if err != nil {
panic(err) panic(err)
} }
for _, m := range r2.Body.Home.Modules { for _, m := range r2.Payload.Body.Home.Modules {
state.ModuleStatus[m.Id] = m state.ModuleStatus[m.ID] = m
} }
for _, r := range r2.Body.Home.Rooms { for _, r := range r2.Payload.Body.Home.Rooms {
state.RoomStatus[r.Id] = r state.RoomStatus[r.ID] = r
} }
return state return state

View File

@ -49,7 +49,7 @@ func main() {
cmd.Parse(os.Args[2:]) cmd.Parse(os.Args[2:])
state := fetchData(*tokenpath) state := fetchData(*tokenpath)
Move(state, shutters, int32(*position)) Move(state, shutters, int64(*position))
default: default:
fmt.Printf("%q is not valid command.\n", os.Args[1]) fmt.Printf("%q is not valid command.\n", os.Args[1])
os.Exit(2) os.Exit(2)

29
move.go
View File

@ -2,41 +2,44 @@ package main
import "fmt" import "fmt"
import "encoding/json" import "encoding/json"
import sw "./go-client" import "velux-cli/models"
import "velux-cli/client/operations"
import "github.com/go-openapi/swag"
func Move(state *State, shutters []string, position int32) { func Move(state *State, shutters []string, position int64) {
fmt.Printf("Moving shutters: %+v to %+v\n", shutters, position) fmt.Printf("Moving shutters: %+v to %+v\n", shutters, position)
if len(shutters) == 0 { if len(shutters) == 0 {
return return
} }
var updates []sw.ModulePercentage var updates []*models.ModulePercentage
for _, x := range shutters { for _, x := range shutters {
m := sw.ModulePercentage{ m := &models.ModulePercentage{
Bridge: state.BridgeId, Bridge: state.BridgeId,
Id: state.ModuleForName[x], ID: state.ModuleForName[x],
TargetPosition: position, TargetPosition: position,
} }
updates = append(updates, m) updates = append(updates, m)
} }
param := sw.SetState{ param := operations.NewSetStateParams()
Home: &sw.SetStateHome{ param.WithBody(&models.SetState{
Id: state.HomeId, Home: &models.SetStateHome{
ID: swag.String(state.HomeId),
Modules: updates, Modules: updates,
}, },
} })
fmt.Printf("> request: %+v\n", param) fmt.Printf("> request: %+v\n", param.Body)
fmt.Printf("> request: %+v\n", param.Home) fmt.Printf("> request: %+v\n", param.Body.Home)
fmt.Printf("> request: %+v\n", param.Home.Modules) fmt.Printf("> request: %+v\n", param.Body.Home.Modules)
j, err := json.Marshal(param) j, err := json.Marshal(param)
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Printf("> request: %+v\n", string(j)) fmt.Printf("> request: %+v\n", string(j))
response, _, err := state.Api.SetState(state.Auth, param) response, err := state.Api.Operations.SetState(param, state.Auth)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -7,13 +7,13 @@ func PrintStatus(state *State) {
if r.Temperature != 0 { if r.Temperature != 0 {
fmt.Printf( fmt.Printf(
"%s (air quality: %d / CO2: %d / Temperature: %d / Humidity: %d / Lux: %d)\n", "%s (air quality: %d / CO2: %d / Temperature: %d / Humidity: %d / Lux: %d)\n",
state.NameForRoom[r.Id], r.AirQuality, r.Co2, r.Temperature/10.0, r.Humidity, r.Lux) state.NameForRoom[r.ID], r.AirQuality, r.Co2, r.Temperature/10.0, r.Humidity, r.Lux)
} else { } else {
fmt.Printf("%s\n", state.NameForRoom[r.Id]) fmt.Printf("%s\n", state.NameForRoom[r.ID])
} }
for _, m := range state.ModulesForRoom[r.Id] { for _, m := range state.ModulesForRoom[r.ID] {
if state.ModuleStatus[m].Type_ == "NXO" { if state.ModuleStatus[m].Type == "NXO" {
fmt.Printf(" - %d %s\n", state.ModuleStatus[m].CurrentPosition, state.NameForModule[m]) fmt.Printf(" - %d %s\n", state.ModuleStatus[m].CurrentPosition, state.NameForModule[m])
} else { } else {
fmt.Printf(" - %s: battery: %d%% rf strength: %d\n", state.NameForModule[m], state.ModuleStatus[m].BatteryPercent, state.ModuleStatus[m].RfStrength) fmt.Printf(" - %s: battery: %d%% rf strength: %d\n", state.NameForModule[m], state.ModuleStatus[m].BatteryPercent, state.ModuleStatus[m].RfStrength)