Skip to content

Commit

Permalink
try get away other callback deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollon77 committed May 9, 2024
1 parent 70eec45 commit 2d2fcd8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 59 deletions.
6 changes: 4 additions & 2 deletions src/mbus-master.cc
Original file line number Diff line number Diff line change
Expand Up @@ -610,15 +610,16 @@ NAN_METHOD(MbusMaster::ScanSecondary) {

MbusMaster* obj = node::ObjectWrap::Unwrap<MbusMaster>(info.This());

Nan::Callback *callback = new Nan::Callback(info[0].As<Function>());
if(obj->connected) {
obj->communicationInProgress = true;

Nan::Callback *callback = new Nan::Callback(info[0].As<Function>());
Nan::AsyncQueueWorker(new ScanSecondaryWorker(callback, &(obj->queueLock), obj->handle, &(obj->communicationInProgress)));
} else {
Local<Value> argv[] = {
Nan::Error("Not connected to port")
};
Nan::Call *callback = new Nan::Call(info[0].As<Function>());
callback->Call(1, argv);
}
info.GetReturnValue().SetUndefined();
Expand Down Expand Up @@ -797,15 +798,16 @@ NAN_METHOD(MbusMaster::SetPrimaryId) {

char *oldAddress = get(Nan::To<v8::String>(info[0]).ToLocalChecked(),"0");
int newAddress = (int)Nan::To<int64_t>(info[1]).FromJust();
Nan::Callback *callback = new Nan::Callback(info[2].As<Function>());
if(obj->connected) {
obj->communicationInProgress = true;

Nan::Callback *callback = new Nan::Callback(info[2].As<Function>());
Nan::AsyncQueueWorker(new SetPrimaryWorker(callback, oldAddress, newAddress, &(obj->queueLock), obj->handle, &(obj->communicationInProgress)));
} else {
Local<Value> argv[] = {
Nan::Error("Not connected to port")
};
Nan::Call *callback = new Nan::Call(info[2].As<Function>());
callback->Call(1, argv);
}
info.GetReturnValue().SetUndefined();
Expand Down
120 changes: 63 additions & 57 deletions test/testSerial.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,81 +155,87 @@ describe('Native libmbus node-module Serial test ...', function() {
};
}
const mbusMaster = new MbusMaster(mbusOptions);
const connectResult = mbusMaster.connect();
console.log(new Date().toString() + ': mbus-Serial-Master Open:' + connectResult);
if (!connectResult) {
socat.kill('SIGKILL');
testSocket && testSocket.destroy();
server.close();
}
var emergencyTimeout = setTimeout(function() {
socat.kill('SIGKILL');
testSocket && testSocket.destroy();
server.close();
done();
}, 60000); // Killswitch!
expect(mbusMaster.mbusMaster.connected).to.be.true;
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.false;
setTimeout(function() {
console.log(new Date().toString() + ': mbus-Serial-Master Send "Get 1"');

mbusMaster.getData(1, function(err, data) {
console.log(new Date().toString() + ': mbus-Serial-Master err: ' + err);
console.log(new Date().toString() + ': mbus-Serial-Master data: ' + JSON.stringify(data, null, 2));
expect(err).to.be.null;
expect(data.SlaveInformation.Id).to.be.equal(11490378);
expect(data.DataRecord[0].Value).to.be.equal(11490378);
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.false;

mbusMaster.getData(2, function(err, data) {

mbusMaster.getData(3, function(err, data) {
expect(err.message).to.be.equal('Not connected to port');

const connectResult = mbusMaster.connect();
console.log(new Date().toString() + ': mbus-Serial-Master Open:' + connectResult);
if (!connectResult) {
socat.kill('SIGKILL');
testSocket && testSocket.destroy();
server.close();
}
var emergencyTimeout = setTimeout(function() {
socat.kill('SIGKILL');
testSocket && testSocket.destroy();
server.close();
done();
}, 60000); // Killswitch!
expect(mbusMaster.mbusMaster.connected).to.be.true;
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.false;
setTimeout(function() {
console.log(new Date().toString() + ': mbus-Serial-Master Send "Get 1"');

mbusMaster.getData(1, function(err, data) {
console.log(new Date().toString() + ': mbus-Serial-Master err: ' + err);
console.log(new Date().toString() + ': mbus-Serial-Master data: ' + JSON.stringify(data, null, 2));
expect(err).to.be.null;
expect(data.SlaveInformation.Id).to.be.equal('500023E');
expect(data.DataRecord[0].Value).to.be.equal(1252);
expect(data.SlaveInformation.Id).to.be.equal(11490378);
expect(data.DataRecord[0].Value).to.be.equal(11490378);
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.false;

mbusMaster.setPrimaryId(1, 3, function(err) {
mbusMaster.getData(2, function(err, data) {
console.log(new Date().toString() + ': mbus-Serial-Master err: ' + err);
console.log(new Date().toString() + ': mbus-Serial-Master data: ' + JSON.stringify(data, null, 2));
expect(err).to.be.null;
expect(data.SlaveInformation.Id).to.be.equal('500023E');
expect(data.DataRecord[0].Value).to.be.equal(1252);
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.false;

mbusMaster.scanSecondary(function(err, data) {
mbusMaster.setPrimaryId(1, 3, function(err) {
console.log(new Date().toString() + ': mbus-Serial-Master err: ' + err);
console.log(new Date().toString() + ': mbus-Serial-Master data: ' + JSON.stringify(data, null, 2));
expect(err).to.be.null;
expect(data).to.be.an('array');
expect(data.length).to.be.equal(3);
expect(data[0]).to.be.equal('17834320B4090107');
expect(data[1]).to.be.equal('6200165396150331');
expect(data[2]).to.be.equal('6802476496151F1B');
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.false;

if (emergencyTimeout) clearTimeout(emergencyTimeout);
setTimeout(function() {
console.log(new Date().toString() + ': mbus-Serial-Master Close: ' + mbusMaster.close());
socat.kill('SIGKILL');
console.log('mbus-Serial-Device: Socat killed');
mbusMaster.scanSecondary(function(err, data) {
console.log(new Date().toString() + ': mbus-Serial-Master err: ' + err);
console.log(new Date().toString() + ': mbus-Serial-Master data: ' + JSON.stringify(data, null, 2));
expect(err).to.be.null;
expect(data).to.be.an('array');
expect(data.length).to.be.equal(3);
expect(data[0]).to.be.equal('17834320B4090107');
expect(data[1]).to.be.equal('6200165396150331');
expect(data[2]).to.be.equal('6802476496151F1B');
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.false;

if (emergencyTimeout) clearTimeout(emergencyTimeout);
setTimeout(function() {
server.close(function(err) {
testSocket.destroy();
console.log('mbus-Serial-Device: Server closed');
clearTimeout(emergencyTimeout);
setTimeout(done, 2000);
});
console.log(new Date().toString() + ': mbus-Serial-Master Close: ' + mbusMaster.close());
socat.kill('SIGKILL');
console.log('mbus-Serial-Device: Socat killed');
setTimeout(function() {
server.close(function(err) {
testSocket.destroy();
console.log('mbus-Serial-Device: Server closed');
clearTimeout(emergencyTimeout);
setTimeout(done, 2000);
});
}, 1000);
}, 1000);
}, 1000);
});
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.true;
expect(mbusMaster.close()).to.be.false;
mbusMaster.getData(3, function(err, data) {
expect(err.message).to.be.equal('Communication already in progress');
});
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.true;
expect(mbusMaster.close()).to.be.false;
mbusMaster.getData(3, function(err, data) {
expect(err.message).to.be.equal('Communication already in progress');
});
});
});
});
});
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.true;
}, 2000);
expect(mbusMaster.mbusMaster.communicationInProgress).to.be.true;
}, 2000);
});

}, 2000);
});

Expand Down

0 comments on commit 2d2fcd8

Please sign in to comment.