summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorctucx <c@ctu.cx>2020-09-15 14:06:52 +0200
committerctucx <c@ctu.cx>2020-09-15 14:06:52 +0200
commit4317db05e4836d05125ad6505f1dd5e4e0cf2e3f (patch)
tree756b82781f7b3f7b0cf5405359c6fa108b12b67b
parent42a64e209458f8833a41fafe9523dc705e0572d4 (diff)
downloadnimtradfri-4317db05e4836d05125ad6505f1dd5e4e0cf2e3f.tar.gz
nimtradfri-4317db05e4836d05125ad6505f1dd5e4e0cf2e3f.tar.bz2
nimtradfri-4317db05e4836d05125ad6505f1dd5e4e0cf2e3f.zip
update
-rw-r--r--coapClient.nim2
-rw-r--r--deviceHelpers.nim2
-rw-r--r--devices.nim (renamed from device.nim)2
-rw-r--r--groupTypes.nim25
-rw-r--r--groups.nim38
-rw-r--r--test.nim11
-rw-r--r--tradfri.nim8
7 files changed, 83 insertions, 5 deletions
diff --git a/coapClient.nim b/coapClient.nim
index ade4349..eeeacb1 100644
--- a/coapClient.nim
+++ b/coapClient.nim
@@ -12,7 +12,7 @@ proc makeCoapRequest* (host: string, port: int, reqMethod: string, user: string,
arguments.add($reqPayload)
arguments.add("coaps://" & host & ":" & $port & endpoint)
-
+
let reqResult = execProcess("coap-client", args = arguments, options = {poUsePath})
try:
diff --git a/deviceHelpers.nim b/deviceHelpers.nim
index d4f1984..c48ff87 100644
--- a/deviceHelpers.nim
+++ b/deviceHelpers.nim
@@ -1,5 +1,5 @@
import colors
-import deviceTypes, helpers, device
+import deviceTypes, helpers, devices
proc setPowerState* (device: TradfriDevice, state: bool): bool =
if device.`type` == Lightbulb:
diff --git a/device.nim b/devices.nim
index 10895da..5f313e1 100644
--- a/device.nim
+++ b/devices.nim
@@ -182,7 +182,7 @@ proc operateDevice* (device: TradfriDevice, action: TradfriDeviceAction): bool =
CheckDeviceType(Plug)
requestParams.add(DevicePlug, %* [{
- ParameterPowerState: action.plugPowerState,
+ ParameterPowerState: boolToInt(action.plugPowerState),
}])
of PlugSetDimmerValue:
diff --git a/groupTypes.nim b/groupTypes.nim
new file mode 100644
index 0000000..6b5a9f2
--- /dev/null
+++ b/groupTypes.nim
@@ -0,0 +1,25 @@
+import gatewayTypes, deviceTypes
+
+type
+ TradfriGroupActionType* = enum
+ GroupRename, GroupSetPowerState
+
+ TradfriGroup* = object
+ gatewayRef*: TradfriGatewayRef
+ `type`*: int
+ name*: string
+ createdAt*: int
+ id*: int
+ powerState*: bool
+ dimmerValue*: int
+ trigger*: int
+ sceneId*: int
+ devices*: seq[TradfriDevice]
+
+ TradfriGroupAction* = object
+ case kind*: TradfriGroupActionType
+ of GroupRename:
+ groupName*: string
+
+ of GroupSetPowerState:
+ groupPowerState*: bool
diff --git a/groups.nim b/groups.nim
new file mode 100644
index 0000000..03f4243
--- /dev/null
+++ b/groups.nim
@@ -0,0 +1,38 @@
+import json, strutils, options
+
+import coapClient
+import gatewayTypes, groupTypes
+import mappings, helpers
+
+proc operateGroup* (group: TradfriGroup, action: TradfriGroupAction): bool =
+ var requestParams = %* {}
+
+ case action.kind:
+ of GroupRename:
+ requestParams.add(ParameterName, %action.groupName)
+
+ of GroupSetPowerState:
+ requestParams.add(DeviceLightbulb, %* [{
+ ParameterPowerState: boolToInt(action.groupPowerState),
+ }])
+
+
+ discard makeCoapRequest(group.gatewayRef.host, group.gatewayRef.port, "put", group.gatewayRef.user, group.gatewayRef.pass, EndpointGroups & $group.id, requestParams)
+
+
+proc getGroup* (gatewayRef: TradfriGatewayRef, groupId: int): TradfriGroup =
+ let request = makeCoapRequest(gatewayRef.host, gatewayRef.port, "get", gatewayRef.user, gatewayRef.pass, EndpointDevices & $groupId, %* {})
+
+ result = TradfriGroup(
+ gatewayRef: gatewayRef,
+
+ )
+
+
+proc getGroups* (gatewayRef: TradfriGatewayRef): seq[TradfriGroup] =
+ let request = makeCoapRequest(gatewayRef.host, gatewayRef.port, "get", gatewayRef.user, gatewayRef.pass, EndpointGroups, %* {})
+
+ result = newSeq[TradfriGroup]()
+
+ for id in request:
+ result.add(getGroup(gatewayRef, id.getInt))
diff --git a/test.nim b/test.nim
new file mode 100644
index 0000000..05a05cd
--- /dev/null
+++ b/test.nim
@@ -0,0 +1,11 @@
+import json, os, strutils
+import tradfri
+
+let tradfriGateway = newTradfriGateway(
+ host = "192.168.100.225",
+ port = 5684,
+ user = "ctucx",
+ pass = "JrSGx6WkAVJUl53b"
+ )
+
+echo tradfriGateway.getGroups()
diff --git a/tradfri.nim b/tradfri.nim
index ff3b075..40151d8 100644
--- a/tradfri.nim
+++ b/tradfri.nim
@@ -1,6 +1,6 @@
-import gatewayTypes, deviceTypes, mappings, helpers
import gateway
-import device, deviceHelpers
+import devices, deviceHelpers
+import groups
#gateway related stuff
export newTradfriGateway
@@ -18,3 +18,7 @@ export setBrightness
export setColorHex
export setColorXY
export setColorXYfromHex
+
+#group related stuff
+export getGroups
+export getGroup