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

View File

@ -1,72 +1,85 @@
package main
import "context"
import sw "./go-client"
import "velux-cli/models"
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 {
HomeId string
BridgeId string
Api *sw.DefaultApiService
Auth context.Context
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]sw.ModuleStatus
RoomStatus map[string]sw.RoomStatus
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(true)
client := apiclient.New(t, nil)
var state = &State{
Api: sw.NewAPIClient(sw.NewConfiguration()).DefaultApi,
Api: client,
BridgeId: BridgeId,
Auth: context.WithValue(context.Background(), sw.ContextAccessToken, token.AccessToken),
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]sw.ModuleStatus),
RoomStatus: make(map[string]sw.RoomStatus),
ModuleStatus: make(map[string]*models.ModuleStatus),
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 {
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 {
state.NameForRoom[r.Id] = r.Name
state.NameForRoom[r.Name] = r.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.RoomForModule[m] = r.ID
}
state.ModulesForRoom[r.Id] = r.Modules
state.ModulesForRoom[r.ID] = r.Modules
}
for _, m := range r.Body.Homes[0].Modules {
state.NameForModule[m.Id] = m.Name
state.ModuleForName[m.Name] = m.Id
for _, m := range r.Payload.Body.Homes[0].Modules {
state.NameForModule[m.ID] = m.Name
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 {
panic(err)
}
for _, m := range r2.Body.Home.Modules {
state.ModuleStatus[m.Id] = m
for _, m := range r2.Payload.Body.Home.Modules {
state.ModuleStatus[m.ID] = m
}
for _, r := range r2.Body.Home.Rooms {
state.RoomStatus[r.Id] = r
for _, r := range r2.Payload.Body.Home.Rooms {
state.RoomStatus[r.ID] = r
}
return state

View File

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

29
move.go
View File

@ -2,41 +2,44 @@ package main
import "fmt"
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)
if len(shutters) == 0 {
return
}
var updates []sw.ModulePercentage
var updates []*models.ModulePercentage
for _, x := range shutters {
m := sw.ModulePercentage{
m := &models.ModulePercentage{
Bridge: state.BridgeId,
Id: state.ModuleForName[x],
ID: state.ModuleForName[x],
TargetPosition: position,
}
updates = append(updates, m)
}
param := sw.SetState{
Home: &sw.SetStateHome{
Id: state.HomeId,
param := operations.NewSetStateParams()
param.WithBody(&models.SetState{
Home: &models.SetStateHome{
ID: swag.String(state.HomeId),
Modules: updates,
},
}
})
fmt.Printf("> request: %+v\n", param)
fmt.Printf("> request: %+v\n", param.Home)
fmt.Printf("> request: %+v\n", param.Home.Modules)
fmt.Printf("> request: %+v\n", param.Body)
fmt.Printf("> request: %+v\n", param.Body.Home)
fmt.Printf("> request: %+v\n", param.Body.Home.Modules)
j, err := json.Marshal(param)
if err != nil {
panic(err)
}
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 {
panic(err)
}

View File

@ -7,13 +7,13 @@ func PrintStatus(state *State) {
if r.Temperature != 0 {
fmt.Printf(
"%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 {
fmt.Printf("%s\n", state.NameForRoom[r.Id])
fmt.Printf("%s\n", state.NameForRoom[r.ID])
}
for _, m := range state.ModulesForRoom[r.Id] {
if state.ModuleStatus[m].Type_ == "NXO" {
for _, m := range state.ModulesForRoom[r.ID] {
if state.ModuleStatus[m].Type == "NXO" {
fmt.Printf(" - %d %s\n", state.ModuleStatus[m].CurrentPosition, state.NameForModule[m])
} else {
fmt.Printf(" - %s: battery: %d%% rf strength: %d\n", state.NameForModule[m], state.ModuleStatus[m].BatteryPercent, state.ModuleStatus[m].RfStrength)