Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
NodeJS & RaspberryPi - onoff ignoring setTimeout

I am setting up a small node server which can handle the switching on/off of some hard disks I've connected to my RaspberryPi. Now, I wrote a simple handler for the switching off, which should take care of 2 tasks:

  • unmounting the hard disk;
  • switch off the pin;

A non-elegant solution I've found so far, is to execute a command, wait for a certain amount of time, then switch off the hard disk:

const Gpio = require('onoff').Gpio;
const {exec} = require('child_process');
    handlers.switchOff = (data, callback) => {
        if (data.meta.destination != ''
            && data.pin != '') {
            let unmountCommand = "sudo -S umount -f -l " + data.meta.destination;
            let pinOut = new Gpio(data.pin, 'out');
            exec(unmountCommand, (error, stdout, stderr) => {
                    return '';
            });
            setTimeout(function(){
                pinOut.write(0);
                console.log('switched off');
            },10000);
            callback(200, { 'message': 'OK' });
        }
        callback(500, { 'message': 'SERVER ERROR' });
    };

But the behaviour of the setTimeout's filling is unexpected: it executes immediately the pinOut.write(0), while the console.log() is exectued after more or less 10 seconds. Any hint or leads about this behaviour? What am I missing here?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda