Skip to content

Commit

Permalink
Use new return types
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Nov 18, 2024
1 parent 8a2ae75 commit 023b354
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 103 deletions.
105 changes: 38 additions & 67 deletions src/inverter/goodwe/et.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { type InverterData } from '../inverterData.js';
import { type Result } from '../../helpers/result.js';
import { ConnectStatusValue } from '../../sep2/models/connectStatus.js';
import { OperationalModeStatusValue } from '../../sep2/models/operationModeStatus.js';
import { InverterDataPollerBase } from '../inverterDataPollerBase.js';
import { type InverterConfiguration } from '../../coordinator/helpers/inverterController.js';
import { type Config } from '../../helpers/config.js';
import { withRetry } from '../../helpers/withRetry.js';
import { writeLatency } from '../../helpers/influxdb.js';
import { averageNumbersNullableArray } from '../../helpers/number.js';
import { DERTyp } from '../../connections/sunspec/models/nameplate.js';
Expand Down Expand Up @@ -43,72 +41,45 @@ export class GoodweEtInverterDataPoller extends InverterDataPollerBase {
void this.startPolling();
}

override async getInverterData(): Promise<Result<InverterData>> {
try {
return await withRetry(
async () => {
const start = performance.now();

const deviceParameters =
await this.connection.getDeviceParameters();

writeLatency({
field: 'GoodweEtInverterDataPoller',
duration: performance.now() - start,
tags: {
inverterIndex: this.inverterIndex.toString(),
model: 'deviceParameters',
},
});

const inverterRunningData1 =
await this.connection.getInverterRunningData1();

writeLatency({
field: 'GoodweEtInverterDataPoller',
duration: performance.now() - start,
tags: {
inverterIndex: this.inverterIndex.toString(),
model: 'inverterRunningData1',
},
});

const models: InverterModels = {
deviceParameters,
inverterRunningData1,
};

const end = performance.now();
const duration = end - start;

this.logger.trace(
{ duration, models },
'Got inverter data',
);
override async getInverterData(): Promise<InverterData> {
const start = performance.now();

const deviceParameters = await this.connection.getDeviceParameters();

writeLatency({
field: 'GoodweEtInverterDataPoller',
duration: performance.now() - start,
tags: {
inverterIndex: this.inverterIndex.toString(),
model: 'deviceParameters',
},
});

const inverterRunningData1 =
await this.connection.getInverterRunningData1();

writeLatency({
field: 'GoodweEtInverterDataPoller',
duration: performance.now() - start,
tags: {
inverterIndex: this.inverterIndex.toString(),
model: 'inverterRunningData1',
},
});

const models: InverterModels = {
deviceParameters,
inverterRunningData1,
};

const end = performance.now();
const duration = end - start;

this.logger.trace({ duration, models }, 'Got inverter data');

const inverterData = generateInverterData(models);

const inverterData = generateInverterData(models);

return {
success: true,
value: inverterData,
};
},
{
attempts: 3,
delayMilliseconds: 100,
functionName: 'get inverter data',
},
);
} catch (error) {
this.logger.error(error, 'Failed to get inverter data');

return {
success: false,
error: new Error(
`Error loading inverter data: ${error instanceof Error ? error.message : 'Unknown error'}`,
),
};
}
return inverterData;
}

override onDestroy(): void {
Expand Down
44 changes: 8 additions & 36 deletions src/meters/goodwe/et.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { type Result } from '../../helpers/result.js';
import { type Config } from '../../helpers/config.js';
import { withRetry } from '../../helpers/withRetry.js';
import { GoodweEtConnection } from '../../connections/goodwe/et.js';
import { SiteSamplePollerBase } from '../../meters/siteSamplePollerBase.js';
import { type GoodweEtMeterData } from '../../connections/goodwe/models/et/meterData.js';
Expand All @@ -27,45 +25,19 @@ export class GoodweEtSiteSamplePoller extends SiteSamplePollerBase {
void this.startPolling();
}

override async getSiteSample(): Promise<Result<SiteSample>> {
try {
return await withRetry(
async () => {
const start = performance.now();
override async getSiteSample(): Promise<SiteSample> {
const start = performance.now();

const meterData = await this.connection.getMeterData();
const meterData = await this.connection.getMeterData();

const end = performance.now();
const duration = end - start;
const end = performance.now();
const duration = end - start;

this.logger.trace(
{ duration, meterData },
'got meter data',
);
this.logger.trace({ duration, meterData }, 'got meter data');

const siteSample = generateSiteSample({ meterData });
const siteSample = generateSiteSample({ meterData });

return {
success: true,
value: siteSample,
};
},
{
attempts: 3,
delayMilliseconds: 100,
functionName: 'getSiteSample',
},
);
} catch (error) {
this.logger.error(error, 'Failed to get meter data');

return {
success: false,
error: new Error(
`Error loading meter data: ${error instanceof Error ? error.message : 'Unknown error'}`,
),
};
}
return siteSample;
}

override onDestroy(): void {
Expand Down

0 comments on commit 023b354

Please sign in to comment.