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

Commit 7f5c5fc

Browse files
committed
implement power status
1 parent 1c25f63 commit 7f5c5fc

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/models/controller-info.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface ControllerInfo {
44
version: string;
55
chipID: string;
66
lux: number;
7+
powerOn: boolean;
78
network: Network;
89
roomWeather: RoomWeather;
910
}

src/server.ts

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

14-
private powerOn = true;
1514
private currentAppIndex = 0;
1615
private appIterations = 0;
1716

@@ -55,7 +54,9 @@ export class Server {
5554
switch (command) {
5655
case 'power': {
5756
if (message === 'on' || message === 'off') {
58-
this.powerOn = message === 'on' ? true : false;
57+
const powerOn = message === 'on' ? true : false;
58+
59+
this.controller.power(powerOn);
5960
}
6061

6162
break;

src/smart-display-controller.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { MqttHelper } from './helper';
1515
export class SmartDisplayController {
1616
private readonly _info = new LastUpdated<ControllerInfo>();
1717

18+
private _powerStatus: boolean = true; // true = on; false = off
19+
1820
get info(): ControllerInfo | null {
1921
// TODO return NULL if data too old
2022
return this._info.value;
@@ -46,6 +48,22 @@ export class SmartDisplayController {
4648
switch (command) {
4749
case 'info': {
4850
this._info.value = JSON.parse(message);
51+
52+
const powerOnDevice = this._info.value?.powerOn;
53+
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);
65+
}
66+
4967
break;
5068
}
5169
}
@@ -111,6 +129,18 @@ export class SmartDisplayController {
111129
);
112130
}
113131

132+
power(status: boolean): void {
133+
this._powerStatus = status;
134+
135+
const dataOut = {
136+
on: status
137+
};
138+
this.client.publish(
139+
'smartDisplay/client/in/power',
140+
JSON.stringify(dataOut)
141+
);
142+
}
143+
114144
destroy(): void {
115145
this.client.end(true);
116146
}

0 commit comments

Comments
 (0)