Skip to content

Adding SunSpec storage 124 Model #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/connections/sunspec/connection/inverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { type SettingsModel } from '../models/settings.js';
import { settingsModel } from '../models/settings.js';
import { type StatusModel } from '../models/status.js';
import { statusModel } from '../models/status.js';
import {
type StorageModel,
type StorageModelWrite,
} from '../models/storage.js';
import { storageModel } from '../models/storage.js';
import { SunSpecConnection } from './base.js';

export class InverterSunSpecConnection extends SunSpecConnection {
Expand All @@ -29,6 +34,11 @@ export class InverterSunSpecConnection extends SunSpecConnection {
// note: if any other software also writes to the controls model for properties we don't care about, we will overwrite them
private controlsModelCache: ControlsModel | null = null;

// this software expects to solely control the inverter
// cache the storage model once permanently to avoid unnecessary reads
// note: if any other software also writes to the storage model for properties we don't care about, we will overwrite them
private storageModelCache: StorageModel | null = null;

async getInverterModel() {
const modelAddressById = await this.getModelAddressById();

Expand Down Expand Up @@ -169,4 +179,45 @@ export class InverterSunSpecConnection extends SunSpecConnection {
values,
});
}

async getStorageModel() {
if (this.storageModelCache) {
return this.storageModelCache;
}

const modelAddressById = await this.getModelAddressById();

const address = modelAddressById.get(124);

if (!address) {
throw new Error('No SunSpec storage model address');
}

const data = await storageModel.read({
modbusConnection: this.modbusConnection,
address,
unitId: this.unitId,
});

this.storageModelCache = data;

return data;
}

async writeStorageModel(values: StorageModelWrite) {
const modelAddressById = await this.getModelAddressById();

const address = modelAddressById.get(124);

if (!address) {
throw new Error('No SunSpec storage model address');
}

return await storageModel.write({
modbusConnection: this.modbusConnection,
address,
unitId: this.unitId,
values,
});
}
}
Loading