Skip to content

Commit

Permalink
Implement more detailed logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
hacktobeer committed Dec 21, 2020
1 parent b888355 commit f1d7dcd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
20 changes: 13 additions & 7 deletions cloudcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -48,13 +48,18 @@ 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.
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 {
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -105,6 +112,8 @@ func NewClient(server string) Client {
client.Server = pt.URLServer
}

log.Debugf("Created new client for %s", client.Server)

return client
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
36 changes: 22 additions & 14 deletions utils/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand Down Expand Up @@ -48,38 +48,43 @@ 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)
fmt.Printf("date: %s\n", date)
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)
}
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -143,30 +150,31 @@ func main() {
}

if *onFlag {
log.Println("Turning device on.....")
log.Infoln("Turning device on.....")
_, err = client.TurnOn()
if err != nil {
log.Fatalln(err)
}
}

if *offFlag {
log.Println("Turning device off....")
log.Infoln("Turning device off....")
_, err = client.TurnOff()
if err != nil {
log.Fatalln(err)
}
}

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)
}
}

if *modeFlag != "" {
log.Infof("Setting mode to %s", pt.Modes[*modeFlag])
_, err = client.SetMode(pt.Modes[*modeFlag])
if err != nil {
log.Fatalln(err)
Expand Down
13 changes: 0 additions & 13 deletions utils/go.mod

This file was deleted.

0 comments on commit f1d7dcd

Please sign in to comment.