2024-12-09 00:56:55 -06:00
|
|
|
|
|
|
|
import QRCode from 'modules/qrcode';
|
2024-12-17 00:10:12 -06:00
|
|
|
import { onQRCodeRendered } from 'src/main/Renderer';
|
2025-01-06 20:35:57 -06:00
|
|
|
import { toast, ToastIcon } from '../components/Toast';
|
|
|
|
|
|
|
|
const connectionStatusText = document.getElementById("connection-status-text");
|
|
|
|
const connectionStatusSpinner = document.getElementById("connection-spinner");
|
|
|
|
const connectionStatusCheck = document.getElementById("connection-check");
|
|
|
|
let connections = JSON.parse(localStorage.getItem('connections')) ?? [];
|
|
|
|
if (connections.length > 0) {
|
|
|
|
connections.forEach(connection => {
|
|
|
|
onConnect(connection);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.targetAPI.onStartupStorageClear((_event, value: any) => {
|
|
|
|
localStorage.clear();
|
|
|
|
localStorage.setItem('connections', JSON.stringify(connections));
|
|
|
|
});
|
2024-12-09 00:56:55 -06:00
|
|
|
|
|
|
|
window.targetAPI.onDeviceInfo(renderIPsAndQRCode);
|
2025-01-06 20:35:57 -06:00
|
|
|
window.targetAPI.onConnect((_event, value: any) => {
|
|
|
|
connections.push(value.id);
|
|
|
|
localStorage.setItem('connections', JSON.stringify(connections));
|
|
|
|
onConnect(value);
|
|
|
|
});
|
|
|
|
window.targetAPI.onDisconnect((_event, value: any) => {
|
|
|
|
console.log(`Device disconnected: ${JSON.stringify(value)}`);
|
|
|
|
const index = connections.indexOf(value.id);
|
|
|
|
if (index != -1) {
|
|
|
|
connections.splice(index, 1);
|
|
|
|
localStorage.setItem('connections', JSON.stringify(connections));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connections.length === 0) {
|
|
|
|
connectionStatusText.textContent = 'Waiting for a connection';
|
|
|
|
connectionStatusSpinner.style.display = 'inline-block';
|
|
|
|
connectionStatusCheck.style.display = 'none';
|
|
|
|
toast("Device disconnected", ToastIcon.INFO);
|
|
|
|
}
|
|
|
|
});
|
2024-12-09 00:56:55 -06:00
|
|
|
|
|
|
|
if(window.targetAPI.getDeviceInfo()) {
|
|
|
|
console.log("device info already present");
|
|
|
|
renderIPsAndQRCode();
|
|
|
|
}
|
|
|
|
|
2025-01-06 20:35:57 -06:00
|
|
|
function onConnect(value: any) {
|
|
|
|
console.log(`Device connected: ${JSON.stringify(value)}`);
|
|
|
|
connectionStatusText.textContent = 'Connected: Ready to cast';
|
|
|
|
connectionStatusSpinner.style.display = 'none';
|
|
|
|
connectionStatusCheck.style.display = 'inline-block';
|
|
|
|
}
|
|
|
|
|
2024-12-09 00:56:55 -06:00
|
|
|
function renderIPsAndQRCode() {
|
|
|
|
const value = window.targetAPI.getDeviceInfo();
|
|
|
|
console.log("device info", value);
|
|
|
|
|
|
|
|
const ipsElement = document.getElementById('ips');
|
|
|
|
if (ipsElement) {
|
|
|
|
ipsElement.innerHTML = `IPs<br>${value.addresses.join('<br>')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fcastConfig = {
|
|
|
|
name: value.name,
|
|
|
|
addresses: value.addresses,
|
|
|
|
services: [
|
|
|
|
{ port: 46899, type: 0 }, //TCP
|
|
|
|
{ port: 46898, type: 1 }, //WS
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
const json = JSON.stringify(fcastConfig);
|
|
|
|
let base64 = btoa(json);
|
|
|
|
base64 = base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
|
|
const url = `fcast://r/${base64}`;
|
|
|
|
console.log("qr", {json, url, base64});
|
|
|
|
|
|
|
|
const qrCodeElement = document.getElementById('qr-code');
|
|
|
|
QRCode.toCanvas(qrCodeElement, url, {
|
|
|
|
margin: 0,
|
|
|
|
width: 256,
|
|
|
|
color: {
|
|
|
|
dark : "#000000",
|
|
|
|
light : "#ffffff",
|
|
|
|
},
|
|
|
|
errorCorrectionLevel : "M",
|
|
|
|
},
|
2024-12-17 22:59:41 -06:00
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(`Error rendering QR Code: ${err}`);
|
2025-01-06 20:35:57 -06:00
|
|
|
toast(`Error rendering QR Code: ${err}`, ToastIcon.ERROR);
|
2024-12-17 22:59:41 -06:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log(`Rendered QR Code`);
|
|
|
|
}
|
2024-12-09 00:56:55 -06:00
|
|
|
});
|
2024-12-17 00:10:12 -06:00
|
|
|
|
|
|
|
onQRCodeRendered();
|
2024-12-09 00:56:55 -06:00
|
|
|
}
|