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

add basic auth setting to chargepoint #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions src/cp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ export default class ChargePoint {
constructor(
readonly id: string,
private readonly requestHandler: RequestHandler<CentralSystemAction<'v1.6-json'>, ValidationError | undefined, 'v1.6-json'>,
private readonly csUrl: string
private readonly csUrl: string,
private readonly basicAuth: {username:string,password:string} | undefined = undefined,
) { }

async connect(): Promise<Connection<CentralSystemAction<'v1.6-json'>>> {
const url = `${this.csUrl}/${this.id}`;
const socket = new WebSocket(url, SUPPORTED_PROTOCOLS);
const authHeader = this.basicAuth ? {
'authorization': 'Basic ' + Buffer.from(`${this.basicAuth.username}:${this.basicAuth.password}`).toString('base64')
}: {};
const socket = new WebSocket(url, SUPPORTED_PROTOCOLS,{
headers: authHeader,
});

const connection = new Connection(
socket,
Expand All @@ -78,7 +84,7 @@ export default class ChargePoint {
/**
* @example
* import { ChargePoint } from '@voltbras/ts-ocpp';
*
*
* async function communicate(chargePoint: ChargePoint) {
* const response = await chargePoint.sendRequest({ action: 'Heartbeat', ocppVersion: 'v1.6-json', payload: {}});
* // it can be used in a functional way
Expand Down