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 
import colors
import deviceTypes, helpers, devices

proc renameDevice* (device: TradfriDevice, name: string): bool = 
  return device.operateDevice(TradfriDeviceAction(
      kind:            DeviceRename,
      deviceName:      name
    ))


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 =
  if device.`type` == Lightbulb:
    return device.operateDevice(TradfriDeviceAction(
      kind: LightSetPowerState,
      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(
      kind: LightSetBrightness,
      lightBrightness: brightness
  ))


proc setColorHex* (device: TradfriDevice, color: string): bool =
  return device.operateDevice(TradfriDeviceAction(
      kind: LightSetColorHex,
      lightColorHex: color
  ))


proc setColorXY* (device: TradfriDevice, colorX: float, colorY: float): bool =
  return device.operateDevice(TradfriDeviceAction(
      kind: LightSetColorXY,
      lightColorX: colorX,
      lightColorY: colorY
  ))


proc setColorXYfromHex* (device: TradfriDevice, color: string): bool =
  let color = extractRGB(parseColor(color))

  let x = (0.4124 * toFloat(color.r)) + (0.3576 * toFloat(color.g)) + (0.1805 * toFloat(color.b))
  let y = (0.2126 * toFloat(color.r)) + (0.7152 * toFloat(color.g)) + (0.0722 * toFloat(color.b))
  let z = (0.0193 * toFloat(color.r)) + (0.1192 * toFloat(color.g)) + (0.9505 * toFloat(color.b))

  let X = (x / (x + y + z))
  let Y = (y / (x + y + z))

  return device.operateDevice(TradfriDeviceAction(
      kind: LightSetColorXY,
      lightColorX: X,
      lightColorY: Y
  ))