Working slider, resets after sliding
This commit is contained in:
		
							
								
								
									
										42
									
								
								app.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								app.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
const http = require('http'),
 | 
			
		||||
      fs = require('fs');
 | 
			
		||||
const { exec } = require('child_process');
 | 
			
		||||
const { parse } = require('querystring');
 | 
			
		||||
 | 
			
		||||
function setBrightness(display, value, res) {
 | 
			
		||||
  exec(`setbrightness "${value}"`, (error, stdout, stderr) => {
 | 
			
		||||
    if (error) {
 | 
			
		||||
      console.error(`exec error: ${error}`);
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    console.log(`stdout: ${stdout}`);
 | 
			
		||||
    console.log(`stderr: ${stderr}`);
 | 
			
		||||
    // Redirect the client back to the main page after the command has run
 | 
			
		||||
    res.writeHead(303, { Location: '/' });
 | 
			
		||||
    res.end();
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
http.createServer((req, res) => {
 | 
			
		||||
  if (req.url.startsWith('/button_pressed?')) {
 | 
			
		||||
    // Parse the query string from the request URL
 | 
			
		||||
    const query = req.url.split('?')[1];
 | 
			
		||||
    // Read the brightness value from the query string
 | 
			
		||||
    //const brightness = query.brightness;
 | 
			
		||||
    // Set the brightness using the value from the slider
 | 
			
		||||
    setBrightness(1, query, res);
 | 
			
		||||
  } else if (req.url === '/') {
 | 
			
		||||
    // Send the form when the root URL is requested
 | 
			
		||||
    let item = fs.readFile('./index.html', function(err, html) {
 | 
			
		||||
        if (err) {
 | 
			
		||||
            throw err; 
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            res.writeHead(200, { 'Content-Type': 'text/html' });
 | 
			
		||||
            res.end(html);
 | 
			
		||||
          }
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
}).listen(5000);
 | 
			
		||||
 | 
			
		||||
console.log('Server listening on port 5000');
 | 
			
		||||
							
								
								
									
										22
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								index.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
<head>
 | 
			
		||||
    <body>
 | 
			
		||||
        <form action="/button_pressed" method="get">
 | 
			
		||||
            <label for="brightness">Brightness:</label>
 | 
			
		||||
            <input type="range" id="brightness" name="brightness" min="0" max="100" onmouseup="updateBrightness(this.value)" ontouchend="updateBrightness(this.value)">
 | 
			
		||||
            <output id="brightness-value"></output>
 | 
			
		||||
            </form>
 | 
			
		||||
              <script>
 | 
			
		||||
                function updateBrightness(value) {
 | 
			
		||||
                  document.querySelector('#brightness-value').innerHTML = value;
 | 
			
		||||
                  window.location.href = '/button_pressed?' + value;
 | 
			
		||||
                }
 | 
			
		||||
        
 | 
			
		||||
                function submitForm(event) {
 | 
			
		||||
                  // Prevent the default form submission behavior
 | 
			
		||||
                  event.preventDefault();
 | 
			
		||||
                  // Submit the form manually
 | 
			
		||||
                  document.querySelector('form').submit();
 | 
			
		||||
                }
 | 
			
		||||
              </script>
 | 
			
		||||
    </body>
 | 
			
		||||
</head>
 | 
			
		||||
		Reference in New Issue
	
	Block a user