Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit c1dbcad

Browse files
committed
fix listen to server topics; implement power off; set console.log() to console.debug(); refactoring
1 parent 7f5c5fc commit c1dbcad

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let server: Server;
3333
//process.stdin.resume(); // so the program will not close instantly
3434

3535
exitHook(() => {
36-
console.log('Exiting');
36+
console.debug('Exiting');
3737

3838
if (server != null) {
3939
server.shutdown();

src/server.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class Server {
1111
private readonly apps: App[] = [];
1212
private readonly controller: SmartDisplayController;
1313

14+
private interval: NodeJS.Timeout | null = null;
1415
private currentAppIndex = 0;
1516
private appIterations = 0;
1617

@@ -22,15 +23,14 @@ export class Server {
2223
username: mqttSettings.username,
2324
password: mqttSettings.password
2425
})
26+
.subscribe('smartDisplay/server/in/#')
2527
.on('message', (topic, message) => {
2628
if (!topic.startsWith('smartDisplay/server/in/')) {
2729
return;
2830
}
2931

3032
const lastPart = MqttHelper.getLastTopicPart(topic);
3133
this.processIncomingMessage(lastPart, message.toString());
32-
33-
console.log('server message', topic, message.toString());
3434
})
3535
.on('error', error => {
3636
console.error('MQTT', error);
@@ -49,14 +49,22 @@ export class Server {
4949
return;
5050
}
5151

52-
console.log('server cmd', command, message);
52+
console.debug('server cmd', command, message);
5353

5454
switch (command) {
5555
case 'power': {
5656
if (message === 'on' || message === 'off') {
5757
const powerOn = message === 'on' ? true : false;
5858

59+
console.debug('switch power-status', powerOn);
60+
5961
this.controller.power(powerOn);
62+
63+
if (powerOn) {
64+
this.startInterval();
65+
} else {
66+
this.stopInterval();
67+
}
6068
}
6169

6270
break;
@@ -73,15 +81,23 @@ export class Server {
7381
run(): void {
7482
this.client.publish('smartDisplay/server/out', 'started');
7583

84+
this.startInterval();
85+
}
86+
87+
private startInterval(): void {
88+
console.debug('startInterval()');
89+
7690
this.appIterations = 0;
7791

7892
this.renderApp();
7993

80-
setInterval(() => {
94+
this.interval = setInterval(() => {
8195
if (!this.client.connected) {
8296
console.error('client not connected');
8397
}
8498

99+
console.debug('power-status', this.controller.powerStatus);
100+
85101
this.renderApp();
86102

87103
if (this.appIterations >= 15) {
@@ -90,8 +106,18 @@ export class Server {
90106
}, 1000);
91107
}
92108

109+
private stopInterval(): void {
110+
console.debug('stopInterval()');
111+
112+
if (this.interval == null) {
113+
return;
114+
}
115+
116+
clearInterval(this.interval);
117+
}
118+
93119
private nextApp(): void {
94-
console.log('next app');
120+
console.debug('next app');
95121

96122
this.currentAppIndex++;
97123

@@ -117,6 +143,8 @@ export class Server {
117143
}
118144

119145
shutdown(): void {
146+
console.debug('shutdown');
147+
120148
this.controller.destroy();
121149
}
122150
}

src/smart-display-controller.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ export class SmartDisplayController {
2222
return this._info.value;
2323
}
2424

25+
get powerStatus(): boolean {
26+
return this._powerStatus;
27+
}
28+
2529
constructor(private client: mqtt.Client) {
2630
client
31+
.subscribe('smartDisplay/client/out/#')
2732
.on('message', (topic, message) => {
2833
if (!topic.startsWith('smartDisplay/client/out/')) {
2934
return;
3035
}
3136

3237
const lastPart = MqttHelper.getLastTopicPart(topic);
3338
this.processIncomingMessage(lastPart, message.toString());
34-
})
35-
.subscribe('smartDisplay/client/out/#');
39+
});
3640
}
3741

3842
private processIncomingMessage(
@@ -43,32 +47,40 @@ export class SmartDisplayController {
4347
return;
4448
}
4549

46-
console.log(new Date(), 'controller cmd', command, message);
50+
console.debug(new Date(), 'controller cmd', command, message);
4751

4852
switch (command) {
4953
case 'info': {
50-
this._info.value = JSON.parse(message);
51-
52-
const powerOnDevice = this._info.value?.powerOn;
54+
try {
55+
this._info.value = JSON.parse(message);
5356

54-
// check power status
55-
if (powerOnDevice !== this._powerStatus) {
56-
// inconsistence detected
57-
console.warn(
58-
'power status inconstistence:',
59-
`server: ${this._powerStatus}`,
60-
`device: ${powerOnDevice}`
61-
);
62-
63-
// fix status
64-
this.power(this._powerStatus);
57+
this.checkPowerStatus();
58+
} catch (error) {
59+
console.error('error on parse info payload', error);
6560
}
6661

6762
break;
6863
}
6964
}
7065
}
7166

67+
private checkPowerStatus(): void {
68+
const powerOnDevice = this._info.value?.powerOn;
69+
70+
// check power status
71+
if (powerOnDevice !== this._powerStatus) {
72+
// inconsistence detected
73+
console.warn(
74+
'power status inconstistence:',
75+
`server: ${this._powerStatus}`,
76+
`device: ${powerOnDevice}`
77+
);
78+
79+
// fix status
80+
this.power(this._powerStatus);
81+
}
82+
}
83+
7284
changeSettings(settings: ControllerSettings): void {
7385
this.client.publish(
7486
'smartDisplay/client/in/changeSettings',

0 commit comments

Comments
 (0)