summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorctucx <c@ctu.cx>2020-09-14 17:30:07 +0200
committerctucx <c@ctu.cx>2020-09-14 17:30:07 +0200
commit35bb0030cd59c14bbd7b8f4944b69246afeebd75 (patch)
tree1b7c63af42493363b14fd095274f0a7ed438bfb6
parent9c93e45919cabbbdf3cf99d1a0ee2f8b7bb9788a (diff)
downloadnimtradfri-35bb0030cd59c14bbd7b8f4944b69246afeebd75.tar.gz
nimtradfri-35bb0030cd59c14bbd7b8f4944b69246afeebd75.tar.bz2
nimtradfri-35bb0030cd59c14bbd7b8f4944b69246afeebd75.zip
update
-rw-r--r--deviceHelpers.nim47
-rw-r--r--mappings.nim59
-rw-r--r--test.nim7
-rw-r--r--tradfri.nim125
-rw-r--r--types.nim4
5 files changed, 139 insertions, 103 deletions
diff --git a/deviceHelpers.nim b/deviceHelpers.nim
new file mode 100644
index 0000000..741fd07
--- /dev/null
+++ b/deviceHelpers.nim
@@ -0,0 +1,47 @@
+import types, tradfri
+
+proc setPowerState* (device: TradfriDevice, state: bool): bool =
+ if device.`type` == Lightbulb:
+ return device.operateDevice(TradfriDeviceAction(
+ kind: LightSetPowerState,
+ lightPowerState: state
+ ))
+
+ if device.`type` == Plug:
+ return device.operateDevice(TradfriDeviceAction(
+ kind: PlugSetPowerState,
+ plugPowerState: state
+ ))
+
+
+proc togglePowerState* (device: TradfriDevice): bool =
+ var state: bool
+
+ if device.state.lightPowered != true:
+ state = true
+ else:
+ state = false
+
+ return device.operateDevice(TradfriDeviceAction(
+ kind: LightSetPowerState,
+ lightPowerState: state
+ ))
+
+proc setBrightness* (device: TradfriDevice, brightness: int): bool =
+ return device.operateDevice(TradfriDeviceAction(
+ kind: LightSetBrightness,
+ 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/mappings.nim b/mappings.nim
index a0bb1ae..20d161d 100644
--- a/mappings.nim
+++ b/mappings.nim
@@ -1,38 +1,31 @@
-import tables
+const EndpointDevices* = "/15001/"
+const EndpointGroups* = "/15004/"
+const EndpointScenes* = "/15005/"
+const EndpointNotifications* = "/15006/"
+const EndpointSmartTasks* = "/15010/"
-let endpoints* = {
- "devices": "/15001/",
- "groups": "/15004/",
- "scenes": "/15005/",
- "notifications": "/15006/",
- "smart_tasks": "/15010/"
-}.toTable
+const DeviceMotionSensor* = "3300"
+const DeviceLightbulb* = "3311"
+const DevicePlug* = "3312"
+const DeviceBlind* = "15015"
-let devices* = {
- "MotionSensor": "3300",
- "Lightbulb": "3311",
- "Plug": "3312",
- "Blind": "15015"
-}.toTable
+const ParameterDeviceName* = "9001"
+const ParameterDeviceCreatedAt* = "9002"
+const ParameterDeviceId* = "9003"
+const ParameterDeviceAlive* = "9019"
+const ParameterDeviceLastSeen* = "9020"
+const ParameterDeviceType* = "5750"
-let parameters* = {
- "DeviceName": "9001",
- "DeviceCreatedAt": "9002",
- "DeviceId": "9003",
- "DeviceAlive": "9019",
- "DeviceLastSeen": "9020",
+const ParameterPowerState* = "5850"
+const ParameterDimmerValue* = "5851"
- "PowerState": "5850",
- "DimmerValue": "5851",
+const ParameterColorHex* = "5706"
+const ParameterHue* = "5707"
+const ParameterSaturation* = "5708"
+const ParameterColorX* = "5709"
+const ParameterColorY* = "5710"
+const ParameterColorTemperature* = "5711"
+const ParameterTransitionTime* = "5712"
- "ColorHex": "5706",
- "Hue": "5707",
- "Saturation": "5708",
- "ColorX": "5709",
- "ColorY": "5710",
- "ColorTemperature": "5711",
- "TransitionTime": "5712",
-
- "blindTrigger": "5523",
- "BlindPosition": "5536",
-}.toTable
+const ParameterBlindTrigger* = "5523"
+const ParameterBlindPosition* = "5536"
diff --git a/test.nim b/test.nim
index f7f3733..d765bef 100644
--- a/test.nim
+++ b/test.nim
@@ -1,4 +1,4 @@
-import types, tradfri, json
+import types, tradfri, deviceHelpers, json
let tradfriGateway = newTradfriGateway(
host = "192.168.100.225",
@@ -13,7 +13,4 @@ let jsonDevices = %* devices
echo pretty jsonDevices
-echo devices[2].operateDevice(TradfriDeviceAction(
- kind: TradfriDeviceActionType(0),
- lightPowerState: false
-))
+echo devices[2].togglePowerState()
diff --git a/tradfri.nim b/tradfri.nim
index e8caa59..f7462a8 100644
--- a/tradfri.nim
+++ b/tradfri.nim
@@ -1,4 +1,4 @@
-import json, strutils, options, tables
+import json, strutils, options
import coapClient, types, mappings, helpers
@@ -12,7 +12,7 @@ proc newTradfrigateway* (host: string, port: int, user: string, pass: string): T
proc parseTradfriDevice (data: JsonNode): TradfriDevice =
- let deviceType = TradfriDeviceType(data["5750"].getInt)
+ let deviceType = TradfriDeviceType(data[ParameterDeviceType].getInt)
var state: TradfriDeviceState
case deviceType:
@@ -31,53 +31,53 @@ proc parseTradfriDevice (data: JsonNode): TradfriDevice =
of Lightbulb:
state = TradfriDeviceState(
kind: deviceType,
- lightPowered: parseBool($data["3311"][0]["5850"].getInt), # on / off
- lightBrightness: data["3311"][0]["5851"].getInt # 1 - 254
+ lightPowered: parseBool($data[DeviceLightbulb][0][ParameterPowerState].getInt), # on / off
+ lightBrightness: data[DeviceLightbulb][0][ParameterDimmerValue].getInt # 1 - 254
)
#get hue and saturation (only for RGB-bulbs)
- if data["3311"][0].hasKey("5707"):
- state.lightHue = some(data["3311"][0]["5707"].getInt)
- state.lightSaturation = some(data["3311"][0]["5708"].getInt)
+ if data[DeviceLightbulb][0].hasKey(ParameterHue):
+ state.lightHue = some(data[DeviceLightbulb][0][ParameterHue].getInt)
+ state.lightSaturation = some(data[DeviceLightbulb][0][ParameterSaturation].getInt)
else:
state.lightHue = none(int)
state.lightSaturation = none(int)
#get color hex-value (for white-spectrum and RGB, but only some presets)
- if data["3311"][0].hasKey("5706"):
- state.lightColorHex = some(data["3311"][0]["5706"].getStr)
+ if data[DeviceLightbulb][0].hasKey(ParameterColorHex):
+ state.lightColorHex = some(data[DeviceLightbulb][0][ParameterColorHex].getStr)
else:
state.lightColorHex = none(string)
#get colorX and colorY values (can be used to set any color on RGB bulbs)
- if data["3311"][0].hasKey("5709"):
- state.lightColorX = some(data["3311"][0]["5709"].getInt)
- state.lightColorY = some(data["3311"][0]["5710"].getInt)
+ if data[DeviceLightbulb][0].hasKey(ParameterColorX):
+ state.lightColorX = some(data[DeviceLightbulb][0][ParameterColorX].getInt)
+ state.lightColorY = some(data[DeviceLightbulb][0][ParameterColorY].getInt)
else:
state.lightColorX = none(int)
state.lightColorY = none(int)
#get color-specturm value
- if data["3311"][0].hasKey("5711"):
- state.lightColorTemperature = some(data["3311"][0]["5711"].getInt)
+ if data[DeviceLightbulb][0].hasKey(ParameterColorTemperature):
+ state.lightColorTemperature = some(data[DeviceLightbulb][0][ParameterColorTemperature].getInt)
else:
state.lightColorTemperature = none(int)
#determine type of bulb
if state.lightHue.isSome:
- state.lightSpectrum = TradfriLightSpectrum(0)
+ state.lightSpectrum = RGB
if state.lightColorTemperature.isSome:
- state.lightSpectrum = TradfriLightSpectrum(1)
+ state.lightSpectrum = White
else:
- state.lightSpectrum = TradfriLightSpectrum(2)
+ state.lightSpectrum = None
of Plug:
state = TradfriDeviceState(
kind: deviceType,
- plugPowered: parseBool($data["3312"][0]["5850"].getInt), # on / off
- plugDimmer: data["3312"][0]["5851"].getInt # 1 - 254 (but currently no dimmable plugs available)
+ plugPowered: parseBool($data[DevicePlug][0][ParameterPowerState].getInt), # on / off
+ plugDimmer: data[DevicePlug][0][ParameterDimmerValue].getInt # 1 - 254 (but currently no dimmable plugs available)
)
of motionSensor:
@@ -95,8 +95,8 @@ proc parseTradfriDevice (data: JsonNode): TradfriDevice =
of Blind:
state = TradfriDeviceState(
kind: deviceType,
- blindPosition: data["3"]["5536"].getFloat,
- blindTrigger: data["3"]["5523"].getFloat
+ blindPosition: data["3"][ParameterBlindPosition].getFloat,
+ blindTrigger: data["3"][ParameterBlindTrigger].getFloat
)
of soundRemote:
@@ -108,11 +108,11 @@ proc parseTradfriDevice (data: JsonNode): TradfriDevice =
return TradfriDevice(
`type`: deviceType,
- id: data["9003"].getInt,
- name: data["9001"].getStr,
- alive: data["9019"].getBool,
- createdAt: data["9002"].getInt,
- lastSeen: data["9020"].getInt,
+ id: data[ParameterDeviceId].getInt,
+ name: data[ParameterDeviceName].getStr,
+ alive: data[ParameterDeviceAlive].getBool,
+ createdAt: data[ParameterDeviceCreatedAt].getInt,
+ lastSeen: data[ParameterDeviceLastSeen].getInt,
state: state,
info: TradfriDeviceInfo(
manufacturer: data["3"]["0"].getStr,
@@ -127,89 +127,88 @@ proc parseTradfriDevice (data: JsonNode): TradfriDevice =
proc operateDevice* (device: TradfriDevice, action: TradfriDeviceAction): bool =
var requestParams = %* {}
- template CheckDeviceType(typeId: int) =
+ template CheckDeviceType(typeId: TradfriDeviceType) =
if device.`type` != TradfriDeviceType(typeId):
raise newException(ValueError, "Wrong action for this Devicetype")
-
case action.kind:
of LightSetPowerState:
- CheckDeviceType(2)
+ CheckDeviceType(Lightbulb)
- requestParams.add(devices["Lightbulb"],%* [{
- parameters["PowerState"]: boolToInt(action.lightPowerState),
- parameters["TransitionTime"]: action.transitionTime
+ requestParams.add(DeviceLightbulb, %* [{
+ ParameterPowerState: boolToInt(action.lightPowerState),
+ ParameterTransitionTime: action.transitionTime
}])
of LightSetBrightness:
- CheckDeviceType(2)
+ CheckDeviceType(Lightbulb)
- requestParams.add(devices["Lightbulb"], %* [{
- parameters["DimmerValue"]: action.lightBrightness,
- parameters["TransitionTime"]: action.transitionTime
+ requestParams.add(DeviceLightbulb, %* [{
+ ParameterDimmerValue: action.lightBrightness,
+ ParameterTransitionTime: action.transitionTime
}])
of LightSetColorHex:
- CheckDeviceType(2)
+ CheckDeviceType(Lightbulb)
- requestParams.add(devices["Lightbulb"], %* [{
- parameters["ColorHex"]: action.lightColorHex,
- parameters["TransitionTime"]: action.transitionTime
+ requestParams.add(DeviceLightbulb, %* [{
+ ParameterColorHex: action.lightColorHex,
+ ParameterTransitionTime: action.transitionTime
}])
of LightSetColorXY:
- CheckDeviceType(2)
+ CheckDeviceType(Lightbulb)
- requestParams.add(devices["Lightbulb"], %* [{
- parameters["ColorX"]: action.lightColorX,
- parameters["ColorY"]: action.lightColorY,
- parameters["TransitionTime"]: action.transitionTime
+ requestParams.add(DeviceLightbulb, %* [{
+ ParameterColorX: action.lightColorX,
+ ParameterColorY: action.lightColorY,
+ ParameterTransitionTime: action.transitionTime
}])
of LightSetHueSaturation:
- CheckDeviceType(2)
+ CheckDeviceType(Lightbulb)
- requestParams.add(devices["Lightbulb"], %* [{
- parameters["Hue"]: action.lightHue,
- parameters["Saturatio"]: action.lightSaturation,
- parameters["TransitionTime"]: action.transitionTime
+ requestParams.add(DeviceLightbulb, %* [{
+ ParameterHue: action.lightHue,
+ ParameterSaturation: action.lightSaturation,
+ ParameterTransitionTime: action.transitionTime
}])
of LightSetColorTemperature:
- CheckDeviceType(2)
+ CheckDeviceType(Lightbulb)
- requestParams.add(devices["Lightbulb"], %* [{
- parameters["ColorTemperature"]: action.lightColorTemperature,
- parameters["TransitionTime"]: action.transitionTime
+ requestParams.add(DeviceLightbulb, %* [{
+ ParameterColorTemperature: action.lightColorTemperature,
+ ParameterTransitionTime: action.transitionTime
}])
of PlugSetPowerState:
- CheckDeviceType(3)
+ CheckDeviceType(Plug)
- requestParams.add(devices["Plug"], %* [{
- parameters["PowerState"]: action.plugPowerState,
+ requestParams.add(DevicePlug, %* [{
+ ParameterPowerState: action.plugPowerState,
}])
of PlugSetDimmerValue:
- CheckDeviceType(3)
+ CheckDeviceType(Plug)
- requestParams.add(devices["Plug"], %* [{
- parameters["DimmerValue"]: action.plugDimmerValue,
+ requestParams.add(DevicePlug, %* [{
+ ParameterDimmerValue: action.plugDimmerValue,
}])
- discard makeCoapRequest(device.gatewayRef.host, device.gatewayRef.port, "put", device.gatewayRef.user, device.gatewayRef.pass, endpoints["devices"] & $device.id, requestParams)
+ discard makeCoapRequest(device.gatewayRef.host, device.gatewayRef.port, "put", device.gatewayRef.user, device.gatewayRef.pass, EndpointDevices & $device.id, requestParams)
proc getDevice* (gatewayRef: TradfriGatewayRef, deviceId: int): TradfriDevice =
- let request = makeCoapRequest(gatewayRef.host, gatewayRef.port, "get", gatewayRef.user, gatewayRef.pass, endpoints["devices"] & $deviceId, %* {})
+ let request = makeCoapRequest(gatewayRef.host, gatewayRef.port, "get", gatewayRef.user, gatewayRef.pass, EndpointDevices & $deviceId, %* {})
result = parseTradfriDevice(request)
result.gatewayRef = gatewayRef
proc getDevices* (gatewayRef: TradfriGatewayRef): seq[TradfriDevice] =
- let request = makeCoapRequest(gatewayRef.host, gatewayRef.port, "get", gatewayRef.user, gatewayRef.pass, endpoints["devices"], %* {})
+ let request = makeCoapRequest(gatewayRef.host, gatewayRef.port, "get", gatewayRef.user, gatewayRef.pass, EndpointDevices, %* {})
result = newSeq[TradfriDevice]()
diff --git a/types.nim b/types.nim
index 60008c6..eefd104 100644
--- a/types.nim
+++ b/types.nim
@@ -83,10 +83,10 @@ type
lightPowerState*: bool
of LightSetBrightness:
- lightBrightness*: bool
+ lightBrightness*: int
of LightSetColorHex:
- lightColorHex*: string
+ lightColorHex*: string
of LightSetColorXY:
lightColorX*: int