Skip to content

Commit

Permalink
Iplement session token support and keep-alive.
Browse files Browse the repository at this point in the history
  • Loading branch information
hacktobeer committed Dec 21, 2020
1 parent f1d7dcd commit 3b31a21
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
33 changes: 20 additions & 13 deletions cloudcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ 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")
req.Header.Set("Connection", "Keep-Alive")

log.Debugf("HTTP headers set to: %#v", req.Header)
}
Expand All @@ -63,7 +64,7 @@ func (c *Client) doPostRequest(url string, postbody []byte) ([]byte, error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
return nil, err
}
defer resp.Body.Close()

Expand All @@ -88,7 +89,7 @@ func (c *Client) doGetRequest(url string) ([]byte, error) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
return nil, err
}
defer resp.Body.Close()

Expand Down Expand Up @@ -117,13 +118,19 @@ func NewClient(server string) Client {
return client
}

// CreateSession initialises a client session to Panasonic Comfort Cloud.
func (c *Client) CreateSession(token string, username string, password string) ([]byte, error) {
if username == "" {
c.Utoken = token
return nil, nil
// ValidateSession checks if the session token is still valid.
func (c *Client) ValidateSession(token string) ([]byte, error) {
c.Utoken = token
body, err := c.doGetRequest(pt.URLValidate1)
if err != nil {
return body, fmt.Errorf("error: %v %s", err, body)
}

return body, nil
}

// CreateSession initialises a client session to Panasonic Comfort Cloud.
func (c *Client) CreateSession(username string, password string) ([]byte, error) {
postBody, _ := json.Marshal(map[string]string{
"language": "0",
"loginId": username,
Expand All @@ -132,7 +139,7 @@ func (c *Client) CreateSession(token string, username string, password string) (

body, err := c.doPostRequest(pt.URLLogin, postBody)
if err != nil {
return nil, fmt.Errorf("Error: %v %s", err, body)
return nil, fmt.Errorf("error: %v %s", err, body)
}

session := pt.Session{}
Expand All @@ -150,7 +157,7 @@ func (c *Client) CreateSession(token string, username string, password string) (
func (c *Client) GetGroups() (pt.Groups, error) {
body, err := c.doGetRequest(pt.URLGroups)
if err != nil {
return pt.Groups{}, fmt.Errorf("Error: %v %s", err, body)
return pt.Groups{}, fmt.Errorf("error: %v %s", err, body)
}
groups := pt.Groups{}
err = njson.Unmarshal([]byte(body), &groups)
Expand Down Expand Up @@ -181,7 +188,7 @@ func (c *Client) ListDevices() ([]string, error) {
func (c *Client) GetDeviceStatus() (pt.Device, error) {
body, err := c.doGetRequest(pt.URLDeviceStatus + url.QueryEscape(c.DeviceGUID))
if err != nil {
return pt.Device{}, fmt.Errorf("Error: %v %s", err, body)
return pt.Device{}, fmt.Errorf("error: %v %s", err, body)
}

device := pt.Device{}
Expand All @@ -204,7 +211,7 @@ func (c *Client) GetDeviceHistory(timeFrame int) (pt.History, error) {

body, err := c.doPostRequest(pt.URLHistory, postBody)
if err != nil {
return pt.History{}, fmt.Errorf("Error: %v %s", err, body)
return pt.History{}, fmt.Errorf("error: %v %s", err, body)
}

history := pt.History{}
Expand All @@ -224,10 +231,10 @@ func (c *Client) control(command pt.Command) ([]byte, error) {

body, err := c.doPostRequest(pt.URLControl, postBody)
if err != nil {
return nil, fmt.Errorf("Error: %v %s", err, body)
return nil, fmt.Errorf("error: %v %s", err, body)
}
if string(body) != pt.SuccessResponse {
return body, fmt.Errorf("Error body: %v %s", err, body)
return body, fmt.Errorf("error body: %v %s", err, body)
}

return body, nil
Expand Down
12 changes: 5 additions & 7 deletions cloudcontrol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Example() {
// Create a new Panasonic Comfort Cloud client
client := cloudcontrol.NewClient("")
// Initiate a session with your username and password
client.CreateSession("", "username", "password")
client.CreateSession("username", "password")
// List the available devices in your account
devices, _ := client.ListDevices()
// Set the device we want to control
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestSetDevice(t *testing.T) {
}

func TestTurnOn(t *testing.T) {
client.CreateSession("", "", "")
client.CreateSession("", "")
body, err := client.TurnOn()
if err != nil {
t.Errorf("TestTurnOn() returned an error: %v", err)
Expand All @@ -120,7 +120,7 @@ func TestTurnOn(t *testing.T) {
}

func TestGetGroups(t *testing.T) {
client.CreateSession("", "", "")
client.CreateSession("", "")
groups, _ := client.GetGroups()
want := "My House"
got := groups.Groups[0].GroupName
Expand All @@ -135,9 +135,8 @@ func TestGetGroups(t *testing.T) {
func TestCreateSessionCustomToken(t *testing.T) {
username := ""
password := ""
token := "token12345"

body, _ := client.CreateSession(token, username, password)
body, _ := client.CreateSession(username, password)
if body != nil {
t.Error("TestCreateSessionCustomToken() got non-nil body")
}
Expand All @@ -152,9 +151,8 @@ func TestCreateSessionCustomToken(t *testing.T) {
func TestCreateSession(t *testing.T) {
username := "[email protected]"
password := "secret1234"
token := ""

client.CreateSession(token, username, password)
client.CreateSession(username, password)

got := client.Utoken
want := "token12345"
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
URLDeviceStatus = "/deviceStatus/now/"
URLHistory = "/deviceHistoryData"
URLControl = "/deviceStatus/control"
URLValidate1 = "/auth/agreement/status/1"
SuccessResponse = `{"result":0}`
FailureResponse = `{"result":1}`
)
Expand Down
31 changes: 23 additions & 8 deletions utils/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,27 @@ func main() {
user := viper.GetString("username")
pass := viper.GetString("password")
server := viper.GetString("server")
token := viper.GetString("token")

client := cloudcontrol.NewClient(server)

_, err := client.CreateSession("", user, pass)
if err != nil {
log.Fatalln(err)
if token != "" {
if body, err := client.ValidateSession(token); err != nil {
log.Debugf("ValidateSession Error: %s", string(body))
if user != "" && pass != "" {
_, err := client.CreateSession(user, pass)
if err != nil {
log.Fatalln(err)
}
viper.Set("token", client.Utoken)
viper.WriteConfig()
log.Debug("New session token requested and written to config")
} else {
log.Fatalln("No username and password given, can't login.")
}
} else {
log.Debugln("Session token passed validation check")
}
}

if *listFlag {
Expand Down Expand Up @@ -107,7 +122,7 @@ func main() {
}
// Exit if no devices are configured
if client.DeviceGUID == "" {
log.Fatalln("error: No device configured, please use flag or configuration file")
log.Fatalln("error: No device configured, please use -device flag or configuration file")
}

if *statusFlag {
Expand Down Expand Up @@ -151,31 +166,31 @@ func main() {

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

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

if *tempFlag != 0 {
log.Infof("Setting temperature to %v degrees Celsius", *tempFlag)
_, err = client.SetTemperature(*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])
_, err := client.SetMode(pt.Modes[*modeFlag])
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 3b31a21

Please sign in to comment.