Skip to content

Commit

Permalink
Add Goodwe meter control models
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Dec 8, 2024
1 parent 570f659 commit e49a3bc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/connections/goodwe/et.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
type GoodweEtMeterData,
} from './models/et/meterData.js';
import { writeLatency } from '../../helpers/influxdb.js';
import {
type GoodweEtMeterControlWrite,
GoodweGoodweEtMeterControlModel,
type GoodweEtMeterControl,
} from './models/et/goodweEtMeterControl.js';

export class GoodweEtConnection {
protected readonly modbusConnection: ModbusConnection;
Expand Down Expand Up @@ -86,6 +91,31 @@ export class GoodweEtConnection {
return data;
}

async getMeterControl(): Promise<GoodweEtMeterControl> {
const data = await GoodweGoodweEtMeterControlModel.read({
modbusConnection: this.modbusConnection,
address: {
start: 47509,
length: 2,
},
unitId: this.unitId,
});

return data;
}

async setMeterControl(values: GoodweEtMeterControlWrite): Promise<void> {
await GoodweGoodweEtMeterControlModel.write({
modbusConnection: this.modbusConnection,
address: {
start: 47509,
length: 2,
},
unitId: this.unitId,
values,
});
}

public onDestroy(): void {
this.modbusConnection.close();
}
Expand Down
42 changes: 42 additions & 0 deletions src/connections/goodwe/models/et/goodweEtMeterControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { modbusModelFactory } from '../../../modbus/modbusModelFactory.js';
import {
int16ToRegisters,
registersToInt16,
registersToUint16,
uint16ToRegisters,
} from '../../../modbus/helpers/converters.js';

export type GoodweEtMeterControl = {
// Use FeedPowerEnable (47509) to activate export power limit function.
// [0, 1: Enable]
FeedPowerEnable: boolean;
// and EMSPowerSet (47510) to set the max allowed export power to grid
// [0,10000]
FeedPowerPara: number;
};

export type GoodweEtMeterControlWrite = Pick<
GoodweEtMeterControl,
'FeedPowerEnable' | 'FeedPowerPara'
>;

export const GoodweGoodweEtMeterControlModel = modbusModelFactory<
GoodweEtMeterControl,
keyof GoodweEtMeterControlWrite
>({
name: 'GoodweGoodweEtMeterControlModel',
mapping: {
FeedPowerEnable: {
start: 0,
end: 1,
readConverter: (value) => registersToUint16(value) === 1,
writeConverter: (value) => uint16ToRegisters(value ? 1 : 0),
},
FeedPowerPara: {
start: 1,
end: 2,
readConverter: registersToInt16,
writeConverter: int16ToRegisters,
},
},
});

0 comments on commit e49a3bc

Please sign in to comment.