ctucx.git: nimtradfri

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

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 import json, os, strutils
import tradfri, deviceTypes

let tradfriGateway = newTradfriGateway(
    host = "192.168.100.225",
    port = 5684,
    user = "ctucx",
    pass = "JrSGx6WkAVJUl53b"
  )


let devices = tradfriGateway.getDevices()

if(paramCount() > 0):
  case paramStr(1):
  of "devices":
    echo "list of connected devices:"
    echo "======================="

    var id = 0

    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))

    if devices[deviceId].name == "":
      echo "This device doesn't exist."
      quit(0)

    discard devices[deviceId].togglePowerState()

  of "turn-off":
    let deviceId = parseInt(paramStr(2))

    if devices[deviceId].name == "":
      echo "This device doesn't exist."
      quit(0)

    discard devices[deviceId].setPowerState(false)

  of "turn-on":
    let deviceId = parseInt(paramStr(2))

    if devices[deviceId].name == "":
      echo "This device doesn't exist."
      quit(0)

    discard devices[deviceId].setPowerState(true)

  of "setColor":
    let deviceId = parseInt(paramStr(2))

    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

  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\nturn-on:\t\tTurns the light on. Number from devices is needed as parameter\nturn-off:\t\tTurns the light off. 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"