Created
July 4, 2020 16:19
-
-
Save rosswilson/f1ebecea84a6f9a04bd540647c9723b8 to your computer and use it in GitHub Desktop.
Node.js TCP server to transmit messages to connected clients when GPIO events happen. Can be used to mute and unmute a Zoom call with a physical button.
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
const net = require("net"); | |
const { spawn } = require("child_process"); | |
const client = new net.Socket(); | |
client.connect(9000, "192.168.1.26", function () { | |
console.log("Connected to button server"); | |
}); | |
const unmuteScript = ` | |
tell application "System Events" | |
tell process "zoom.us" | |
click menu item "Unmute audio" of menu "Meeting" of menu bar 1 | |
end tell | |
end tell | |
`; | |
const muteScript = ` | |
tell application "System Events" | |
tell process "zoom.us" | |
click menu item "Mute audio" of menu "Meeting" of menu bar 1 | |
end tell | |
end tell | |
`; | |
client.on("data", function (data) { | |
console.log("Received: " + data); | |
const convertedData = Buffer.from(data).toString(); | |
if (convertedData === "RED\n") { | |
console.log("yo"); | |
const newProcess = spawn("osascript", ["-e", muteScript]); | |
newProcess.stdout.on("data", (data) => { | |
console.log(`stdout: ${data}`); | |
}); | |
newProcess.stderr.on("data", (data) => { | |
console.error(`stderr: ${data}`); | |
}); | |
newProcess.on("close", (code) => { | |
console.log(`child process exited with code ${code}`); | |
}); | |
} | |
if (convertedData === "GREEN\n") { | |
const newProcess = spawn("osascript", ["-e", unmuteScript]); | |
newProcess.stdout.on("data", (data) => { | |
console.log(`stdout: ${data}`); | |
}); | |
newProcess.stderr.on("data", (data) => { | |
console.error(`stderr: ${data}`); | |
}); | |
newProcess.on("close", (code) => { | |
console.log(`child process exited with code ${code}`); | |
}); | |
} | |
}); | |
client.on("close", function () { | |
console.log("Connection closed"); | |
}); |
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
const net = require('net'); | |
const Gpio = require('onoff').Gpio; | |
const buttonRed = new Gpio(27, 'in', 'both', { debounceTimeout: 10 }); | |
const buttonGreen = new Gpio(17, 'in', 'both', { debounceTimeout: 10 }); | |
buttonRed.watch((err, value) => { | |
if (err) { | |
throw err; | |
} | |
if (value) { | |
currentClients.forEach((client) => client.write("RED\n")); | |
console.log("Red button press!"); | |
} | |
}); | |
buttonGreen.watch((err, value) => { | |
if (err) { | |
throw err; | |
} | |
if (value) { | |
currentClients.forEach((client) => client.write("GREEN\n")); | |
console.log("Green button press!"); | |
} | |
}); | |
const server = net.createServer(); | |
let currentClients = []; | |
let connectionId = 0; | |
server.on('connection', handleConnection); | |
server.listen(9000, function () { | |
console.log(`Buttons server listening on port 9000`); | |
}); | |
function handleConnection(conn) { | |
const remoteAddress = conn.remoteAddress + ':' + conn.remotePort; | |
conn.connectionId = connectionId++; | |
currentClients = [...currentClients, conn]; | |
console.log(`[${conn.connectionId}] New client connection from ${remoteAddress}`); | |
conn.on('data', onConnectionData); | |
conn.once('close', onConnectionClose); | |
conn.on('error', onConnectionError); | |
function onConnectionData(data) { | |
console.log(`[${conn.connectionId}] Message from ${remoteAddress}: ${data}`); | |
conn.write("ACK"); | |
} | |
function onConnectionClose() { | |
currentClients = currentClients.filter(({ connectionId }) => connectionId !== conn.connectionId); | |
console.log(`[${conn.connectionId}] Connection from ${remoteAddress} closed`); | |
} | |
function onConnectionError(error) { | |
console.log(`[${conn.connectionId}] Connection ${remoteAddress} error: ${error.message}`); | |
} | |
} | |
process.on('SIGINT', _ => { | |
buttonRed.unexport(); | |
buttonGreen.unexport(); | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment