-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
86 lines (67 loc) · 2.78 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const { exec } = require('child_process');
const TouchPortalAPI = require('touchportal-api');
const TPClient = new TouchPortalAPI.Client();
const pluginId = 'com.github.orangopus.mediamixer';
// Define the function to set application volume
function setApplicationVolume(applicationName, volumePercentage) {
const findSinkInputCommand = `pactl list sink-inputs`;
exec(findSinkInputCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error listing sink inputs: ${stderr}`);
return;
}
const sinkInput = stdout.split('\n\n').find(input => input.includes(`application.name = "${applicationName}"`));
if (!sinkInput) {
console.error(`Application '${applicationName}' not found in sink inputs.`);
return;
}
const match = sinkInput.match(/Sink Input #(\d+)/);
if (!match) {
console.error(`Sink input index not found for application '${applicationName}'.`);
return;
}
const sinkInputIndex = match[1];
const setVolumeCommand = `pactl set-sink-input-volume ${sinkInputIndex} ${volumePercentage}%`;
console.log(`Executing command: ${setVolumeCommand}`);
exec(setVolumeCommand, (error, stdout, stderr) => {
if (error) {
console.error(`Error setting volume: ${stderr}`);
return;
}
console.log(`Set volume of '${applicationName}' to ${volumePercentage}%`);
// Send a message to Touch Portal
console.log(`Sending connector update for '${applicationName}' to ${volumePercentage}%`);
});
});
}
// Event listener for ConnectorChange
TPClient.on("ConnectorChange", (data) => {
console.log("Connector event received:", data);
const applicationName = data.data.find(d => d.id === "applicationName").value;
const volumePercentage = data.value;
console.log(`Received data: applicationName=${applicationName}, volumePercentage=${volumePercentage}`)
setApplicationVolume(applicationName, volumePercentage);
if (!applicationName || isNaN(volumePercentage)) {
console.error(`Invalid data received: applicationName=${applicationName}, volumePercentage=${volumePercentage}`);
return;
}
});
// Event listener for Info
TPClient.on("Info", (data) => {
console.log("Info event received:", data);
});
// Event listener for ClosePlugin
TPClient.on("ClosePlugin", () => {
console.log("Plugin closed");
process.exit();
});
// Event listener for Connected
TPClient.on("Connected", () => {
console.log("Connected to Touch Portal");
});
// Connect to Touch Portal
TPClient.connect({ pluginId });
// Add a small delay to keep the script running
setTimeout(() => {
console.log("Waiting for events...");
}, 1000);