Skip to content
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

Meter value #30

Merged
merged 5 commits into from
May 5, 2024
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
22 changes: 17 additions & 5 deletions app/components/evse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { SyntheticEvent, useRef, useState } from 'react';
import Input from './input';
import { ConState, IWriter } from '../service/websocket/websocket.model';
import WebSocket from './WebSocket';
import WebSocket from './web.socket';
import { ChargingSocket, IChargingSocket } from '../service/ocpp/connector';
import { HandleOcpp } from '../service/ocpp/ocpp.handler';
import { SendBootNotification } from '../service/ocpp/command/boot-notification/boot.notification';
Expand All @@ -14,6 +14,7 @@ import {
} from '../service/ocpp/command/status-notification/status.notification';
import { SendStatusNotification } from '../service/ocpp/command/status-notification/statusnotification';
import Transaction from './transaction';
import MeterValue from './meter.value';

const defaultValue = 'ws://localhost:8080/ocpp/JwNpTpPxPm/CHR202305102';
const connectorId = 1;
Expand Down Expand Up @@ -74,10 +75,12 @@ export default function Evse() {
onMessage={onMessage}
online={online}
/>
<StatusNotificationUI
state={socket[0].State}
changeState={changeState}
/>
{online ? (
<StatusNotificationUI
state={socket[0].State}
changeState={changeState}
/>
) : null}
</div>

<div className={online ? '' : 'hidden'}>
Expand All @@ -88,6 +91,15 @@ export default function Evse() {
changeState={changeState}
/>
</div>
{socket[0].State == StatusNotification.CHARGING ? (
<div>
<MeterValue
state={socket[0].State}
w={writer.current[0]}
connectorId={connectorId}
/>
</div>
) : null}
</div>
);
}
59 changes: 59 additions & 0 deletions app/components/meter.value.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useEffect, useState } from 'react';
import Select, { ReturnValue } from './select';
import { meterValueList } from './transaction';
import { StatusNotification } from '../service/ocpp/command/status-notification/status.notification';
import Button from './button';
import { SendMeterValue } from '../service/ocpp/command/meter-value/meter.value';
import { IWriter } from '../service/websocket/websocket.model';

const DEFAULT_INTERVAL = 20000;

type meterValue = {
w: IWriter;
connectorId: number;
state: StatusNotification;
};

export default function MeterValue({
w,
connectorId,
state,
}: meterValue): React.JSX.Element {
const [meter, setMeter] = useState<Array<number>>([250]);
const meterValue = { name: meter.toString(), value: meter[0] };

const onSelectChange = (item: ReturnValue) => {
meter[0] = item as number;
setMeter([...meter]);
};

const onClick = () => {
SendMeterValue(w, connectorId, meter[0]);
};

useEffect(() => {
console.info('start interval');
const id = setInterval(onClick, DEFAULT_INTERVAL - 19000);
return () => {
console.info('clean up interval');
clearInterval(id);
};
}, []);

return (
<div className='border border-black p-2 my-2 rounded-md'>
<p className='my-2'>Meter value</p>
<p className='text-xs'>Notes: Meter Values are sent every 20 seconds</p>
<Select
items={meterValueList}
onChange={onSelectChange}
defaultItem={meterValue}
/>
<Button
onClick={onClick}
text='Send Meter Values'
disabled={state != StatusNotification.CHARGING}
/>
</div>
);
}
6 changes: 4 additions & 2 deletions app/components/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ export default function Transaction({
onChange={onChange}
disabled={false}
/>
<p className='my-2'>Meter value</p>
<p className='my-2'>Stop Transaction Meter value</p>
<Select
items={meterValueList}
onChange={onSelectChange}
defaultItem={meterValue}
/>
<div className='grid gap-2 grid-cols-2'>
<div className='grid gap-2 grid-cols-1 md:grid-cols-2'>
<Button
onClick={onStart}
text='Start Transaction'
Expand All @@ -117,3 +117,5 @@ export default function Transaction({
</div>
);
}

export { meterValueList };
File renamed without changes.
Empty file.
32 changes: 32 additions & 0 deletions app/service/ocpp/command/meter-value/meter.value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IWriter } from '../../../websocket/websocket.model';
import { Action, CreateRequestFrame, GetRequestFrame } from '../../ocpp.action';
import { CreateTransaction } from '../../transaction/transaction.handler';
import { GetSession } from '../charging/start.transaction';

function SendMeterValue(w: IWriter, connectorId: number, value: number): void {
const session = GetSession();
const payload: any = {
connectorId,
meterValue: [
{
context: 'Sample.Periodic',
value: value,
unit: 'W',
measurand: 'Power.Active.Import',
},
],
};

if (session != null && session.transactionId != 0)
payload['transactionId'] = session.transactionId;

const frame = CreateRequestFrame(Action.METER_VALUES, payload);
CreateTransaction(GetRequestFrame(frame), MeterValue);
w.Write(frame);
}

function MeterValue(): void {
console.log('meter value accepted by CSMS');
}

export { SendMeterValue, MeterValue };
Loading