Skip to content

Commit

Permalink
Use my Node Package for finding devices
Browse files Browse the repository at this point in the history
  • Loading branch information
richrace committed May 18, 2023
1 parent f8b018b commit a5de284
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 165 deletions.
73 changes: 36 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-multi-str */
/* eslint-disable no-console */
const { ipcRenderer } = require('electron');
const { remote } = require('electron');
const { findDevices, processDevices } = require('./lib/devices');
const { ipcRenderer } = require("electron");
const { remote } = require("electron");
const { getHeadphones } = require("arctis-usb-finder");

const deviceHTMLTemplate = " \
const deviceHTMLTemplate =
" \
<div id='{{ deviceID }}' class='device'> \
<div class='product'>{{ product }}</div> \
<div class='details'> \
Expand All @@ -19,7 +20,8 @@ const deviceHTMLTemplate = " \
</div> \
";

const deviceTurnedOffTemplate = " \
const deviceTurnedOffTemplate =
" \
<div id='{{ deviceID }}' class='device'> \
<div class='product'>{{ product }}</div> \
<div>This device is not connected or the headset is not turned on.</div> \
Expand All @@ -35,77 +37,74 @@ function checkWindowSize() {

let height = otherPadding;

height += deviceRow * document.querySelectorAll('.device').length;
height += deviceRow * document.querySelectorAll(".device").length;

height = Math.min(400, height);

win.setSize(width, height, animate);
}

function updateStatus() {
const devices = findDevices();
const devices = getHeadphones();

if (devices.length === 0) {
document.querySelector('.devices').innerHTML = '<div>Sorry no devices found!</div>';
document.querySelector(".devices").innerHTML =
"<div>Sorry no devices found!</div>";

return;
}

processDevices(devices, (device, status) => {
let percentage = status.batteryPercent > 100 ? 100 : status.batteryPercent;
devices.map((device, index) => {
let percentage = device.batteryPercent > 100 ? 100 : device.batteryPercent;
percentage = percentage < 0 ? 0 : percentage;

const muted = status.micStatus === 1 ? 'Yes' : 'No';
const charging = status.chargingInfo === 1;
const discharging = status.chargingInfo === 3;
const notConnected = status.chargingInfo === 0;
const muted = device.isMuted ? "Yes" : "No";
const charging = device.isCharging;
const discharging = device.isDischarging;
const notConnected = !device.isConnected;

const deviceID = `device-${device.productId}`;
const deviceID = `device-${index}`;

console.log('Rendering device: ', device);
console.log("Rendering device: ", device);
let html;

if (notConnected) {
console.log('Not connected');
console.log("Not connected");
html = `${deviceTurnedOffTemplate}`
.replace('{{ deviceID }}', deviceID)
.replace('{{ product }}', device.additional.name);
.replace("{{ deviceID }}", deviceID)
.replace("{{ product }}", device.modelName);
} else {
console.log('Connected');
const dischargingIcon = discharging ? 'down' : false;
const icon = charging ? 'up' : dischargingIcon;
let chargingIcon = '';
console.log("Connected");
const dischargingIcon = discharging ? "down" : false;
const icon = charging ? "up" : dischargingIcon;
let chargingIcon = "";

if (icon) {
chargingIcon = `<span class="icon icon-${icon}"></span>`;
}

html = `${deviceHTMLTemplate}`
.replace('{{ deviceID }}', deviceID)
.replace('{{ product }}', device.additional.name)
.replace('{{ batPercent }}', percentage)
.replace('{{ isMuted }}', muted)
.replace('{{ chargingStatus }}', chargingIcon);
.replace("{{ deviceID }}", deviceID)
.replace("{{ product }}", device.modelName)
.replace("{{ batPercent }}", percentage)
.replace("{{ isMuted }}", muted)
.replace("{{ chargingStatus }}", chargingIcon);
}

console.log('Rendering:', html);
console.log("Rendering:", html);
if (document.querySelector(`#${deviceID}`)) {
document.querySelector(`#${deviceID}`).outerHTML = html;
} else {
document.querySelector('.devices').insertAdjacentHTML('beforeend', html);
}

if (status.micStatus === undefined) {
document.querySelector(`#${deviceID}`).querySelector('.muted').remove();
document.querySelector(".devices").insertAdjacentHTML("beforeend", html);
}

checkWindowSize();
});
}

function init() {
document.querySelector('button#quit').addEventListener('click', () => {
ipcRenderer.send('quit-from-tray');
document.querySelector("button#quit").addEventListener("click", () => {
ipcRenderer.send("quit-from-tray");
});

updateStatus();
Expand All @@ -115,4 +114,4 @@ function init() {
setInterval(updateStatus, time);
}

document.addEventListener('DOMContentLoaded', init);
document.addEventListener("DOMContentLoaded", init);
72 changes: 0 additions & 72 deletions lib/devices.js

This file was deleted.

56 changes: 0 additions & 56 deletions lib/headphones.js

This file was deleted.

0 comments on commit a5de284

Please sign in to comment.