Skip to content

Commit

Permalink
Merge pull request #4 from rhjensen79/dev
Browse files Browse the repository at this point in the history
Added GetChargerID function
  • Loading branch information
rhjensen79 authored Dec 29, 2022
2 parents 0e79c69 + 7ae064e commit f85b858
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
bin
.vscode
.vscode
bak
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ to add them to the applications config file.

The config file will be places in `$home/.easee-cli/config.yaml`

If you have problems finding your Charger's Id, then run

```
easee-cli config getchargerid
```

To get the id of your charger.
Note we currently only support one charger.

### Prerequisites

To be able to run any commands, you must have a valid token.
Expand Down
25 changes: 0 additions & 25 deletions bak/go.mod

This file was deleted.

63 changes: 63 additions & 0 deletions cmd/getchargerid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getchargeridCmd represents the getchargerid command
var getchargeridCmd = &cobra.Command{
Use: "getchargerid",
Short: "Get ChargerID of your charger",
Long: `Get ChargerID of your charger`,
Run: func(cmd *cobra.Command, args []string) {
viper.Set("chargerid", GetChargerId(viper.GetString("token")))
},
}

func init() {
configCmd.AddCommand(getchargeridCmd)

}

func GetChargerId(accesstoken string) (chargerid string) {
type ResponseData struct {
ID string `json:"id"`
}

url := "https://api.easee.cloud/api/chargers"

req, _ := http.NewRequest("GET", url, nil)

req.Header.Add("accept", "application/json")
req.Header.Add("Authorization", accesstoken)

res, _ := http.DefaultClient.Do(req)

if res.StatusCode == 200 {
log.Println("ChargerID Token recieved")
} else {
log.Fatal("Error Getting ChargerID code : ", res.StatusCode)
}

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

st := string(body)
st = strings.Trim(st, "[]")

var responseData ResponseData
json.Unmarshal([]byte(st), &responseData)

return responseData.ID

}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
Copyright © 2022 Robert Jensen https://github.com/rhjensen79
*/
package main

Expand Down

0 comments on commit f85b858

Please sign in to comment.