Home Automation (volume)

/* Beware! Windows 10, presses kesy on your keyboard By Sven Neumann <killroy@gmail.com> */ var http = require('http'); var robot = require('robotjs'); var child_process = require('child_process'); var KEY_DELAY = 10; var cmdSndVol = 'C:\\windows\\System32\\SndVol.exe -f 49825268'; var redirectHeader = { 'Content-Type': 'text/html', 'Location': '/', 'Cache-Control': 'private, no-cache, no-store, must-revalidate', 'Expires': -1, 'Pragma': 'no-cache' }; var homeHeader = { 'Content-Type': 'text/html', 'Cache-Control': 'private, no-cache, no-store, must-revalidate', 'Expires': -1, 'Pragma': 'no-cache' }; var css = ''+ 'display: block;'+ 'position: relative;'+ 'float: left;'+ 'width: 40%;'+ 'height: 10rem;'+ 'line-height: 10rem;'+ 'background: #333;'+ 'color: #fff;'+ 'font-family: sans-serif;'+ 'font-size: 3rem;'+ 'text-align: center;'+ 'border: 10px outset;'+ ''; var html = ''+ '<a href="/up" style="'+css+'">Up</a>'+ '<a href="/down" style="'+css+'">Down</a>'+ ''; function launchVolumeMixer() { console.info('launchVolumeMixer();'); return new Promise(function(resolve, reject) { var p = child_process.exec( cmdSndVol, function(err, stdout, stderr) { console.log('command ready'); if(err) return reject(err); resolve(stdout); } ); }); } function wait(ms) { return function() { return new Promise(function(resolve, reject) { setTimeout(resolve, ms); }); }; } function tap(key) { return function() { return new Promise(function(resolve, reject) { robot.keyTap(key); setTimeout(resolve, KEY_DELAY); }); }; } function adjustVolume(amount) { console.info('adjustVolume(%s);', amount); var volKey = 'up'; if(amount < 0) volKey = 'down'; amount = Math.abs(amount); var p = wait(100)(); for(var i = 0; i < amount; i++) p = p.then(tap(volKey)); p = p.then(tap('tab')).then(tap('space')).then(tap('escape')); return Promise.all([launchVolumeMixer(), p]); } var server = http.createServer(function(req, res) { console.log(req.url); if(req.url === '/up') { adjustVolume(5); res.writeHead(301, redirectHeader); res.end(); } else if(req.url === '/down') { adjustVolume(-5); res.writeHead(301, redirectHeader); res.end(); } else { res.writeHead(200, homeHeader); res.end(html); } }); server.listen(8888);
Simple node.js server to let me change the volume of my PC from my phone or tablet.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.