Skip to content

Commit

Permalink
stop transaction implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Nasar165 committed May 5, 2024
1 parent 727e9c9 commit 6b55cfc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 9 deletions.
10 changes: 5 additions & 5 deletions app/service/ocpp/command/remote/remote.start.transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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 { SendStartTransaction } from '../charging/start.transaction';
import Validate from '@/app/helper/validation.helper';
import { CreateError, ErrorCode } from '../../ocpp.error';
import {
IRemoteStartTransaction,
IRemoteStartTransactionRes,
RemoteStartTransaction,
Status,
IRemoteStartTransaction,
RemoteStartTransaction,
} 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,
Expand Down
13 changes: 13 additions & 0 deletions app/service/ocpp/command/remote/remote.stop.transaction.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Min } from 'class-validator';

interface IRemoteStopTransaction {
transactionId: number;
}

class RemoteStopTransaction implements IRemoteStopTransaction {
@Min(0)
transactionId: number = 0;
}

export type { IRemoteStopTransaction };
export { RemoteStopTransaction };
62 changes: 62 additions & 0 deletions app/service/ocpp/command/remote/remote.stop.transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 {
IRemoteStopTransaction,
RemoteStopTransaction,
} from './remote.stop.transaction.model';
import { GetSession } from '../charging/start.transaction';
import Validate from '@/app/helper/validation.helper';
import { CreateError, ErrorCode } from '../../ocpp.error';
import {
IRemoteStartTransactionRes,
Status,
} from './remote.start.transaction.model';
import { SendStopTransaction } from '../charging/stop.transaction';
import { ChangeState } from '../../ocpp.handler';

export function RemoteStopTransactionReq(
w: IWriter,
frame: IRequest,
state: StatusNotification,
changeState: ChangeState
): void {
let status: IRemoteStartTransactionRes = { status: Status.REJECTED };
if (state == StatusNotification.CHARGING) {
const [result, validation] = Validate<IRemoteStopTransaction>(
RemoteStopTransaction,
frame.payload
);

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

status.status = Status.ACCEPTED;
const session = GetSession();
if (session.transactionId != result.transactionId) {
w.Write(
CreateError(ErrorCode.SecurityError, {
error: 'transaction id violation',
})
);
return;
}

SendStopTransaction(
w,
session.connectorId,
session.idTag,
session.transactionId,
changeState
);
}

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

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

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

function FindAction(action: string): ActionItem {
Expand Down
7 changes: 4 additions & 3 deletions app/service/ocpp/ocpp.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ function getFullFrame(frame: BaseTuple): [CallType, OCPPData] {
function processCall(
w: IWriter,
frame: IRequest,
state: StatusNotification
state: StatusNotification,
changeState: ChangeState
): void {
const handler = FindAction(frame.action);
handler.handel(w, frame, state);
handler.handel(w, frame, state, changeState);
}

function processReturn(
Expand Down Expand Up @@ -79,7 +80,7 @@ function handleFrame(
): void {
const [call, result] = getFullFrame(frame);
if (call == CallType.CALL) {
processCall(w, result as IRequest, state);
processCall(w, result as IRequest, state, changeState);
} else {
processReturn(w, result, changeState);
}
Expand Down

0 comments on commit 6b55cfc

Please sign in to comment.