Skip to content

Commit

Permalink
Add MVP websocket connectrion
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Manke committed Oct 8, 2021
1 parent ee45f1f commit edb1ad0
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import WallboxConfig from './lib/WallboxConfig.svelte'
import ConnectButton from './lib/ConnectButton.svelte'
import WallboxSimulator from './lib/wallbox-simulator/WallboxSimulator.svelte'
let webSocketUrl: string;
</script>
Expand All @@ -11,7 +11,7 @@
<WallboxConfig bind:connectionUrl={webSocketUrl} />
</p>
<p>
<ConnectButton bind:connectionUrl={webSocketUrl}/>
<WallboxSimulator bind:webSocketUrl={webSocketUrl} />
</p>
</main>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
<script lang="ts">
import { connectStation, disconnectStation } from './websocket';
export let connectionUrl: string = 'not provided';
export let webSocket: WebSocket;
let connectionState: 'disconnected' | 'connecting' | 'connected' | 'error' = 'disconnected';
let buttonText: string;
let errorMessage: string;
const setConnectionState = (connected: boolean) => {
connected ? connectionState = 'connected' : connectionState = 'disconnected';
}
const setErrorMessage = (message: string) => {
errorMessage = message;
}
const addLogMessage = (message: string) => {
console.log(message);
}
$: {
switch (connectionState) {
Expand All @@ -23,11 +39,15 @@
const connect = () => {
if (connectionState === 'disconnected') {
connectionState = 'connecting';
alert(`Connecting to: ${connectionUrl}`)
} else if (connectionState === 'connecting') {
connectionState = 'connected';
} else {
connectionState = 'disconnected';
webSocket = connectStation(
webSocket,
connectionUrl,
setConnectionState,
setErrorMessage,
addLogMessage
);
} else if (connectionState === 'connected') {
webSocket = disconnectStation(webSocket, setConnectionState);
}
}
</script>
Expand Down
10 changes: 10 additions & 0 deletions src/lib/wallbox-simulator/WallboxSimulator.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
import ConnectButton from './ConnectButton.svelte'
export let webSocketUrl = 'wss://connection/string...'
let webSocket: WebSocket;
</script>

<p>
<ConnectButton bind:connectionUrl={webSocketUrl} bind:webSocket={webSocket}/>
</p>
37 changes: 37 additions & 0 deletions src/lib/wallbox-simulator/ocpp-messages/bootNotification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export function BootNotification(websocket) {
if (!websocket || !websocket.send) {
console.warn(
"No websocket connection found for sending BootNotification... aborting."
);
return;
}

var BN = JSON.stringify([
2,
randomMessageId(),
"BootNotification",
{
chargingStation: {
serialNumber: "SOME-serial-NUMBER-123",
model: "MGWB Fake",
vendorName: "Unicorn GmbH",
firmwareVersion: "1.2.3",
},
reason: "PowerUp",
},
]);

console.log("Sending BootNotification.");
websocket.send(BN);
console.log("BootNotification sent!");
}

function randomMessageId() {
const allowedChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let id = "";
for (var i = 0; i < 36; i++) {
id += allowedChars.charAt(Math.floor(Math.random() * allowedChars.length));
}
return id;
}
64 changes: 64 additions & 0 deletions src/lib/wallbox-simulator/websocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { BootNotification } from "./ocpp-messages/bootNotification";

export function disconnectStation(websocket, setConnectionState) {
if (websocket) {
websocket.close(3001);
setConnectionState(false);
console.log("Disconnected!");
} else {
console.log("Can't disconnect - no websocket connection found...");
}
return websocket;
}

export function connectStation(
websocket,
connectionString,
setConnectionState,
setErrorMessage,
addLogMessage
) {
setErrorMessage("");

if (websocket) {
websocket.close(3001);
setConnectionState(false);
}
websocket = new WebSocket(connectionString, ["ocpp2.0"]);

websocket.onopen = function (authorizationData) {
setConnectionState(true);

addLogMessage({
source: "CS simulator",
payload: "Websocket connection opened successfully!",
});
sessionStorage.setItem("LastAction", "BootNotification");
BootNotification(websocket);
};

websocket.onerror = function (errorEvent) {
if (errorEvent.target.readyState === 3) {
setErrorMessage("The connection was closed or could not be established.");
addLogMessage({
source: "CS simulator",
payload: "The connection was closed or could not be established.",
});
}
setConnectionState(false);
};

websocket.onclose = function () {
addLogMessage({
source: "CS simulator",
payload: "Websocket connection closed.",
});
setConnectionState(false);
};

websocket.onmessage = function (event) {
addLogMessage({ source: "CPO backend", payload: event.data });
};

return websocket;
}

0 comments on commit edb1ad0

Please sign in to comment.