summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorctucx <c@ctu.cx>2020-09-14 17:55:57 +0200
committerctucx <c@ctu.cx>2020-09-14 17:55:57 +0200
commit34063920e9efb24e99465bc16a2a77eb4864e634 (patch)
tree8fc53858548eff29585423bbf57c18f158afe572
parent35bb0030cd59c14bbd7b8f4944b69246afeebd75 (diff)
downloadnimtradfri-34063920e9efb24e99465bc16a2a77eb4864e634.tar.gz
nimtradfri-34063920e9efb24e99465bc16a2a77eb4864e634.tar.bz2
nimtradfri-34063920e9efb24e99465bc16a2a77eb4864e634.zip
update
-rw-r--r--deviceHelpers.nim24
-rw-r--r--helpers.nim4
-rw-r--r--test.nim16
-rw-r--r--tradfri-cli.nim32
-rw-r--r--tradfri.nim2
5 files changed, 52 insertions, 26 deletions
diff --git a/deviceHelpers.nim b/deviceHelpers.nim
index 741fd07..c138d0d 100644
--- a/deviceHelpers.nim
+++ b/deviceHelpers.nim
@@ -1,4 +1,4 @@
-import types, tradfri
+import types, helpers, tradfri
proc setPowerState* (device: TradfriDevice, state: bool): bool =
if device.`type` == Lightbulb:
@@ -17,15 +17,18 @@ proc setPowerState* (device: TradfriDevice, state: bool): bool =
proc togglePowerState* (device: TradfriDevice): bool =
var state: bool
- if device.state.lightPowered != true:
- state = true
- else:
- state = false
-
- return device.operateDevice(TradfriDeviceAction(
+ if device.`type` == Lightbulb:
+ return device.operateDevice(TradfriDeviceAction(
kind: LightSetPowerState,
- lightPowerState: state
- ))
+ lightPowerState: invertBool(device.state.lightPowered)
+ ))
+
+ if device.`type` == Plug:
+ return device.operateDevice(TradfriDeviceAction(
+ kind: PlugSetPowerState,
+ plugPowerState: invertBool(device.state.plugPowered)
+ ))
+
proc setBrightness* (device: TradfriDevice, brightness: int): bool =
return device.operateDevice(TradfriDeviceAction(
@@ -33,15 +36,18 @@ proc setBrightness* (device: TradfriDevice, brightness: int): bool =
lightBrightness: brightness
))
+
proc setColorHex* (device: TradfriDevice, color: string): bool =
return device.operateDevice(TradfriDeviceAction(
kind: LightSetColorHex,
lightColorHex: color
))
+
proc setColorXY* (device: TradfriDevice, colorX: int, colorY: int): bool =
return device.operateDevice(TradfriDeviceAction(
kind: LightSetColorXY,
lightColorX: colorX,
lightColorY: colorY
))
+
diff --git a/helpers.nim b/helpers.nim
index 85c21e3..234782a 100644
--- a/helpers.nim
+++ b/helpers.nim
@@ -1,3 +1,7 @@
proc boolToInt* (value: bool): int =
if value != true: return 0
else: return 1
+
+proc invertBool* (value: bool): bool =
+ if value != true: return true
+ else: return false
diff --git a/test.nim b/test.nim
deleted file mode 100644
index d765bef..0000000
--- a/test.nim
+++ /dev/null
@@ -1,16 +0,0 @@
-import types, tradfri, deviceHelpers, json
-
-let tradfriGateway = newTradfriGateway(
- host = "192.168.100.225",
- port = 5684,
- user = "ctucx",
- pass = "JrSGx6WkAVJUl53b"
- )
-
-
-let devices = tradfriGateway.getDevices()
-let jsonDevices = %* devices
-
-echo pretty jsonDevices
-
-echo devices[2].togglePowerState()
diff --git a/tradfri-cli.nim b/tradfri-cli.nim
new file mode 100644
index 0000000..47abea7
--- /dev/null
+++ b/tradfri-cli.nim
@@ -0,0 +1,32 @@
+import types, tradfri, deviceHelpers, json, os
+
+let tradfriGateway = newTradfriGateway(
+ host = "192.168.100.225",
+ port = 5684,
+ user = "ctucx",
+ pass = "JrSGx6WkAVJUl53b"
+ )
+
+
+let devices = tradfriGateway.getDevices()
+
+case paramStr(1):
+of "list-devices":
+ echo "list of all devices:"
+ echo "======================="
+
+ var id = 0
+
+ for device in devices:
+ echo $id & ": \tType:\t" & $device.`type`
+ echo "\tName:\t" & $device.name
+ echo ""
+ id = id+1
+
+of "devices-json":
+ let devicesJson = %* devices
+ echo devicesJson
+
+of "devices-json-pretty":
+ let devicesJson = %* devices
+ echo pretty devicesJson
diff --git a/tradfri.nim b/tradfri.nim
index f7462a8..0de9208 100644
--- a/tradfri.nim
+++ b/tradfri.nim
@@ -105,7 +105,6 @@ proc parseTradfriDevice (data: JsonNode): TradfriDevice =
soundRemoteSupported: false
)
-
return TradfriDevice(
`type`: deviceType,
id: data[ParameterDeviceId].getInt,
@@ -124,6 +123,7 @@ proc parseTradfriDevice (data: JsonNode): TradfriDevice =
)
)
+
proc operateDevice* (device: TradfriDevice, action: TradfriDeviceAction): bool =
var requestParams = %* {}