Skip to content

Commit

Permalink
v3.0.0-alpha - add debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
MickMake committed Nov 28, 2022
1 parent 188aa0f commit b6bd802
Show file tree
Hide file tree
Showing 1,051 changed files with 11,317 additions and 3,302 deletions.
6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2,309 changes: 1,255 additions & 1,054 deletions .idea/workspace.xml

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions Only/const.go

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Note: I'm currently going through a big refactor. The next major release, (v3.0.

[Latest releases here](https://github.com/MickMake/GoSungrow/releases)

[![Go Reference](https://pkg.go.dev/badge/github.com/MickMake/GoSungrow.svg)](https://pkg.go.dev/github.com/MickMake/GoSungrow)


## What is it?

Expand Down
123 changes: 74 additions & 49 deletions cmd/cmd_api.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"GoSungrow/Only"
"GoSungrow/iSolarCloud"
"GoSungrow/iSolarCloud/AppService/login"
"GoSungrow/iSolarCloud/api/GoStruct/output"
"errors"
"fmt"
"github.com/MickMake/GoUnify/Only"
"github.com/MickMake/GoUnify/cmdConfig"
"github.com/MickMake/GoUnify/cmdHelp"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -137,7 +138,15 @@ func (c *CmdApi) AttachCommand(cmd *cobra.Command) *cobra.Command {
}
return nil
},
Run: c.CmdApiLogin,
RunE: func(cmd *cobra.Command, args []string) error {
c.Error = c.ApiLogin(true)
if c.Error != nil {
return c.Error
}

c.SunGrow.Auth.Print()
return nil
},
Args: cobra.MinimumNArgs(0),
}
cmdApi.AddCommand(cmdApiLogin)
Expand Down Expand Up @@ -230,6 +239,35 @@ func (c *CmdApi) AttachCommand(cmd *cobra.Command) *cobra.Command {
cmdApi.AddCommand(cmdApiSave)
cmdApiSave.Example = cmdHelp.PrintExamples(cmdApiSave, "[area].<endpoint>")

// ******************************************************************************** //
var cmdApiStruct = &cobra.Command{
Use: output.StringTypeStruct,
Aliases: []string{},
Short: fmt.Sprintf("Show response as Go structure (debug)"),
Long: fmt.Sprintf("Show response as Go structure (debug)"),
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: func(cmd *cobra.Command, args []string) error {
cmds.Error = cmds.ProcessArgs(cmd, args)
if cmds.Error != nil {
return cmds.Error
}
cmds.Error = cmds.SunGrowArgs(cmd, args)
if cmds.Error != nil {
return cmds.Error
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// c.SunGrow.SaveAsFile = true
c.SunGrow.OutputType.SetStruct()
return c.CmdApiGet(cmd, args)
},
Args: cobra.MinimumNArgs(1),
}
cmdApi.AddCommand(cmdApiStruct)
cmdApiStruct.Example = cmdHelp.PrintExamples(cmdApiStruct, "[area].<endpoint>")

// ******************************************************************************** //
var cmdApiPut = &cobra.Command{
Use: "put",
Expand Down Expand Up @@ -303,37 +341,14 @@ func (ca *Cmds) SunGrowArgs(_ *cobra.Command, _ []string) error {
ca.Api.AppKey = defaultApiAppKey
}

ca.Error = ca.Api.SunGrow.Login(login.SunGrowAuth{
AppKey: ca.Api.AppKey,
UserAccount: ca.Api.Username,
UserPassword: ca.Api.Password,
TokenFile: ca.Api.ApiTokenFile,
Force: false,
})
ca.Error = ca.Api.ApiLogin(false)
if ca.Error != nil {
break
}

if ca.Debug {
ca.Api.SunGrow.Auth.Print()
}

if ca.Api.SunGrow.HasTokenChanged() {
ca.Api.LastLogin = ca.Api.SunGrow.GetLastLogin()
ca.Api.ApiToken = ca.Api.SunGrow.GetToken()
ca.Error = cmds.Unify.WriteConfig()
}

// if Cmd.GoogleSheetUpdate {
// SunGrow.OutputType = iSolarCloud.TypeGoogle
// }

// Git.Error = Cmd.GitSet()
// if Cmd.Error != nil {
// break
// }
//
// ca.Valid = true
}

return ca.Error
Expand Down Expand Up @@ -378,29 +393,6 @@ func (c *CmdApi) CmdApiList(cmd *cobra.Command, args []string) {
}
}

func (c *CmdApi) CmdApiLogin(_ *cobra.Command, _ []string) {
for range Only.Once {
c.Error = c.SunGrow.Login(login.SunGrowAuth{
AppKey: c.AppKey,
UserAccount: c.Username,
UserPassword: c.Password,
TokenFile: c.ApiTokenFile,
Force: true,
})
if c.Error != nil {
break
}

c.SunGrow.Auth.Print()

if c.SunGrow.HasTokenChanged() {
c.LastLogin = c.SunGrow.GetLastLogin()
c.ApiToken = c.SunGrow.GetToken()
c.Error = cmds.Unify.WriteConfig()
}
}
}

func (c *CmdApi) CmdApiGet(_ *cobra.Command, args []string) error {
for range Only.Once {
args = cmdConfig.FillArray(2, args)
Expand Down Expand Up @@ -438,3 +430,36 @@ func (c *CmdApi) CmdApiPut(_ *cobra.Command, _ []string) {
// }
}
}


func (c *CmdApi) ApiLogin(force bool) error {
for range Only.Once {
if c.SunGrow == nil {
c.Error = errors.New("sungrow instance not configured")
break
}

auth := login.SunGrowAuth {
AppKey: c.AppKey,
UserAccount: c.Username,
UserPassword: c.Password,
TokenFile: c.ApiTokenFile,
Force: force,
}
c.Error = c.SunGrow.Login(auth)
if c.Error != nil {
break
}

if c.SunGrow.HasTokenChanged() {
c.LastLogin = c.SunGrow.GetLastLogin()
c.ApiToken = c.SunGrow.GetToken()

sf := cmds.Api.SaveFile
cmds.Api.SaveFile = false // We don't want to lock this in the config.
c.Error = cmds.Unify.WriteConfig()
cmds.Api.SaveFile = sf
}
}
return c.Error
}
28 changes: 27 additions & 1 deletion cmd/cmd_data.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cmd

import (
"GoSungrow/Only"
"GoSungrow/iSolarCloud"
"GoSungrow/iSolarCloud/api/GoStruct/output"
"fmt"
"github.com/MickMake/GoUnify/Only"
"github.com/MickMake/GoUnify/cmdConfig"
"github.com/MickMake/GoUnify/cmdHelp"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -218,6 +218,32 @@ func (c *CmdData) AttachCommand(cmd *cobra.Command) *cobra.Command {
}
c.SelfCmd.AddCommand(cmdDataGraph)
cmdDataGraph.Example = cmdHelp.PrintExamples(cmdDataGraph, "queryDeviceList", "WebAppService.showPSView", "stats")

// ******************************************************************************** //
var cmdApiStruct = &cobra.Command{
Use: output.StringTypeStruct + " <[area.]endpoint> [endpoint args ...]",
Aliases: []string{},
Annotations: map[string]string{"group": "Data"},
Short: fmt.Sprintf("Show response as Go structure (debug)"),
Long: fmt.Sprintf("Show response as Go structure (debug)"),
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: func(cmd *cobra.Command, args []string) error {
cmds.Error = cmds.ProcessArgs(cmd, args)
if cmds.Error != nil {
return cmds.Error
}
cmds.Error = cmds.SunGrowArgs(cmd, args)
if cmds.Error != nil {
return cmds.Error
}
return nil
},
RunE: c.GetEndpoints,
Args: cobra.MinimumNArgs(0),
}
c.SelfCmd.AddCommand(cmdApiStruct)
cmdApiStruct.Example = cmdHelp.PrintExamples(cmdApiStruct, "queryDeviceList", "WebAppService.showPSView")
}
return c.SelfCmd
}
Expand Down
Loading

0 comments on commit b6bd802

Please sign in to comment.