如何同时处理多个帖子请求

时间:2018-03-21 23:23:31

标签: node.js

我需要处理来自GPS提供商(货运公司)的POST请求,以便当卡车违反某些规则时,使用Raspberry Pi GPIO + Relay闪烁X秒。

我有以下问题: 当我在同时收到多个POST请求时,闪烁重叠,这是我不想要的。我希望在忽略闪光灯时收到的POST请求。

以下是代码:

var app = require('express')();
var http = require('http').Server(app);
var myParser = require("body-parser");
var io = require('socket.io')(http);
var moment = require('moment');
var Gpio = require('onoff').Gpio, // Constructor function for Gpio objects.
  led1 = new Gpio(4, 'out'),         // Export GPIO17 as an output.
  led2 = new Gpio(17, 'out'),
  iv;
var busy = 0;
console.log("Inicio " + busy);

led1.writeSync(0);
led2.writeSync(0);

app.use(myParser.urlencoded({extended : true}));

function timestamp(fecha, hora){
  var a = new Date(fecha + ' ' + hora);
  return moment(a,'MMM, DD, YYYY hh:mm:ss a', false).add(-6,'hour').format('DD/MM/YY HH:mm');
}

function unidad(device){
  var res = device.split(" ");
  return res[0];
}

//ALERTA ROJA
app.post("/geotab/alerta_roja/", function(request, response) {
    if (busy == 0) {
        busy = 1;
    console.log(busy);
        response.send("200");
        console.log("--Alerta Roja-- " + request.body.duracion);
        io.emit('chat message', timestamp(request.body.fecha, request.body.hora) + " : Alerta Roja - " + request.body.regla);

        iv = setInterval(function () {
            led1.writeSync(led1.readSync() ^ 1); // 1 = on, 0 = off :)
        }, 200);

        setTimeout(function () {
         clearInterval(iv); // Stop blinking
        led1.writeSync(0);  // Turn LED off.
        // led.unexport();    // Unexport GPIO and free resources
        }, 1000 * request.body.duracion);
    busy = 0;
    console.log(busy);
    } else {
    console.log("Ocupada");
    }
});

//ALERTA AMARILLA
app.post("/geotab/alerta_amarilla/", function(request, response) {
    response.send("200");
    console.log("--Alerta Amarilla-- " + request.body.duracion);
    io.emit('chat message', timestamp(request.body.fecha, request.body.hora) + " : Alerta Amarilla - " + request.body.regla);

    iv = setInterval(function () {
        led2.writeSync(led2.readSync() ^ 1); // 1 = on, 0 = off :)
       }, 200);

    setTimeout(function () {
       clearInterval(iv); // Stop blinking
    led2.writeSync(0);  // Turn LED off.
    // led.unexport();    // Unexport GPIO and free resources
    }, 1000 * request.body.duracion);
});

app.get('/',function(req, res){
  res.sendFile(__dirname + '/index.html');
  led.writeSync(0);  // Turn LED off.
});

io.on('connection', function(socket){
  console.log('Usuario Conectado');
  socket.on('disconnect', function(){
  LED.writeSync(0); // Turn LED off
  LED.unexport(); // Unexport GPIO to free resources 
 console.log('Usuario Desconectado');
  });
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    console.log('message: ' + msg);
  });
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('Escuchando en puerto 3000');
});

1 个答案:

答案 0 :(得分:1)

听起来您需要在服务器中保留一个标志,告诉您LED当前是否正在闪烁。并且,如果设置了该标志,则忽略任何其他想要启动闪烁的操作。

如果将blink逻辑封装到每个人都调用的单个函数中,那么您可以在该函数中设置,清除并检查该标志。

例如,您可以使用此功能来控制led1:

let currentlyBlinking = false;
let led1Interval;

function initiateBlink(blinkDuration = 5000, blinkInterval = 500) {
    if (!currentlyBlinking) {
        currentlyBlinking = true;

        // stop blinking after blinkDuration
        setTimeout(stopBlink, blinkDuration);

        // turn on initially, then toggle afterwards
        led1.writeSync(1);
        led1Interval = setInterval(() => {
            led1.writeSync(led1.readSync() ^ 1); // 1 = on, 0 = off :)
        }, blinkInterval);
    }

}

function stopBlink() {
    clearInterval(led1Interval);
    currentlyBlinking = false;
    led1.writeSync(0);
}

然后,不要直接访问代码中的任何位置led1。仅使用这些功能启动闪烁或停止当前闪烁。如果您有多个LED都具有相似的功能,您可以将其封装在一个简单的对象中,只为每个LED创建一个对象,该LED的状态将是每个对象的实例变量。

如果您可以访问异步读/写操作,那么从规模的角度来看,这些操作会更好。