-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Nasar165/bootnotification
Bootnotification
- Loading branch information
Showing
15 changed files
with
383 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { plainToClass } from 'class-transformer'; | ||
import { validateSync, ValidationError } from 'class-validator'; | ||
|
||
type ValidationResult<T> = [T, Array<ValidationError>]; | ||
|
||
const defaultValue = { | ||
enableDebugMessages: false, | ||
validationError: { target: false }, | ||
}; | ||
|
||
export default function Validate<T>(c: any, obj: unknown): ValidationResult<T> { | ||
const result: object = plainToClass(c, obj); | ||
const validation = validateSync(result, defaultValue); | ||
return [result as T, validation]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
'use client'; | ||
|
||
import Evse from './components/evse'; | ||
|
||
export default function Home() { | ||
|
94 changes: 94 additions & 0 deletions
94
app/service/ocpp/command/boot-notification/bootnotification.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { | ||
IsNumber, | ||
IsEnum, | ||
Min, | ||
Max, | ||
MinLength, | ||
MaxLength, | ||
IsOptional, | ||
IsString, | ||
} from 'class-validator'; | ||
|
||
enum Status { | ||
ACCEPTED = 'Accepted', | ||
REJECTED = 'Rejected', | ||
PENDING = 'Pending', | ||
} | ||
|
||
interface IBootNotification { | ||
chargePointVendor: string; | ||
chargePointModel: string; | ||
chargeBoxSerialNumber: string; | ||
chargePointSerialNumber: string; | ||
firmwareVersion: string; | ||
iccid: string; | ||
imsi: string; | ||
meterSerialNumber: string; | ||
meterType: string; | ||
} | ||
interface IBootNotificationRes { | ||
currentTime: Date; | ||
interval: number; | ||
status: Status; | ||
} | ||
|
||
class BootNotification implements IBootNotification { | ||
@MinLength(1) | ||
@MaxLength(20) | ||
chargePointVendor = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(20) | ||
chargePointModel = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(25) | ||
@IsOptional() | ||
chargeBoxSerialNumber = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(25) | ||
@IsOptional() | ||
chargePointSerialNumber = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(50) | ||
@IsOptional() | ||
firmwareVersion = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(20) | ||
@IsOptional() | ||
iccid = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(20) | ||
@IsOptional() | ||
imsi = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(25) | ||
@IsOptional() | ||
meterSerialNumber = ''; | ||
|
||
@MinLength(1) | ||
@MaxLength(25) | ||
@IsOptional() | ||
meterType = ''; | ||
} | ||
|
||
class BootNotificationRes implements IBootNotificationRes { | ||
@IsString() | ||
currentTime: Date = new Date(); | ||
|
||
@IsNumber() | ||
@Min(3600) | ||
@Max(84000) | ||
interval = 0; | ||
|
||
@IsEnum(Status) | ||
status = Status.PENDING; | ||
} | ||
|
||
export type { IBootNotification }; | ||
export { Status, BootNotification, BootNotificationRes }; |
81 changes: 81 additions & 0 deletions
81
app/service/ocpp/command/boot-notification/bootnotification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { DEFAULT_TIMER, IWriter } from '../../../websocket/websocket.model'; | ||
import { Action, CreateRequestFrame, GetRequestFrame } from '../../ocpp.action'; | ||
import { IResponse } from '../../ocpp.frame'; | ||
import { NewTransaction } from '../../transaction/transaction.handler'; | ||
import { | ||
BootNotificationRes, | ||
IBootNotification, | ||
Status, | ||
} from './bootnotification.model'; | ||
import { CreateError, ErrorCode } from '../../ocpp.error'; | ||
import Validate from '@/app/helper/validation.helper'; | ||
|
||
const defaultValue: IBootNotification = { | ||
chargePointVendor: 'EW', | ||
chargePointModel: 'CHR22', | ||
chargeBoxSerialNumber: 'CR-00', | ||
chargePointSerialNumber: 'CR-00', | ||
firmwareVersion: '', | ||
iccid: '', | ||
imsi: '', | ||
meterSerialNumber: '', | ||
meterType: 'power', | ||
}; | ||
|
||
let id: ReturnType<typeof setTimeout>; | ||
let success = false; | ||
|
||
function retry(w: IWriter): void { | ||
id = setTimeout(() => { | ||
try { | ||
SendBootNotification(w); | ||
} catch (error) { | ||
clearTimeout(id); | ||
} | ||
}, DEFAULT_TIMER); | ||
} | ||
|
||
function SendBootNotification(w: IWriter): void { | ||
try { | ||
clearTimeout(id); | ||
if (success) return; | ||
const frame = CreateRequestFrame(Action.BOOT_NOTIFICATION, defaultValue); | ||
NewTransaction(GetRequestFrame(frame), BootNotification); | ||
w.Write(frame); | ||
} catch (error) { | ||
retry(w); | ||
} | ||
} | ||
|
||
function BootNotification(w: IWriter, frame: IResponse): void { | ||
clearTimeout(id); | ||
const [result, validation] = Validate<BootNotificationRes>( | ||
BootNotificationRes, | ||
frame.payload | ||
); | ||
|
||
console.log(validation); | ||
|
||
if (validation.length > 0) { | ||
w.Write(CreateError(ErrorCode.PropertyConstraintViolation, validation)); | ||
return retry(w); | ||
} | ||
|
||
if (result.status == Status.PENDING) { | ||
w.Write( | ||
CreateError(ErrorCode.NotSupported, { | ||
err: 'Pending functionality is not supported', | ||
}) | ||
); | ||
|
||
return retry(w); | ||
} | ||
|
||
if (result.status == Status.REJECTED) { | ||
return retry(w); | ||
} | ||
|
||
success = true; | ||
} | ||
|
||
export { SendBootNotification, BootNotification }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.