Skip to content

Commit

Permalink
Get lights and rooms by their name
Browse files Browse the repository at this point in the history
  • Loading branch information
thibauult committed Jan 10, 2024
1 parent 3d2554f commit 41c732e
Show file tree
Hide file tree
Showing 15 changed files with 1,024 additions and 905 deletions.
63 changes: 34 additions & 29 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"openhue-cli/util"
)

type GetFlags struct {
type CmdGetOptions struct {
Json bool
Name bool
}

var GetConfig GetFlags

// NewCmdGet returns an initialized Command instance for 'get' sub command
func NewCmdGet(ctx *openhue.Context) *cobra.Command {

o := CmdGetOptions{}

cmd := &cobra.Command{
Use: "get",
Short: "Display one or many resources",
Expand All @@ -24,45 +26,48 @@ func NewCmdGet(ctx *openhue.Context) *cobra.Command {
Retrieve information for any kind of resources exposed by your Hue Bridge: lights, rooms, scenes, etc.
`,
Run: func(cmd *cobra.Command, args []string) {
resp, err := ctx.Api.GetResourcesWithResponse(context.Background())
cobra.CheckErr(err)
resources := *(*resp.JSON200).Data

typeFlag := cmd.Flag("type").Value.String()

if len(typeFlag) > 0 {
// filter resources by type
n := 0
for _, r := range resources {
if *r.Type == gen.ResourceGetType(typeFlag) {
resources[n] = r
n++
}
}
resources = resources[:n]
}

if GetConfig.Json {
util.PrintJson(ctx.Io, resources)
} else {
util.PrintTable(ctx.Io, resources, PrintResource, "Resource ID", "Resource Type")
}
RunGetAllResources(ctx, cmd.Flag("type").Value.String(), &o)
},
}

// local flags
cmd.Flags().StringP("type", "t", "", "Filter by resource type (light, scene, room...)")

// persistence flags
cmd.PersistentFlags().BoolVar(&GetConfig.Json, "json", false, "Format output as JSON")
cmd.PersistentFlags().BoolVarP(&o.Json, "json", "j", false, "Format output as JSON")
cmd.PersistentFlags().BoolVarP(&o.Name, "name", "n", false, "Get resource(s) by name")

// sub commands
cmd.AddCommand(NewCmdGetLight(ctx))
cmd.AddCommand(NewCmdGetRoom(ctx))
cmd.AddCommand(NewCmdGetLight(ctx, &o))
cmd.AddCommand(NewCmdGetRoom(ctx, &o))

return cmd
}

func RunGetAllResources(ctx *openhue.Context, typeFlag string, o *CmdGetOptions) {
resp, err := ctx.Api.GetResourcesWithResponse(context.Background())
cobra.CheckErr(err)
resources := *(*resp.JSON200).Data

if len(typeFlag) > 0 {
// filter resources by type
n := 0
for _, r := range resources {
if *r.Type == gen.ResourceGetType(typeFlag) {
resources[n] = r
n++
}
}
resources = resources[:n]
}

if o.Json {
util.PrintJson(ctx.Io, resources)
} else {
util.PrintTable(ctx.Io, resources, PrintResource, "Resource ID", "Resource Type")
}
}

func PrintResource(resource gen.ResourceGet) string {
return *resource.Id + "\t" + string(*resource.Type)
}
62 changes: 32 additions & 30 deletions cmd/get/get_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package get
import (
"fmt"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/openhue/gen"
"openhue-cli/util"
"os"
)

const (
Expand All @@ -23,21 +20,29 @@ openhue get light --json
# Get details for a single light
openhue get light aa31ba26-98a7-4830-8ae9-1b7caa8b5700 --json
# Get light by name
openhue get light -n "Hue Go"
`
)

type LightOptions struct {
LightId string
LightParam string // ID or Name
Json *bool
Name *bool
}

func NewGetLightOptions() *LightOptions {
return &LightOptions{}
func NewGetLightOptions(co *CmdGetOptions) *LightOptions {
return &LightOptions{
Json: &co.Json,
Name: &co.Name,
}
}

// NewCmdGetLight returns initialized Command instance for the 'get light' sub command
func NewCmdGetLight(ctx *openhue.Context) *cobra.Command {
func NewCmdGetLight(ctx *openhue.Context, co *CmdGetOptions) *cobra.Command {

o := NewGetLightOptions()
o := NewGetLightOptions(co)

cmd := &cobra.Command{
Use: "light [lightId]",
Expand All @@ -46,58 +51,55 @@ func NewCmdGetLight(ctx *openhue.Context) *cobra.Command {
Example: docExampleGetLight,
Args: cobra.MatchAll(cobra.RangeArgs(0, 1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
o.PrepareGetRoomCmd(args)
o.PrepareGetLightCmd(args)
o.RunGetLightCmd(ctx)
},
}

return cmd
}

func (o *LightOptions) PrepareGetRoomCmd(args []string) {
func (o *LightOptions) PrepareGetLightCmd(args []string) {
if len(args) > 0 {
o.LightId = args[0]
o.LightParam = args[0]
}
}

func (o *LightOptions) RunGetLightCmd(ctx *openhue.Context) {
var lights *[]gen.LightGet
var lights []openhue.Light

if len(o.LightId) > 0 {
resp, err := ctx.Api.GetLightWithResponse(context.Background(), o.LightId)
cobra.CheckErr(err)
if len(o.LightParam) > 0 {

if resp.JSON200 == nil {
fmt.Println("\nNot light found with ID", o.LightId)
os.Exit(0)
if *o.Name {
lights = openhue.FindLightByName(ctx.Home, o.LightParam)
} else {
lights = openhue.FindLightsByIds(ctx.Home, []string{o.LightParam})
}

lights = (*resp.JSON200).Data
} else {
resp, err := ctx.Api.GetLightsWithResponse(context.Background())
cobra.CheckErr(err)
lights = (*resp.JSON200).Data
lights = openhue.FindAllLights(ctx.Home)
}

if !GetConfig.Json {
util.PrintTable(ctx.Io, *lights, PrintLight, "ID", "Name", "Type", "Status", "Brightness")
if *o.Json {
util.PrintJsonArray(ctx.Io, lights)
} else {
util.PrintJsonArray(ctx.Io, *lights)
util.PrintTable(ctx.Io, lights, PrintLight, "ID", "Name", "Type", "Status", "Brightness", "Room")
}
}

func PrintLight(light gen.LightGet) string {
func PrintLight(light openhue.Light) string {

status := "[ ]"
brightness := "N/A"
room := light.Parent.Parent.Name // parent of a light is the device that belongs to a room

if *light.On.On {
if *light.HueData.On.On {
status = "[on]"
}

if light.Dimming != nil {
brightness = fmt.Sprint(*light.Dimming.Brightness) + "%"
if light.HueData.Dimming != nil {
brightness = fmt.Sprint(*light.HueData.Dimming.Brightness) + "%"
}

return *light.Id + "\t" + *light.Metadata.Name + "\t" + string(*light.Metadata.Archetype) + "\t" + status + "\t" + brightness
return light.Id + "\t" + light.Name + "\t" + string(*light.HueData.Metadata.Archetype) + "\t" + status + "\t" + brightness + "\t" + room
}
64 changes: 39 additions & 25 deletions cmd/get/get_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package get
import (
"fmt"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"openhue-cli/openhue"
"openhue-cli/openhue/gen"
"openhue-cli/util"
"os"
)

const (
Expand All @@ -23,21 +20,29 @@ openhue get room --json
# Get details for a single room
openhue get room aa31ba26-98a7-4830-8ae9-1b7caa8b5700 --json
# Get room by name
openhue get room -n "Studio"
`
)

type RoomOptions struct {
RoomId string
RoomParam string
Json *bool
Name *bool
}

func NewGetRoomOptions() *RoomOptions {
return &RoomOptions{}
func NewGetRoomOptions(co *CmdGetOptions) *RoomOptions {
return &RoomOptions{
Json: &co.Json,
Name: &co.Name,
}
}

// NewCmdGetRoom returns initialized Command instance for the 'get light' sub command
func NewCmdGetRoom(ctx *openhue.Context) *cobra.Command {
func NewCmdGetRoom(ctx *openhue.Context, co *CmdGetOptions) *cobra.Command {

o := NewGetRoomOptions()
o := NewGetRoomOptions(co)

cmd := &cobra.Command{
Use: "room [roomId]",
Expand All @@ -56,36 +61,45 @@ func NewCmdGetRoom(ctx *openhue.Context) *cobra.Command {

func (o *RoomOptions) PrepareGetRoomCmd(args []string) {
if len(args) > 0 {
o.RoomId = args[0]
o.RoomParam = args[0]
}
}

func (o *RoomOptions) RunGetRoomCmd(ctx *openhue.Context) {
var rooms *[]gen.RoomGet

if len(o.RoomId) > 0 {
resp, err := ctx.Api.GetRoomWithResponse(context.Background(), o.RoomId)
cobra.CheckErr(err)
var rooms []openhue.Room

if len(o.RoomParam) > 0 {

if resp.JSON200 == nil {
fmt.Println("\nNot room found with ID", o.RoomId)
os.Exit(0)
if *o.Name {
rooms = openhue.FindRoomByName(ctx.Home, o.RoomParam)
} else {
rooms = openhue.FindRoomById(ctx.Home, o.RoomParam)
}

rooms = (*resp.JSON200).Data
} else {
resp, err := ctx.Api.GetRoomsWithResponse(context.Background())
cobra.CheckErr(err)
rooms = (*resp.JSON200).Data
rooms = openhue.FindAllRooms(ctx.Home)
}

if !GetConfig.Json {
util.PrintTable(ctx.Io, *rooms, PrintRoom, "ID", "Name", "Type")
if *o.Json {
util.PrintJsonArray(ctx.Io, rooms)
} else {
util.PrintJsonArray(ctx.Io, *rooms)
util.PrintTable(ctx.Io, rooms, PrintRoom, "ID", "Name", "Type", "Status", "Brightness")
}
}

func PrintRoom(room gen.RoomGet) string {
return *room.Id + "\t" + *room.Metadata.Name + "\t" + string(*room.Metadata.Archetype)
func PrintRoom(room openhue.Room) string {

status := "[ ]"
brightness := "N/A"

if room.GroupedLight.IsOn() {
status = "[on]"
}

if room.GroupedLight.HueData.Dimming != nil {
brightness = fmt.Sprint(*room.GroupedLight.HueData.Dimming.Brightness) + "%"
}

return room.Id + "\t" + room.Name + "\t" + string(*room.HueData.Metadata.Archetype) + "\t" + status + "\t" + brightness
}
13 changes: 12 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cmd

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"openhue-cli/cmd/get"
"openhue-cli/cmd/set"
"openhue-cli/cmd/setup"
"openhue-cli/cmd/version"
"openhue-cli/openhue"
"os"
"time"
)

// NewCmdOpenHue represents the `openhue` base command, AKA entry point of the CLI
Expand Down Expand Up @@ -36,8 +39,16 @@ func Execute(buildInfo *openhue.BuildInfo) {
api := c.NewOpenHueClient()
ctx := openhue.NewContext(openhue.NewIOStreams(), buildInfo, api)

// load the home context
t0 := time.Now()
home, err := openhue.LoadHome(api)
cobra.CheckErr(err)
ctx.Home = home
log.Infof("It took %dms to load the Home Context", time.Since(t0).Milliseconds())

// create the root command
root := NewCmdOpenHue()
log.Infof("Running the '%s' command", os.Args)

// init groups
initGroups(root)
Expand All @@ -52,7 +63,7 @@ func Execute(buildInfo *openhue.BuildInfo) {
root.AddCommand(get.NewCmdGet(ctx))

// execute the root command
err := root.Execute()
err = root.Execute()
cobra.CheckErr(err)
}

Expand Down
23 changes: 0 additions & 23 deletions cmd/set/helper.go

This file was deleted.

Loading

0 comments on commit 41c732e

Please sign in to comment.