Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address all devices #10

Merged
merged 6 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/core/GetDevicesFrom.go
Original file line number Diff line number Diff line change
@@ -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]}
}
}
41 changes: 14 additions & 27 deletions commands/brightness/Entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
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"
)
Expand All @@ -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 {
Expand All @@ -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)
}
}


43 changes: 14 additions & 29 deletions commands/color/Entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
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"
Expand All @@ -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
Expand All @@ -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)
}
}


41 changes: 13 additions & 28 deletions commands/query/Entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
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"
)
Expand All @@ -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)
}
41 changes: 14 additions & 27 deletions commands/turn/Entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
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"
Expand All @@ -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)
}
}