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

Feat: Implement websocketOptions as arg to CS connection #58

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion docs/modules/cp/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const chargePoint = new ChargePoint(
**Signature**

```ts
async connect(): Promise<Connection<CentralSystemAction<'v1.6-json'>>>
async connect(websocketOptions: WebsocketOptions): Promise<Connection<CentralSystemAction<'v1.6-json'>>>
```

### sendRequest (method)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/jest": "^26.0.22",
"@types/node": "^22.10.2",
"@types/uuid": "^8.3.0",
"@types/ws": "^7.4.1",
"@typescript-eslint/eslint-plugin": "^4.21.0",
Expand Down
11 changes: 8 additions & 3 deletions src/cp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Connection, SUPPORTED_PROTOCOLS } from '../ws';
import { ChargePointAction, chargePointActions } from '../messages/cp';
import { EitherAsync, Left } from 'purify-ts';
import { OCPPRequestError, ValidationError } from '../errors';
import { OCPPVersion } from '../types';
import { OCPPVersion, WebsocketOptions } from '../types';

export type CPSendRequestArgs<T extends ChargePointAction<V>, V extends OCPPVersion> = {
ocppVersion: 'v1.6-json',
Expand Down Expand Up @@ -45,6 +45,7 @@ export type CPSendRequestArgs<T extends ChargePointAction<V>, V extends OCPPVers
*
* @category Charge Point
*/

export default class ChargePoint {
private connection?: Connection<CentralSystemAction<'v1.6-json'>>;

Expand All @@ -54,9 +55,13 @@ export default class ChargePoint {
private readonly csUrl: string
) { }

async connect(): Promise<Connection<CentralSystemAction<'v1.6-json'>>> {
async connect(websocketOptions: WebsocketOptions): Promise<Connection<CentralSystemAction<'v1.6-json'>>> {
websocketOptions
const url = `${this.csUrl}/${this.id}`;
const socket = new WebSocket(url, SUPPORTED_PROTOCOLS);
const socket = new WebSocket(url, SUPPORTED_PROTOCOLS, {
auth: `${this.id}:${websocketOptions.auth}`,
...websocketOptions,
});

const connection = new Connection(
socket,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export interface IBootNotificationInput {

export interface IBootNotificationOutput {
/** urn://Ocpp/Cs/2012/06/#RegistrationStatus(Accepted,Rejected) */
status: "Accepted" | "Rejected" | 'Pending';
status: "Accepted" | "Rejected" | "Pending";
/** urn://Ocpp/Cs/2012/06/#s:dateTime(undefined) */
currentTime: string;
/** urn://Ocpp/Cs/2012/06/#s:int(undefined) */
Expand Down
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import WebSocket from "ws";
import * as http from 'http';

export type Timestamp = Date; //TODO: maybe string?

export type OCPPVersionV16 = 'v1.6-json';
export type OCPPVersionV15 = 'v1.5-soap';
export type OCPPVersion = OCPPVersionV16 | OCPPVersionV15;
export type OCPPVersion = OCPPVersionV16 | OCPPVersionV15;

export type WebsocketOptions = http.ClientRequestArgs & WebSocket.ClientOptions;
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,13 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f"
integrity sha512-6QlRuqsQ/Ox/aJEQWBEJG7A9+u7oSYl3mem/K8IzxXG/kAGbV1YPD9Bg9Zw3vyxC/YP+zONKwy8hGkSt1jxFMw==

"@types/node@^22.10.2":
version "22.10.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.2.tgz#a485426e6d1fdafc7b0d4c7b24e2c78182ddabb9"
integrity sha512-Xxr6BBRCAOQixvonOye19wnzyDiUtTeqldOOmj3CkeblonbccA12PFwlufvRdrpjXxqnmUaeiU5EOA+7s5diUQ==
dependencies:
undici-types "~6.20.0"

"@types/normalize-package-data@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"
Expand Down Expand Up @@ -4457,6 +4464,11 @@ underscore@~1.7.0:
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.7.0.tgz#6bbaf0877500d36be34ecaa584e0db9fef035209"
integrity sha1-a7rwh3UA02vjTsqlhODbn+8DUgk=

undici-types@~6.20.0:
version "6.20.0"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.20.0.tgz#8171bf22c1f588d1554d55bf204bc624af388433"
integrity sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==

union-value@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.1.tgz#0b6fe7b835aecda61c6ea4d4f02c14221e109847"
Expand Down
Loading