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