windows update / librdsparser update

This commit is contained in:
NoobishSVK
2024-01-16 22:11:04 +01:00
parent febb7575a9
commit ac99acd01f
3 changed files with 127 additions and 76 deletions
+126 -75
View File
@@ -1,89 +1,129 @@
const koffi = require('koffi'); const koffi = require('koffi');
const path = require('path'); const path = require('path');
const lib = koffi.load(path.join(__dirname, "librds.so")); //const lib = koffi.load(path.join(__dirname, "librds.so"));
//const lib = koffi.load(path.join(__dirname, "librds.dll"));
const os = require('os');
const win32 = (os.platform() == "win32");
const unicode_type = (win32 ? 'int16_t' : 'int32_t');
const lib = koffi.load(path.join(__dirname, "librdsparser." + (win32 ? "dll" : "so")));
koffi.proto('void callback_pi(uint16_t pi, void *user_data)'); koffi.proto('void callback_pi(void *rds, void *user_data)');
koffi.proto('void callback_pty(uint8_t pty, void *user_data)'); koffi.proto('void callback_pty(void *rds, void *user_data)');
koffi.proto('void callback_tp(bool tp, void *user_data)'); koffi.proto('void callback_tp(void *rds, void *user_data)');
koffi.proto('void callback_ta(bool ta, void *user_data)'); koffi.proto('void callback_ta(void *rds, void *user_data)');
koffi.proto('void callback_ms(bool ms, void *user_data)'); koffi.proto('void callback_ms(void *rds, void *user_data)');
koffi.proto('void callback_af(uint8_t af, void *user_data)'); koffi.proto('void callback_ecc(void *rds, void *user_data)');
koffi.proto('void callback_ecc(uint8_t ecc, void *user_data)'); koffi.proto('void callback_af(void *rds, uint32_t af, void *user_data)');
koffi.proto('void callback_ps(const char *ps, const uint8_t *errors, void *user_data)'); koffi.proto('void callback_ps(void *rds, void *user_data)');
koffi.proto('void callback_rt(const char *rt, const uint8_t *errors, int flag, void *user_data)'); koffi.proto('void callback_rt(void *rds, int flag, void *user_data)');
koffi.proto('void callback_ptyn(void *rds, void *user_data)');
const librds = { const rdsparser = {
new: lib.func('void* librds_new()'), new: lib.func('void* rdsparser_new()'),
clear: lib.func('void librds_clear(void *context)'), clear: lib.func('void* rdsparser_clear()'),
free: lib.func('void librds_free(void *context)'), free: lib.func('void rdsparser_free(void *rds)'),
parse: lib.func('bool librds_parse(void *context, const char *input)'), parse_string: lib.func('bool rdsparser_parse_string(void *rds, const char *input)'),
get_ps: lib.func('char* librds_get_ps(void *context)'), get_pi: lib.func('int32_t rdsparser_get_pi(void *rds)'),
get_rt: lib.func('char* librds_get_rt(void *context, int flag)'), get_pty: lib.func('int8_t rdsparser_get_pty(void *rds)'),
register_pi: lib.func('void librds_register_pi(void *context, callback_pi *cb)'), get_tp: lib.func('int8_t rdsparser_get_tp(void *rds)'),
register_pty: lib.func('void librds_register_pty(void *context, callback_pty *cb)'), get_ta: lib.func('int8_t rdsparser_get_ta(void *rds)'),
register_tp: lib.func('void librds_register_tp(void *context, callback_tp *cb)'), get_ms: lib.func('int8_t rdsparser_get_ms(void *rds)'),
register_ta: lib.func('void librds_register_ta(void *context, callback_ta *cb)'), get_ecc: lib.func('int8_t rdsparser_get_ecc(void *rds)'),
register_ms: lib.func('void librds_register_ms(void *context, callback_ms *cb)'), get_ps: lib.func('void* rdsparser_get_ps(void *rds)'),
register_af: lib.func('void librds_register_af(void *context, callback_af *cb)'), get_rt: lib.func('void* rdsparser_get_rt(void *rds, int flag)'),
register_ecc: lib.func('void librds_register_ecc(void *context, callback_ecc *cb)'), get_ptyn: lib.func('void* rdsparser_get_ptyn(void *rds)'),
register_ps: lib.func('void librds_register_ps(void *context, callback_ps *cb1)'), register_pi: lib.func('void rdsparser_register_pi(void *rds, callback_pi *cb)'),
register_rt: lib.func('void librds_register_rt(void *context, callback_rt *cb)') register_pty: lib.func('void rdsparser_register_pty(void *rds, callback_pty *cb)'),
register_tp: lib.func('void rdsparser_register_tp(void *rds, callback_tp *cb)'),
register_ta: lib.func('void rdsparser_register_ta(void *rds, callback_ta *cb)'),
register_ms: lib.func('void rdsparser_register_ms(void *rds, callback_ms *cb)'),
register_ecc: lib.func('void rdsparser_register_ecc(void *rds, callback_ecc *cb)'),
register_af: lib.func('void rdsparser_register_af(void *rds, callback_af *cb)'),
register_ps: lib.func('void rdsparser_register_ps(void *rds, callback_ps *cb)'),
register_rt: lib.func('void rdsparser_register_rt(void *rds, callback_rt *cb)'),
register_ptyn: lib.func('void rdsparser_register_ptyn(void *rds, callback_ptyn *cb)'),
string_get_content: lib.func(unicode_type + '* rdsparser_string_get_content(void *string)'),
string_get_length: lib.func('uint8_t rdsparser_string_get_length(void *string)')
} }
const decode_unicode = function(string)
{
let content = rdsparser.string_get_content(string);
let length = rdsparser.string_get_length(string);
let array = koffi.decode(content, koffi.array(unicode_type, length));
return Buffer.from(array, 'utf-8').toString();
};
const callbacks = { const callbacks = {
pi: koffi.register((value) => { pi: koffi.register(rds => (
console.log('PI: ' + value.toString(16).toUpperCase()); value = rdsparser.get_pi(rds),
}, 'callback_pi *'), console.log('PI: ' + value.toString(16).toUpperCase())
), 'callback_pi *'),
pty: koffi.register((value) => { pty: koffi.register(rds => (
dataToSend.pty = value; value = rdsparser.get_pty(rds),
}, 'callback_pty *'), dataToSend.pty = value
), 'callback_pty *'),
tp: koffi.register((value) => { tp: koffi.register(rds => (
dataToSend.tp = value; value = rdsparser.get_tp(rds),
}, 'callback_tp *'), dataToSend.tp = value
), 'callback_tp *'),
ta: koffi.register((value) => { ta: koffi.register(rds => (
console.log('TA: ' + value); value = rdsparser.get_ta(rds),
}, 'callback_ta *'), console.log('TA: ' + value)
), 'callback_ta *'),
ms: koffi.register((value) => { ms: koffi.register(rds => (
console.log('MS: ' + value); value = rdsparser.get_ms(rds),
}, 'callback_ms *'), console.log('MS: ' + value)
), 'callback_ms *'),
af: koffi.register((value) => { af: koffi.register((rds, value) => (
dataToSend.af.push(87500 + value * 100); dataToSend.af.push(value)
}, 'callback_af *'), ), 'callback_af *'),
ecc: koffi.register((value) => { ecc: koffi.register(rds => (
console.log('ECC: ' + value.toString(16).toUpperCase()); value = rdsparser.get_ecc(rds),
}, 'callback_ecc *'), console.log('ECC: ' + value.toString(16).toUpperCase())
), 'callback_ecc *'),
ps: koffi.register((value) => { ps: koffi.register(rds => (
dataToSend.ps = value; value = decode_unicode(rdsparser.get_ps(rds)),
}, 'callback_ps *'), dataToSend.ps = value
), 'callback_ps *'),
rt: koffi.register((rds, flag) => {
const value = decode_unicode(rdsparser.get_rt(rds, flag));
rt: koffi.register((value, errors, flag) => {
if (flag === 0) { if (flag === 0) {
dataToSend.rt0 = value; dataToSend.rt0 = value;
} }
if (flag === 1) { if (flag === 1) {
dataToSend.rt1 = value; dataToSend.rt1 = value;
} }
}, 'callback_rt *'), }, 'callback_rt *'),
ptyn: koffi.register((rds, flag) => (
value = decode_unicode(rdsparser.get_ptyn(rds)),
console.log('PTYN: ' + value)
), 'callback_ptyn *')
}; };
let rds = librds.new() let rds = rdsparser.new()
librds.register_pi(rds, callbacks.pi) rdsparser.register_pi(rds, callbacks.pi);
librds.register_pty(rds, callbacks.pty) rdsparser.register_pty(rds, callbacks.pty);
librds.register_tp(rds, callbacks.tp) rdsparser.register_tp(rds, callbacks.tp);
librds.register_ta(rds, callbacks.ta) rdsparser.register_ta(rds, callbacks.ta);
librds.register_ms(rds, callbacks.ms) rdsparser.register_ms(rds, callbacks.ms);
librds.register_af(rds, callbacks.af) rdsparser.register_ecc(rds, callbacks.ecc);
librds.register_ecc(rds, callbacks.ecc) rdsparser.register_af(rds, callbacks.af);
librds.register_ps(rds, callbacks.ps) rdsparser.register_ps(rds, callbacks.ps);
librds.register_rt(rds, callbacks.rt) rdsparser.register_rt(rds, callbacks.rt);
rdsparser.register_ptyn(rds, callbacks.ptyn);
const updateInterval = 75; const updateInterval = 75;
const clientUpdateIntervals = new Map(); // Store update intervals for each client const clientUpdateIntervals = new Map(); // Store update intervals for each client
@@ -120,11 +160,11 @@ const initialData = {
const resetToDefault = dataToSend => Object.assign(dataToSend, initialData); const resetToDefault = dataToSend => Object.assign(dataToSend, initialData);
var rdsBuffer = []; var rdsBuffer = [];
function handleBuffer() {
function handleBuffer() {
for (let group of rdsBuffer) for (let group of rdsBuffer)
{ {
librds.parse(rds, group); rdsparser.parse_string(rds, group);
} }
} }
@@ -133,19 +173,22 @@ function handleData(ws, receivedData) {
let lastUpdateTime = clientUpdateIntervals.get(ws) || 0; let lastUpdateTime = clientUpdateIntervals.get(ws) || 0;
const currentTime = Date.now(); const currentTime = Date.now();
let modifiedData, parsedValue; let modifiedData, parsedValue;
const receivedLines = receivedData.split('\n');
for (const receivedLine of receivedLines) {
switch (true) { switch (true) {
case receivedData.startsWith('P'): case receivedLine.startsWith('P'):
modifiedData = receivedData.substring(1, 5); modifiedData = receivedLine.substring(1, 5);
dataToSend.pi = modifiedData; dataToSend.pi = modifiedData;
break; break;
case receivedData.startsWith('T'): case receivedLine.startsWith('T'):
rdsBuffer = []; rdsBuffer = [];
resetToDefault(dataToSend); resetToDefault(dataToSend);
dataToSend.af.length = 0; dataToSend.af.length = 0;
librds.clear(rds); rdsparser.clear(rds);
modifiedData = receivedData.substring(1); modifiedData = receivedLine.substring(1);
parsedValue = parseFloat(modifiedData); parsedValue = parseFloat(modifiedData);
if (!isNaN(parsedValue)) { if (!isNaN(parsedValue)) {
@@ -154,8 +197,8 @@ function handleData(ws, receivedData) {
} }
break; break;
case receivedData.startsWith('Sm'): case receivedLine.startsWith('Sm'):
modifiedData = receivedData.substring(2); modifiedData = receivedLine.substring(2);
parsedValue = parseFloat(modifiedData); parsedValue = parseFloat(modifiedData);
dataToSend.st = false; dataToSend.st = false;
@@ -173,9 +216,8 @@ function handleData(ws, receivedData) {
} }
break; break;
case receivedData.startsWith('R'): case receivedLine.startsWith('R'):
modifiedData = dataToSend.pi.toUpperCase() + receivedData.slice(1, -1).toUpperCase(); modifiedData = receivedLine.slice(1, -1).toUpperCase();
// Ensure modifiedData is exactly 18 characters long // Ensure modifiedData is exactly 18 characters long
if (modifiedData.length < 18) { if (modifiedData.length < 18) {
modifiedData = modifiedData.padEnd(18, '0'); // Add zeroes at the end modifiedData = modifiedData.padEnd(18, '0'); // Add zeroes at the end
@@ -188,21 +230,30 @@ function handleData(ws, receivedData) {
rdsBuffer.shift(); rdsBuffer.shift();
} }
rdsBuffer.push(modifiedData); rdsBuffer.push(modifiedData);
if (rdsBuffer.length > 1) { if (rdsBuffer.length > 1) {
handleBuffer(); handleBuffer();
} }
break; break;
} }
}
// Send the updated data to the client // Send the updated data to the client
const dataToSendJSON = JSON.stringify(dataToSend); const dataToSendJSON = JSON.stringify(dataToSend);
if (currentTime - lastUpdateTime >= updateInterval) { if (currentTime - lastUpdateTime >= updateInterval) {
clientUpdateIntervals.set(ws, currentTime); // Update the last update time for this client clientUpdateIntervals.set(ws, currentTime); // Update the last update time for this client
ws.send(dataToSendJSON); ws.send(dataToSendJSON);
} }
} }
/*setInterval(function () {
// some code
if (rdsBuffer.length > 50) {
handleBuffer();
//console.log("handling buffer");
}
//console.log(rdsBuffer.length);
}, 150);*/
module.exports = { module.exports = {
handleData handleData
+1 -1
View File
@@ -11,7 +11,7 @@ const axios = require('axios');
const dataHandler = require('./datahandler'); const dataHandler = require('./datahandler');
/* Server settings */ /* Server settings */
const webServerHost = '192.168.1.39'; // IP of the web server const webServerHost = '192.168.1.14'; // IP of the web server
const webServerPort = 8080; // web server port const webServerPort = 8080; // web server port
const xdrdServerHost = '192.168.1.15'; // xdrd server iP const xdrdServerHost = '192.168.1.15'; // xdrd server iP
BIN
View File
Binary file not shown.