cabana/src/models/can/frame.js

63 lines
1.1 KiB
JavaScript
Raw Normal View History

export default class Frame {
2017-12-12 19:27:20 -07:00
constructor({
name,
id = 0,
size = 0,
transmitters = [],
extended = 0,
comment = null,
signals = {}
}) {
Object.assign(this, {
name,
id,
size,
transmitters,
extended,
comment,
signals
});
}
nextNewTransmitterName() {
let txNum = 1;
let txName;
2017-12-12 19:27:20 -07:00
do {
txName = `NEW_TRANSMITTER_${txNum}`;
2017-12-12 19:27:20 -07:00
txNum++;
} while (this.transmitters.indexOf(txName) !== -1);
return txName;
}
addTransmitter() {
const txName = this.nextNewTransmitterName();
this.transmitters.push(txName);
return txName;
}
header() {
return (
`BO_ ${this.id} ${this.name}: ${this.size} `
+ `${this.transmitters[0] || 'XXX'}`
2017-12-12 19:27:20 -07:00
);
}
text() {
const signals = Object.values(this.signals)
.map((signal) => ` ${signal.text()}`) // indent
.join('\n');
2017-12-12 19:27:20 -07:00
if (signals.length > 0) {
return `${this.header()}\n${signals}`;
}
return this.header();
2017-12-12 19:27:20 -07:00
}
2017-06-13 18:40:05 -06:00
2017-12-12 19:27:20 -07:00
copy() {
const copy = Object.assign(Object.create(this), this);
2017-06-21 23:57:35 -06:00
2017-12-12 19:27:20 -07:00
return copy;
}
}