From e5ab45d548df2153b21d1eccc505804caa868082 Mon Sep 17 00:00:00 2001 From: hacktobeer Date: Fri, 18 Dec 2020 08:51:01 +0100 Subject: [PATCH] Refactor URLs. --- cloudcontrol.go | 12 +++++------ types/types.go | 53 +++++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/cloudcontrol.go b/cloudcontrol.go index 1f84870..47309cf 100644 --- a/cloudcontrol.go +++ b/cloudcontrol.go @@ -96,7 +96,7 @@ func (c *Client) CreateSession(token string, username string, password string, s if server != "" { c.Server = server } else { - c.Server = "https://accsmart.panasonic.com/" + c.Server = pt.URLServer } postBody, _ := json.Marshal(map[string]string{ @@ -105,7 +105,7 @@ func (c *Client) CreateSession(token string, username string, password string, s "password": password, }) - body, err := c.doPostRequest("auth/login", postBody) + body, err := c.doPostRequest(pt.URLLogin, postBody) if err != nil { return fmt.Errorf("Error: %v %s", err, body) } @@ -123,7 +123,7 @@ func (c *Client) CreateSession(token string, username string, password string, s // GetGroups gets all Panasonic Control groups associated to this account func (c *Client) GetGroups() (pt.Groups, error) { - body, err := c.doGetRequest("/device/group") + body, err := c.doGetRequest(pt.URLGroups) if err != nil { return pt.Groups{}, fmt.Errorf("Error: %v %s", err, body) } @@ -154,7 +154,7 @@ func (c *Client) ListDevices() ([]string, error) { // GetDevice gets all details on a specific device func (c *Client) GetDevice(deviceGUID string) (pt.Device, error) { - body, err := c.doGetRequest("/deviceStatus/now/" + url.QueryEscape(deviceGUID)) + body, err := c.doGetRequest(pt.URLDeviceStatus + url.QueryEscape(deviceGUID)) if err != nil { return pt.Device{}, fmt.Errorf("Error: %v %s", err, body) } @@ -177,7 +177,7 @@ func (c *Client) GetDeviceHistory(timeFrame int) (pt.History, error) { "osTimezone": "+01:00", }) - body, err := c.doPostRequest("deviceHistoryData", postBody) + body, err := c.doPostRequest(pt.URLHistory, postBody) if err != nil { return pt.History{}, fmt.Errorf("Error: %v %s", err, body) } @@ -198,7 +198,7 @@ func (c *Client) control(command pt.Command) error { log.Println("JSON to be sent:") log.Println(string(postBody)) - body, err := c.doPostRequest("deviceStatus/control", postBody) + body, err := c.doPostRequest(pt.URLControl, postBody) if err != nil { return fmt.Errorf("Error: %v %s", err, body) } diff --git a/types/types.go b/types/types.go index 7aa016e..35fc02e 100644 --- a/types/types.go +++ b/types/types.go @@ -1,5 +1,32 @@ package types +// Exported constants +const ( + URLServer = "https://accsmart.panasonic.com" + URLLogin = "/auth/login" + URLGroups = "/device/group" + URLDeviceStatus = "/deviceStatus/now/" + URLHistory = "/deviceHistoryData" + URLControl = "/deviceStatus/control" +) + +// HistoryDataMode maps out the time intervals to fetch history data +var HistoryDataMode = map[string]int{ + "day": 0, + "week": 1, + "month": 2, + "year": 3, +} + +// Modes define the different AC modes the device can be in +var Modes = map[string]int{ + "auto": 0, + "dry": 1, + "cool": 2, + "heat": 3, + "fan": 3, +} + // Session is a login session structure type Session struct { Utoken string `njson:"uToken"` @@ -103,32 +130,6 @@ type HistoryEntry struct { AverageOutsideTemp float64 `njson:"averageOutsideTemp"` } -// HistoryDataMode maps out the time intervals to fetch history data -var HistoryDataMode = map[string]int{ - "day": 0, - "week": 1, - "month": 2, - "year": 3, -} - -// Exported constants -const ( - ModeAuto = 0 - ModeDry = 1 - ModeCool = 2 - ModeHeat = 3 - ModeFan = 4 -) - -// Modes definesthe different AC modes the device can be in -var Modes = map[string]int{ - "auto": 0, - "dry": 1, - "cool": 2, - "heat": 3, - "fan": 3, -} - // Command is basic command control structure type Command struct { DeviceGUID string `json:"deviceGuid"`