mtui 0.7.3

An extensive Modbus client for your terminal.
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
use crate::app::PinnedRegisters;
use crate::custom::{CustomOp, CustomRepr, CustomRule, EnumEntry, OpKind};
use crate::input::KeyCode;
use crate::modbus::{DeviceConfig, Interface};
use crate::register::RegisterType;
use crate::state::ReadPanel;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default)]
pub struct Config {
    pub name: String,
    pub device: DeviceConfig,
    pub startup: Startup,
    pub interpretations: InterpretorConfig,
    pub registers_batch: u16,
    pub update_interval_ms: Option<u64>,
    pub graph_history_cap: u16,
    pub matrix_cols: u16,
    pub read_only: bool,
    pub log_writes: bool,
    pub ignore_dirty: bool,
    pub show_clock: bool,
    pub show_frame_time: bool,
    pub cycle_types: CycleTypes,
    pub port: Option<u16>,
    pub allow_api_slave_id: bool,
    pub pinned_registers: PinnedRegisters,
    pub labels: Labels,
    pub custom_rules: CustomRules,
    pub keybinds: Keybinds,
}

macro_rules! keybinds {
    ($($action:ident => $field:ident : $label:literal = $default:ident),+ $(,)?) => {
        #[derive(Clone, Copy, Debug, Deserialize, Serialize)]
        #[serde(default)]
        pub struct Keybinds {
            $(pub $field: KeyCode,)+
        }

        impl Default for Keybinds {
            fn default() -> Self {
                use crate::constants::keybind;
                Self { $($field: keybind::$default,)+ }
            }
        }

        impl Keybinds {
            pub fn get(&self, action: KeybindAction) -> KeyCode {
                match action {
                    $(KeybindAction::$action => self.$field,)+
                }
            }

            pub fn set(&mut self, action: KeybindAction, key: KeyCode) {
                match action {
                    $(KeybindAction::$action => self.$field = key,)+
                }
            }
        }

        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
        pub enum KeybindAction {
            $($action,)+
        }

        impl KeybindAction {
            pub const ALL: &'static [KeybindAction] = &[$(KeybindAction::$action),+];

            pub fn label(self) -> &'static str {
                match self {
                    $(KeybindAction::$action => $label,)+
                }
            }
        }
    };
}

keybinds! {
    Exit => exit : "Quit" = EXIT,
    Pin => pin : "Add/remove pin" = PIN,
    Dump => dump : "Dump read data" = DUMP,
    Help => help : "Help" = HELP,
    About => about : "About" = ABOUT,
    Refresh => refresh : "Refresh" = REFRESH,
    Toggle => toggle : "Switch register type" = TOGGLE,
    Write => write : "Write register" = WRITE,
    Jump => jump : "Go to address/label" = JUMP,
    Label => label : "Label register" = LABEL,
    Custom => custom : "Custom rule" = CUSTOM,
    Columns => columns : "Toggle columns" = COLUMNS,
    Pause => pause : "Pause/resume" = PAUSE,
    WordOrder => word_order : "Cycle word order" = WORD_ORDER,
    Slave => slave : "Set slave id" = SLAVE,
    CyclePosition => cycle_position : "Previous position" = CYCLE_POSITION,
    Inspect => inspect : "Inspect register" = INSPECT,
    DeviceId => device_id : "Device identification" = DEVICE_ID,
    Raw => raw : "Raw function call" = RAW,
    Graph => graph : "Value graph" = GRAPH,
    Discovery => discovery : "Switch device" = DISCOVERY,
    Settings => settings : "Settings" = SETTINGS,
    CopyAddress => copy_address : "Copy address" = COPY_ADDRESS,
    Logs => logs : "View write logs" = LOGS,
    AppLogs => app_logs : "App logs" = APP_LOGS,
    Stats => stats : "Statistics" = STATS,
    Sweep => sweep : "Sweep" = SWEEP,
    Clear => clear : "Clear session data" = CLEAR,
    SwitchView => switch_view : "Cycle panel" = SWITCH_VIEW,
    Action => action : "Read / confirm" = ACTION,
    MoveUp => move_up : "Move up" = MOVE_UP,
    MoveDown => move_down : "Move down" = MOVE_DOWN,
    PageUp => page_up : "Page up" = PAGE_UP,
    PageDown => page_down : "Page down" = PAGE_DOWN,
}

impl Keybinds {
    pub fn action_for(&self, code: KeyCode) -> Option<KeybindAction> {
        KeybindAction::ALL
            .iter()
            .copied()
            .find(|&action| self.get(action) == code)
    }
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct CustomRules {
    pub holdings: Vec<CustomRule>,
    pub inputs: Vec<CustomRule>,
    pub coils: Vec<CustomRule>,
    pub discretes: Vec<CustomRule>,
    pub show_continuation: bool,
}

#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct Startup {
    pub address: u16,
    #[serde(rename = "type")]
    pub register_type: RegisterType,
    pub panel: ReadPanel,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(default)]
pub struct Labels {
    pub holdings: Vec<Label>,
    pub inputs: Vec<Label>,
    pub coils: Vec<Label>,
    pub discretes: Vec<Label>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Label {
    #[serde(rename = "i")]
    pub address: u16,
    #[serde(rename = "t")]
    pub text: String,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(default)]
pub struct CycleTypes {
    pub holdings: bool,
    pub inputs: bool,
    pub coils: bool,
    pub discretes: bool,
}

impl Default for CycleTypes {
    fn default() -> Self {
        Self {
            holdings: true,
            inputs: true,
            coils: true,
            discretes: true,
        }
    }
}

impl CycleTypes {
    pub fn enabled(&self, register_type: RegisterType) -> bool {
        match register_type {
            RegisterType::Holding => self.holdings,
            RegisterType::Input => self.inputs,
            RegisterType::Coil => self.coils,
            RegisterType::Discrete => self.discretes,
        }
    }

    pub fn toggle(&mut self, register_type: RegisterType) {
        match register_type {
            RegisterType::Holding => self.holdings = !self.holdings,
            RegisterType::Input => self.inputs = !self.inputs,
            RegisterType::Coil => self.coils = !self.coils,
            RegisterType::Discrete => self.discretes = !self.discretes,
        }
    }

    pub fn enabled_count(&self) -> usize {
        RegisterType::ALL
            .iter()
            .filter(|&&t| self.enabled(t))
            .count()
    }
}

impl Config {
    pub fn display_device(&self) -> String {
        match &self.device.interface {
            Interface::Mock => "Mock".to_string(),
            Interface::Wired(p) => format!("Wired {} ({})", p.path, p.baud_rate),
            Interface::Network(p) => format!("Network: {}:{}", p.ip, p.port),
        }
    }
}

fn label(address: u16, text: &str) -> Label {
    Label {
        address,
        text: text.to_string(),
    }
}

fn scaled(address: u16, repr: CustomRepr, div: f64, decimals: u8, suffix: &str) -> CustomRule {
    CustomRule {
        address,
        repr,
        ops: vec![CustomOp {
            op: OpKind::Div,
            v: div,
        }],
        decimals: Some(decimals),
        suffix: suffix.to_string(),
        ..Default::default()
    }
}

fn plain(address: u16, repr: CustomRepr, decimals: Option<u8>, suffix: &str) -> CustomRule {
    CustomRule {
        address,
        repr,
        decimals,
        suffix: suffix.to_string(),
        ..Default::default()
    }
}

fn switch(address: u16, entries: &[(i64, &str)]) -> CustomRule {
    CustomRule {
        address,
        repr: CustomRepr::U16,
        enum_map: entries
            .iter()
            .map(|&(value, text)| EnumEntry {
                value,
                text: text.to_string(),
            })
            .collect(),
        ..Default::default()
    }
}

fn demo_labels() -> Labels {
    Labels {
        holdings: vec![
            label(0, "model (ascii)"),
            label(8, "fw version (bcd)"),
            label(9, "serial (u32)"),
            label(11, "slave id"),
            label(12, "uptime (u32 s)"),
            label(50, "set: voltage"),
            label(51, "set: current"),
            label(52, "set: ripple"),
            label(53, "set: noise"),
            label(54, "set: time scale"),
            label(1000, "energy (u32 Wh)"),
            label(1002, "on-time (u32 s)"),
            label(1004, "write count"),
            label(1005, "energy (m10k)"),
            label(1100, "status bits"),
            label(1101, "alarm count"),
        ],
        inputs: vec![
            label(0, "voltage L1"),
            label(1, "voltage L2"),
            label(2, "voltage L3"),
            label(3, "current L1"),
            label(4, "current L2"),
            label(5, "current L3"),
            label(6, "frequency"),
            label(7, "temperature"),
            label(8, "active power (f32)"),
            label(10, "power factor (f32)"),
            label(12, "apparent (u32 VA)"),
            label(14, "reactive (i32 var)"),
            label(16, "energy (m10k)"),
            label(20, "energy (f64 kWh)"),
            label(30, "seconds"),
            label(31, "sawtooth"),
            label(32, "square"),
            label(33, "noise"),
            label(34, "random walk"),
        ],
        coils: vec![
            label(0, "main breaker"),
            label(1, "phase L1 enable"),
            label(2, "phase L2 enable"),
            label(3, "phase L3 enable"),
            label(4, "maintenance bypass"),
            label(5, "auto mode"),
        ],
        discretes: vec![
            label(0, "device ready"),
            label(1, "grid present"),
            label(2, "warning"),
            label(3, "heartbeat"),
            label(7, "noise enabled"),
            label(8, "fault"),
        ],
    }
}

fn demo_rules() -> CustomRules {
    CustomRules {
        holdings: vec![
            scaled(50, CustomRepr::U16, 10.0, 1, " V"),
            scaled(51, CustomRepr::U16, 100.0, 2, " A"),
            switch(53, &[(0, "off"), (1, "on")]),
            plain(54, CustomRepr::U16, None, " %"),
            scaled(1000, CustomRepr::U32, 1000.0, 2, " kWh"),
        ],
        inputs: vec![
            scaled(0, CustomRepr::U16, 10.0, 1, " V"),
            scaled(1, CustomRepr::U16, 10.0, 1, " V"),
            scaled(2, CustomRepr::U16, 10.0, 1, " V"),
            scaled(3, CustomRepr::U16, 100.0, 2, " A"),
            scaled(4, CustomRepr::U16, 100.0, 2, " A"),
            scaled(5, CustomRepr::U16, 100.0, 2, " A"),
            scaled(6, CustomRepr::U16, 100.0, 2, " Hz"),
            scaled(7, CustomRepr::I16, 10.0, 1, " C"),
            plain(8, CustomRepr::F32, Some(2), " kW"),
            plain(10, CustomRepr::F32, Some(2), " pf"),
            plain(12, CustomRepr::U32, None, " VA"),
            plain(14, CustomRepr::I32, None, " var"),
            switch(32, &[(0, "low"), (1, "high")]),
        ],
        coils: vec![
            switch(0, &[(0, "open"), (1, "closed")]),
            switch(4, &[(0, "normal"), (1, "bypass")]),
        ],
        discretes: vec![
            switch(0, &[(0, "no"), (1, "yes")]),
            switch(8, &[(0, "ok"), (1, "FAULT")]),
        ],
        show_continuation: true,
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            name: "demo".to_string(),
            device: DeviceConfig::default(),
            startup: Startup {
                address: 5,
                register_type: RegisterType::Input,
                panel: ReadPanel::Main,
            },
            interpretations: InterpretorConfig::default(),
            registers_batch: 10,
            update_interval_ms: Some(1000),
            graph_history_cap: 180,
            matrix_cols: 10,
            read_only: false,
            log_writes: false,
            ignore_dirty: false,
            show_clock: true,
            show_frame_time: false,
            cycle_types: CycleTypes::default(),
            port: None,
            allow_api_slave_id: false,
            pinned_registers: Default::default(),
            labels: Labels::default(),
            custom_rules: CustomRules::default(),
            keybinds: Keybinds::default(),
        }
    }
}

impl Config {
    pub fn demo() -> Self {
        Self {
            labels: demo_labels(),
            custom_rules: demo_rules(),
            ..Self::default()
        }
    }
}

macro_rules! interpretation_columns {
    ($($variant:ident => $field:ident : $name:literal = $default:literal),+ $(,)?) => {
        #[derive(Clone, Debug, Deserialize, Serialize)]
        #[serde(default)]
        pub struct InterpretorConfig {
            $(pub $field: bool,)+
        }

        impl Default for InterpretorConfig {
            fn default() -> Self {
                Self { $($field: $default,)+ }
            }
        }

        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
        pub enum Column {
            $($variant,)+
        }

        impl Column {
            pub const ALL: &'static [Column] = &[$(Column::$variant),+];

            pub fn name(self) -> &'static str {
                match self {
                    $(Column::$variant => $name,)+
                }
            }
        }

        impl InterpretorConfig {
            pub fn get(&self, column: Column) -> bool {
                match column {
                    $(Column::$variant => self.$field,)+
                }
            }

            pub fn toggle(&mut self, column: Column) {
                match column {
                    $(Column::$variant => self.$field = !self.$field,)+
                }
            }
        }
    };
}

interpretation_columns! {
    IndexHex => index_hex : "index (hex)" = false,
    U8s => u8s : "u8s" = false,
    I8s => i8s : "i8s" = false,
    U16 => u16 : "u16" = true,
    I16 => i16 : "i16" = true,
    F16 => f16 : "f16" = false,
    U32 => u32 : "u32" = false,
    I32 => i32 : "i32" = false,
    U32M10K => u32_m10k : "u32 m10k" = false,
    I32M10K => i32_m10k : "i32 m10k" = false,
    F32 => f32 : "f32" = false,
    F64 => f64 : "f64" = false,
    U64 => u64 : "u64" = false,
    I64 => i64 : "i64" = false,
    Hex => hex : "hex" = true,
    Hex32 => hex32 : "hex32" = false,
    Bcd => bcd : "bcd" = false,
    Bcd32 => bcd32 : "bcd32" = false,
    Bits => bits : "bits" = true,
    Ascii => ascii : "ascii" = true,
    Custom => custom : "custom" = true,
    Time => time : "time (read at)" = true,
    Ago => ago : "ago (read)" = false,
    Label => label : "label" = true,
}