diff --git a/api/core/GetDevicesFrom.go b/api/core/GetDevicesFrom.go new file mode 100644 index 0000000..8d06b6f --- /dev/null +++ b/api/core/GetDevicesFrom.go @@ -0,0 +1,24 @@ +package core + +import ( + "github.com/bandev/lux/api/general" + "github.com/bandev/lux/commands/devices" + "github.com/fatih/color" + "os" + "strconv" +) + +func GetDevicesFrom(arg string, c general.Connection) []devices.Device { + var ds devices.Devices + ds.Get(c) + if arg == "@a" { + return ds.Data.Devices + } else { + var dID, _ = strconv.Atoi(arg) + if len(ds.Data.Devices) <= dID || dID < 0 { + general.PrintHeading("Device id provided is invalid", color.FgRed) + os.Exit(1) + } + return []devices.Device{ds.Data.Devices[dID]} + } +} \ No newline at end of file diff --git a/commands/brightness/Entry.go b/commands/brightness/Entry.go index 6d754a9..6323ac9 100644 --- a/commands/brightness/Entry.go +++ b/commands/brightness/Entry.go @@ -21,9 +21,9 @@ along with this program. If not, see . package brightness import ( + "github.com/bandev/lux/api/core" "github.com/bandev/lux/api/general" "github.com/bandev/lux/api/keymanager" - "github.com/bandev/lux/commands/devices" "github.com/fatih/color" "strconv" ) @@ -44,24 +44,11 @@ func Entry(args []string) { return } - // Determine the device id from the args - var dID, _ = strconv.Atoi(args[2]) - // Create a new connection struct var c general.Connection c.Key = keymanager.GetAPIKey() c.Base = "https://developer-api.govee.com/" - // Get a list of devices owned by the user - var ds devices.Devices - ds.Get(c) - - // Check dId provided is valid - if len(ds.Data.Devices) <= dID || dID < 0 { - general.PrintHeading("Device id provided is invalid. E.g. lux brightness 0 50", color.FgRed) - return - } - // Find the power var power, err = strconv.Atoi(args[3]) if err != nil { @@ -75,19 +62,19 @@ func Entry(args []string) { return } - // Find the device - var d = ds.Data.Devices[dID] - - // Create control and response structs - // & send the data. - var control Control - var response Response - response.Fill(control.Send(d, c, power)) - - // Output data afterwards - general.PrintHeading("BRIGHTNESS " + args[2] + " " + args[3] + "%", color.FgWhite) - general.PrintStringParagraph("brightness", strconv.Itoa(power) + "%", color.FgWhite) - general.PrintStringParagraph("transaction", response.Message, color.FgWhite) + // For each device + for i, d := range core.GetDevicesFrom(args[2], c) { + // Create control and response structs + // & send the data. + var control Control + var response Response + response.Fill(control.Send(d, c, power)) + + // Output data afterwards + general.PrintHeading("BRIGHTNESS " + strconv.Itoa(i) + " " + args[3] + "%", color.FgWhite) + general.PrintStringParagraph("brightness", strconv.Itoa(power) + "%", color.FgWhite) + general.PrintStringParagraph("transaction", response.Message, color.FgWhite) + } } diff --git a/commands/color/Entry.go b/commands/color/Entry.go index 547765f..69ded0d 100644 --- a/commands/color/Entry.go +++ b/commands/color/Entry.go @@ -21,9 +21,9 @@ along with this program. If not, see . package color import ( + "github.com/bandev/lux/api/core" "github.com/bandev/lux/api/general" "github.com/bandev/lux/api/keymanager" - "github.com/bandev/lux/commands/devices" "github.com/fatih/color" "strconv" "strings" @@ -45,25 +45,11 @@ func Entry(args []string) { return } - // Determine the device id from the args - var dID, _ = strconv.Atoi(args[2]) - // Create a new connection struct var c general.Connection c.Key = keymanager.GetAPIKey() c.Base = "https://developer-api.govee.com/" - - // Get a list of devices owned by the user - var ds devices.Devices - ds.Get(c) - - // Check dId provided is valid - if len(ds.Data.Devices) <= dID || dID < 0 { - general.PrintHeading("Device id provided is invalid. E.g. lux color 0 #0067f4", color.FgRed) - return - } - // Check for hexadecimal colour codes var hex = args[3] var colour ControlCmdColor @@ -78,20 +64,19 @@ func Entry(args []string) { return } - - // Find the device - var d = ds.Data.Devices[dID] - - // Create control and response structs - // & send the data. - var control Control - var response Response - response.Fill(control.Send(d, c, colour)) - - // Output data afterwards - general.PrintHeading("COLOR " + args[2] + " " + strings.ToUpper(args[3]), color.FgWhite) - general.PrintStringParagraph("colour", args[3], color.FgWhite) - general.PrintStringParagraph("transaction", response.Message, color.FgWhite) + // For each device + for i, d := range core.GetDevicesFrom(args[2], c) { + // Create control and response structs + // & send the data. + var control Control + var response Response + response.Fill(control.Send(d, c, colour)) + + // Output data afterwards + general.PrintHeading("COLOR " + strconv.Itoa(i) + " " + strings.ToUpper(args[3]), color.FgWhite) + general.PrintStringParagraph("colour", args[3], color.FgWhite) + general.PrintStringParagraph("transaction", response.Message, color.FgWhite) + } } diff --git a/commands/query/Entry.go b/commands/query/Entry.go index 9e85e1d..2ecae26 100644 --- a/commands/query/Entry.go +++ b/commands/query/Entry.go @@ -21,9 +21,9 @@ along with this program. If not, see . package query import ( + "github.com/bandev/lux/api/core" "github.com/bandev/lux/api/general" "github.com/bandev/lux/api/keymanager" - "github.com/bandev/lux/commands/devices" "github.com/fatih/color" "strconv" ) @@ -40,37 +40,22 @@ func Entry(args []string) { return } - // Determine the device id from the args - var dID, _ = strconv.Atoi(args[2]) - // Create a new connection struct var c general.Connection c.Key = keymanager.GetAPIKey() c.Base = "https://developer-api.govee.com/" - // Get a list of devices owned by the user - var ds devices.Devices - ds.Get(c) - - // Check dId provided is valid - if len(ds.Data.Devices) <= dID || dID < 0 { - general.PrintHeading("Device id provided is invalid", color.FgRed) - return + for i, d := range core.GetDevicesFrom(args[2], c) { + // Generate and parse the query + var q Query + q.Fill(c, d) + // Print query to screen + general.PrintHeading("QUERY "+strconv.Itoa(i), color.FgWhite) + general.PrintStringParagraph("Device:", q.Device, color.FgWhite) + general.PrintStringParagraph("Model:", q.Model, color.FgWhite) + general.PrintBoolParagraph("Online:", color.FgWhite, q.Online) + general.PrintBoolParagraph("Power:", color.FgWhite, q.Power) + general.PrintStringParagraph("Brightness:", strconv.Itoa(int(q.Brightness))+"%", color.FgWhite) + general.PrintStringParagraph("Colour:", q.Colour.ToString(), color.FgWhite) } - - // Find the device - var d = ds.Data.Devices[dID] - - // Generate and parse the query - var q Query - q.Fill(c, d) - - // Print query to screen - general.PrintHeading("QUERY RESULTS", color.FgWhite) - general.PrintStringParagraph("Device:", q.Device, color.FgWhite) - general.PrintStringParagraph("Model:", q.Model, color.FgWhite) - general.PrintBoolParagraph("Online:", color.FgWhite, q.Online) - general.PrintBoolParagraph("Power:", color.FgWhite, q.Power) - general.PrintStringParagraph("Brightness:", strconv.Itoa(int(q.Brightness)) + "%", color.FgWhite) - general.PrintStringParagraph("Colour:", q.Colour.ToString(), color.FgWhite) } diff --git a/commands/turn/Entry.go b/commands/turn/Entry.go index 367629d..7bfe9cb 100644 --- a/commands/turn/Entry.go +++ b/commands/turn/Entry.go @@ -21,9 +21,9 @@ along with this program. If not, see . package turn import ( + "github.com/bandev/lux/api/core" "github.com/bandev/lux/api/general" "github.com/bandev/lux/api/keymanager" - "github.com/bandev/lux/commands/devices" "github.com/fatih/color" "strconv" "strings" @@ -45,40 +45,27 @@ func Entry(args []string) { return } - // Determine the device id from the args - var dID, _ = strconv.Atoi(args[2]) - // Create a new connection struct var c general.Connection c.Key = keymanager.GetAPIKey() c.Base = "https://developer-api.govee.com/" - // Get a list of devices owned by the user - var ds devices.Devices - ds.Get(c) - - // Check dId provided is valid - if len(ds.Data.Devices) <= dID || dID < 0 { - general.PrintHeading("Device id provided is invalid", color.FgRed) - return - } - // Find the command var cmd = general.StringToBool(args[3]) - // Find the device - var d = ds.Data.Devices[dID] - - // Create control and response structs - // & send the data. - var control Control - var response Response - response.Fill(control.Send(d, c, cmd)) - - // Output data afterwards - general.PrintHeading("TURN " + args[2] + " " + strings.ToUpper(args[3]), color.FgWhite) - general.PrintBoolParagraph("power", color.FgWhite, cmd) - general.PrintStringParagraph("transaction", response.Message, color.FgWhite) + // For each device + for i, d := range core.GetDevicesFrom(args[2], c) { + // Create control and response structs + // & send the data. + var control Control + var response Response + response.Fill(control.Send(d, c, cmd)) + + // Output data afterwards + general.PrintHeading("TURN " + strconv.Itoa(i) + " " + strings.ToUpper(args[3]), color.FgWhite) + general.PrintBoolParagraph("power", color.FgWhite, cmd) + general.PrintStringParagraph("transaction", response.Message, color.FgWhite) + } }