ctucx.git: nimtradfri

[nimlang] incomplete library to interact with ikea tradfri-gateways

commit ebb05c1e00a8445f6efa5000c70167cd5b4ed651
parent 2149d7385accadf3deded63643c88309d734850b
Author: Isabelle K <hi@f2k1.de>
Date: Tue, 15 Sep 2020 13:34:35 +0200

update
1 file changed, 52 insertions(+), 30 deletions(-)
M
tradfriCli.nim
|
82
++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
diff --git a/tradfriCli.nim b/tradfriCli.nim
@@ -1,5 +1,5 @@
 import json, os, strutils
-import tradfri
+import tradfri, deviceTypes
 
 let tradfriGateway = newTradfriGateway(
     host = "192.168.100.225",

@@ -11,41 +11,62 @@ let tradfriGateway = newTradfriGateway(
 
 let devices = tradfriGateway.getDevices()
 
-case paramStr(1):
-of "list":
-  echo "list of connected devices:"
-  echo "======================="
+if(paramCount() > 0):
+  case paramStr(1):
+  of "devices":
+    echo "list of connected devices:"
+    echo "======================="
 
-  var id = 0
+    var id = 0
 
-  for device in devices:
-    echo $id & ": \tType:\t" & $device.`type`
-    echo "\tName:\t" & $device.name
-    echo ""
-    id = id+1
+    for device in devices:
+      if device.alive == false:
+        stdout.write "\u001b[30;1m"
+      echo $id & ": \tType:\t\t" & $device.`type`
+      echo "\tName:\t\t" & $device.name
+      echo "\tAlive:\t\t" & $device.alive
+      if device.type == Lightbulb:
+        stdout.write "\tLightSpectrum:\t"
+        if device.state.lightSpectrum == RGB:
+          echo "🎨 RGB"
+        elif device.state.lightSpectrum == White:
+          echo "⚪ White"
+        elif device.state.lightSpectrum == None:
+          echo "☐ None"
+        if device.state.lightPowered:
+          echo "\tPowered:\t💡 yes"
+        else:
+          echo "\tPowered:\tno"
+      if device.info.power == Battery:
+        echo "\tBattery:\t" & $device.info.battery & "%"
+      echo "\u001b[0m"
+      id = id+1
 
-of "toggle":
-  let deviceId = parseInt(paramStr(2))
+  of "toggle":
+    let deviceId = parseInt(paramStr(2))
 
-  if devices[deviceId].name == "":
-    echo "This device doesn't exist."
-    quit(0)
+    if devices[deviceId].name == "":
+      echo "This device doesn't exist."
+      quit(0)
 
-  discard devices[deviceId].togglePowerState()
+    discard devices[deviceId].togglePowerState()
 
-of "setColor":
-  let deviceId = parseInt(paramStr(2))
+  of "setColor":
+    let deviceId = parseInt(paramStr(2))
 
-  if devices[deviceId].name == "":
-    echo "This device doesn't exist."
-    quit(0)
+    if devices[deviceId].name == "":
+      echo "This device doesn't exist."
+      quit(0)
 
-  discard devices[deviceId].setColorXYfromHex(paramStr(3))
-  
-of "devices-json":
-  let devicesJson = %* devices
-  echo devicesJson
+    discard devices[deviceId].setColorXYfromHex(paramStr(3))
 
-of "devices-json-pretty":
-  let devicesJson = %* devices
-  echo pretty devicesJson
+  of "devices-json":
+    let devicesJson = %* devices
+    echo devicesJson
+
+  of "devices-json-pretty":
+    let devicesJson = %* devices
+    echo pretty devicesJson
+
+else:
+  echo "tradfriCli\nUsage: ./trafriCli [params]\n\ndevices:\t\tLists devices. No additional options.\ntoggle:\t\t\tToggles device. Number from devices is needed as parameter\nsetColor:\t\tSets color for device. Number from devices is needed as parameter\ndevices-json:\t\tPrints out json of devices. No additional options\ndevices-json-pretty:\tSame as devices-json, but in pretty"+
\ No newline at end of file