diff --git a/LICENSE b/LICENSE index 62bb0c7..619219a 100644 --- a/LICENSE +++ b/LICENSE @@ -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 @@ -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. diff --git a/cmd/debug.go b/cmd/debug.go index d758ba9..cc8d4f8 100644 --- a/cmd/debug.go +++ b/cmd/debug.go @@ -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 } diff --git a/cmd/proxy.go b/cmd/proxy.go index c439a87..ebc2b32 100644 --- a/cmd/proxy.go +++ b/cmd/proxy.go @@ -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 } diff --git a/cmd/root.go b/cmd/root.go index 057226c..7e125b0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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", @@ -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)) diff --git a/internal/powerwall/baseapi.go b/internal/powerwall/baseapi.go index 47abc65..84112e8 100644 --- a/internal/powerwall/baseapi.go +++ b/internal/powerwall/baseapi.go @@ -4,8 +4,8 @@ import ( "bytes" "encoding/json" "errors" - "fmt" "io" + "log" "net/http" "time" ) @@ -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)) diff --git a/internal/powerwall/queries/controller.go b/internal/powerwall/queries/controller.go index c0f3e2d..ecd6f76 100644 --- a/internal/powerwall/queries/controller.go +++ b/internal/powerwall/queries/controller.go @@ -1,5 +1,7 @@ package queries +import "golang.org/x/exp/maps" + var querySet = map[string]*SignedQuery{} func init() { @@ -13,3 +15,7 @@ func addQuery(dq *SignedQuery) { func GetQuery(name string) *SignedQuery { return querySet[name] } + +func QueryList() []string { + return maps.Keys(querySet) +} diff --git a/main.go b/main.go index 48d18ed..96f53b1 100644 --- a/main.go +++ b/main.go @@ -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"