forked from dev-lab/tmbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmbus.htm
332 lines (302 loc) · 8.44 KB
/
tmbus.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-3TNM6DDWPG"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-3TNM6DDWPG');
</script>
<!-- Copyright (c) 2023 Taras Greben <[email protected]>. All rights Reserved. -->
<meta charset=utf-8>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="Keywords" content="tmbus,mbus,M-Bus,OMS,datagram,telegram,decoder,library,javascript">
<meta name="Description" content="Live M-Bus datagram decoder (using tmbus javascript library).">
<style>
* {box-sizing:border-box}
html, body {color: #424242; background-color: #F4F4FF;}
body {font-family:Arial,sans-serif}
textarea {border:1px solid #234; background-color: white; padding:8px; font-family:"Courier New",monospace; resize:none; flex:0; width:100%}
table {max-width:50rem; width:100%; margin-left:auto; margin-right:auto}
h1 {color:#1e90ff}
td {min-width:0.5rem}
tr:nth-child(even) {background:#EBEBFF;}
th {background:#1e90ff; color:white}
#ld {position:absolute; left:50%; top:50%; border-radius:50%; border: 1rem solid #55EA; margin: -3rem 0 0 -3rem; -webkit-mask: linear-gradient(#FFF1, #000E, #FFF1); width: 6rem; height: 6rem; animation: spin 1s linear infinite}
.header {
display: flex;
position: fixed;
top: 0.2rem;
left: 0.2rem;
right: 0.2rem;
width: 100%;
height: 4rem;
overflow: hidden;
color: white;
padding: 0 0 0 0;
justify-content: center;
align-items: center;
}
.body {
position: absolute;
display: flex;
top: 4rem;
bottom: 1.4rem;
overflow: hidden;
}
.main {
padding-right: 8px;
width: 100%;
height: 100%;
overflow: auto;
}
.footer {
position: fixed;
bottom: 0;
width: 100%;
height: 1rem;
text-align: center;
font-size: 0.8rem;
}
@keyframes spin {0% {transform: rotate(0deg)} 100% {transform: rotate(360deg)}}
@media only screen and (max-width:30rem) {body {margin:0.2rem} .main {padding-right:0.2rem}}
</style>
<title>Live M-Bus datagram decoder</title>
<script src="tmbus.min.js"></script>
<script>
var H = ["Storage", "Device", "Tariff"];
function tMbus(data, u, filter) {
var raw = tmbus(data);
unitConv(u, function(d) {
if(d.unit == "days" && d.value > 364) {
d.value = p10(Math.round(d.value/36.5), -1);
d.unit = "years";
}
}).process(raw);
var m = [0, 0, 0], ml = ln(H) - 1, r = {h: {}, s: {}};
function c(v, i, h) {
if(!v[i = i || 0]) {
v[i] = h < ml ? {} : [];
if(i > m[h]) m[h] = i;
}
return v[i];
}
function addV(v) {
c(c(c(r.s, v.storage, 0), v.device, 1), v.tariff, 2).push(v);
}
function a0(v, h) {
if(ln(v)) for(var i in v) if(v[i].value) return 0;
return 1;
}
function v0(v, h) {
var e = h < ml ? v0 : a0, d = [], c = 0, i;
for(i in v) {
++c;
if(e(v[i], h + 1)) d.push(i);
else if(m[h] < i) m[h] = i;
}
if(ln(d) == c) return 1;
for(i in d) delete v[d[i]];
return 0;
}
for(var n in raw) {
var v = raw[n];
if(n == "data") for(var i in v) addV(v[i]);
else r.h[n] = v;
}
if(filter) {
m = [0, 0, 0];
v0(r.s, 0);
}
r.m = m;
return r;
}
function vT(v) {
var r = [v.type];
if(ln(v.typeE)) r.push("(" + v.typeE.join(" ") + ")");
if(v.func && v.func != "Instantaneous") r.push("(" + v.func + ")");
return r.join(" ");
}
function renderState(c, plain) {
var h = [], u = {};
if(c) {
var n = c.unit, i, t;
if(n) for(i in n) {
t = n[i];
if(t.l && t.m) u[t.l] = t.m;
}
}
function p(d) {h.push(d);}
function e(v) {return hTd(escH(v));}
function cs(i) {
return i > 0 ? hA("colspan", i) : null;
}
function r(i, v, t) {
var s = t - i;
p(hTr((i > 0 ? hTd("", cs(i)) : "") + hTd(hT(v, "b", hA("style", "color:#1e90ff;")), s > 1 ? cs(s) : null)));
}
function s(v, m, l) {
var i, t;
for(i in v) {
t = v[i];
if(l < ln(H)) {
if(m[l]) r(l, H[l] + " " + i, 5);
s(t, m, l + 1);
} else p(hTr(hTd("", cs(3)) + e(vT(t)) + e(t.value + oa(t.unit))));
}
}
function rM(d, plain) {
p("<table>");
p(hTr(hT(d.l, "th", cs(5))));
p(hTr(sNc(hTd(), 5)));
if(!d.v) p(hTr(hTd(d.e || "Error", cs(5))));
else try {
if(plain) {
var m = tmbus(d.v), i, v;
for(i in m) {
v = m[i];
if(i == "data") {
p(hTr(hTd(i, cs(3)) + hTd("", cs(2))));
for(var d in v) {
var dv = v[d];
p(hTr(hTd("") + hTd(i + "[" + d + "]", cs(2)) + hTd("", cs(2))));
for(var dvn in dv) {
p(hTr(hTd("", cs(3)) + e(dvn) + e(dv[dvn])));
}
}
} else {
p(hTr(hTd("", cs(3)) + e(s2S(i)) + e(v)));
}
}
} else {
var m = tMbus(d.v, u, 1), i, v;
for(i in m.h) if(["c", "ci", "l", "fixed", "status", "len"].indexOf(i) < 0) p(hTr(hTd("", cs(3)) + e(s2S(i)) + e(m.h[i])));
s(m.s, m.m, 0);
}
} catch(x) {
p(hTr(hTd("Parsing Error: " + x, cs(5))));
}
p("</table><br/>");
}
ge("ld").style.display = "none";
var a = c ? c.gpio : [], i, d;
for(i in a) {
d = a[i];
if(d && d.c == "meter") rM(d, plain);
}
var h = h.join("\n");
sh(ge("cmds"), h);
}
function updateState(v, plain) {
var cfg = v && v.length > 0 ? {"gpio":[{"l":"Decoded M-Bus data:","a":"0","t":"MB","c":"meter", v:v}]} : null;
renderState(cfg, plain);
}
function onBodyLoad() {
refresh();
}
function onDatagramPaste(event) {
event.stopPropagation();
event.preventDefault();
var clipboardData = event.clipboardData || window.clipboardData;
onPasteText(clipboardData.getData("text"));
}
function onPasteText(datagram) {
const MAX_LEN = 300;
var bytes = [];
if(typeof datagram === 'string' || datagram instanceof String) {
try {
bytes = hs2a(datagram);
} catch (e) {
var l = datagram.length;
if(l > MAX_LEN) l = MAX_LEN;
for(var i = 0; i < l; ++i) bytes.push(datagram.charCodeAt(i));
}
} else {
try {
if(datagram.length > MAX_LEN) {
bytes = datagram.slice(0, MAX_LEN);
} else {
bytes = datagram;
}
} catch(e) {}
}
var value = ba2hs(bytes, " ");
sv(ge("datagram"), value);
refresh();
}
function onDatagramDrop(event) {
event.preventDefault();
var data, file, dt = event.dataTransfer;
if(dt) {
if(dt.items) {
if(dt.items.length > 0) {
var item = dt.items[0];
if(item.kind == "file") {
file = item.getAsFile();
}
}
} else if(dt.files) {
if(dt.files.length > 0) {
file = dt.files[0];
}
}
if(file) {
if(file.type.indexOf("octet-stream") >= 0) {
file.arrayBuffer().then(b => onPasteText(new Uint8Array(b)));
} else {
file.text().then(t => onPasteText(t));
}
return;
}
if(!data) data = dt.getData("text");
if(data) onPasteText(data);
}
}
function refresh() {
updateState(gv(ge("datagram")), ge("plain").checked);
}
</script>
</head>
<body>
<div class="header">
<h1>Live M-Bus datagram decoder</h1>
</div>
<div class="body">
<div class="main">
<p><a href="https://m-bus.com/" target="_blank">M-Bus</a> is a standard for remote reading of consumption meters (e.g. water, heat, electricity meters, etc.).
As of today, the standard is maintained by <a href="https://oms-group.org/en/" target="_blank">OMS-Group</a>, and evolved to support other device types, including
sensors, detectors, controllers, etc.
</p><p>A data frame sent over M-Bus from one device to another is called a <b>datagram</b> (it is also referred as a <b>telegram</b> in M-Bus protocol specification).
This page allows you to decode M-Bus datagram using <a href="https://dev-lab.github.io/tmbus" target="_blank">tmbus</a> JavaScript (ES5) library.
The library is created primarily for <a href="https://dev-lab.github.io/esp-iot-mbus" target="_blank">embedded device</a>s with limited storage,
but despite its compactness, it is capable of decoding all data from consumption meters.
</p>
<label for="datagram">Enter M-Bus datagram (in HEX), or drag & drop data or file (either in binary or in HEX format) into the box below:</label>
<textarea id="datagram" onkeyup="refresh();" onpaste="onDatagramPaste(event);" ondrop="onDatagramDrop(event)" rows="10" ></textarea>
<input type="radio" class="radio" name="mbusFormat" value="group" id="group" onclick="refresh();" checked/>
<label for="group">Postprocess</label>
<input type="radio" class="radio" name="mbusFormat" value="plain" id="plain" onclick="refresh();"/>
<label for="plain">Raw</label>
<section>
<div id="ld" style="display:none">
</div>
<div id="cmds">
</div>
</section>
</div>
</div>
<div class="footer">
Copyright © 2023 Taras Greben.
</div>
<script>
window.onload = function() {
var j = document.createElement("script");
j.src = "ju0.js";
j.onload = onBodyLoad;
document.body.appendChild(j);
};
</script>
</body>
</html>