Skip to content

Instantly share code, notes, and snippets.

@imposibrus
Created January 22, 2016 12:31
Show Gist options
  • Save imposibrus/f3cc5d17e9612332a5eb to your computer and use it in GitHub Desktop.
Save imposibrus/f3cc5d17e9612332a5eb to your computer and use it in GitHub Desktop.
EGTS_SR_POS_DATA parser (EGTS_TELEDATA_SERVICE)
const net = require('net');
var getBits = (octet) => {
var bits = [];
for (var i = 7; i >= 0; i--) {
var bit = octet & (1 << i) ? 1 : 0;
bits.push(bit);
}
return bits;
};
var getUInt = (valArr, base) => {
if(!base) {
base = 256;
}
return valArr.reduce((mem, cur, index) => {
return mem + (cur * Math.pow(base, index));
}, 0);
};
var secsFrom1970to2010 = 1262304000;
const server = net.createServer((socket) => { //'connection' listener
console.log('client connected');
socket.on('end', () => {
console.log('client disconnected');
});
socket.on('data', (data) => {
var byteArray = new Uint8Array(data);
console.log('------------------------------------------------------------------------------------');
console.log('Date:', new Date());
console.log('Packed data:');
console.log(' ', new Buffer(data).toString('hex'));
console.log(' ', byteArray.toString());
console.log('\n');
console.log('EGTS Transport Layer:');
console.log('---------------------');
console.log(' Protocol Version -', byteArray[0].toString());
console.log(' Security Key ID -', byteArray[1].toString());
var flagsBits = getBits('0x' + new Buffer([byteArray[2]]).toString('hex'));
console.log(' Flags -', flagsBits.join(''));
console.log(`
Prefix - ${flagsBits.slice(0, 2).join('')}
Route - ${flagsBits[2]}
Encryption Alg - ${flagsBits.slice(3, 5).join('')}
Compression - ${flagsBits[5]}
Priority - ${flagsBits.slice(6, 8).join('')} (the highest)`);
console.log(' Header Length -', byteArray[3].toString());
console.log(' Header Encoding -', byteArray[4].toString());
console.log(' Frame Data Length -', byteArray.slice(5, 7).reduce((mem, val) => mem + val, 0).toString());
console.log(' Packet ID -', getUInt(byteArray.slice(7, 9)));
console.log(' No route info -');
console.log(' Header Check Sum -', '0x' + byteArray[10].toString());
console.log('');
console.log('EGTS Service Layer:');
console.log('---------------------');
console.log(' Packet Type -', byteArray[9].toString());
console.log('');
console.log(' Service Layer Record:');
console.log(' ---------------------');
console.log(' Record Length -', byteArray.slice(11, 13).reduce((mem, val) => mem + val, 0).toString());
console.log(' Record Number -', byteArray.slice(13, 15).reduce((mem, val) => mem + val, 0).toString());
var recordFlagsBits = getBits('0x' + new Buffer([byteArray[15]]).toString('hex'));
console.log(' Record flags - ', recordFlagsBits.join(''));
console.log(`
Source Service On Device - ${recordFlagsBits[0]}
Recipient Service On Device - ${recordFlagsBits[1]}
Group Flag - ${recordFlagsBits[2]}
Record Processing Priority - ${recordFlagsBits.slice(3, 5).join('')}
Time Field Exists - ${recordFlagsBits[5]}
Event ID Field Exists - ${recordFlagsBits[6]}
Object ID Field Exists - ${recordFlagsBits[7]}`);
console.log(' Object Identifier -', getUInt(byteArray.slice(16, 20)));
console.log(' Source Service Type -', byteArray[20].toString());
console.log(' Recipient Service Type -', byteArray[21].toString());
console.log(' Subrecord Data:');
console.log(' ------------------');
console.log(' Subrecord Type -', byteArray[22].toString());
console.log(' Subrecord Length -', getUInt(byteArray.slice(23, 25)));
console.log(' Navigation Time -', new Date((getUInt(byteArray.slice(25, 29)) + secsFrom1970to2010) * 1000));
console.log(' Position Latitude -', (getUInt(byteArray.slice(29, 33)) * 90) / 4294967295);
console.log(' Position Longitude -', (getUInt(byteArray.slice(33, 37)) * 180) / 4294967295);
console.log('');
var subRecordFlagsHexBuffer = new Buffer([byteArray[37]]).toString('hex'),
subRecordFlagsBits = getBits('0x' + subRecordFlagsHexBuffer);
console.log(' Record flags - ', subRecordFlagsBits.join(''), '(', '0x' + subRecordFlagsHexBuffer, ')');
console.log(`
Altitude exist - ${subRecordFlagsBits[0]}
Longitude hemisphere - ${subRecordFlagsBits[1] == 0 ? 'east' : 'west'}
Latitude hemisphere - ${subRecordFlagsBits[2] == 0 ? 'north' : 'south'}
Is moving? - ${subRecordFlagsBits[3]}
Black box - ${subRecordFlagsBits[4]}
Coordinates system - ${subRecordFlagsBits[5] == 0 ? 'WGS-84' : 'ПЗ-90.02'}
FIX - ${subRecordFlagsBits[6] == 0 ? '2D fix' : '3D fix'}
Is valid? - ${subRecordFlagsBits[7]}`);
console.log('');
//console.log(' Speed - ', subRecordFlagsBits.join(''));
//console.log(' direction - ', subRecordFlagsBits.join(''));
console.log(' Odometer - ', getUInt(byteArray.slice(41, 44)));
console.log(' Source - ', byteArray[45]);
console.log(' Altitude - ', getUInt(byteArray.slice(46, 48)));
console.log('=================================');
});
});
server.listen(8124, () => { //'listening' listener
console.log('server bound');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment