Skip to content

Commit

Permalink
Feature/complete temp command (#1)
Browse files Browse the repository at this point in the history
Added output for temp cmd
  • Loading branch information
mariusbreivik authored Jan 5, 2021
1 parent 3d03879 commit b569f13
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*.dll
*.so
*.dylib

.idea
# Test binary, built with `go test -c`
*.test

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
//fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
62 changes: 33 additions & 29 deletions cmd/temp.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,62 @@
/*
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
netatmo2 "github.com/mariusbreivik/netatmo/api/netatmo"
"github.com/mariusbreivik/netatmo/internal/netatmo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ttacon/chalk"
)

// tempCmd represents the temp command
var indoor bool
var outdoor bool

// tempCmd is the command for retrieving temperature
var tempCmd = &cobra.Command{
Use: "temp",
Short: "read temperature data from netatmo station",
Long: `read temperature data from netatmo station`,
Example: "netatmo --temp indoor",
Example: "netatmo temp indoor",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := netatmo.NewClient(netatmo.Config{
netatmoClient, err := netatmo.NewClient(netatmo.Config{
ClientID: viper.GetString("netatmo.clientID"),
ClientSecret: viper.GetString("netatmo.clientSecret"),
Username: viper.GetString("netatmo.username"),
Password: viper.GetString("netatmo.password"),
})

//fmt.Println("client", client)
if err != nil {
return err
}

err = client.Read()
if indoor {
printIndoorTemp(netatmoClient.GetStationData())
} else if outdoor {
printOutdoorTemp(netatmoClient.GetStationData())
} else {
fmt.Println(cmd.UsageString())
}

return err
return nil
},
}

func printOutdoorTemp(stationData netatmo2.StationData) {
fmt.Println("Station name: ", stationData.Body.Devices[0].StationName)
fmt.Println("Temperature outdoor:", chalk.Blue, stationData.Body.Devices[0].Modules[0].DashboardData.Temperature, chalk.Reset)

}

func printIndoorTemp(stationData netatmo2.StationData) {
fmt.Println("Station name: ", stationData.Body.Devices[0].StationName)
fmt.Println("Temperature indoor:", chalk.Red, stationData.Body.Devices[0].DashboardData.Temperature, chalk.Reset)
}

func init() {
rootCmd.AddCommand(tempCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// tempCmd.PersistentFlags().String("foo", "", "A help for foo")
tempCmd.Flags().BoolVarP(&indoor, "indoor", "i", false, "netatmo temp -i|--indoor")
tempCmd.Flags().BoolVarP(&outdoor, "outdoor", "o", false, "netatmo temp -o|--outdoor")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// tempCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.1
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31 h1:OXcKh35JaYsGMRzpvFkLv/MEyPuL49CThT1pZ8aSml4=
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
28 changes: 10 additions & 18 deletions internal/netatmo/netatmoClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"net/url"

"github.com/mariusbreivik/netatmo/api/netatmo"
Expand Down Expand Up @@ -63,19 +62,16 @@ func (c *Client) doHTTP(req *http.Request) (*http.Response, error) {
return c.httpResponse, nil
}

// GetStations returns the list of stations owned by the user, and their modules
func (c *Client) Read() error {
// GetStationData returns data from netatmo api
func (c *Client) GetStationData() netatmo.StationData {
resp, err := c.doHTTPGet(deviceURL, url.Values{"app_type": {"app_station"}})
//dc := &DeviceCollection{}

if err != nil {
fmt.Println(err)
}
if err = processHTTPResponse(resp, err); err != nil {
stationData := processHTTPResponse(resp, err)

}

return nil
return stationData
}

// send http GET request
Expand All @@ -92,30 +88,26 @@ func (c *Client) doHTTPGet(url string, data url.Values) (*http.Response, error)
}

// process HTTP response
// Unmarshall received data into holder struct
func processHTTPResponse(resp *http.Response, err error) error {
func processHTTPResponse(resp *http.Response, err error) netatmo.StationData {
defer resp.Body.Close()
if err != nil {
return err
fmt.Printf("An error occured %s", err)
}

// debug
debug, _ := httputil.DumpResponse(resp, true)
fmt.Printf("%s\n\n", debug)
//debug, _ := httputil.DumpResponse(resp, true)
//fmt.Printf("%s\n\n", debug)

// check http return code
if resp.StatusCode != 200 {
//bytes, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Bad HTTP return code %d", resp.StatusCode)
fmt.Printf("Bad HTTP return code %d", resp.StatusCode)
}

var devices netatmo.StationData
// Unmarshall response into given struct
err = json.NewDecoder(resp.Body).Decode(&devices)
if err != nil {
fmt.Println(err)
}
fmt.Println(devices.Body.Devices[0].DashboardData.Temperature)

return nil
return devices
}

0 comments on commit b569f13

Please sign in to comment.