fly-socket/www/js/fly-ws.js

174 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-07-31 01:08:19 +02:00
class FLYSocket {
constructor(ws,
callbackData = this.callbackDummy,
callbackRaw = this.callbackDummy) {
this.ws = new WebSocket(ws);
this.pointer = 0;
this.callbackData = callbackData;
this.callbackRaw = callbackRaw;
this.ws.onmessage = this.onMessage.bind(this);
}
onMessage(e) {
new Response(event.data).arrayBuffer().then(
function(buff) {
let data = new DataView(buff);
this.callbackRaw(data);
if (this.callbackData != this.callbackDummy || true) { // DEBUG
this.callbackData( this.parse(data) );
}
}.bind(this)
);
}
parse(data) {
window.data = data; // DEBUG
let packets = [];
this.pointer = 0;
while (this.pointer < data.byteLength) {
let packetID = data.getInt8(this.pointer++);
packets.push({
packetID: packetID,
value: (this["handle_packet" + packetID] || this.handle_packetInvalid)(data)
});
}
return packets;
}
handle_packet0(data) {
// BxI
// B: Length
// I: FlightUUID
let ret = [];
let length = data.getUint8(this.pointer++);
for (let i = 0; i < length; i++) {
2019-07-31 17:14:15 +02:00
ret.push( data.getUint32(this.pointer) );
this.pointer += 4;
2019-07-31 01:08:19 +02:00
}
return ret;
}
handle_packet1(data) {
// ?
// ?: Subscribed to changes
return data.getUint8(this.pointer++) != 0;
}
handle_packet2(data) {
// I?
// I: FlightUUID
// ?: Subscribed to flight data
let ret = [];
2019-07-31 17:14:15 +02:00
ret.push( data.getUint32(this.pointer) ); this.pointer += 4;
2019-07-31 01:08:19 +02:00
ret.push( data.getUint8(this.pointer++) != 0 );
return ret;
}
handle_packet3(data) { // TODO: implement (Whole data / history)
2019-08-01 01:14:28 +02:00
// xs
2019-07-31 01:08:19 +02:00
// s: DataStream
2019-08-01 01:14:28 +02:00
let ret = {};
ret["uuid"] = data.getUint32(this.pointer); this.pointer += 4;
ret["maxFuel"] = data.getFloat32(this.pointer); this.pointer += 4;
ret["model"] = data.getUint16(this.pointer); this.pointer += 2;
let playernameLength = data.getUint8(this.pointer++);
let playername = "";
for (let i = 0; i < playernameLength; i++)
playername += String.fromCharCode( data.getUint8(this.pointer++) );
ret["playername"] = playername;
let timeline = [];
while (this.pointer < data.byteLength)
timeline.push( this.parseFrame(data) );
ret["timeline"] = timeline;
return ret;
2019-07-31 01:08:19 +02:00
}
handle_packet4(data) {
// I?
// I: FlightUUID
// ?: Flight active
let ret = [];
2019-07-31 17:14:15 +02:00
ret.push( data.getUint32(this.pointer) ); this.pointer += 4;
2019-07-31 01:08:19 +02:00
ret.push( data.getUint8(this.pointer++) != 0 );
return ret;
}
2019-08-01 01:14:28 +02:00
handle_packet5(data) {
2019-07-31 01:08:19 +02:00
// Ixs
2019-08-01 01:14:28 +02:00
// I: FlightUUID
2019-07-31 01:08:19 +02:00
// s: DataStream
2019-08-01 01:14:28 +02:00
let ret = [];
ret.push( data.getUint32(this.pointer) ); this.pointer += 4;
ret.push( this.parseFrame(data) );
return ret;
2019-07-31 01:08:19 +02:00
}
2019-07-31 17:10:07 +02:00
handle_packet255(data) {
// Hxs
// H: Length
// s: ErrorMessage
let ret = "";
let length = data.getUint16(this.pointer); this.pointer += 2;
for (let i = 0; i < length; i++) {
ret += String.fromCharCode( data.getUint8(this.pointer++) );
}
return ret;
}
2019-07-31 01:08:19 +02:00
handle_packetInvalid(data) {
throw TypeError(`Invalid DATA: ${Array.from( this.buffer2HexArray(data, --this.pointer) ).join(" ")}`);
}
2019-08-01 01:14:28 +02:00
parseFrame(data) {
// IBxs
// I: Timestamp
// B: Flag
// s: FrameStream
let ret = {};
ret["timestamp"] = data.getUint32(this.pointer); this.pointer += 4;
let flags = data.getUint8(this.pointer++);
if (flags & (1<<0)) {
let statusFlag = data.getUint8(this.pointer++);
ret["paused"] = !!( statusFlag & (1<<0) );
ret["engine"] = !!( statusFlag & (1<<1) );
}
if (flags & (1<<1)) {
ret["satisfaction"] = data.getUint8(this.pointer++);
}
if (flags & (1<<2)) {
ret["speed"] = data.getUint16(this.pointer); this.pointer += 2;
}
if (flags & (1<<3)) {
ret["altitude"] = data.getInt16(this.pointer); this.pointer += 2;
}
if (flags & (1<<4)) {
ret["health"] = data.getUint16(this.pointer); this.pointer += 2;
}
if (flags & (1<<5)) {
ret["fuel"] = data.getFloat32(this.pointer); this.pointer += 4;
}
if (flags & (1<<6)) {
ret["x"] = data.getFloat32(this.pointer); this.pointer += 4;
}
if (flags & (1<<7)) {
ret["y"] = data.getFloat32(this.pointer); this.pointer += 4;
}
return ret;
}
2019-07-31 01:08:19 +02:00
*buffer2HexArray(buff, start = 0, end = buff.byteLength) {
for (let i = start; i < end; i++) {
yield hex.push( buff.getUint8(i).toString(16) );
}
}
callbackDummy(_) {}
}
console.log("OK") // TODO: please remove...