-
Notifications
You must be signed in to change notification settings - Fork 232
/
advanced.ts
74 lines (59 loc) · 2.44 KB
/
advanced.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as vscode from 'vscode';
import { SWODecoder } from './common';
import { SWOAdvancedDecoderConfig, AdvancedDecoder, GrapherDataMessage } from '../common';
import { EventEmitter } from 'events';
import { Packet } from '../common';
declare function __webpack_require__();
declare const __non_webpack_require__: NodeRequire;
const dynamicRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require;
export class SWOAdvancedProcessor extends EventEmitter implements SWODecoder {
private output: vscode.OutputChannel;
public readonly format: string = 'advanced';
private ports: number[];
private decoder: AdvancedDecoder;
constructor(config: SWOAdvancedDecoderConfig) {
super();
this.ports = [];
const decoderPath = config.decoder;
const resolved = dynamicRequire.resolve(decoderPath);
if (dynamicRequire.cache[resolved]) { delete dynamicRequire.cache[resolved]; } // Force reload
const decoderModule = dynamicRequire(decoderPath);
if (decoderModule && decoderModule.default) {
const decoderClass = decoderModule.default;
try {
this.decoder = new decoderClass();
this.decoder.init(config, this.displayOutput.bind(this), this.graphData.bind(this));
}
catch (e) {
throw new Error(`Error instantiating decoder class: ${e.toString()}`);
}
this.ports = config.ports;
this.output = vscode.window.createOutputChannel(`SWO: ${this.decoder.outputLabel() || ''} [type: ${this.decoder.typeName()}]`);
}
else {
throw new Error(`Unable to load decoder class from: ${config.decoder}`);
}
}
public softwareEvent(packet: Packet) {
if (this.ports.indexOf(packet.port) !== -1) {
this.decoder.softwareEvent(packet.port, packet.data);
}
}
public hardwareEvent(event: Packet) {}
public synchronized() {
this.decoder.synchronized();
}
public lostSynchronization() {
this.decoder.lostSynchronization();
}
public displayOutput(output: string, timestamp: boolean = true) {
this.output.append(output);
}
public graphData(data: number, id: string) {
const message: GrapherDataMessage = { type: 'data', data: data, id: id };
this.emit('data', message);
}
public dispose() {
this.output.dispose();
}
}