Skip to content

Commit

Permalink
debug refactor
Browse files Browse the repository at this point in the history
Signed-off-by: ygelfand <yuri@shlitz.com>
  • Loading branch information
ygelfand committed Feb 23, 2024
1 parent 1d59726 commit 15b4b42
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 60 deletions.
13 changes: 6 additions & 7 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License
The MIT License (MIT)

Copyright (c) 2024 Yuri Gelfand
Copyright © 2024 Yuri Gelfand

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,14 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
30 changes: 6 additions & 24 deletions cmd/debug.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"log"

"github.com/spf13/cobra"
"github.com/ygelfand/go-powerwall/internal/powerwall"
"github.com/ygelfand/go-powerwall/cmd/debug"
_ "github.com/ygelfand/go-powerwall/cmd/debug"
"github.com/ygelfand/go-powerwall/cmd/options"
)

func newDebugCmd(opts *powerwallOptions) *cobra.Command {
func newDebugCmd(opts *options.PowerwallOptions) *cobra.Command {
debugCmd := &cobra.Command{
Use: "debug",
Short: "run debug",
Long: `run some statuses for debug`,
Run: func(cmd *cobra.Command, args []string) {
pwr := powerwall.NewPowerwallGateway(opts.endpoint, opts.password)
debug := pwr.RunQuery("DeviceControllerQuery", nil)
var prettyJSON bytes.Buffer
//debug = pwr.RunQuery("ComponentsQuery", nil)
err := json.Indent(&prettyJSON, []byte(*debug), "", "\t")
if err != nil {
fmt.Println("JSON parse error: ", err)
}
log.Println(string(prettyJSON.Bytes()))
debug = pwr.GetConfig()
err = json.Indent(&prettyJSON, []byte(*debug), "", "\t")
if err != nil {
fmt.Println("JSON parse error: ", err)
}
log.Println(string(prettyJSON.Bytes()))
},
}
debugCmd.AddCommand(debug.NewDebugQueryCmd(opts))
debugCmd.AddCommand(debug.NewDebugConfigCmd(opts))
return debugCmd
}
30 changes: 12 additions & 18 deletions cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,32 @@ import (

"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"github.com/ygelfand/go-powerwall/cmd/options"
"github.com/ygelfand/go-powerwall/internal/api"
"github.com/ygelfand/go-powerwall/internal/powerwall"
)

type proxyOptions struct {
*powerwallOptions
refreshInterval uint32
onDemand bool
listenOn string
}

func newProxyCmd(opts *powerwallOptions) *cobra.Command {
o := &proxyOptions{powerwallOptions: opts}
func newProxyCmd(opts *options.PowerwallOptions) *cobra.Command {
o := &options.ProxyOptions{PowerwallOptions: opts}
proxyCmd := &cobra.Command{
Use: "proxy",
Short: "start powerwall proxy",
Long: `Start powerwall proxy server`,
Run: func(cmd *cobra.Command, args []string) {
pwr := powerwall.NewPowerwallGateway(o.endpoint, o.password)
if !o.debugMode {
pwr := powerwall.NewPowerwallGateway(o.Endpoint, o.Password)
if !o.DebugMode {
gin.SetMode(gin.ReleaseMode)
}
gin.ForceConsoleColor()
app := api.NewApi(pwr, o.onDemand)
if !o.onDemand {
go pwr.PeriodicRefresh(time.Duration(o.refreshInterval) * time.Second)
app := api.NewApi(pwr, o.OnDemand)
if !o.OnDemand {
go pwr.PeriodicRefresh(time.Duration(o.RefreshInterval) * time.Second)
}
app.Run(o.listenOn)
app.Run(o.ListenOn)
},
}
proxyCmd.Flags().BoolVarP(&o.onDemand, "ondemand", "o", false, "disable periodic refresh")
proxyCmd.Flags().StringVarP(&o.listenOn, "listen", "l", ":8080", "host:port to listen on")
proxyCmd.Flags().Uint32VarP(&o.refreshInterval, "refresh", "r", 30, "periodic refresh frequency in seconds")
proxyCmd.Flags().BoolVarP(&o.OnDemand, "ondemand", "o", false, "disable periodic refresh")
proxyCmd.Flags().StringVarP(&o.ListenOn, "listen", "l", ":8080", "host:port to listen on")
proxyCmd.Flags().Uint32VarP(&o.RefreshInterval, "refresh", "r", 30, "periodic refresh frequency in seconds")
return proxyCmd
}
13 changes: 4 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ygelfand/go-powerwall/cmd/options"
)

// set at build time
var debugMode = "true"

type powerwallOptions struct {
endpoint string
password string
debugMode bool
}

var rootCmd = &cobra.Command{
Use: "go-powerwall",
Short: "go powerwall proxy",
Expand All @@ -34,9 +29,9 @@ func init() {
viper.SetDefault("ENDPOINT", "https://192.168.91.1/")
viper.BindEnv("PASSWORD")
viper.BindEnv("ENDPOINT")
o := &powerwallOptions{debugMode: debugMode == "true"}
rootCmd.PersistentFlags().StringVarP(&o.endpoint, "endpoint", "e", viper.GetString("ENDPOINT"), "powerwall endpoint url")
rootCmd.PersistentFlags().StringVarP(&o.password, "password", "p", viper.GetString("PASSWORD"), "powerwall installer password")
o := &options.PowerwallOptions{DebugMode: debugMode == "true"}
rootCmd.PersistentFlags().StringVarP(&o.Endpoint, "endpoint", "e", viper.GetString("ENDPOINT"), "powerwall endpoint url")
rootCmd.PersistentFlags().StringVarP(&o.Password, "password", "p", viper.GetString("PASSWORD"), "powerwall installer password")
rootCmd.MarkPersistentFlagRequired("password")
rootCmd.AddCommand(newProxyCmd(o))
rootCmd.AddCommand(newDebugCmd(o))
Expand Down
4 changes: 2 additions & 2 deletions internal/powerwall/baseapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -61,7 +61,7 @@ func (p *PowerwallGateway) makeAPIRequest(method, path string, body io.Reader) (
}

func (p *PowerwallGateway) refreshAuthToken() error {
fmt.Println("Refreshing auth token")
log.Println("Refreshing auth token")
auth := map[string]string{"username": "customer", "password": p.password[len(p.password)-5:]}
jsonAuth, _ := json.Marshal(auth)
req, err := http.NewRequest("POST", p.endpoint.JoinPath("api/login/Basic").String(), bytes.NewBuffer(jsonAuth))
Expand Down
6 changes: 6 additions & 0 deletions internal/powerwall/queries/controller.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package queries

import "golang.org/x/exp/maps"

var querySet = map[string]*SignedQuery{}

func init() {
Expand All @@ -13,3 +15,7 @@ func addQuery(dq *SignedQuery) {
func GetQuery(name string) *SignedQuery {
return querySet[name]
}

func QueryList() []string {
return maps.Keys(querySet)
}
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
Copyright © 2024 Yuri Gelfand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package main

import "github.com/ygelfand/go-powerwall/cmd"
Expand Down

0 comments on commit 15b4b42

Please sign in to comment.