This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
busmonitor.ts
67 lines (55 loc) · 2.14 KB
/
busmonitor.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
import { Subscription } from 'rxjs';
import * as process from 'process';
import { KnxConnection, KnxTunnelConnection, ServiceType } from '../';
import { Logger, ConnectionState } from '../lib/utils/index';
export class BusMonitor {
protected logger: Logger = new Logger(this.constructor.name);
protected subscription: Subscription;
//private connection: KnxConnection = new KnxTunnelConnection({ localAddress: null, destinationAddress: '10.2.0.239', multicast: false });
private connection: KnxConnection = new KnxTunnelConnection({ localAddress: '10.2.0.100', destinationAddress: null, multicast: false });
constructor() {
process.once('exit', (code: number) => {
this.logger.debug('process exit');
if (this.connection.isConnected()) {
this.disconnect();
}
});
process.once('SIGINT', () => {
this.logger.debug('process sigint received');
var listeners = process.listeners('SIGINT');
if (listeners.length > 0) {
this.logger.warn('Nice SIGINT-handler, following sigint handlers could block process from immediate and clean exit:');
}
for (var i = 0; i < listeners.length; i++) {
this.logger.warn(listeners[i].toString());
}
if (this.connection.isConnected()) {
this.disconnect();
}
});
process.once('uncaughtException', (error: Error) => {
this.logger.error(error, 'process uncaughtException received');
if (this.connection.isConnected()) {
this.disconnect();
}
throw error;
});
this.connection.getState$()
.filter(state => state === ConnectionState.connected)
.subscribe(state => this.logger.info(ConnectionState[state]));
}
public connect() {
this.subscription = this.connection.subscribe(
frame => {
this.logger.info('message received:', ServiceType[frame.header.serviceType]);
this.logger.trace(frame, 'message received:', ServiceType[frame.header.serviceType]);
}
);
}
public disconnect() {
this.logger.debug('disconnecting...');
this.subscription.unsubscribe();
}
}
var busmonitor: BusMonitor = new BusMonitor();
busmonitor.connect();