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

33 bug add ability to display status messages form system components #34

Merged
Show file tree
Hide file tree
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
5 changes: 0 additions & 5 deletions .husky/pre-commit

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/pre-push

This file was deleted.

16 changes: 10 additions & 6 deletions src/lib/Navbar.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<script lang="ts">
import StatusText from '$lib/StatusText.svelte';
</script>

<section>
<nav>
<a href="/">Home</a>
<a href="/map">Map</a>
</nav>
</section>
<div>
<section>
<nav>
<a href="/">Home</a>
<a href="/map">Map</a>
</nav>
<StatusText />
</section>
</div>

<style>
section {
Expand Down
110 changes: 110 additions & 0 deletions src/lib/StatusText.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<script lang="ts">
import { onMount, onDestroy } from 'svelte';

var mapData = [];
$: Text = '';

/**
* Function to find the index of a given sourceIdentifier in the mapData array.
* @param {Object} sourceIdentifier - The source identifier to search for in mapData.
* @returns {number} - The index of the sourceIdentifier in mapData, or -1 if not found.
*/
function GetIdentifierIndex(sourceIdentifier) {
let index = -1;
for (let i = 0; i < mapData.length; i++) {
if (JSON.stringify(mapData[i].sourceIdentifier) === JSON.stringify(sourceIdentifier)) {
index = i;
break;
}
}

return index;
}

/**
* Function to update the mapData array with new chunkData for a given sourceIdentifier.
* If the dataIndex is -1, a new entry is added. Otherwise, the existing entry at dataIndex is updated.
* @param {number} dataIndex - The index where the sourceIdentifier is found, or -1 if not found.
* @param {Object} sourceIdentifier - The source identifier to be updated or added.
* @param {Object} chunkData - The data to be associated with the sourceIdentifier.
*/
function UpdateMapData(dataIndex, sourceIdentifier, chunkData) {
if (dataIndex == -1) {
if (mapData.length == 0) {
dataIndex = 0;
mapData[dataIndex] = {
sourceIdentifier: sourceIdentifier,
value: chunkData
};
} else {
dataIndex = mapData.length;
mapData[dataIndex] = {
sourceIdentifier: sourceIdentifier,
value: chunkData
};
}
} else {
mapData[dataIndex].value = chunkData;
}
}

function updateDisplaytext() {
let tmp = '';

for (let i = 0; i < mapData.length; i++) {
tmp = tmp + '\n' + JSON.stringify(mapData[i].value.SystemInfo);
}

Text = tmp;
}

function updateItemInMap(StatusParsedData) {
// Start by checking if we have seen this source identifier before
let dataIndex = GetIdentifierIndex(StatusParsedData['SystemInfo']['StatName']);
UpdateMapData(dataIndex, StatusParsedData['SystemInfo']['StatName'], StatusParsedData);
}

let StatWebSocket = null;
function connectWebSocket() {
StatWebSocket = new WebSocket('ws://localhost:10101/DataTypes/SystemInfo');

StatWebSocket.addEventListener('open', () => {
console.log('Stat WebSocket connected');
});

StatWebSocket.addEventListener('close', (event) => {
console.log('Stat WebSocket closed', event);
//setTimeout(connectWebSocket, 5000); // Attempt to reconnect after 1 second
});

StatWebSocket.addEventListener('error', (error) => {
console.log('Stat WebSocket error', error);
StatWebSocket.close();
});

StatWebSocket.addEventListener('message', async (event) => {
var StatusParsedData = JSON.parse(event.data);
updateItemInMap(StatusParsedData);
updateDisplaytext();
});
}

function closeWebSockets() {
if (StatWebSocket) {
StatWebSocket.close();
}
}

onDestroy(() => {
closeWebSockets();
mapData = null;
});

onMount(() => {
connectWebSocket(); // Initial connection
});
</script>

<div>
{Text}
</div>
6 changes: 4 additions & 2 deletions svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ const config = {
'va.vercel-scripts.com',
'vercel.live',
// @ts-expect-error csp directives are inferred as strings https://github.com/sveltejs/kit/issues/11906
'ws://localhost:10100/'
'ws://localhost:10100/',
'ws://localhost:10101/'
],
'connect-src': [
'self',
'vitals.vercel-insights.com',
'vitals.vercel-analytics.com',
// @ts-expect-error csp directives are inferred as strings https://github.com/sveltejs/kit/issues/11906
'ws://localhost:10100/'
'ws://localhost:10100/',
'ws://localhost:10101/'
]
}
}
Expand Down
Loading