Я использую Node.js для создания приложения, которое может считывать данные модуля USB LoRa и управлять выводами Raspberry Pi GPIO.
Существует три основных функции:
Установить GPIO для включения (сигнал с контакта 0)
Выключить GPIO (сигнал с контакта 1)
Установить GPIO навыключить через некоторое время (используя функцию setTimeout
)
Основные функции работают хорошо.
Проблема в том, что в цикле setTimeout
Я хочу прекратить его в любое время.
Я пытался использовать clearTimeout
для отмены цикла setTimeout
, но, похоже, он не работает.Я что-то не так делаю?
Например.
Использование строки "TurnOn15s" в пакете, полученном LoRa.Я не могу прекратить время ожидания в любое время.Я должен ждать до конца 15 секунд, чтобы отключить контакты GPIO.Если я вызову функцию deviceTurnOff (1) во время выполнения тайм-аута, я получу два выхода после окончания цикла.
Вывод программы выглядит следующим образом:
// Output by `deviceTurnOn(0,15000)` from received LoRa packet.
Received:0, the solenoid valve has been opened!
Received packet: !20!96 0 0 0 53 0 0 28 11 3 TurnOn15s RSSI: -40 SNR: 6
// Output by `setTimeout` function.
Received:1, the solenoid valve has been closed!
// Output by deviceTurnOff(1) from received LoRa packet.
Received:1, the solenoid valve has been closed!
Received packet: !18!96 0 0 0 53 0 0 29 9 3 TurnOff RSSI: -41 SNR: 7
Здесьчастичный код:
var turnOffTimeout = null; // turnOffTimeout
// Control Raspberry Pi GPIO pins.
function deviceTurnOn(controlValueType, timeout) {
// Do Turn On Raspberry Pi GPIO pins.
if (timeout == -1) {
// if no time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
} else {
// if contains time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
turnOffTimer(timeout);
}
}
// Control Raspberry Pi GPIO pins.
function deviceTurnOff(controlValueType) {
// Do Turn Off Raspberry Pi GPIO pins.
console.log(`Received:${controlValueType}, the solenoid valve has been closed!`);
OutputDevice.writeSync(controlValueType);
// clearTimeout
if (turnOffTimeout) {
clearTimeout(turnOffTimeout);
}
}
// Raspberry Pi GPIO pins turn off timeout.
function turnOffTimer(timeout) {
turnOffTimeout = setTimeout(function() {
deviceTurnOff(1);
}, timeout);
}
Содержит три функции: deviceTurnOn
, deviceTurnOff
, turnOffTimer
Формат вызова функции:
deviceTurnOff(1);
deviceTurnOn(0,timeout);
// Timeout is milliseconds
Обновление;
Я пытался изменить свою программу следующим образом, я получаю тот же вывод, что и код предыдущей версии, проблема все еще не решена.
var turnOffTimeout = null; // turnOffTimeout
// Control Raspberry Pi GPIO pins.
function deviceTurnOn(controlValueType, timeout) {
// Do Turn On Raspberry Pi GPIO pins.
if (timeout == -1) {
// if no time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
}
else {
// if contains time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
// clearTimeout.
clearTimeout(turnOffTimeout);
// set new timeout.
turnOffTimeout = setTimeout(function () {
deviceTurnOff(1);
}, timeout);
}
}
// Control Raspberry Pi GPIO pins.
function deviceTurnOff(controlValueType) {
// Do Turn Off Raspberry Pi GPIO pins.
console.log(`Received:${controlValueType}, the solenoid valve has been closed!`);
clearTimeout(turnOffTimeout);
OutputDevice.writeSync(controlValueType);
}