ctucx.git: solax2mqtt

Publish data from Solax solar inverters via MQTT

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 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 import std/asyncdispatch
import std/[httpclient, times]
import std/[tables, math, json]
import utils

type Operator = enum
  div10, div100, none,
  read16BitSignedDiv10,   read16BitSignedDiv100,   read16BitSignedNone
  read32BitUnsignedDiv10, read32BitUnsignedDiv100, read32BitUnsignedNone
  read32BitSignedDiv10  , read32BitSignedDiv100,   read32BitSignedNone

var inverterModel = {
    14: "X3-Hybrid G4",
    15: "X1-Hybrid G4",
    23: "X1-Hybrid G5"
  }.toTable

var inverterMode = {
    0:  "Waiting", 
    1:  "Checking",
    2:  "Normal",
    3:  "Off",
    4:  "Permanent Failure",
    5:  "Updating",
    6:  "EPS Check",
    7:  "EPS Mode",
    8:  "Self Test",
    9:  "Idle",
    10: "Standby"
  }.toTable

var batteryMode = {
    0: "Self Use Mode",
    1: "Force Time use",
    2: "Back Up Mode",
    3: "Feed-in Priority"
  }.toTable

var batteryStatus = {
    0: "Failure",
    1: "OK"
  }.toTable

proc convert (json: JsonNode, operator: Operator, a: int, b:int = 0): JsonNode =
  result = case operator 
    of div10:
      newJFloat(utils.round(json["Data"][a].getInt / 10, 1))
    of div100:
      newJFloat(utils.round(json["Data"][a].getInt / 100, 2))
    of none:
      json["Data"][a]

    of read16BitSignedDiv10:
      newJFloat(utils.round(
        read16BitSigned(
          json["Data"][a].getInt
        ) / 10
      , 1))
    of read16BitSignedDiv100:
      newJFloat(utils.round(
        read16BitSigned(
          json["Data"][a].getInt
        ) / 100
      , 2))
    of read16BitSignedNone:
      newJInt(
        read16BitSigned(
          json["Data"][a].getInt
        )
      )

    of read32BitUnsignedDiv10:
      newJFloat(utils.round(
        read32BitUnsigned(
          json["Data"][a].getInt,
          json["Data"][b].getInt
        ).float64 / 10.float64
      , 1))
    of read32BitUnsignedDiv100:
      newJFloat(utils.round(
        read32BitUnsigned(
          json["Data"][a].getInt,
          json["Data"][b].getInt
        ).float64 / 100.float64
      , 2))
    of read32BitUnsignedNone:
      newJInt(
        read32BitUnsigned(
          json["Data"][a].getInt,
          json["Data"][b].getInt
        )
      )


    of read32BitSignedDiv10:
      newJFloat(utils.round(
        read32BitSigned(
          json["Data"][a].getInt,
          json["Data"][b].getInt
        ).float64 / 10.float64
      , 1))
    of read32BitSignedDiv100:
      newJFloat(utils.round(
        read32BitSigned(
          json["Data"][a].getInt,
          json["Data"][b].getInt
        ).float64 / 100.float64
      , 2))
    of read32BitSignedNone:
      newJInt(
        read32BitSigned(
          json["Data"][a].getInt,
          json["Data"][b].getInt
        )
      )

proc parseInverterData* (json: JsonNode): JsonNode = 
  result = newJObject()
  result["model"]               = newJString(inverterModel[json["type"].getInt])
  result["registration_number"] = json["sn"]

  case json["type"].getInt:
    # X3-Hybrid G4
    of 14:
      result["serial_number"]        = json["Information"][2]
      result["firmware_version"]     = json["ver"]
      result["mode"]                 = newJString(inverterMode[json["Data"][19].getInt])

      # volts
      result["l1_voltage"]           = convert(json, div10, 0)
      result["l2_voltage"]           = convert(json, div10, 1)
      result["l3_voltage"]           = convert(json, div10, 2)
      result["eps_l1_voltage"]       = convert(json, div10, 23)
      result["eps_l2_voltage"]       = convert(json, div10, 23)
      result["eps_l3_voltage"]       = convert(json, div10, 25)
      result["pv1_voltage"]          = convert(json, div10, 10)
      result["pv2_voltage"]          = convert(json, div10, 11)

      # amps
      result["l1_current"]       = convert(json, div10, 3)
      result["l2_current"]       = convert(json, div10, 4)
      result["l3_current"]       = convert(json, div10, 5)
      result["eps_l1_current"]   = convert(json, div10, 26)
      result["eps_l2_current"]   = convert(json, div10, 27)
      result["eps_l3_current"]   = convert(json, div10, 28)
      result["pv1_current"]      = convert(json, div10, 12)
      result["pv2_current"]      = convert(json, div10, 13)

      # watts
      result["l1_power"]            = convert(json, none, 6)
      result["l2_power"]            = convert(json, none, 7)
      result["l3_power"]            = convert(json, none, 8)
      result["eps_l1_power"]        = convert(json, none, 29)
      result["eps_l2_power"]        = convert(json, none, 30)
      result["eps_l3_power"]        = convert(json, none, 31)
      result["power"]               = convert(json, none, 9)
      result["pv1_power"]           = convert(json, none, 14)
      result["pv2_power"]           = convert(json, none, 15)
      result["pv_power"]            = newJInt(json["Data"][14].getInt + json["Data"][15].getInt)
      result["grid_in_power"]       = convert(json, read32BitSignedNone, 34, 35)
      result["power_total"]         = convert(json, read16BitSignedNone, 47)

      # hz
      result["l1_frequency"]     = convert(json, div100, 16)
      result["l2_frequency"]     = convert(json, div100, 17)
      result["l3_frequency"]     = convert(json, div100, 18)

      # kwh
      result["yield_energy_today"]    = convert(json, div10, 70)
      result["yield_energy_total"]    = convert(json, read32BitUnsignedDiv10, 68, 69)
      result["grid_out_energy_today"] = convert(json, div100, 92)
      result["grid_in_energy_today"]  = convert(json, div100, 90)
      result["grid_in_energy_total"]  = convert(json, read32BitUnsignedDiv100, 86, 87)
      result["energy_total"]          = convert(json, read32BitUnsignedDiv100, 88, 89)

    # X4-Hybrid G4
    # X4-Hybrid G5
    of 15, 23:
      result["serial_number"]         = json["Information"][2]
      result["firmware_version"]      = json["ver"]
      result["mode"]                  = newJString(inverterMode[json["Data"][10].getInt])

      # volts
      result["l1_voltage"]            = convert(json, div10, 0)
      result["eps_l1_voltage"]        = convert(json, div10, 0)
      result["pv1_voltage"]           = convert(json, div10, 4)
      result["pv2_voltage"]           = convert(json, div10, 5)

      # amps
      result["l1_current"]            = convert(json, read16BitSignedDiv10, 1)
      result["eps_l1_current"]        = convert(json, read16BitSignedDiv10, 1)
      result["pv1_current"]           = convert(json, div10, 6)
      result["pv2_current"]           = convert(json, div10, 7)

      # watts
      result["l1_power"]              = convert(json, none, 2)
      result["epsl1_power"]           = convert(json, none, 2)
      result["total_power"]           = convert(json, none, 2)
      result["pv1_power"]             = convert(json, none, 8)
      result["pv2_power"]             = convert(json, none, 9)
      result["pv_power"]              = newJInt(json["Data"][8].getInt + json["Data"][9].getInt)
      result["grid_in_power"]         = convert(json, read32BitSignedNone, 32, 33)

      # hz
      result["l1_frequency"]          = convert(json, div100, 3)

      # kwh
      result["grid_in_energy"]        = convert(json, read32BitUnsignedDiv100, 34, 35)
      result["yield_energy_today"]    = convert(json, div10, 13)
      result["yield_energy_total"]    = convert(json, read32BitUnsignedDiv10, 11, 12)
      result["energy_total"]          = convert(json, read32BitUnsignedDiv100, 36, 37)

    else:
      result["error"] = newJString("Unknown model")

proc parseBatteryData* (json: JsonNode): JsonNode = 
  result = newJObject()

  case json["type"].getInt:
    # X3-Hybrid G4
    of 14:
      result["mode"]               = newJString(batteryMode[json["Data"][168].getInt])
      result["status"]             = newJString(batteryStatus[json["Data"][45].getInt])
      result["temperature"]        = convert(json, read16BitSignedNone, 105)
      
      # volts
      result["voltage"]            = convert(json, div100, 39)

      # amps
      result["current"]            = convert(json, read16BitSignedDiv100, 40)

      # watts
      result["power"]              = convert(json, read16BitSignedNone, 41)

      # percent
      result["soc"]                = convert(json, none,   103)

      # kwh
      result["remaining_capacity"] = convert(json, div10, 106)

      # kwh
      result["discharge_today"]    = convert(json, div10, 78)
      result["charge_today"]       = convert(json, div10, 79)
      result["discharge_total"]    = convert(json, read32BitUnsignedDiv10, 74, 75)
      result["charge_total"]       = convert(json, read32BitUnsignedDiv10, 76, 77)

    # X1-Hybrid G4
    # X1-Hybrid G5
    of 15, 23:
      result["temperature"]        = convert(json, read16BitSignedNone, 17)
      
      # volts
      result["voltage"]            = convert(json, div100, 14)

      # amps
      result["current"]            = convert(json, div100, 15)

      # watts
      result["power"]              = convert(json, none, 16)

      # percent
      result["soc"]                = convert(json, none,   18)

      # kwh
      result["remaining_capacity"] = convert(json, div10, 106)

    else:
      result["error"] = newJString("Unknown model")

proc getSolaxData* (ip: string, password: string): Future[JsonNode] {.async.} =
  result = newJObject()

  result["last_update"] = newJInt(now().toTime.toUnix)

  try:
    let client = newAsyncHttpClient()
    let body   = "optType=ReadRealTimeData&pwd=" & password

    let response = await client.postContent("http://" & ip, body)
    let json     = parseJson(response)

    client.close()

    for key, value in parseInverterData(json):
      result["inverter_" & key] = value 

    for key, value in parseBatteryData(json):
      result["battery_" & key] = value 

  except:
    echo "Error[getSolaxData]:\n" & getCurrentExceptionMsg()
    result["error"] = newJString(getCurrentExceptionMsg())