Skip to content

Commit

Permalink
+ message definitions & basic code template usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rascafr committed Aug 30, 2019
1 parent 838a2a9 commit 6138d1f
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
25 changes: 25 additions & 0 deletions definitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const D = module.exports;

D.headerBytes = [
'5A', 'A5'
]

D.addresses = {
'20': 'ESC',
'21': 'BLE',
'22': 'BMS',
'23': 'Ext-BMS',
'00': '?-ESC',
'01': '?',
'3D': 'APP',
'3E': 'APP',
'3F': 'APP',
}

D.commands = {
'1': 'Ask read registers',
'4': 'Response read registers',
'61': 'Read registers, update head',
'64': 'Update head display',
'65': 'Update head sensors'
}
105 changes: 105 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const fs = require('fs');
const clc = require('cli-color');
const sha1 = require('js-sha1');
const blue = clc.blueBright;
const D = require('./definitions');

const INPUT_FILE_LOG = `unlockThenRideV2Full.log`;
const INPUT_PATH_LOG = `${__dirname}/logfiles/${INPUT_FILE_LOG}`;
const LOG_HEADER = 'Ready...';

const OUTPUT_FILE_LOG = `resume.log`;
const OUTPUT_PATH_LOG = `${__dirname}/${OUTPUT_FILE_LOG}`;

console.log(blue('Reading file'), INPUT_PATH_LOG);

const logData = fs.readFileSync(INPUT_PATH_LOG).toString();
const logLines = logData
.split('\n')
.map(l => l.replace('\n', '')
.replace('\r', '')
.trim())
.filter(l => l.length && l !== LOG_HEADER)
.map(l => l.split(' '));

console.log('Got', logLines.length, 'lines of log');

console.log(logLines[1]);

const LINES_TYPES = {};
let strLog = '';

logLines.forEach(line => {
let sign = sha1(line.join(':'));
if (!LINES_TYPES[sign]) {
LINES_TYPES[sign] = line;
}
let msg = messageToString(parseMessage(line));
console.log(msg);
strLog += msg + '\n';
});

fs.writeFileSync(OUTPUT_PATH_LOG, strLog);

console.log('Different lines log values types:', Object.keys(LINES_TYPES).length);

function parseMessage(line) {

const message = {
length: null,
src: null,
dst: null,
cmd: null,
arg: null,
payload: null
}

// check length
if (line.length < 7) {
console.log(line);
throw 'Line error, invalid length';
}

// check header
if (line[0] !== D.headerBytes[0] || line[1] !== D.headerBytes[1]) {
console.log(line);
throw 'Line error, invalid header';
}

// fill message
message.length = line[2];
message.src = line[3];
message.dst = line[4];
message.cmd = line[5];
message.arg = line[6];
// todo payload

return message;
}

function messageToString(message) {
let str = '';

// who -> whom?
str += `[${D.addresses[message.src]} --> `;
str += `${D.addresses[message.dst]}] `;

// command
str += `using cmd ${D.commands[message.cmd] || '-----'} (${message.cmd}) `;

// argument
str += `and arg ${(message.arg < 10 ? ' ' : '') + message.arg} `;

return str;
}

function hashCode(str) {
var hash = 0, i, chr;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

0 comments on commit 6138d1f

Please sign in to comment.