Skip to content

Commit

Permalink
add get cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
tupyy committed May 11, 2023
1 parent 591b1d0 commit e2c2ca1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
49 changes: 45 additions & 4 deletions cmd/zone/root.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
package zone

import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/tupyy/airzone/cmd/common"
"github.com/tupyy/airzone/internal/hvac"
)

// zoneCmd represents the zone command
var RootCmd = &cobra.Command{
Use: "zone",
Short: "Control one zone only.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("zone called")
Use: "zone",
Short: "Control one zone only.",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Name or ZoneID is required")
}

// check if it is a zoneID or name
zoneName := ""
zoneID := -1
zoneID, err := strconv.Atoi(strings.ToLower(args[0]))
if err != nil {
zoneName = strings.ToLower(args[0])
}

if zoneName != "" {
names, err := hvac.GetZoneNames(common.Host, common.SystemID)
if err != nil {
return err
}
id, ok := names[zoneName]
if !ok {
return fmt.Errorf("Zone %q not found", zoneName)
}
zoneID = id
}

fmt.Println(zoneID)
hvac, err := hvac.GetData(common.Host, common.SystemID, zoneID)
if err != nil {
return err
}
j, err := json.Marshal(hvac)
if err != nil {
return err
}
fmt.Printf("%s", string(j))
return nil
},
}
14 changes: 7 additions & 7 deletions cmd/zones/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import (

// rootZonesCmd represents the get command
var RootCmd = &cobra.Command{
Use: "zones",
Short: "Control all the zones all together.",
Run: func(c *cobra.Command, args []string) {
Use: "zones",
Short: "Control all the zones all together.",
SilenceUsage: true,
RunE: func(c *cobra.Command, args []string) error {
hvac, err := hvac.GetData(common.Host, common.SystemID, common.AllZones)
if err != nil {
fmt.Printf("%+v", err)
return
return err
}
j, err := json.Marshal(hvac)
if err != nil {
fmt.Printf("%+v", err)
return
return err
}
fmt.Printf("%s", string(j))
return nil
},
}
1 change: 1 addition & 0 deletions internal/hvac/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Hvac struct {
}

type Zone struct {
ID int `json:"zoneID"`
Name string `json:"name"`
On int `json:"on"`
CoolSetPoint float64 `json:"coolsetpoint"`
Expand Down
13 changes: 13 additions & 0 deletions internal/hvac/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)

const (
Expand Down Expand Up @@ -40,3 +41,15 @@ func GetData(host string, systemID, zoneID int) (Hvac, error) {

return hvac, nil
}

func GetZoneNames(host string, systemID int) (map[string]int, error) {
data, err := GetData(host, systemID, 0)
if err != nil {
return nil, err
}
names := make(map[string]int)
for _, z := range data.Zones {
names[strings.ToLower(z.Name)] = z.ID
}
return names, nil
}

0 comments on commit e2c2ca1

Please sign in to comment.