Skip to content

Commit

Permalink
Add storageModeStatus to DERStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Nov 18, 2024
1 parent cc828fe commit edd818a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 14 deletions.
84 changes: 70 additions & 14 deletions src/sep2/models/derStatus.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
import { it, expect } from 'vitest';
import { it, expect, describe } from 'vitest';
import { objectToXml } from '../helpers/xml.js';
import { generateDerStatusResponse } from './derStatus.js';
import { ConnectStatusValue } from './connectStatus.js';
import { OperationalModeStatusValue } from './operationModeStatus.js';
import { StorageModeStatusValue } from './storageModeStatus.js';

it('should generate DERStatus XML', () => {
const response = generateDerStatusResponse({
readingTime: new Date(1682475028 * 1000),
operationalModeStatus: {
dateTime: new Date(1682475028 * 1000),
value: OperationalModeStatusValue.OperationalMode,
},
genConnectStatus: {
dateTime: new Date(1682475028 * 1000),
value: ConnectStatusValue.Connected,
},
describe('generateDerStatusResponse', () => {
it('should generate DERStatus XML', () => {
const response = generateDerStatusResponse({
readingTime: new Date(1682475028 * 1000),
operationalModeStatus: {
dateTime: new Date(1682475028 * 1000),
value: OperationalModeStatusValue.OperationalMode,
},
genConnectStatus: {
dateTime: new Date(1682475028 * 1000),
value: ConnectStatusValue.Connected,
},
});

const xml = objectToXml(response);

expect(xml).toBe(`<?xml version="1.0"?>
<DERStatus xmlns="urn:ieee:std:2030.5:ns">
<readingTime>1682475028</readingTime>
<operationalModeStatus>
<dateTime>1682475028</dateTime>
<value>2</value>
</operationalModeStatus>
<genConnectStatus>
<dateTime>1682475028</dateTime>
<value>01</value>
</genConnectStatus>
</DERStatus>`);
});

const xml = objectToXml(response);
it('should generate optional storage XML', () => {
const response = generateDerStatusResponse({
readingTime: new Date(1682475028 * 1000),
operationalModeStatus: {
dateTime: new Date(1682475028 * 1000),
value: OperationalModeStatusValue.OperationalMode,
},
genConnectStatus: {
dateTime: new Date(1682475028 * 1000),
value: ConnectStatusValue.Connected,
},
storConnectStatus: {
dateTime: new Date(1682475028 * 1000),
value: ConnectStatusValue.Connected,
},
stateOfChargeStatus: {
dateTime: new Date(1682475028 * 1000),
value: 50,
},
storageModeStatus: {
dateTime: new Date(1682475028 * 1000),
value: StorageModeStatusValue.StorageCharging,
},
});

expect(xml).toBe(`<?xml version="1.0"?>
const xml = objectToXml(response);

expect(xml).toBe(`<?xml version="1.0"?>
<DERStatus xmlns="urn:ieee:std:2030.5:ns">
<readingTime>1682475028</readingTime>
<operationalModeStatus>
Expand All @@ -30,5 +73,18 @@ it('should generate DERStatus XML', () => {
<dateTime>1682475028</dateTime>
<value>01</value>
</genConnectStatus>
<storConnectStatus>
<dateTime>1682475028</dateTime>
<value>01</value>
</storConnectStatus>
<stateOfChargeStatus>
<dateTime>1682475028</dateTime>
<value>50</value>
</stateOfChargeStatus>
<storageModeStatus>
<dateTime>1682475028</dateTime>
<value>2</value>
</storageModeStatus>
</DERStatus>`);
});
});
11 changes: 11 additions & 0 deletions src/sep2/models/derStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { numberToHex } from '../../helpers/number.js';
import { operationalModeStatusSchema } from './operationModeStatus.js';
import { z } from 'zod';
import { stateOfChargeStatusSchema } from './stateOfChargeStatus.js';
import { storageModeStatusSchema } from './storageModeStatus.js';

export const derStatusSchema = z.object({
readingTime: z.coerce.date(),
operationalModeStatus: operationalModeStatusSchema,
genConnectStatus: connectStatusSchema,
storConnectStatus: connectStatusSchema.optional(),
stateOfChargeStatus: stateOfChargeStatusSchema.optional(),
storageModeStatus: storageModeStatusSchema.optional(),
});

export type DERStatus = z.infer<typeof derStatusSchema>;
Expand All @@ -22,6 +24,7 @@ export function generateDerStatusResponse({
genConnectStatus,
storConnectStatus,
stateOfChargeStatus,
storageModeStatus,
}: DERStatus) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const response: { DERStatus: any } = {
Expand Down Expand Up @@ -55,5 +58,13 @@ export function generateDerStatusResponse({
};
}

if (storageModeStatus) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
response.DERStatus.storageModeStatus = {
dateTime: dateToStringSeconds(storageModeStatus.dateTime),
value: operationalModeStatus.value.toString(),
};
}

return response;
}
19 changes: 19 additions & 0 deletions src/sep2/models/storageModeStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { z } from 'zod';

// DER StorageModeStatus value:
// 0 – storage charging
// 1 – storage discharging
// 2 – storage holding
// All other values reserved.
export enum StorageModeStatusValue {
StorageCharging = 0,
StorageDischarging = 1,
StorageHolding = 2,
}

const storageModeStatusValueSchema = z.nativeEnum(StorageModeStatusValue);

export const storageModeStatusSchema = z.object({
dateTime: z.coerce.date(),
value: storageModeStatusValueSchema,
});

0 comments on commit edd818a

Please sign in to comment.