brewdrivers 0.17.0

Brewkit drivers
Documentation
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
556
557
558
559
560
561
562
//! Validators for when the RTU is deserialized from the config file
//!
//! These are called on the RTU and return an Err([ModelError](crate::model::ModelError)) if
//! the RTU doesn't pass the test. It's another layer of validation on top of `serde_yaml`. This ensures
//! the values in the RTU are actually correct, not just that it's valid YAML syntax.
//!
//! `serde` takes care of making sure the proper values are present; only values in an `Option<>` or that provide a default can be missing.

use log::{error, info, warn};
use std::collections::HashMap;

use crate::controllers::Controller;

use super::{ModelError, RTU};

// Note that when an RTU generates, if it recieves an error from one of these methods,
// it will call log::error!() on it, then bubble up the error.

pub fn all_validators(rtu: &RTU) -> Result<(), ModelError> {
    devices_have_unique_ids(&rtu)?;
    id_has_no_whitespace(&rtu)?;
    serial_port_is_valid(&rtu)?;
    controller_baudrate_is_valid(&rtu)?;
    timeout_valid(&rtu)?;
    command_retries_valid(&rtu)?;
    retry_delay_valid(&rtu)?;
    Ok(())
}

/// Returns `Ok(())` if each device in the RTU has a unique ID
pub fn devices_have_unique_ids(rtu: &RTU) -> Result<(), ModelError> {
    let mut seen: HashMap<&String, bool> = HashMap::new();
    for device in &rtu.devices {
        if seen.get(&device.id).is_some() {
            return Err(ModelError::validation_error(
                &device.id,
                ("id", device.id.as_str()),
                "devices must have unique IDs across all RTUs",
            ));
        }
        seen.insert(&device.id, true);
    }

    info!("RTU passed devices_have_unique_ids() validator");
    Ok(())
}

/// Returns `Ok(())` if the RTU ID and every device ID does not contain whitespace
pub fn id_has_no_whitespace(rtu: &RTU) -> Result<(), ModelError> {
    if rtu.id.contains(char::is_whitespace) {
        return Err(ModelError::validation_error(
            "RTU",
            ("id", &rtu.id),
            "RTU ID cannot contain whitespace",
        ));
    }

    for dev in &rtu.devices {
        if dev.id.contains(char::is_whitespace) {
            return Err(ModelError::validation_error(
                &dev.id,
                ("id", &dev.id),
                "device ID cannot contain whitespace",
            ));
        }
    }

    info!("RTU passed id_has_no_whitespace() validator");
    Ok(())
}

/// This will actually *not* fail if the serial port doesn't exist. Sometimes we disconnect
/// the cable and the port goes away, but it's still valid. Instead, it just checks that it's a
/// valid path in `/dev/`.
///
/// This will however print a `warn!()` statement if the port doesn't exist, if a logger is configured.
/// That will help if the brewer configures the wrong port or there's an electrical error.
pub fn serial_port_is_valid(rtu: &RTU) -> Result<(), ModelError> {
    for dev in &rtu.devices {
        // If they somehow pass an empty string
        // maybe with port: "" in the config file
        if dev.conn.port().len() == 0 {
            return Err(ModelError::validation_error(
                &dev.id,
                ("port", &dev.conn.port()),
                "serial port cannot be empty",
            ));
        }

        let path = &dev.conn.port;

        if !path.starts_with("/dev") {
            return Err(ModelError::validation_error(
                &dev.id,
                ("port", &dev.conn.port()),
                "port path must be in /dev/*",
            ));
        }

        match path.try_exists() {
            Ok(true) => {},
            Ok(false) => warn!("The serial port you configured is valid but does not currently exist. Are your cables plugged in?"),
            Err(e) => {
                error!("The port path you configured is hidden from me (or something similar). I can't determine if it exists or not.");
                error!("I'll let it slide this time since we're not using the serial port at this moment,
                        but maybe double check your serial port configuration");
                error!("{}", e);
            }
        }
    }

    info!("RTU passed serial_port_is_valid() validator");
    Ok(())
}

pub fn controller_baudrate_is_valid(rtu: &RTU) -> Result<(), ModelError> {
    use crate::controllers::{
        cn7500::CN7500_BAUDRATES, str1::STR1_BAUDRATES, wavesharev2::WAVESHAREV2_BAUDRATES,
    };
    for dev in &rtu.devices {
        match dev.conn.controller() {
            Controller::STR1 => {
                if !STR1_BAUDRATES.contains(dev.conn.baudrate()) {
                    return Err(ModelError::validation_error(
                        &dev.id,
                        ("baudrate", &format!("{}", dev.conn.baudrate())),
                        "invalid baudrate for STR1 controller",
                    ));
                }
            }
            Controller::CN7500 => {
                if !CN7500_BAUDRATES.contains(dev.conn.baudrate()) {
                    return Err(ModelError::validation_error(
                        &dev.id,
                        ("baudrate", &format!("{}", dev.conn.baudrate())),
                        "invalid baudrate for CN7500 controller",
                    ));
                }
            }
            Controller::Waveshare => {
                // This uses the same baudrates as Version 2
                if !WAVESHAREV2_BAUDRATES.contains(dev.conn.baudrate()) {
                    return Err(ModelError::validation_error(
                        &dev.id,
                        ("baudrate", &format!("{}", dev.conn.baudrate())),
                        "invalid baudrate for WaveshareV2 controller",
                    ));
                }
            }
            Controller::WaveshareV2 => {
                if !WAVESHAREV2_BAUDRATES.contains(dev.conn.baudrate()) {
                    return Err(ModelError::validation_error(
                        &dev.id,
                        ("baudrate", &format!("{}", dev.conn.baudrate())),
                        "invalid baudrate for WaveshareV2 controller",
                    ));
                }
            }
        }
    }

    info!("RTU passed controller_baudrate_is_valid() validator");
    Ok(())
}

pub fn timeout_valid(rtu: &RTU) -> Result<(), ModelError> {
    for dev in &rtu.devices {
        match dev.conn.timeout {
            // Not allowed
            (0..=15) => {
                return Err(ModelError::validation_error(
                    &dev.id,
                    ("timeout", &format!("{}ms", dev.conn.timeout)),
                    "Timeout cannot be lower than 16 ms",
                ));
            }
            // Allowed, but warn the user
            (16..=35) => {
                warn!(
                    "Timeout for device `{}` with controller type `{}` is low. This *might* work,
                    but you may experience device instability, especially under load.
                    Consider raising your timeout to 30ms or 40ms",
                    dev.name,
                    dev.conn.controller()
                );
                info!("I've tested the following to be reasonbly stable:");
                info!("STR1:\t\t17ms at baud 38400");
                info!("CN7500:\t\t36ms at baud 19200");
                info!("WaveshareV2:\t41ms at baud 38400");
            }
            _ => {}
        }
    }

    info!("RTU passed timeout_valid() validator");
    Ok(())
}

pub fn command_retries_valid(rtu: &RTU) -> Result<(), ModelError> {
    for device in &rtu.devices {
        match device.command_retries {
            0..=5 => {}
            _ => {
                return Err(ModelError::validation_error(
                    &device.id,
                    ("command_retries", &format!("{}", device.command_retries)),
                    "command retries must be in range [0, 6]",
                ))
            }
        }
    }

    info!("RTU passed command_retries_valid() validator");
    Ok(())
}

pub fn retry_delay_valid(rtu: &RTU) -> Result<(), ModelError> {
    for device in &rtu.devices {
        if device.retry_delay <= device.conn.timeout || device.retry_delay >= 2000 {
            return Err(ModelError::validation_error(
                &device.id,
                ("retry_delay", &format!("{}", device.retry_delay)),
                &format!(
                    "retry delay for this device must be in the range [{}, 2000] (units in ms)",
                    device.conn.timeout
                ),
            ));
        }
    }

    info!("RTU passed retry_delay_valid() validator");
    Ok(())
}

#[cfg(test)]
mod test_validators {
    use super::*;

    use std::{net::Ipv4Addr, str::FromStr};
    use tokio_test::{assert_err, assert_ok};

    use crate::model::{Device, RTU};

    // Just quickly sets up an RTU for testing purposes
    fn rtu(name: &str, id: &str, devices: Vec<Device>) -> RTU {
        RTU {
            name: String::from(name),
            id: String::from(id),
            ip_addr: Ipv4Addr::from_str("0.0.0.0").unwrap(),
            devices,
        }
    }

    // Quickly builds a device for testing purposes
    fn device(input: &str) -> Device {
        serde_yaml::from_str(input).unwrap()
    }

    #[test]
    fn test_devices_have_unique_ids() {
        let devices = vec![
            device(
                r#"
                id: pump
                name: Pump
                conn:
                    port: /dev/ttyUSB0
                    baudrate: 9600
                    timeout: 100
                    controller: STR1
                    controller_addr: 254
                    addr: 0
            "#,
            ),
            device(
                r#"
                id: pump
                name: Other pump with same ID
                conn:
                    port: /dev/ttyUSB0
                    baudrate: 9600
                    timeout: 100
                    controller: STR1
                    controller_addr: 254
                    addr: 1
            "#,
            ),
            device(
                r#"
                id: pump2
                name: Other pump with different ID
                conn:
                    port: /dev/ttyUSB0
                    baudrate: 9600
                    timeout: 100
                    controller: STR1
                    controller_addr: 254
                    addr: 2
            "#,
            ),
        ];

        let mut rtu = rtu("Testing RTU", "testing-id", devices);

        assert_err!(devices_have_unique_ids(&rtu));
        rtu.devices.remove(1);
        assert_ok!(devices_have_unique_ids(&rtu));
    }

    #[test]
    fn test_id_has_no_whitespace() {
        let devices = vec![device(
            r#"
                id: pump id with whitespace
                name: Pump
                conn:
                    port: /dev/ttyUSB0
                    baudrate: 9600
                    timeout: 100
                    controller: STR1
                    controller_addr: 254
                    addr: 2
            "#,
        )];

        let mut rtu = rtu("Testing RTU", "testing id with whitespace", devices);

        assert_err!(id_has_no_whitespace(&rtu));
        rtu.devices[0].id = String::from("something-without-whitespace");
        // Still an error because the RTU id has whitespace
        assert_err!(id_has_no_whitespace(&rtu));
        rtu.id = String::from("no-whitespace");
        assert_ok!(id_has_no_whitespace(&rtu));
    }

    #[test]
    fn test_serial_port_is_valid() {
        // This port may or may not exist, but it's valid
        let devices = vec![device(
            r#"
                id: pump
                name: Pump
                conn:
                    port: /dev/ttyUSB0
                    baudrate: 9600
                    timeout: 100
                    controller: STR1
                    controller_addr: 254
                    addr: 2
            "#,
        )];

        let mut rtu = rtu("testing RTU", "test-id", devices);

        assert_ok!(serial_port_is_valid(&rtu));

        // This port definitely doesn't exist, but it's still valid
        rtu.devices.push(device(
            r#"
            id: pump
            name: Pump
            conn:
                port: /dev/peepee_poopoo
                baudrate: 9600
                timeout: 100
                controller: STR1
                controller_addr: 254
                addr: 2
        "#,
        ));

        assert_ok!(serial_port_is_valid(&rtu));

        // This port is not valid (not in /dev)
        rtu.devices.push(device(
            r#"
            id: pump
            name: Pump
            conn:
                port: /etc/different
                baudrate: 9600
                timeout: 100
                controller: STR1
                controller_addr: 254
                addr: 2
        "#,
        ));

        assert_err!(serial_port_is_valid(&rtu));
    }

    #[test]
    fn test_baudrate() {
        let devices = vec![device(
            r#"
                id: pump
                name: Pump
                conn:
                    port: /dev/ttyUSB0
                    baudrate: 9600
                    timeout: 100
                    controller: STR1
                    controller_addr: 254
                    addr: 2
            "#,
        )];

        let mut rtu = rtu("testing RTU", "test-id", devices);

        assert_ok!(controller_baudrate_is_valid(&rtu));

        rtu.devices.push(device(
            r#"
            id: pump
            name: Pump
            conn:
                port: /dev/ttyUSB0
                baudrate: 9601
                timeout: 100
                controller: STR1
                controller_addr: 254
                addr: 2
        "#,
        ));

        assert_err!(controller_baudrate_is_valid(&rtu));
    }

    #[test]
    fn test_timeout_valid() {
        let devices = vec![device(
            r#"
                id: pump
                name: Pump
                conn:
                    port: /dev/ttyUSB0
                    baudrate: 9600
                    timeout: 100
                    controller: STR1
                    controller_addr: 254
                    addr: 2
            "#,
        )];

        let mut rtu = rtu("testing RTU", "test-id", devices);

        assert_ok!(timeout_valid(&rtu));

        // Timeout less than 20
        rtu.devices.push(device(
            r#"
            id: pump
            name: Pump
            conn:
                port: /dev/ttyUSB0
                baudrate: 9600
                timeout: 15
                controller: STR1
                controller_addr: 254
                addr: 2
        "#,
        ));

        assert_err!(timeout_valid(&rtu));
    }

    #[test]
    fn test_command_retries_valid() {
        let valid_device = device(
            r#"
            id: pump
            name: pump
            command_retries: 0
            conn:
                port: /dev/ttyUSB0
                baudrate: 9600
                timeout: 15
                controller: STR1
                controller_addr: 254
                addr: 2
            "#,
        );

        let invalid_device = device(
            r#"
            id: pump
            name: pump
            command_retries: 6
            conn:
                port: /dev/ttyUSB0
                baudrate: 9600
                timeout: 15
                controller: STR1
                controller_addr: 254
                addr: 2
            "#,
        );

        let undeserializable_device = device(
            r#"
            id: pump
            name: pump
            command_retries: 6
            conn:
                port: /dev/ttyUSB0
                baudrate: 9600
                timeout: 15
                controller: STR1
                controller_addr: 254
                addr: 2
            "#,
        );

        let rtu1 = rtu("Valid RTU", "testing-id", vec![valid_device]);
        assert_ok!(command_retries_valid(&rtu1));

        let rtu2 = rtu("Invalid RTU", "testing-id", vec![invalid_device]);
        assert_err!(command_retries_valid(&rtu2));

        let rtu3 = rtu("Invalid RTU", "testing-id", vec![undeserializable_device]);
        assert_err!(command_retries_valid(&rtu3));
    }

    #[test]
    fn test_retry_delay_valid() {
        let valid_device = device(
            r#"
            id: pump
            name: pump
            retry_delay: 400
            conn:
                port: /dev/ttyUSB0
                baudrate: 9600
                timeout: 15
                controller: STR1
                controller_addr: 254
                addr: 2
            "#,
        );

        let invalid_device = device(
            r#"
            id: pump
            name: pump
            retry_delay: 5000
            conn:
                port: /dev/ttyUSB0
                baudrate: 9600
                timeout: 15
                controller: STR1
                controller_addr: 254
                addr: 2
            "#,
        );

        let rtu1 = rtu("Valid RTU", "testing-id", vec![valid_device]);
        assert_ok!(retry_delay_valid(&rtu1));

        let rtu2 = rtu("Invalid RTU", "testing-id", vec![invalid_device]);
        assert_err!(retry_delay_valid(&rtu2));
    }
}