diff --git a/cloudcontrol.go b/cloudcontrol.go index 9f80130..c4b2eac 100644 --- a/cloudcontrol.go +++ b/cloudcontrol.go @@ -7,13 +7,13 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "net/url" "time" pt "github.com/hacktobeer/go-panasonic/types" "github.com/m7shapan/njson" + log "github.com/sirupsen/logrus" ) // Client is a Panasonic Comfort Cloud client. @@ -48,6 +48,8 @@ func (c *Client) setHeaders(req *http.Request) { req.Header.Set("User-Agent", "G-RAC") req.Header.Set("Accept", "application/json; charset=utf-8") req.Header.Set("Content-Type", "application/json") + + log.Debugf("HTTP headers set to: %#v", req.Header) } // doPostRequest will send a HTTP POST request. @@ -55,6 +57,9 @@ func (c *Client) doPostRequest(url string, postbody []byte) ([]byte, error) { req, err := http.NewRequest("POST", c.Server+url, bytes.NewBuffer(postbody)) c.setHeaders(req) + log.Debugf("POST request URL: %#v\n", req.URL) + log.Debugf("POST request body: %#v\n", string(postbody)) + client := &http.Client{} resp, err := client.Do(req) if err != nil { @@ -64,6 +69,8 @@ func (c *Client) doPostRequest(url string, postbody []byte) ([]byte, error) { body, _ := ioutil.ReadAll(resp.Body) + log.Debugf("POST response body: %s", string(body)) + if resp.StatusCode > 200 { return body, fmt.Errorf("HTTP Error: %s", resp.Status) } @@ -76,7 +83,7 @@ func (c *Client) doGetRequest(url string) ([]byte, error) { req, err := http.NewRequest("GET", c.Server+url, nil) c.setHeaders(req) - //log.Printf("%#v\n", req) + log.Debugf("GET request URL: %#v\n", req.URL) client := &http.Client{} resp, err := client.Do(req) @@ -87,7 +94,7 @@ func (c *Client) doGetRequest(url string) ([]byte, error) { body, _ := ioutil.ReadAll(resp.Body) - // log.Println(string(body)) + log.Debugf("GET response body: %s", string(body)) if resp.StatusCode > 200 { return body, fmt.Errorf("HTTP Error: %s", resp.Status) @@ -105,6 +112,8 @@ func NewClient(server string) Client { client.Server = pt.URLServer } + log.Debugf("Created new client for %s", client.Server) + return client } @@ -211,8 +220,7 @@ func (c *Client) GetDeviceHistory(timeFrame int) (pt.History, error) { func (c *Client) control(command pt.Command) ([]byte, error) { postBody, _ := json.Marshal(command) - // log.Println("JSON to be sent:") - // log.Println(string(postBody)) + log.Debugf("Command: %s", postBody) body, err := c.doPostRequest(pt.URLControl, postBody) if err != nil { @@ -222,8 +230,6 @@ func (c *Client) control(command pt.Command) ([]byte, error) { return body, fmt.Errorf("Error body: %v %s", err, body) } - // log.Println(string(body)) - return body, nil } diff --git a/go.mod b/go.mod index 4c81a9d..5a94b99 100644 --- a/go.mod +++ b/go.mod @@ -9,5 +9,6 @@ require ( github.com/hacktobeer/go-panasonic v1.0.0 github.com/hacktobeer/go-panasonic/types v0.0.0-00010101000000-000000000000 github.com/m7shapan/njson v1.0.1 + github.com/sirupsen/logrus v1.2.0 github.com/spf13/viper v1.7.1 ) diff --git a/utils/cli.go b/utils/cli.go index 8cafcb8..01f5f9f 100644 --- a/utils/cli.go +++ b/utils/cli.go @@ -3,12 +3,11 @@ package main import ( "flag" "fmt" - "io/ioutil" - "log" "os" "github.com/hacktobeer/go-panasonic/cloudcontrol" pt "github.com/hacktobeer/go-panasonic/types" + log "github.com/sirupsen/logrus" "github.com/spf13/viper" ) @@ -19,6 +18,7 @@ var ( version = "development" configFlag = flag.String("config", "gopanasonic.yaml", "Path of YAML configuration file") + debugFlag = flag.Bool("debug", false, "Show debug output") deviceFlag = flag.String("device", "", "Device to issue command to") historyFlag = flag.String("history", "", "Display history: day,week,month,year") listFlag = flag.Bool("list", false, "List available devices") @@ -48,6 +48,15 @@ func main() { flag.Parse() + if *quietFlag { + log.SetLevel(log.PanicLevel) + } + + if *debugFlag { + log.SetLevel(log.DebugLevel) + log.Debug("Logging set to debug level") + } + if *versionFlag { fmt.Printf("version: %s\n", version) fmt.Printf("commit: %s\n", commit) @@ -55,31 +64,27 @@ func main() { os.Exit(0) } - if *quietFlag { - log.SetFlags(0) - log.SetOutput(ioutil.Discard) - } - readConfig() user := viper.GetString("username") pass := viper.GetString("password") server := viper.GetString("server") client := cloudcontrol.NewClient(server) + _, err := client.CreateSession("", user, pass) if err != nil { log.Fatalln(err) } if *listFlag { - log.Println("Listing available devices.....") + log.Infoln("Listing available devices.....") devices, err := client.ListDevices() if err != nil { log.Fatalln(err) } if len(devices) != 0 { - log.Printf("%d device(s) found:\n", len(devices)) + log.Infof("%d device(s) found:\n", len(devices)) for _, device := range devices { fmt.Println(device) } @@ -91,11 +96,13 @@ func main() { // Read device from flag if *deviceFlag != "" { + log.Debugf("Device set to %s", *deviceFlag) client.SetDevice(*deviceFlag) } // Read device from configuration file configDevice := viper.GetString("device") if configDevice != "" { + log.Debugf("Device set to %s", configDevice) client.SetDevice(configDevice) } // Exit if no devices are configured @@ -104,7 +111,7 @@ func main() { } if *statusFlag { - log.Println("Fetching status.....") + log.Infoln("Fetching status.....") status, err := client.GetDeviceStatus() if err != nil { log.Fatalln(err) @@ -131,7 +138,7 @@ func main() { } if *historyFlag != "" { - log.Printf("Fetching historical data for this %s.....\n", *historyFlag) + log.Infof("Fetching historical data for this %s.....\n", *historyFlag) history, err := client.GetDeviceHistory(pt.HistoryDataMode[*historyFlag]) if err != nil { log.Fatalln(err) @@ -143,7 +150,7 @@ func main() { } if *onFlag { - log.Println("Turning device on.....") + log.Infoln("Turning device on.....") _, err = client.TurnOn() if err != nil { log.Fatalln(err) @@ -151,7 +158,7 @@ func main() { } if *offFlag { - log.Println("Turning device off....") + log.Infoln("Turning device off....") _, err = client.TurnOff() if err != nil { log.Fatalln(err) @@ -159,7 +166,7 @@ func main() { } if *tempFlag != 0 { - log.Printf("Setting temperature to %v degrees Celsius", *tempFlag) + log.Infof("Setting temperature to %v degrees Celsius", *tempFlag) _, err = client.SetTemperature(*tempFlag) if err != nil { log.Fatalln(err) @@ -167,6 +174,7 @@ func main() { } if *modeFlag != "" { + log.Infof("Setting mode to %s", pt.Modes[*modeFlag]) _, err = client.SetMode(pt.Modes[*modeFlag]) if err != nil { log.Fatalln(err) diff --git a/utils/go.mod b/utils/go.mod deleted file mode 100644 index 9782019..0000000 --- a/utils/go.mod +++ /dev/null @@ -1,13 +0,0 @@ -module cli - -replace github.com/hacktobeer/go-panasonic/cloudcontrol => ../ - -replace github.com/hacktobeer/go-panasonic/types => ../types/ - -go 1.15 - -require ( - github.com/hacktobeer/go-panasonic/cloudcontrol v0.0.0-00010101000000-000000000000 - github.com/hacktobeer/go-panasonic/types v0.0.0-00010101000000-000000000000 - github.com/spf13/viper v1.7.1 -)