mirror of
https://github.com/jwetzell/psn-js.git
synced 2026-08-17 13:23:43 +00:00
organization
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/* eslint-disable no-case-declarations */
|
||||
/* eslint-disable no-param-reassign */
|
||||
const { Parser } = require('binary-parser');
|
||||
const Decoders = require('../../decoders');
|
||||
|
||||
class DataPacket {
|
||||
constructor(packet) {
|
||||
this.packet = packet;
|
||||
this.subChunks = [];
|
||||
// console.log(this.packet);
|
||||
if (this.packet.has_subchunks) {
|
||||
this.subChunkParser = new Parser().array('chunks', {
|
||||
type: Decoders.Chunk,
|
||||
lengthInBytes: this.packet.data_len,
|
||||
});
|
||||
this.subChunks = this.subChunkParser.parse(this.packet.chunk_data)?.chunks;
|
||||
this.subChunks = this.subChunks.map((subChunk) => {
|
||||
let populatedSubChunk = {};
|
||||
populatedSubChunk.data_len_valid = subChunk.chunk_data.length === subChunk.data_len;
|
||||
switch (subChunk.id) {
|
||||
case 0:
|
||||
populatedSubChunk = {
|
||||
...subChunk,
|
||||
...Decoders.PacketHeaderChunk.parse(subChunk.chunk_data),
|
||||
};
|
||||
break;
|
||||
case 1:
|
||||
const dataTrackerList = Decoders.Data.TrackerListChunk.parse(subChunk.chunk_data);
|
||||
dataTrackerList.trackers?.forEach((tracker) => {
|
||||
if (tracker.data && tracker.data_len > 0) {
|
||||
const fields = new Parser()
|
||||
.array('fields', {
|
||||
type: Decoders.Data.TrackerFieldChunk,
|
||||
lengthInBytes: tracker.data_len,
|
||||
})
|
||||
.parse(tracker.data);
|
||||
if (fields.fields) {
|
||||
fields.fields.forEach((field) => {
|
||||
switch (field.id) {
|
||||
case 0x0000:
|
||||
tracker.pos = field.data;
|
||||
break;
|
||||
case 0x0001:
|
||||
tracker.speed = field.data;
|
||||
break;
|
||||
case 0x0002:
|
||||
tracker.ori = field.data;
|
||||
break;
|
||||
case 0x0003:
|
||||
tracker.status = field.data;
|
||||
break;
|
||||
case 0x0004:
|
||||
tracker.accel = field.data;
|
||||
break;
|
||||
case 0x0005:
|
||||
tracker.trgtpos = field.data;
|
||||
break;
|
||||
case 0x0006:
|
||||
tracker.timestamp = field.data;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
populatedSubChunk = {
|
||||
...subChunk,
|
||||
...dataTrackerList,
|
||||
};
|
||||
break;
|
||||
default:
|
||||
populatedSubChunk = {
|
||||
...subChunk,
|
||||
};
|
||||
break;
|
||||
}
|
||||
return populatedSubChunk;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getHeaderPacket() {
|
||||
return this.subChunks.find((subChunk) => subChunk.id === 0x0000);
|
||||
}
|
||||
|
||||
getSystemPacket() {
|
||||
return this.subChunks.find((subChunk) => subChunk.id === 0x0001);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DataPacket;
|
||||
@@ -0,0 +1,7 @@
|
||||
const DataPacket = require('./data-packet');
|
||||
const InfoPacket = require('./info-packet');
|
||||
|
||||
module.exports = {
|
||||
DataPacket,
|
||||
InfoPacket,
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
const { Parser } = require('binary-parser');
|
||||
const Decoders = require('../../decoders');
|
||||
|
||||
class InfoPacket {
|
||||
constructor(packet) {
|
||||
this.packet = packet;
|
||||
this.subChunks = [];
|
||||
if (this.packet.has_subchunks) {
|
||||
this.subChunkParser = new Parser().array('chunks', {
|
||||
type: Decoders.Chunk,
|
||||
lengthInBytes: this.packet.data_len,
|
||||
});
|
||||
this.subChunks = this.subChunkParser.parse(this.packet.chunk_data)?.chunks;
|
||||
this.subChunks = this.subChunks.map((subChunk) => {
|
||||
let populatedSubChunk = {};
|
||||
populatedSubChunk.data_len_valid = subChunk.chunk_data.length === subChunk.data_len;
|
||||
switch (subChunk.id) {
|
||||
case 0:
|
||||
populatedSubChunk = {
|
||||
...subChunk,
|
||||
...Decoders.PacketHeaderChunk.parse(subChunk.chunk_data),
|
||||
};
|
||||
break;
|
||||
case 1:
|
||||
populatedSubChunk = {
|
||||
...subChunk,
|
||||
...Decoders.Info.SystemNameChunk.parse(subChunk.chunk_data),
|
||||
};
|
||||
break;
|
||||
case 2:
|
||||
populatedSubChunk = {
|
||||
...subChunk,
|
||||
...Decoders.Info.TrackerListChunk.parse(subChunk.chunk_data),
|
||||
};
|
||||
break;
|
||||
default:
|
||||
populatedSubChunk = {
|
||||
...subChunk,
|
||||
};
|
||||
break;
|
||||
}
|
||||
return populatedSubChunk;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getHeaderPacket() {
|
||||
return this.subChunks.find((subChunk) => subChunk.id === 0x0000);
|
||||
}
|
||||
|
||||
getSystemPacket() {
|
||||
return this.subChunks.find((subChunk) => subChunk.id === 0x0001);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = InfoPacket;
|
||||
@@ -0,0 +1,92 @@
|
||||
const dataTrackerAccelChunk = require('../encoders/data/data-tracker-accel-chunk');
|
||||
const dataTrackerChunk = require('../encoders/data/data-tracker-chunk');
|
||||
const dataTrackerOriChunk = require('../encoders/data/data-tracker-ori-chunk');
|
||||
const dataTrackerPosChunk = require('../encoders/data/data-tracker-pos-chunk');
|
||||
const dataTrackerSpeedChunk = require('../encoders/data/data-tracker-speed-chunk');
|
||||
const dataTrackerStatusChunk = require('../encoders/data/data-tracker-status-chunk');
|
||||
const dataTrackerTimestampChunk = require('../encoders/data/data-tracker-timestamp-chunk');
|
||||
const dataTrackerTrgtposChunk = require('../encoders/data/data-tracker-trgtpos-chunk');
|
||||
const infoTrackerChunk = require('../encoders/info/info-tracker-chunk');
|
||||
const infoTrackerNameChunk = require('../encoders/info/info-tracker-name-chunk');
|
||||
|
||||
class Tracker {
|
||||
constructor(id, name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.pos = undefined;
|
||||
this.speed = undefined;
|
||||
this.ori = undefined;
|
||||
this.validity = undefined;
|
||||
this.accel = undefined;
|
||||
this.trgtpos = undefined;
|
||||
this.timestamp = undefined;
|
||||
}
|
||||
|
||||
setPos(x, y, z) {
|
||||
this.pos = [x, y, z];
|
||||
}
|
||||
|
||||
setSpeed(x, y, z) {
|
||||
this.speed = [x, y, z];
|
||||
}
|
||||
|
||||
setOri(x, y, z) {
|
||||
this.ori = [x, y, z];
|
||||
}
|
||||
|
||||
setStatus(validity) {
|
||||
this.validity = validity;
|
||||
}
|
||||
|
||||
setAccel(x, y, z) {
|
||||
this.accel = [x, y, z];
|
||||
}
|
||||
|
||||
setTrgtPos(x, y, z) {
|
||||
this.trgtpos = [x, y, z];
|
||||
}
|
||||
|
||||
setTimestamp(timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
getDataChunk() {
|
||||
const fieldChunks = [];
|
||||
|
||||
if (this.pos) {
|
||||
fieldChunks.push(dataTrackerPosChunk(...this.pos));
|
||||
}
|
||||
|
||||
if (this.speed) {
|
||||
fieldChunks.push(dataTrackerSpeedChunk(...this.speed));
|
||||
}
|
||||
|
||||
if (this.ori) {
|
||||
fieldChunks.push(dataTrackerOriChunk(...this.ori));
|
||||
}
|
||||
|
||||
if (this.validity) {
|
||||
fieldChunks.push(dataTrackerStatusChunk(this.validity));
|
||||
}
|
||||
|
||||
if (this.accel) {
|
||||
fieldChunks.push(dataTrackerAccelChunk(...this.accel));
|
||||
}
|
||||
|
||||
if (this.trgtpos) {
|
||||
fieldChunks.push(dataTrackerTrgtposChunk(...this.trgtpos));
|
||||
}
|
||||
|
||||
if (this.timestamp) {
|
||||
fieldChunks.push(dataTrackerTimestampChunk(this.timestamp));
|
||||
}
|
||||
|
||||
return dataTrackerChunk(this.id, fieldChunks);
|
||||
}
|
||||
|
||||
getInfoChunk() {
|
||||
return infoTrackerChunk(this.id, infoTrackerNameChunk(this.name));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Tracker;
|
||||
Reference in New Issue
Block a user