summaryrefslogblamecommitdiffstats
path: root/deviceHelpers.nim
blob: 46f74dd8feaed5b161973707fc3b432bf598493f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
             
                                    
 






                                                                 














                                                                

                                                    
                               








                                                            






                                                                    
 





                                                                
 
                                                                              




                                                  
 















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