Skip to content

Commit

Permalink
Format macaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
mskvortsov committed Apr 11, 2024
1 parent 4d198ec commit 5ff101e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions meshmon.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ function formatTime(v) {
return t.toISOString();
}

function formatHex(v, width) {
return v.toString(16).padStart(width, '0');
}

function formatId(v) {
return v.toString(16).padStart(8, '0');
return formatHex(v, 8);
}

function formatNodeId(v) {
return '!' + formatId(v);
return '!' + formatHex(v, 8);
}

function formatFloat(v) {
Expand All @@ -50,6 +54,12 @@ function formatRoute(v) {
return route;
}

function formatMacaddr(v) {
const words = CryptoJS.enc.Base64.parse(v);
const bytes = wordsToByteArray(words);
return Array.from(bytes).map((v) => formatHex(v, 2)).join(':');
}

const formatters = new Map([
['nodeId', formatNodeId],
['lastSentById', formatNodeId],
Expand All @@ -63,6 +73,7 @@ const formatters = new Map([
['latitudeI', formatCoord],
['longitudeI', formatCoord],
['route', formatRoute],
['macaddr', formatMacaddr],
]);

function decodeDefault(proto, name, payload) {
Expand Down Expand Up @@ -146,7 +157,7 @@ function wordsToByteArray(wordArray) {
}

function arrayToString(arr) {
return Array.from(arr).map((v) => v.toString(16).padStart(2, '0')).join('');
return Array.from(arr).map((v) => formatHex(v, 2)).join('');
}

function decodeEncrypted(packet, key) {
Expand Down

0 comments on commit 5ff101e

Please sign in to comment.