ctucx.git: smartied

[nimlang] smarthome server

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 
91 
92 
93 
import asyncdispatch, json, tables, options
import types, vars, utils
import devices/[modbusRelayboard, zigbee2mqttLamp, zigbee2mqttRelay]

proc handleDeviceAction*(action: Action): Future[JsonNode] {.async.} =
  case action.type
  of SwitchStateAction:
    let config = server.config.devices[action.deviceName]

    if action.state.isNone and action.toggle.isNone:
      return JsonNode()

    if action.state.isSome and action.toggle.isSome:
      return JsonNode()

    if action.state.isSome:
      let state = action.state.get

      case config.type
      of RelayBoard:
        if action.relay.isNone:
          return JsonNode()
        else:
          await setModbusRelayState(action.deviceName, action.relay.get, state)
          broadcastServerState()
          return JsonNode()

      of Zigbee2MqttLamp:
        await setZigbee2MqttLampState(action.deviceName, state)
        return JsonNode()

      of Zigbee2MqttRelay:
        await setZigbee2MqttRelayState(action.deviceName, state)
        return JsonNode()

      else:
        return JsonNode()

    if action.toggle.isSome:
      case config.type
      of RelayBoard:
        if action.relay.isNone:
          return JsonNode()
        else:
          await toggleModbusRelayState(action.deviceName, action.relay.get)
          broadcastServerState()
          return JsonNode()

      of Zigbee2MqttLamp:
        await toggleZigbee2MqttLampState(action.deviceName)
        return JsonNode()

      of Zigbee2MqttRelay:
        await toggleZigbee2MqttRelayState(action.deviceName)
        return JsonNode()

      else:
        return JsonNode()

  of SetBrightnessAction:
    let config = server.config.devices[action.deviceName]

    if config.type != Zigbee2MqttLamp:
      return JsonNode()

    await setZigbee2MqttLampBrightness(action.deviceName, action.brightness)
    return JsonNode()

  of SetColorXYAction:
    let config = server.config.devices[action.deviceName]

    if config.type != Zigbee2MqttLamp:
      return JsonNode()

    await setZigbee2MqttLampColor(action.deviceName, action.colorX, action.colorY)
    return JsonNode()

  of SetColorTemperatureAction:
    let config = server.config.devices[action.deviceName]

    if config.type != Zigbee2MqttLamp:
      return JsonNode()

    await setZigbee2MqttLampColorTemperature(action.deviceName, action.colorTemperature)
    return JsonNode()

  of GetClientConfigAction:
    let clientConfig: JsonNode = server.config.clientConfigs[action.configName]
    return clientConfig

  else:
    return JsonNode()