From e49a3bcd47909208e6f7d138fa0110d59a3459ea Mon Sep 17 00:00:00 2001 From: Long Zheng Date: Sun, 8 Dec 2024 21:26:11 +1100 Subject: [PATCH] Add Goodwe meter control models --- src/connections/goodwe/et.ts | 30 +++++++++++++ .../goodwe/models/et/goodweEtMeterControl.ts | 42 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/connections/goodwe/models/et/goodweEtMeterControl.ts diff --git a/src/connections/goodwe/et.ts b/src/connections/goodwe/et.ts index d656332..4b2a9d0 100644 --- a/src/connections/goodwe/et.ts +++ b/src/connections/goodwe/et.ts @@ -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; @@ -86,6 +91,31 @@ export class GoodweEtConnection { return data; } + async getMeterControl(): Promise { + const data = await GoodweGoodweEtMeterControlModel.read({ + modbusConnection: this.modbusConnection, + address: { + start: 47509, + length: 2, + }, + unitId: this.unitId, + }); + + return data; + } + + async setMeterControl(values: GoodweEtMeterControlWrite): Promise { + await GoodweGoodweEtMeterControlModel.write({ + modbusConnection: this.modbusConnection, + address: { + start: 47509, + length: 2, + }, + unitId: this.unitId, + values, + }); + } + public onDestroy(): void { this.modbusConnection.close(); } diff --git a/src/connections/goodwe/models/et/goodweEtMeterControl.ts b/src/connections/goodwe/models/et/goodweEtMeterControl.ts new file mode 100644 index 0000000..7ea8775 --- /dev/null +++ b/src/connections/goodwe/models/et/goodweEtMeterControl.ts @@ -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, + }, + }, +});