Skip to content

Commit

Permalink
Command pattern refactoring.
Browse files Browse the repository at this point in the history
Refactored command pattern into files and added new commands.
  • Loading branch information
lordofscripts authored Jul 10, 2024
1 parent cac8d6b commit 4a6c0ee
Show file tree
Hide file tree
Showing 7 changed files with 470 additions and 0 deletions.
70 changes: 70 additions & 0 deletions cmd/command_brightness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* (LIGHTS ONLY) SET BRIGHTNESS COMMAND
*-----------------------------------------------------------------*/
package main

import (
"fmt"

veex "github.com/loxhill/go-vee"
)


/* ----------------------------------------------------------------
* T y p e s
*-----------------------------------------------------------------*/

type BrightnessCommand struct {
GoveeCommand
brightness int
}

/* ----------------------------------------------------------------
* C o n s t r u c t o r s
*-----------------------------------------------------------------*/

func newCmdBrightness(clientPtr *veex.Client, address, model string, value uint) *BrightnessCommand {
// verify it is a light device
if dev := clientPtr.Device(address, model); dev != nil {
if hasLightControl(*dev) {
o := &BrightnessCommand{
GoveeCommand: GoveeCommand{
Client: clientPtr,
Address: address,
Model: model,
},
brightness: int(value),
}

return o
}
}

die(RETVAL_CMD_EXEC_ABORT, "Device model %s %q is not a LIGHT\n", model, address)
return nil
}

/* ----------------------------------------------------------------
* M e t h o d s
*-----------------------------------------------------------------*/

func (c *BrightnessCommand) name() string {
return fmt.Sprintf("Brightness %d", c.brightness)
}

// implements ICommand for OnCommand
func (c *BrightnessCommand) execute() error {
var controlRequest veex.DeviceControlRequest
var err error
controlRequest, err = c.Client.Device(c.Address, c.Model).SetBrightness(c.brightness)
if err == nil {
//var rsp veex.GoveeResponse
_, err = c.Client.Run(controlRequest) // GoveeResponse, error
//fmt.Printf("Response %#+v\n", rsp)
}

return err
}
70 changes: 70 additions & 0 deletions cmd/command_color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* (LIGHTS ONLY) SET COLOR COMMAND
*-----------------------------------------------------------------*/
package main

import (
"fmt"

veex "github.com/loxhill/go-vee"
)


/* ----------------------------------------------------------------
* T y p e s
*-----------------------------------------------------------------*/

type ColorCommand struct {
GoveeCommand
color veex.Color
}

/* ----------------------------------------------------------------
* C o n s t r u c t o r s
*-----------------------------------------------------------------*/

func newCmdColor(clientPtr *veex.Client, address, model string, color veex.Color) *ColorCommand {
// verify it is a light device
if dev := clientPtr.Device(address, model); dev != nil {
if hasLightControl(*dev) {
o := &ColorCommand{
GoveeCommand: GoveeCommand{
Client: clientPtr,
Address: address,
Model: model,
},
color: color,
}

return o
}
}

die(RETVAL_CMD_EXEC_ABORT, "Device model %s %q is not a LIGHT\n", model, address)
return nil
}

/* ----------------------------------------------------------------
* M e t h o d s
*-----------------------------------------------------------------*/

func (c *ColorCommand) name() string {
return fmt.Sprintf("Color #%02x%02x%02x", c.color.R, c.color.G, c.color.B)
}

// implements ICommand for OnCommand
func (c *ColorCommand) execute() error {
var controlRequest veex.DeviceControlRequest
var err error
controlRequest, err = c.Client.Device(c.Address, c.Model).SetColor(c.color)
if err == nil {
//var rsp veex.GoveeResponse
_, err = c.Client.Run(controlRequest) // GoveeResponse, error
//fmt.Printf("Response %#+v\n", rsp)
}

return err
}
79 changes: 79 additions & 0 deletions cmd/command_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* LIST LOCAL DEVICES COMMAND
*-----------------------------------------------------------------*/
package main

import (
"fmt"
"strings"

veex "github.com/loxhill/go-vee"
)

/* ----------------------------------------------------------------
* T y p e s
*-----------------------------------------------------------------*/

type ListCommand struct {
GoveeCommand
}

/* ----------------------------------------------------------------
* C o n s t r u c t o r s
*-----------------------------------------------------------------*/

func newCmdList(clientPtr *veex.Client) *ListCommand {
o := &ListCommand{
GoveeCommand: GoveeCommand{
Client: clientPtr,
Address: "",
Model: "",
},
}
return o
}

/* ----------------------------------------------------------------
* M e t h o d s
*-----------------------------------------------------------------*/

func (c *ListCommand) name() string {
return fmt.Sprintf("List")
}

// implements ICommand for ListCommand
func (c *ListCommand) execute() error {
listRequest := c.Client.ListDevices()
if response, err := c.Client.Run(listRequest); err != nil {
return err
} else {
devices := response.Devices()
for i,d := range devices {
dev := findByMAC(d.Device)
var alias string = "(please add to config file)"
var controllable string = "(can't be controlled)"
if dev != nil{
alias = dev.Alias
}
if d.Controllable {
controllable = ""
}
fmt.Printf("#%d %q\n\tDevice: %s\n\tModel : %s %s\n\tType : %s\n",
i+1,
alias,
d.Device,
d.Model,
controllable,
d.DeviceName)
fmt.Printf("\tCommands: %s\n", strings.Join(d.SupportCmds, "|"))
if is, rng := isLight(d); is {
fmt.Printf("\tLights: %t Range:(%d, %d)\n", is, rng.Min, rng.Max)
}
}
}
return nil
}

78 changes: 78 additions & 0 deletions cmd/command_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* DEVICE INFO & STATE COMMAND
*-----------------------------------------------------------------*/
package main

import (
"fmt"

veex "github.com/loxhill/go-vee"

"lordofscripts/govee"
)

/* ----------------------------------------------------------------
* T y p e s
*-----------------------------------------------------------------*/

// the State commands obtains the online and powered state of a device
// along its h/w address and model-
type StateCommand struct {
GoveeCommand
}


/* ----------------------------------------------------------------
* C o n s t r u c t o r s
*-----------------------------------------------------------------*/

func newCmdState(clientPtr *veex.Client, address, model string) *StateCommand {
o := &StateCommand{
GoveeCommand: GoveeCommand{
Client: clientPtr,
Address: address,
Model: model,
},
}
return o
}

/* ----------------------------------------------------------------
* M e t h o d s
*-----------------------------------------------------------------*/

func (c *StateCommand) name() string {
return fmt.Sprintf("State")
}

// implements ICommand for StateCommand
func (c *StateCommand) execute() error {
stateRequest := c.Client.Device(c.Address, c.Model).State()
// {Code:200 Message:Success Data:{Device:D7:B6:60:74:F4:02:D5:A2 Model:H5083 Properties:[map[online:true] map[powerState:off]] Devices:[]}}
rsp, err := c.Client.Run(stateRequest) // govee.GoveeResponse

//fmt.Printf("%+v\n", rsp)
if rsp.Code != 200 {
err = fmt.Errorf("State request failed: %q", rsp.Message)
} else {
if dprop := govee.NewGoveeDataProperties(rsp.Data); dprop != nil {
fmt.Println("\tMAC :", dprop.Address) // rsp.Data.Device
fmt.Println("\tModel :", dprop.Model) // rsp.Data.Model
// rsp.Data.Properties[]
fmt.Println("\tOnline :", dprop.Online)
fmt.Println("\tPowered:", dprop.Powered)
light := dprop.IsLight()
fmt.Println("\tLight? :", light)
if light {
fmt.Println("\t\tBrightness :", dprop.Brightness)
fmt.Println("\t\tColor :", dprop.Color)
fmt.Println("\t\tTemperature:", dprop.Temperature)
}
}
}
return err
}

51 changes: 51 additions & 0 deletions cmd/command_turn_off.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* -----------------------------------------------------------------
* L o r d O f S c r i p t s (tm)
* Copyright (C)2024 Dídimo Grimaldo T.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* TURN OFF DEVICE COMMAND
*-----------------------------------------------------------------*/
package main

import (
"fmt"

veex "github.com/loxhill/go-vee"
)

/* ----------------------------------------------------------------
* T y p e s
*-----------------------------------------------------------------*/

type OffCommand struct {

Check failure on line 19 in cmd/command_turn_off.go

View workflow job for this annotation

GitHub Actions / build

other declaration of OffCommand
GoveeCommand
}

/* ----------------------------------------------------------------
* C o n s t r u c t o r s
*-----------------------------------------------------------------*/

func newCmdOff(clientPtr *veex.Client, address, model string) *OffCommand {
o := &OffCommand{
GoveeCommand: GoveeCommand{
Client: clientPtr,
Address: address,
Model: model,
},
}
return o
}

/* ----------------------------------------------------------------
* M e t h o d s
*-----------------------------------------------------------------*/

func (c *OffCommand) name() string {
return fmt.Sprintf("TurnOff")
}

// implements ICommand for OffCommand
func (c *OffCommand) execute() error {
controlRequest, _ := c.Client.Device(c.Address, c.Model).TurnOff()
_, err := c.Client.Run(controlRequest)
return err
}
Loading

0 comments on commit 4a6c0ee

Please sign in to comment.