From 7526308d057c59f876f7d50e9df07f4993becfb9 Mon Sep 17 00:00:00 2001 From: Jack Devey Date: Wed, 25 Aug 2021 19:43:01 +0100 Subject: [PATCH 1/6] Create GetDevicesFrom function in core --- api/core/GetDevicesFrom.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 api/core/GetDevicesFrom.go 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 From 23cf4bd381481bcc44d1af20207b90d78af4283a Mon Sep 17 00:00:00 2001 From: Jack Devey Date: Wed, 25 Aug 2021 19:43:32 +0100 Subject: [PATCH 2/6] Enable all devices in turn --- commands/turn/Entry.go | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/commands/turn/Entry.go b/commands/turn/Entry.go index 367629d..1cb2c4e 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,30 @@ 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 - } + // Determine the devices from the args + var ds = core.GetDevicesFrom(args[2], c) // 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 ds { + // 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) + } } From 9349e2d2cc7b522368b7dc0c1af9dd743b3d7b05 Mon Sep 17 00:00:00 2001 From: Jack Devey Date: Wed, 25 Aug 2021 19:53:27 +0100 Subject: [PATCH 3/6] Code cleanup --- commands/turn/Entry.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/commands/turn/Entry.go b/commands/turn/Entry.go index 1cb2c4e..7bfe9cb 100644 --- a/commands/turn/Entry.go +++ b/commands/turn/Entry.go @@ -50,14 +50,11 @@ func Entry(args []string) { c.Key = keymanager.GetAPIKey() c.Base = "https://developer-api.govee.com/" - // Determine the devices from the args - var ds = core.GetDevicesFrom(args[2], c) - // Find the command var cmd = general.StringToBool(args[3]) // For each device - for i, d := range ds { + for i, d := range core.GetDevicesFrom(args[2], c) { // Create control and response structs // & send the data. var control Control From 19582c29fa35fb0748c5360b30f185e7b8b85fbe Mon Sep 17 00:00:00 2001 From: Jack Devey Date: Wed, 25 Aug 2021 19:53:55 +0100 Subject: [PATCH 4/6] Implement for brightness command --- commands/brightness/Entry.go | 41 ++++++++++++------------------------ 1 file changed, 14 insertions(+), 27 deletions(-) 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) + } } From c5ad24fe3a5db04ce7b8e043495cfc1efbc1186c Mon Sep 17 00:00:00 2001 From: Jack Devey Date: Wed, 25 Aug 2021 19:54:02 +0100 Subject: [PATCH 5/6] Implement for color command --- commands/color/Entry.go | 43 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) 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) + } } From a368c3e6d7ead3d6a3709e3cd1026b824d85983b Mon Sep 17 00:00:00 2001 From: Jack Devey Date: Wed, 25 Aug 2021 19:54:07 +0100 Subject: [PATCH 6/6] Implement for query command --- commands/query/Entry.go | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) 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) }