@hamstudy/flamp
Version:
JavaScript Amateur Multicast Protocol AMP-2 Version 3 Implemented from specification document http://www.w1hkj.com/files/flamp/Amp-2.V3.0.Protocol.pdf • Version 1.0.0 - W5ALT, Walt Fair, Jr. (Derived From) • Version 2.0.0 - W1HKJ, Dave Freese, w
62 lines (61 loc) • 2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Block = void 0;
const amp_1 = require("./amp");
const crc16_1 = require("./crc16");
class Block {
keyword;
data;
hash;
controlWord;
blockNum; // Data block number
constructor(keyword, data) {
this.keyword = keyword;
this.data = data;
let closeBrace = data.indexOf('}');
let blockStuff = data.substring(1, closeBrace).split(':');
this.hash = blockStuff[0];
this.data = this.data.substring(closeBrace + 1);
if (this.keyword === amp_1.LTypes.DATA) {
this.blockNum = parseInt(blockStuff[1], 10);
}
else if (this.keyword === amp_1.LTypes.CNTL) {
this.controlWord = blockStuff[1];
}
}
static MakeBlock(opts) {
switch (opts.keyword) {
case amp_1.LTypes.DATA:
return new this(opts.keyword, `{${opts.hash}:${opts.blockNum || 0}}${opts.data}`);
case amp_1.LTypes.CNTL:
return new this(opts.keyword, `{${opts.hash}:${opts.controlWord}}`);
default:
return new this(opts.keyword, `{${opts.hash}}${opts.data}`);
}
}
get checksum() {
return (0, crc16_1.crc16)(this.getHashString() + this.data);
}
get byteCount() {
return this.getHashString().length + this.data.length;
}
getHashString() {
let hash;
switch (this.keyword) {
case amp_1.LTypes.DATA:
hash = `{${this.hash}:${this.blockNum}}`;
break;
case amp_1.LTypes.CNTL:
hash = `{${this.hash}:${this.controlWord}}`;
break;
default:
hash = `{${this.hash}}`;
break;
}
return hash;
}
toString() {
return `<${this.keyword} ${this.byteCount} ${this.checksum}>${this.getHashString()}${this.data}`;
}
}
exports.Block = Block;