Skip to content

Commit

Permalink
updated wait function
Browse files Browse the repository at this point in the history
  • Loading branch information
chasenicholl committed Nov 2, 2023
1 parent ee27de5 commit f8ab3f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
46 changes: 29 additions & 17 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ export class WeatherFlowTempestPlatform implements DynamicPlatformPlugin {
this.tempest_battery_level = 0;
this.tempest_device_id = 0;

if (this.config.local_api === false) {
// Make sure the Station ID is the integer ID
if (isNaN(this.config.station_id)) {
log.warn(
'Station ID is not an Integer! Please make sure you are using the ID integer found here: ' +
'https://tempestwx.com/station/<STATION_ID>/',
);
return;
}
// Make sure the Station ID is the integer ID
if (this.config.local_api === false && isNaN(this.config.station_id)) {
log.warn(
'Station ID is not an Integer! Please make sure you are using the ID integer found here: ' +
'https://tempestwx.com/station/<STATION_ID>/',
);
return;
}

this.api.on('didFinishLaunching', () => {
Expand All @@ -92,20 +90,15 @@ export class WeatherFlowTempestPlatform implements DynamicPlatformPlugin {

}

private initializeBySocket() {
private async initializeBySocket() {

try {
this.log.info('Using Tempest Local API.');
this.tempestSocket = new TempestSocket(this.log);
this.tempestSocket.start();
// Hold thread for first message.
this.log.info('Waiting for first local broadcast. This could take up to 60 seconds...');
while (!this.tempestSocket.hasData()) {
continue;
}
this.log.info('Local broadcast recieved.');

// Set values
// Hold thread for first message and set values
await this.socketDataRecieved();
this.observation_data = this.tempestSocket.getStationCurrentObservation();
this.tempest_battery_level = this.tempestSocket.getBatteryLevel();

Expand All @@ -116,11 +109,30 @@ export class WeatherFlowTempestPlatform implements DynamicPlatformPlugin {
// Poll every minute for local API
this.pollLocalStationCurrentObservation();


} catch(exception) {
this.log.error(exception as string);
}
}

private socketDataRecieved(): Promise<void> {

this.log.info('Waiting for first local broadcast. This could take up to 60 seconds...');
return new Promise((resolve) => {
const socket_interval = setInterval(() => {
if (this.tempestSocket === undefined) {
return;
}
if (this.tempestSocket.hasData()) {
clearInterval(socket_interval);
this.log.info('Initial local broadcast recieved.');
resolve();
}
}, 1000);
});

}

private initializeByApi() {

try {
Expand Down
4 changes: 2 additions & 2 deletions src/tempest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ export class TempestSocket {

}

private processReceivedData(data: any) {
private processReceivedData(data) {

if (data.type === 'obs_st') {
this.setTempestData(data);
}

}

private setTempestData(data: any): void {
private setTempestData(data): void {

const obs = data.obs[0];
// const windLull = (obs[1] !== null) ? obs[1] * 2.2369 : 0;
Expand Down

0 comments on commit f8ab3f5

Please sign in to comment.