From b569f134a5a62f2e54da4785b8bb5fd852912c03 Mon Sep 17 00:00:00 2001 From: Marius Breivik Date: Tue, 5 Jan 2021 21:06:20 +0100 Subject: [PATCH] Feature/complete temp command (#1) Added output for temp cmd --- .gitignore | 2 +- cmd/root.go | 2 +- cmd/temp.go | 62 ++++++++++++++++--------------- go.mod | 1 + go.sum | 2 + internal/netatmo/netatmoClient.go | 28 +++++--------- 6 files changed, 48 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 66fd13c..9ab9220 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ *.dll *.so *.dylib - +.idea # Test binary, built with `go test -c` *.test diff --git a/cmd/root.go b/cmd/root.go index 4de5180..9e2feb2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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()) } } diff --git a/cmd/temp.go b/cmd/temp.go index 0b562fe..3d7fad5 100644 --- a/cmd/temp.go +++ b/cmd/temp.go @@ -1,58 +1,62 @@ -/* -Copyright © 2020 NAME HERE - -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") } diff --git a/go.mod b/go.mod index f4f5b13..b790735 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 4da28ea..fbae7ac 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/netatmo/netatmoClient.go b/internal/netatmo/netatmoClient.go index fe4f5ed..9cd4a4e 100644 --- a/internal/netatmo/netatmoClient.go +++ b/internal/netatmo/netatmoClient.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net/http" - "net/http/httputil" "net/url" "github.com/mariusbreivik/netatmo/api/netatmo" @@ -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 @@ -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 }