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 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
import json, tables, options

type ActionType* = enum
  SwitchStateAction,
  SetBrightnessAction,
  SetColorXYAction,
  SetColorTemperatureAction,
  GetClientConfigAction,
  SetSubscriptionStateAction

type Action* = object
  accessToken*: Option[string]
  deviceName*: string
  case type*: ActionType
  of SwitchStateAction:
    relay*: Option[uint8]
    state*: Option[bool]
    toggle*: Option[bool]
  of SetBrightnessAction:
    brightness*: uint8
  of SetColorXYAction:
    colorX*: float
    colorY*: float
  of SetColorTemperatureAction:
    colorTemperature*: int
  of GetClientConfigAction:
    configName*: string
  of SetSubscriptionStateAction:
    subscribed*: bool

type DeviceType* = enum
  PowerMeter,
  RelayBoard,
  LacrosseTempSensor,
  Zigbee2MqttLamp,
  Zigbee2MqttRelay,
  Zigbee2MqttRemote,
  Zigbee2MqttMotionSensor

type Zigbee2MqttLampType* = enum
  RGB,
  WhiteSpectrum
  Switchable 

type DeviceState* = object
  lastUpdated*: Option[int64]
  case type*: DeviceType
  of PowerMeter:
    power*: float32
    cosphi*: float32
    voltage*: float32
    `import`*: float32
    frequency*: float32
  of RelayBoard:
    relays*: seq[bool]
  of LacrosseTempSensor:
    humidity*: float32
    temperature*: float32
    weakBattery*: bool
  of Zigbee2MqttLamp:
    lampType*: Zigbee2MqttLampType
    lampState*: bool
    lampBrightness*:  int
    lampColorX*: float
    lampColorY*: float
    lampColorTemperature*: int
    lampLinkquality*: int
  of Zigbee2MqttRelay:
    relayState*: bool
    relayLinkquality*: int
  else:
    unusedValue: Option[bool]

type DeviceConfig* = object
  address*: Option[uint8]
  deviceName*: Option[string]
  case type*: DeviceType
  of PowerMeter:
    model*: string
  of RelayBoard:
    firstRegister*: uint8
    count*: uint8
  of LacrosseTempSensor:
    id*: string
  of Zigbee2MqttLamp:
    lampType*: Zigbee2MqttLampType
  of Zigbee2MqttRemote:
    actions*: Table[string, seq[Action]]
  of Zigbee2MqttMotionSensor:
    occupyActions*: seq[Action]
    clearActions*: seq[Action]
  else:
    unusedValue: Option[bool]



type ModbusConfig* = object
  host*: string
  port*: uint16

type MqttConfig* = object
  host*:     string
  port*:     int
  username*: Option[string]
  password*: Option[string]

type LacrosseConfig* = object
  host*: string
  port*: uint16

type InfluxConfig* = object
  host*:                string
  port*:                int
  authToken*:           Option[string]
  powermetersDatabase*: Option[string]
  sensorsDatabase*:     Option[string]

type SmartiedConfig* = object
  frontendPort*: uint16
  modbus*: Option[ModbusConfig]
  mqtt*: Option[MqttConfig]
  lacrosse*: Option[LacrosseConfig]
  influx*: Option[InfluxConfig]
  powermeterUpdateIntervalSec*: uint
  accessToken*: string

type Config* = object
  devices*: Table[string, DeviceConfig]
  clientConfigs*: Table[string, JsonNode]
  serverConfig*: SmartiedConfig

type Server* = object
  state*: Table[string, DeviceState]
  config*: Config

type ResponseStatus* = enum
  Err,
  Ok

type Response* = object
  status*: ResponseStatus
  data*: JsonNode

type LacrosseMessage* = object
  id*: string
  temp*: float32
  hum*: float32
  weakBatt*: int

type modbus* = pointer