-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
555 lines (497 loc) · 15.4 KB
/
lib.rs
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use serde::Deserialize;
use serde_aux::prelude::*;
use thiserror::Error;
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
use tracing::{debug, error};
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
WebsocketErr(#[from] tungstenite::error::Error),
#[error(transparent)]
HttpErr(#[from] reqwest::Error),
// Thank you stranger https://github.com/dtolnay/thiserror/pull/175
#[error("{code}{}", match .message {
Some(msg) => format!(" - {}", &msg),
None => "".to_owned(),
})]
SungrowError { code: u16, message: Option<String> },
#[error(transparent)]
JSONError(#[from] serde_json::Error),
#[error("Expected attached data")]
ExpectedData,
#[error("No token")]
NoToken,
}
impl From<Error> for std::io::Error {
fn from(e: Error) -> Self {
use std::io::ErrorKind;
// TODO: Likely there are reasonable mappings from some of our errors to specific io Errors but, for now, this
// is just so tokio_modbus-winets can fail conveniently.
std::io::Error::new(ErrorKind::Other, e)
}
}
#[derive(Debug)]
pub struct Client {
http: reqwest::Client,
host: String,
token: String,
devices: Vec<Device>,
}
const WS_PORT: u16 = 8082;
type Result<T> = std::result::Result<T, Error>;
impl Client {
pub async fn new<H>(host: H) -> Result<Self>
where
H: Into<String>,
{
let host = host.into();
let ws_url = format!("ws://{}:{}/ws/home/overview", &host, WS_PORT);
use futures_util::SinkExt;
use futures_util::StreamExt;
let (mut ws, _) = connect_async(ws_url).await?;
ws.send(Message::Text(
serde_json::json!({"lang":"en_us","token":"","service":"connect"}).to_string(),
))
.await?;
// TODO: maintan WS connection, pinging and watching for updated tokens
let token = if_chain::if_chain! {
if let Some(Ok(Message::Text(msg))) = ws.next().await ;
if let Ok(value) = serde_json::from_str::<SungrowResult>(&msg);
if let Some(ResultData::WebSocketMessage(WebSocketMessage::Connect { token })) = value.data;
then {
debug!(token, "Got WiNet-S token");
token
} else {
// TODO: it might be that we get some other WS messages here that are fine so we might need to take a
// few WS messages to find the token.
return Err(Error::NoToken);
}
};
Self::new_with_token(host, token).await
}
pub async fn new_with_token<H>(host: H, token: String) -> Result<Self>
where
H: Into<String>,
{
let host = host.into();
let http = reqwest::Client::new();
let data: ResultData = parse_response(
http.post(format!("http://{}/inverter/list", &host))
.send()
.await?,
)
.await?;
if let ResultData::DeviceList(ResultList { items, .. }) = data {
Ok(Client {
token,
devices: items,
host,
http,
})
} else {
Err(Error::ExpectedData)
}
}
#[tracing::instrument(level = "debug")]
pub async fn read_register(
&self,
register_type: RegisterType,
address: u16,
count: u16,
) -> Result<Vec<u16>> {
// FIXME: find device by phys_addr
let device = &self.devices[0];
#[derive(serde::Serialize)]
struct Params {
#[serde(rename = "type")]
type_: u8,
dev_id: u8,
dev_type: u8,
dev_code: u16,
param_type: u8,
param_addr: u16,
param_num: u16,
}
let request = self.get("/device/getParam").query(&Params {
type_: 3,
dev_id: device.dev_id,
dev_type: device.dev_type,
dev_code: device.dev_code,
param_type: register_type.param(),
param_addr: address,
param_num: count,
});
let response = request.send().await?;
let result = parse_response(response).await?;
if let ResultData::GetParam { param_value } = result {
Ok(param_value)
} else {
Err(Error::ExpectedData)
}
}
#[tracing::instrument(level = "debug")]
pub async fn write_register(&self, address: u16, data: &[u16]) -> Result<()> {
if data.is_empty() {
return Err(Error::ExpectedData);
}
// FIXME: find device by phys_addr
let device = &self.devices[0];
use serde_json::json;
let body = json!({
"lang": "en_us",
"token": &self.token,
"dev_id": device.dev_id,
"dev_type": device.dev_type,
"dev_code": device.dev_code,
"param_addr": address.to_string(),
"param_size": data.len().to_string(),
"param_value": data[0].to_string(),
});
let request = self
.http
.post(format!("http://{}{}", &self.host, "/device/setParam"))
.json(&body);
let response = request.send().await?;
parse_response(response).await?;
Ok(())
}
pub async fn running_state(&self) -> Result<RunningState> {
let raw = *self
.read_register(RegisterType::Input, 13001, 1)
.await?
.first()
.ok_or(Error::ExpectedData)?;
let bits: RunningStateBits = raw.into();
let battery_state = if bits.intersects(RunningStateBits::BatteryCharging) {
BatteryState::Charging
} else if bits.intersects(RunningStateBits::BatteryDischarging) {
BatteryState::Discharging
} else {
BatteryState::Inactive
};
let trading_state = if bits.intersects(RunningStateBits::ImportingPower) {
TradingState::Importing
} else if bits.intersects(RunningStateBits::ExportingPower) {
TradingState::Exporting
} else {
TradingState::Inactive
};
Ok(RunningState {
battery_state,
trading_state,
generating_pv_power: bits.intersects(RunningStateBits::GeneratingPVPower),
positive_load_power: bits.intersects(RunningStateBits::LoadActive),
power_generated_from_load: bits.intersects(RunningStateBits::GeneratingPVPower),
state: bits,
})
}
fn get(&self, path: &str) -> reqwest::RequestBuilder {
self.http
.get(format!("http://{}{}", &self.host, path))
.query(&[("lang", "en_us"), ("token", self.token.as_str())])
}
}
#[derive(Debug)]
pub enum BatteryState {
Charging,
Discharging,
Inactive,
}
#[derive(Debug)]
pub enum TradingState {
Importing,
Exporting,
Inactive,
}
#[derive(Debug)]
pub struct RunningState {
state: RunningStateBits,
pub battery_state: BatteryState,
pub trading_state: TradingState,
pub generating_pv_power: bool,
pub positive_load_power: bool,
pub power_generated_from_load: bool,
}
impl RunningState {
pub fn raw(&self) -> RunningStateBits {
self.state
}
}
// See Appendix 1.2 of Sungrow modbus documentation for hybrid inverters
#[bitmask_enum::bitmask(u16)]
#[derive(Debug)]
pub enum RunningStateBits {
GeneratingPVPower = 0b00000001,
BatteryCharging = 0b00000010,
BatteryDischarging = 0b00000100,
LoadActive = 0b00001000,
LoadReactive = 0b00000000,
ExportingPower = 0b00010000,
ImportingPower = 0b00100000,
PowerGeneratedFromLoad = 0b0100000,
}
#[tracing::instrument(level = "debug")]
async fn parse_response<T>(response: reqwest::Response) -> Result<T>
where
Result<T>: From<SungrowResult>,
{
let body = response.text().await?;
debug!(%body, "parsing");
let sg_result = serde_json::from_slice::<SungrowResult>(body.as_bytes());
sg_result?.into()
}
#[derive(Debug)]
pub enum RegisterType {
Input,
Holding,
}
impl RegisterType {
fn param(&self) -> u8 {
match self {
Self::Input => 0,
Self::Holding => 1,
}
}
}
// {
// "id": 1,
// "dev_id": 1,
// "dev_code": 3343,
// "dev_type": 35,
// "dev_procotol": 2,
// "inv_type": 0,
// "dev_sn": "REDACTED",
// "dev_name": "SH5.0RS(COM1-001)",
// "dev_model": "SH5.0RS",
// "port_name": "COM1",
// "phys_addr": "1",
// "logc_addr": "1",
// "link_status": 1,
// "init_status": 1,
// "dev_special": "0",
// "list": []
// }
#[derive(Debug, Deserialize)]
struct Device {
dev_id: u8,
dev_code: u16,
// Available from `GET /device/getType`:
//
// {
// "result_code": 1,
// "result_msg": "success",
// "result_data": {
// "count": 5,
// "list": [{
// "name": "I18N_COMMON_STRING_INVERTER",
// "value": 1
// }, {
// "name": "I18N_COMMON_SOLAR_INVERTER",
// "value": 21
// }, {
// "name": "I18N_COMMON_STORE_INVERTER",
// "value": 35
// }, {
// "name": "I18N_COMMON_AMMETER",
// "value": 18
// }, {
// "name": "I18N_COMMON_CHARGING_PILE",
// "value": 46
// }]
// }
// }
//
// TODO: Extract into enum represented by underlying number?
dev_type: u8,
// unit/slave ID
#[allow(dead_code)]
#[serde(deserialize_with = "serde_aux::prelude::deserialize_number_from_string")]
phys_addr: u8,
// UNUSED:
//
// id: u8,
// dev_protocol: u8,
// dev_sn: String,
// dev_model: String,
// port_name: String,
// logc_address: String,
// link_status: u8,
// init_status: u8,
// dev_special: String,
// list: Option<Vec<()>> // unknown
}
#[test]
fn test_deserialize_device() {
let json = r#"{
"id": 1,
"dev_id": 1,
"dev_code": 3343,
"dev_type": 35,
"dev_procotol": 2,
"inv_type": 0,
"dev_sn": "REDACTED",
"dev_name": "SH5.0RS(COM1-001)",
"dev_model": "SH5.0RS",
"port_name": "COM1",
"phys_addr": "1",
"logc_addr": "1",
"link_status": 1,
"init_status": 1,
"dev_special": "0"
}"#;
let dev: Device = serde_json::from_str(json).unwrap();
assert!(matches!(
dev,
Device {
dev_id: 1,
dev_code: 3343,
dev_type: 35,
phys_addr: 1
}
));
}
#[derive(Debug, Deserialize)]
#[serde(tag = "service", rename_all = "lowercase")]
enum WebSocketMessage {
Connect { token: String },
// DeviceList { list: Vec<Device> },
// Not yet used:
// State, // system state
// Real, // real time info
// Notice, // on some error messages?
// Statistics,
// Runtime,
// Local,
// Fault,
// #[serde(rename = "proto_modbus104")]
// Modbus,
Other,
}
#[derive(Debug, Deserialize)]
struct ResultList<T> {
// count: u16,
#[serde(rename = "list")]
items: Vec<T>,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum ResultData {
// TODO: custom deserializer into words
GetParam {
#[serde(deserialize_with = "words_from_string")]
param_value: Vec<u16>,
},
DeviceList(ResultList<Device>),
WebSocketMessage(WebSocketMessage),
// // String = name - http://<host>/i18n/en_US.properties has the translations for these item names
// // i32 = value - unclear if this is always an int, so making this a JSON::Value for now
// GetType(ResultList<(String, serde_json::Value)>),
// Product {
// #[serde(rename = "product_name")]
// name: String,
// #[serde(rename = "product_code")]
// code: u8,
// },
Other,
}
#[test]
fn test_deserialize_get_param() {
let json = r#"{"param_value": "82 00 "}"#;
let data: ResultData = serde_json::from_str(json).unwrap();
assert!(matches!(data, ResultData::GetParam { .. }));
let json = r#"{
"result_code": 1,
"result_msg": "success",
"result_data": {
"param_value": "82 00 "
}
}"#;
let data: SungrowResult = serde_json::from_str(json).unwrap();
assert!(matches!(
data,
SungrowResult {
code: 1,
message: Some(m),
data: Some(ResultData::GetParam { .. })
} if m == "success"
));
}
// TODO: can I make this an _actual_ `Result<ResultData, SungrowError>`?
// - if code == 1, it is Ok(SungrowData), otherwise create error from code and message?
#[derive(Deserialize)]
struct SungrowResult {
// 1 = success
// 100 = hit user limit?
// {
// "result_code": 100,
// "result_msg": "normal user limit",
// "result_data": {
// "service": "notice"
// }
// }
#[serde(rename = "result_code")]
code: u16,
#[serde(rename = "result_msg")]
// http://<host>/i18n/en_US.properties has the translations for messages (only ones which start with I18N_*)
message: Option<String>, // at least one result I saw (code = 200 at the time) had no message :\
#[serde(rename = "result_data")]
data: Option<ResultData>,
}
impl From<SungrowResult> for Result<Option<ResultData>> {
fn from(sg_result: SungrowResult) -> Self {
match sg_result {
SungrowResult { code: 1, data, .. } => Ok(data),
SungrowResult { code, message, .. } => Err(Error::SungrowError { code, message }),
}
}
}
impl From<SungrowResult> for Result<ResultData> {
fn from(sg_result: SungrowResult) -> Self {
let data: Result<Option<ResultData>> = sg_result.into();
if let Some(data) = data? {
Ok(data)
} else {
Err(Error::ExpectedData)
}
}
}
impl From<SungrowResult> for Result<()> {
fn from(sg_result: SungrowResult) -> Self {
let data: Result<Option<ResultData>> = sg_result.into();
data.map(|_| ())
}
}
// WiNet-S returns data encoded as space-separated hex byte string. E.g.:
//
// "aa bb cc dd " (yes, including trailing whitespace)
//
// Modbus uses u16 "words" instead of bytes, and the data above should always represent this, so we can take groups
// of 2 and consume them as a hex-represented u16.
fn words_from_string<'de, D>(deserializer: D) -> std::result::Result<Vec<u16>, D::Error>
where
D: serde::Deserializer<'de>,
{
StringOrVecToVec::new(' ', |s| u8::from_str_radix(s, 16), true).into_deserializer()(
deserializer,
)
.map(|vec| {
vec.chunks_exact(2)
.map(|bytes| u16::from_be_bytes(bytes.try_into().unwrap()))
.collect()
})
}
#[test]
fn test_words_from_string() {
#[derive(serde::Deserialize, Debug)]
struct MyStruct {
#[serde(deserialize_with = "words_from_string")]
list: Vec<u16>,
}
let s = r#" { "list": "00 AA 00 01 00 0D 00 1E 00 0F 00 00 00 55 " } "#;
let a: MyStruct = serde_json::from_str(s).unwrap();
assert_eq!(
&a.list,
&[0x00AA, 0x0001, 0x000D, 0x001E, 0x000F, 0x0000, 0x0055]
);
}