Skip to content

Commit

Permalink
Merge pull request #28 from Nasar165/remote-start
Browse files Browse the repository at this point in the history
Remote start
  • Loading branch information
Nasar165 authored May 5, 2024
2 parents e99aba7 + 3f7cd82 commit 727e9c9
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 27 deletions.
24 changes: 15 additions & 9 deletions app/components/evse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import { SendStatusNotification } from '../service/ocpp/command/status-notificat
import Transaction from './transaction';

const defaultValue = 'ws://localhost:8080/ocpp/JwNpTpPxPm/CHR202305102';

const connectorId = 1;
export default function Evse() {
const [url, setUrl] = useState(defaultValue);
const [online, setOnline] = useState(false);
const writer = useRef<Array<IWriter>>([]);
const [socket, setSocket] = useState<IChargingSocket>(new ChargingSocket());
const [socket, setSocket] = useState<Array<IChargingSocket>>([
new ChargingSocket(),
]);

const onlineChange: ConState = (connected: boolean, w?: IWriter) => {
setOnline(connected);
Expand All @@ -35,20 +37,21 @@ export default function Evse() {
setUrl(event.currentTarget.value);
};

const onMessage = (ev: MessageEvent) => {
const onMessage: (ev: MessageEvent) => void = (ev: MessageEvent) => {
if (writer == null) return;
HandleOcpp(writer.current[0], ev.data, changeState);
HandleOcpp(writer.current[0], ev.data, socket[0].State, changeState);
};

const changeState = (
state: StatusNotification,
error?: ChargePointErrorCodes
) => {
setSocket({ ...socket, State: state });
socket[0].State = state;
setSocket([...socket]);
if (writer.current[0] == null) return;
SendStatusNotification(
writer.current[0],
0,
connectorId,
error ?? ChargePointErrorCodes.NoError,
state
);
Expand All @@ -71,14 +74,17 @@ export default function Evse() {
onMessage={onMessage}
online={online}
/>
<StatusNotificationUI state={socket.State} changeState={changeState} />
<StatusNotificationUI
state={socket[0].State}
changeState={changeState}
/>
</div>

<div className={online ? '' : 'hidden'}>
<Transaction
writer={writer.current[0]}
connectorId={0}
state={socket.State}
connectorId={connectorId}
state={socket[0].State}
changeState={changeState}
/>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/components/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React, { SyntheticEvent, useState } from 'react';
import {
GetSession,
SendStartTransaction,
} from '../service/ocpp/charging/start.transaction';
} from '../service/ocpp/command/charging/start.transaction';
import Input from './input';
import Button from './button';
import { IWriter } from '../service/websocket/websocket.model';
import { StatusNotification } from '../service/ocpp/command/status-notification/status.notification';
import { SendStopTransaction } from '../service/ocpp/charging/stop.transaction';
import { SendStopTransaction } from '../service/ocpp/command/charging/stop.transaction';
import { ChangeState } from '../service/ocpp/ocpp.handler';
import Select, { ReturnValue } from './select';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Validate from '@/app/helper/validation.helper';
import { IWriter } from '../../websocket/websocket.model';
import { Action, CreateRequestFrame, GetRequestFrame } from '../ocpp.action';
import { CreateError, ErrorCode } from '../ocpp.error';
import { IResponse } from '../ocpp.frame';
import { ChangeState } from '../ocpp.handler';
import { CreateTransaction } from '../transaction/transaction.handler';
import { IWriter } from '../../../websocket/websocket.model';
import { Action, CreateRequestFrame, GetRequestFrame } from '../../ocpp.action';
import { CreateError, ErrorCode } from '../../ocpp.error';
import { IResponse } from '../../ocpp.frame';
import { ChangeState } from '../../ocpp.handler';
import { CreateTransaction } from '../../transaction/transaction.handler';
import {
AuthorizationStatus,
IChargingSession,
IStartTransaction,
StartTransactionsRes,
} from './start.transaction.model';
import { StatusNotification } from '../command/status-notification/status.notification';
import { StatusNotification } from '../status-notification/status.notification';

let session: IChargingSession;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IWriter } from '../../websocket/websocket.model';
import { StatusNotification } from '../command/status-notification/status.notification';
import { Action, CreateRequestFrame, GetRequestFrame } from '../ocpp.action';
import { ChangeState } from '../ocpp.handler';
import { CreateTransaction } from '../transaction/transaction.handler';
import { IWriter } from '../../../websocket/websocket.model';
import { StatusNotification } from '../status-notification/status.notification';
import { Action, CreateRequestFrame, GetRequestFrame } from '../../ocpp.action';
import { ChangeState } from '../../ocpp.handler';
import { CreateTransaction } from '../../transaction/transaction.handler';
import { ResetSession } from './start.transaction';
import { IStopTransaction } from './stop.transaction.model';

Expand Down
29 changes: 29 additions & 0 deletions app/service/ocpp/command/remote/remote.start.transaction.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { IsOptional, IsString, Min, Max } from 'class-validator';

enum Status {
ACCEPTED = 'Accepted',
REJECTED = 'Rejected',
}

interface IRemoteStartTransactionRes {
status: Status;
}

interface IRemoteStartTransaction {
connectorId: number;
idTag: string;
}

class RemoteStartTransaction implements IRemoteStartTransaction {
@Min(0)
@Max(1)
@IsOptional()
connectorId: number = 0;
@IsString()
idTag: string = '';
@IsOptional()
chargingProfile: unknown;
}

export type { IRemoteStartTransactionRes, IRemoteStartTransaction };
export { RemoteStartTransaction, Status };
51 changes: 51 additions & 0 deletions app/service/ocpp/command/remote/remote.start.transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { IWriter } from '@/app/service/websocket/websocket.model';
import { IRequest } from '../../ocpp.frame';
import { StatusNotification } from '../status-notification/status.notification';
import { CreateResponseFrame } from '../../ocpp.action';
import {
IRemoteStartTransaction,
IRemoteStartTransactionRes,
RemoteStartTransaction,
Status,
} from './remote.start.transaction.model';
import { SendStartTransaction } from '../charging/start.transaction';
import Validate from '@/app/helper/validation.helper';
import { CreateError, ErrorCode } from '../../ocpp.error';

export function RemoteStartTransactionReq(
w: IWriter,
frame: IRequest,
state: StatusNotification
): void {
console.log(state);

let status: IRemoteStartTransactionRes = { status: Status.REJECTED };
if (
state == StatusNotification.AVAILABLE ||
state == StatusNotification.PREPARING ||
state == StatusNotification.FINISHING
) {
const [result, validation] = Validate<IRemoteStartTransaction>(
RemoteStartTransaction,
frame.payload
);

if (validation.length > 0) {
w.Write(CreateError(ErrorCode.PropertyConstraintViolation, validation));
return;
}

if (result.connectorId == 0) {
result.connectorId = 1;
}

status.status = Status.ACCEPTED;
SendStartTransaction(w, result.connectorId, result.idTag);
}

const response = CreateResponseFrame<IRemoteStartTransactionRes>(
frame.uuid,
status
);
w.Write(response);
}
23 changes: 23 additions & 0 deletions app/service/ocpp/ocpp.action.list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IWriter } from '../websocket/websocket.model';
import { RemoteStartTransactionReq } from './command/remote/remote.start.transaction';
import { StatusNotification } from './command/status-notification/status.notification';
import { Action } from './ocpp.action';
import { ErrorCode } from './ocpp.error';
import { IRequest } from './ocpp.frame';

type ActionItem = {
name: string;
handel: (w: IWriter, frame: IRequest, state: StatusNotification) => void;
};

const List: Array<ActionItem> = [
{ name: Action.REMOTE_START_TRANSACTION, handel: RemoteStartTransactionReq },
];

function FindAction(action: string): ActionItem {
const handler = List.find((a) => a.name == action);
if (handler == null) throw new Error(ErrorCode.NotImplemented);
return handler;
}

export { FindAction };
16 changes: 12 additions & 4 deletions app/service/ocpp/ocpp.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
StatusNotification,
} from './command/status-notification/status.notification';
import { FindTransaction } from './transaction/transaction.handler';
import { FindAction } from './ocpp.action.list';

type OCPPData = IRequest | IResponse | IErrorFrame;
type ChangeState = (
Expand Down Expand Up @@ -44,8 +45,13 @@ function getFullFrame(frame: BaseTuple): [CallType, OCPPData] {
return [callType, data];
}

function processCall(frame: IRequest) {
console.log(frame);
function processCall(
w: IWriter,
frame: IRequest,
state: StatusNotification
): void {
const handler = FindAction(frame.action);
handler.handel(w, frame, state);
}

function processReturn(
Expand All @@ -68,11 +74,12 @@ function processReturn(
function handleFrame(
w: IWriter,
frame: BaseTuple,
state: StatusNotification,
changeState: ChangeState
): void {
const [call, result] = getFullFrame(frame);
if (call == CallType.CALL) {
processCall(result as IRequest);
processCall(w, result as IRequest, state);
} else {
processReturn(w, result, changeState);
}
Expand All @@ -97,13 +104,14 @@ function handlerError(err: Error, w: IWriter): void {
export function HandleOcpp(
w: IWriter,
json: string,
state: StatusNotification,
changeState: ChangeState
): void {
try {
const data: unknown = JSON.parse(json);
if (!Array.isArray(data)) throw new Error(ErrorCode.ProtocolError);
let frame = isValidFrame(data);
handleFrame(w, frame, changeState);
handleFrame(w, frame, state, changeState);
} catch (error: unknown) {
handlerError(error as Error, w);
}
Expand Down

0 comments on commit 727e9c9

Please sign in to comment.