Skip to content

Commit

Permalink
rename deviceName to deviceAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
neroist committed Jun 22, 2023
1 parent c29137b commit d94612c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ proc colorToAnsi*(color: colors.Color; foreground: bool = true): string =
proc colorToAnsi*(color: tuple[r, g, b: range[0..255]]; foreground: bool = true): string =
colorToAnsi(rgb(color.r, color.g, color.b), foreground)

func getDeviceInfo*(jsonData: JsonNode; device): tuple[deviceName, model: string] =
func getDeviceInfo*(jsonData: JsonNode; device): tuple[deviceAddr, model: string] =
let
deviceName = jsonData["data"]["devices"][device]["device"].getStr()
deviceAddr = jsonData["data"]["devices"][device]["device"].getStr()
model = jsonData["data"]["devices"][device]["model"].getStr()

result = (deviceName: deviceName, model: model)
result = (deviceAddr: deviceAddr, model: model)

proc sendCompletionMsg*(code: int; message: JsonNode; codeMsg: HttpCode) =
if code == 200:
Expand Down
39 changes: 21 additions & 18 deletions src/nova.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ proc turn(device = 0; state: string = ""; toggle: bool = false, output = on): st

let
resp = parseJson fetch(DevicesURI, @{"Govee-API-Key": apiKey})
(deviceName, model) = getDeviceInfo(resp, device)
(deviceAddr, model) = getDeviceInfo(resp, device)

var state = state

if state == "" and not toggle:
let response = parseJson(
fetch(
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceName, false)}&model={model}",
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceAddr, false)}&model={model}",
@{"Govee-API-Key": apiKey}
)
)
Expand All @@ -146,7 +146,7 @@ proc turn(device = 0; state: string = ""; toggle: bool = false, output = on): st
if toggle:
let response = parseJson(
fetch(
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceName, false)}&model={model}",
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceAddr, false)}&model={model}",
@{"Govee-API-Key": apiKey}
)
)
Expand All @@ -158,7 +158,7 @@ proc turn(device = 0; state: string = ""; toggle: bool = false, output = on): st
return

let body = %* {
"device": deviceName,
"device": deviceAddr,
"model": model,
"cmd": {
"name": "turn",
Expand All @@ -169,6 +169,9 @@ proc turn(device = 0; state: string = ""; toggle: bool = false, output = on): st
let re = put(ControlURI, @{"Govee-API-Key": apiKey, "Content-Type": "application/json"}, $body)

if output:
echo "Set device power state to ", state
echo ""

sendCompletionMsg re.code, parseJson(re.body)["message"], HttpCode(re.code)

return state
Expand All @@ -192,12 +195,12 @@ proc color(device = 0; color: string = ""; output = on): string =

let
resp = parseJson fetch(DevicesURI, @{"Govee-API-Key": apiKey})
(deviceName, model) = getDeviceInfo(resp, device)
(deviceAddr, model) = getDeviceInfo(resp, device)

if color == "":
let response = parseJson(
fetch(
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceName, false)}&model={model}",
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceAddr, false)}&model={model}",
@{"Govee-API-Key": apiKey}
)
)
Expand Down Expand Up @@ -243,7 +246,7 @@ proc color(device = 0; color: string = ""; output = on): string =
b = rgb[2]

let body = %* {
"device": deviceName,
"device": deviceAddr,
"model": model,
"cmd": {
"name": "color",
Expand Down Expand Up @@ -273,12 +276,12 @@ proc brightness(device = 0; brightness = -1; output = on): int =

let
resp = parseJson fetch(DevicesURI, @{"Govee-API-Key": apiKey})
(deviceName, model) = getDeviceInfo(resp, device)
(deviceAddr, model) = getDeviceInfo(resp, device)

if brightness == -1: # if brightness is default value
let response = parseJson(
fetch(
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceName, false)}&model={model}",
fmt"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceAddr, false)}&model={model}",
@{"Govee-API-Key": apiKey}
)
)
Expand All @@ -295,7 +298,7 @@ proc brightness(device = 0; brightness = -1; output = on): int =
return

let body = %* {
"device": deviceName,
"device": deviceAddr,
"model": model,
"cmd": {
"name": "brightness",
Expand All @@ -319,13 +322,13 @@ proc colorTemp(device = 0; output = on; temperature: int = -1): int =

let
resp = parseJson fetch(DevicesURI, @{"Govee-API-Key": apiKey})
(deviceName, model) = getDeviceInfo(resp, device)
(deviceAddr, model) = getDeviceInfo(resp, device)

if temperature == -1:
let
response = parseJson(
fetch(
&"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceName, false)}&model={model}",
&"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceAddr, false)}&model={model}",
@{"Govee-API-Key": apiKey}
)
)
Expand All @@ -351,7 +354,7 @@ proc colorTemp(device = 0; output = on; temperature: int = -1): int =
return

let body = %* {
"device": deviceName,
"device": deviceAddr,
"model": model,
"cmd": {
"name": "colorTem",
Expand Down Expand Up @@ -384,11 +387,11 @@ proc state(device = 0) =

let
resp = parseJson fetch(DevicesURI, @{"Govee-API-Key": apiKey})
(deviceName, model) = getDeviceInfo(resp, device)
(deviceAddr, model) = getDeviceInfo(resp, device)

response = parseJson(
fetch(
&"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceName)}&model={model}",
&"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceAddr)}&model={model}",
@{"Govee-API-Key": apiKey}
)
)
Expand Down Expand Up @@ -441,14 +444,14 @@ proc rgbCmd(rgb: seq[int] = @[-1, -1, -1]; device = 0; output = on): tuple[r, g,

let
resp = parseJson fetch(DevicesURI, @{"Govee-API-Key": apiKey})
(deviceName, model) = getDeviceInfo(resp, device)
(deviceAddr, model) = getDeviceInfo(resp, device)

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

let response = parseJson(
fetch(
&"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceName, false)}&model={model}",
&"https://developer-api.govee.com/v1/devices/state?device={encodeUrl(deviceAddr, false)}&model={model}",
@{"Govee-API-Key": apiKey}
)
)
Expand Down Expand Up @@ -481,7 +484,7 @@ proc rgbCmd(rgb: seq[int] = @[-1, -1, -1]; device = 0; output = on): tuple[r, g,
color = colorToAnsi rgb(rgb[0], rgb[1], rgb[2])

let body = %* {
"device": deviceName,
"device": deviceAddr,
"model": model,
"cmd": {
"name": "color",
Expand Down

0 comments on commit d94612c

Please sign in to comment.