-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Felix Manke
committed
Oct 8, 2021
1 parent
ee45f1f
commit edb1ad0
Showing
5 changed files
with
138 additions
and
7 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
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,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
37
src/lib/wallbox-simulator/ocpp-messages/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,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; | ||
} |
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,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; | ||
} |