-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.gengo
More file actions
115 lines (102 loc) · 3.56 KB
/
Copy pathcli.gengo
File metadata and controls
115 lines (102 loc) · 3.56 KB
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
// cli.gengo — Command-line M-Bus decoder.
// Reads hex input from stdin and prints structured decode output.
mbus := import("./mbus")
std := import("std")
func print_diagnostics(diags []mbus.Diagnostic) {
for d in diags {
sv := d.severity
off_str := ""
if d.offset != null {
off_str = " @" + std.conv.to_string(d.offset)
}
std.io.eprintln(" [", sv, "] ", d.code, off_str, ": ", d.msg)
}
}
func print_frame(f mbus.Frame) {
std.io.println("Frame:")
std.io.println(" kind: ", f.kind)
std.io.println(" checksum: ", f.checksum)
std.io.println(" checksum_valid: ", f.checksum_valid)
if std.core.len(f.diagnostics) > 0 {
std.io.println(" diagnostics:")
print_diagnostics(f.diagnostics)
}
}
func print_variable_data(vd mbus.VariableDataTelegram) {
h := vd.header
std.io.println("Header:")
std.io.println(" identification: ", h.identification)
std.io.println(" manufacturer: ", h.manufacturer)
std.io.println(" version: ", h.version)
std.io.println(" medium: ", h.medium)
std.io.println(" access_number: ", h.access_number)
std.io.println(" status: ", h.status)
std.io.println(" signature: ", h.signature)
std.io.println("Records: (", std.core.len(vd.records), ")")
for i := 0; i < std.core.len(vd.records); i++ {
rec := vd.records[i]
vif := rec.vif
val := rec.value
std.io.println(" ", i + 1, ". ", vif.vif_kind)
std.io.println(" unit: ", vif.unit.name, " (", vif.unit.symbol, ")")
std.io.println(" value: ", val.value)
if val.scaled {
std.io.println(" scaled: true")
}
}
}
func print_fixed_data(fd mbus.FixedDataTelegram) {
h := fd.header
std.io.println("Header:")
std.io.println(" identification: ", h.identification)
std.io.println(" access_number: ", h.access_number)
std.io.println(" status: ", h.status)
if h.medium_unit != null {
mu := h.medium_unit
std.io.println(" medium: ", mu.medium)
std.io.println(" medium_code: ", mu.medium_code)
}
std.io.println("Counters: (", std.core.len(fd.counters), ")")
for i := 0; i < std.core.len(fd.counters); i++ {
c := fd.counters[i]
std.io.println(" ", i + 1, ". ", c.value, " ", c.unit.symbol, " (", c.unit.name, ")")
}
if fd.undecoded_data != "" {
std.io.println("Undecoded data: ", fd.undecoded_data)
}
}
// Read hex string from stdin (first line)
std.io.eprint("enter hex: ")
hex_line := std.io.readline()
if hex_line == null {
std.io.eprintln("usage: echo <hex> | gengo cli.gengo")
} else {
result := mbus.decode(hex_line)
print_frame(result.frame)
std.io.println()
if result.telegram != null {
t := result.telegram
std.io.println("Telegram:")
std.io.println(" kind: ", t.kind)
if t.body != null {
switch t.body {
case .variable_data as vd {
print_variable_data(vd)
}
case .fixed_data as fd {
print_fixed_data(fd)
}
default {
std.io.println(" (telegram body not yet printable)")
}
}
}
if std.core.len(t.diagnostics) > 0 {
std.io.println()
std.io.println("Telegram diagnostics:")
print_diagnostics(t.diagnostics)
}
} else {
std.io.println("No telegram decoded")
}
}