Skip to content

Commit

Permalink
Add more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
longzheng committed Sep 3, 2024
1 parent 88a616f commit 983242e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 95 deletions.
39 changes: 39 additions & 0 deletions src/helpers/influxdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { FallbackControl } from '../sep2/helpers/derControls';
import { numberWithPow10 } from './number';
import type { SiteMonitoringSample } from '../coordinator/helpers/siteMonitoring';
import type { DerMonitoringSample } from '../coordinator/helpers/derMonitoring';
import type { InverterControlLimit } from '../coordinator/helpers/inverterController';

const influxDB = new InfluxDB({
url: `http://influxdb:${process.env['INFLUXDB_PORT']}`,
Expand Down Expand Up @@ -308,3 +309,41 @@ export function writeInverterControllerPoints({
),
]);
}

export function writeAmberPrice(number: number | undefined) {
influxDbWriteApi.writePoints(
[
number !== undefined
? new Point('amber').floatField('price', number)
: null,
].filter((point) => point !== null),
);
}

export function writeControlLimit({
limit,
name,
}: {
limit: InverterControlLimit;
name: string;
}) {
const point = new Point('controlLimit').tag('name', name);

if (limit.opModConnect !== undefined) {
point.booleanField('opModConnect', limit.opModConnect);
}

if (limit.opModEnergize !== undefined) {
point.booleanField('opModEnergize', limit.opModEnergize);
}

if (limit.opModExpLimW !== undefined) {
point.floatField('opModExpLimW', limit.opModExpLimW);
}

if (limit.opModGenLimW !== undefined) {
point.floatField('opModGenLimW', limit.opModGenLimW);
}

influxDbWriteApi.writePoint(point);
}
58 changes: 1 addition & 57 deletions src/limiters/amber/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
it,
vi,
} from 'vitest';
import { AmberLimiter, getControlLimitFromIntervals } from '.';
import { AmberLimiter } from '.';
import { setupServer } from 'msw/node';
import { HttpResponse, http } from 'msw';

Expand Down Expand Up @@ -133,59 +133,3 @@ describe('AmberControlLimit', () => {
});
});
});

describe('getControlLimitFromIntervals', () => {
it('should return correct control limit with active interval', () => {
vi.setSystemTime(new Date('2024-01-01T00:00:01Z'));

const feedInIntervals = [
{
start: new Date('2024-01-01T00:00:00Z'),
end: new Date('2024-01-01T00:30:00Z'),
price: -60.0,
},
{
start: new Date('2024-01-01T00:30:00Z'),
end: new Date('2024-01-01T01:00:00Z'),
price: 30.0,
},
];

const result = getControlLimitFromIntervals(feedInIntervals);

expect(result).toEqual({
opModConnect: undefined,
opModEnergize: undefined,
opModExpLimW: 0,
opModGenLimW: undefined,
});
});

it('should return correct control limit when no applicable intervals', () => {
// january
vi.setSystemTime(new Date('2024-01-01T00:00:01Z'));

// all future intervals
const feedInIntervals = [
{
start: new Date('2024-02-01T00:00:00Z'),
end: new Date('2024-02-01T00:30:00Z'),
price: -60.0,
},
{
start: new Date('2024-02-01T00:30:00Z'),
end: new Date('2024-02-01T01:00:00Z'),
price: 30.0,
},
];

const result = getControlLimitFromIntervals(feedInIntervals);

expect(result).toEqual({
opModConnect: undefined,
opModEnergize: undefined,
opModExpLimW: undefined,
opModGenLimW: undefined,
});
});
});
73 changes: 37 additions & 36 deletions src/limiters/amber/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { LimiterType } from '../../coordinator/helpers/limiter';
import type { InverterControlLimit } from '../../coordinator/helpers/inverterController';
import type { Logger } from 'pino';
import { logger as pinoLogger } from '../../helpers/logger';
import { writeAmberPrice, writeControlLimit } from '../../helpers/influxdb';

type Interval = {
start: Date;
Expand Down Expand Up @@ -32,7 +33,29 @@ export class AmberLimiter implements LimiterType {
}

getInverterControlLimit(): InverterControlLimit {
return getControlLimitFromIntervals(this.feedInIntervals);
const price = this.getCurrentPrice();

const limit =
price && price < 0
? {
// if feed in price is negative, limit export to 0
opModConnect: undefined,
opModEnergize: undefined,
opModExpLimW: 0,
opModGenLimW: undefined,
}
: {
// can't find current interval, assume export is fine
// if feed in price is positive, export is fine
opModConnect: undefined,
opModEnergize: undefined,
opModExpLimW: undefined,
opModGenLimW: undefined,
};

writeControlLimit({ limit, name: 'amber' });

return limit;
}

private async getSiteFeedInPrices() {
Expand Down Expand Up @@ -86,42 +109,20 @@ export class AmberLimiter implements LimiterType {
);
}
}
}

export function getControlLimitFromIntervals(
feedInIntervals: Interval[],
): InverterControlLimit {
// find current feed in price
const now = new Date();
const currentInterval = feedInIntervals.find(
(interval) => interval.start <= now && now < interval.end,
);

// can't find current interval, assume export is fine
if (!currentInterval) {
return {
opModConnect: undefined,
opModEnergize: undefined,
opModExpLimW: undefined,
opModGenLimW: undefined,
};
}
private getCurrentPrice() {
// find current feed in price
const now = new Date();
const currentInterval = this.feedInIntervals.find(
(interval) => interval.start <= now && now < interval.end,
);

// if feed in price is positive, export is fine
if (currentInterval.price >= 0) {
return {
opModConnect: undefined,
opModEnergize: undefined,
opModExpLimW: undefined,
opModGenLimW: undefined,
};
}
this.logger.trace({ currentInterval }, 'Current interval ');

const currentPrice = currentInterval?.price;

// if feed in price is negative, limit export to 0
return {
opModConnect: undefined,
opModEnergize: undefined,
opModExpLimW: 0,
opModGenLimW: undefined,
};
writeAmberPrice(currentPrice);

return currentPrice;
}
}
7 changes: 6 additions & 1 deletion src/limiters/fixed/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { InverterControlLimit } from '../../coordinator/helpers/inverterController';
import type { LimiterType } from '../../coordinator/helpers/limiter';
import type { Config } from '../../helpers/config';
import { writeControlLimit } from '../../helpers/influxdb';

type FixedLimiterConfig = NonNullable<Config['limiters']['fixed']>;

Expand All @@ -12,11 +13,15 @@ export class FixedLimiter implements LimiterType {
}

getInverterControlLimit(): InverterControlLimit {
return {
const limit = {
opModConnect: this.config.connect,
opModEnergize: this.config.connect,
opModExpLimW: this.config.exportLimitWatts,
opModGenLimW: this.config.generationLimitWatts,
};

writeControlLimit({ limit, name: 'fixed' });

return limit;
}
}
7 changes: 6 additions & 1 deletion src/limiters/sep2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { DerControlsHelperChangedData } from '../../sep2/helpers/derControl
import EventEmitter from 'events';
import type { LimiterType } from '../../coordinator/helpers/limiter';
import { numberWithPow10 } from '../../helpers/number';
import { writeControlLimit } from '../../helpers/influxdb';

export class Sep2Limiter
extends EventEmitter<{
Expand Down Expand Up @@ -72,7 +73,7 @@ export class Sep2Limiter
const opModGenLimW =
this.schedulerByControlType.opModGenLimW.getActiveScheduleDerControlBaseValue();

return {
const limit = {
opModExpLimW: opModExpLimW
? numberWithPow10(opModExpLimW.value, opModExpLimW.multiplier)
: undefined,
Expand All @@ -84,5 +85,9 @@ export class Sep2Limiter
opModConnect:
this.schedulerByControlType.opModConnect.getActiveScheduleDerControlBaseValue(),
};

writeControlLimit({ limit, name: 'sep2' });

return limit;
}
}

0 comments on commit 983242e

Please sign in to comment.