0
I am developing a Node application for Guardhouse Communication (API) and I am having trouble changing the value of a variable. follows below the code:
var net = require("net");
var express = require('express');
var http = require('http');
var cors = require('cors');
var app = express();
var server = http.createServer(app);
var bodyParser = require('body-parser');
app.use(cors());
app.options('*', cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser({limit: '500mb'}));
server.listen(8080, function(){
console.log('\nlistening on *:8080');
});
var client = net.createConnection(/*ip da aplicação*/);
client.on("connect", function(data){
console.log(`linear conectado porta: 9001`);
});
client.on("data", function(data){
var hex = data.toString(`hex`);
console.log(data);
console.log('recebe dados: ' +hex+'\n-- end --\n');
});
client.on("end", function(data){
console.log(`end: `);
console.log(data);
});
app.get("/teste", (req, res) => {
var acao = req.query.acao;
//http://easyonlineconverter.com/converters/checksum_converter.html
//https://www.rapidtables.com/convert/number/hex-to-decimal.html
var data_hora = [0x00, 0x0C];
var id = [0x00, 0x03];
var dispositivos = [0x00, 0x46];
var lbl_veiculos = [0x00, 0x1A];
var reiniciar = [0x00, 0x12];
var atualizar = [0x00, 0x1D];
// terimnar alterar data
var alterar_data = [0x00, 0x0B, 30, 12, 2033, 12, 16, 11];
switch(acao){
case 'data_hora':
var bytes = data_hora;
break;
case 'id':
var bytes = id;
break;
case 'dispositivos':
var bytes = dispositivos;
break;
case 'lbl_veiculos':
var bytes = lbl_veiculos;
break;
case 'atualizar':
var bytes = atualizar;
break;
case 'reiniciar':
var bytes = reiniciar;
break;
case 'alterar_data':
var bytes = alterar_data;
break;
}
checksum = 0;
var checksum = bytes.reduce((x, y) => x + y);
bytes.push(checksum);
var buffer = new Buffer.from(bytes);
console.log('\nwrite sent: ' + bytes,'\n');
client.write(buffer);
res.send("OK");
});
To do the test I call localhost in the browser with the command as querystring. EX:
- http://localhost:8080/teste?acao=alterar_data -> chama função alterar data
My problem is that when running the first time with the Node operation reschedule it does everything right, but if I try to change the variable it acts as if I haven’t made modifications, and only act normally (sending the changes) when I close the application Ode and start again, follow the print’s:
-First change (expected behaviour):
-Second amendment (modified the contents of the variable, however the answer is the same):
-Third change (restarted Node application using Runner code. variable value is presented as rewrite):
Does anyone know why it is necessary to restart Node for this variable to be rewritten? There is a way for me to get around this situation without having to end my application?