Skip to content

Commit

Permalink
check supported commands
Browse files Browse the repository at this point in the history
  • Loading branch information
neroist committed Mar 14, 2024
1 parent eafa477 commit d0e59f8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/commands/brightness.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ proc brightness*(device = 0, brightness: int = -1; output = on, all: bool = fals
devices = parseJson readFile(devicesFile)
(deviceAddr, model) = getDeviceInfo(devices, device)

if newJString("brightness") notin devices[device]["supportCmds"].getElems():
error "This command is not supported by device ", $device
return

if brightness == -1: # if brightness is default value
let response = getDeviceState(deviceAddr, model, apiKey)

Expand Down
5 changes: 5 additions & 0 deletions src/commands/color.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ../common

proc color*(device: int = 0; color: string = ""; output = on, all: bool = false): string =
## Set device color with an HTML/hex color code.
##
## NOTE: when called with no parameters, the device's current color will be #000000 if:
##
## 1. Music mode is on.
Expand All @@ -29,6 +30,10 @@ proc color*(device: int = 0; color: string = ""; output = on, all: bool = false)
devices = parseJson readFile(devicesFile)
(deviceAddr, model) = getDeviceInfo(devices, device)

if newJString("color") notin devices[device]["supportCmds"].getElems():
error "This command is not supported by device ", $device
return

var
color = color.replace(" ").replace("-").normalize()
colorJson = %* {"r": 0, "g": 0, "b": 0}
Expand Down
4 changes: 4 additions & 0 deletions src/commands/color_temp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ proc colorTemp*(device = 0, temperature: int = -1; output = on, all: bool = fals
devices = parseJson readFile(devicesFile)
(deviceAddr, model) = getDeviceInfo(devices, device)

if newJString("colorTem") notin devices[device]["supportCmds"].getElems():
error "This command is not supported by device ", $device
return

if temperature == -1:
let
response = getDeviceState(deviceAddr, model, apiKey)
Expand Down
5 changes: 2 additions & 3 deletions src/commands/devices.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ proc devices* =
let devices = parseJson readFile(devicesFile)

for dev, i in devices.getElems():
let
cmds = collect(for i in i["supportCmds"]: i.getStr())
## seq of all supported commands of the device
let cmds = collect(for i in i["supportCmds"]: i.getStr())
## seq of all supported commands of the device

echo "\e[1m", "DEVICE ", $dev, ansiResetCode
echo " Mac Address: ", i["device"].getStr()
Expand Down
4 changes: 4 additions & 0 deletions src/commands/rgb.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ proc rgb*(rgb: seq[int] = @[-1, -1, -1]; device: int = 0; output = on, all: bool
devices = parseJson readFile(devicesFile)
(deviceAddr, model) = getDeviceInfo(devices, device)

if newJString("color") notin devices[device]["supportCmds"].getElems():
error "This command is not supported by device ", $device
return

if rgb == @[-1 ,-1, -1]:
var colorJson = %* {"r": 0, "g": 0, "b": 0}

Expand Down
8 changes: 6 additions & 2 deletions src/commands/turn.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ proc turn*(device: int = 0; state: string = ""; toggle = false, output = on, all
let
apiKey = readFile(keyFile)

resp = parseJson readFile(devicesFile)
(deviceAddr, model) = getDeviceInfo(resp, device)
devices = parseFile(devicesFile)
(deviceAddr, model) = getDeviceInfo(devices, device)

if newJString("turn") notin devices[device]["supportCmds"].getElems():
error "This command is not supported by device ", $device
return

var state = state

Expand Down

0 comments on commit d0e59f8

Please sign in to comment.