I'm having troubles with a function (uci) expecting a return of a promise but i'm returning two at the same time and i just want one but dont't know how to do it... I need to pass the apply and commit value and resolve the value and then return to my function
ServicesController.prototype.updateWifi = function (req, res, next) { //this is what i call when i click on the button in the screen
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'inicio');
var data = req.body;
var state;
if(data.state=="1"){
state=1;
} else if(data.state=="0"){
state=0;
}
if (isEmptyObject(data)) {
res.status(400).send({error: errorMessage.emptyBody});
return;
}
if (data.sn === undefined || data.sn === "") {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'Invalid serial number');
res.status(400).send({error: "Invalid serial number"});
return;
}
Database.Devices.getDeviceBySn(data.sn).then(function (device) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'device', device);
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'device.id', device[0].id);
var ifname=data.ifname;
return ServicesController.prototype.uci(device[0], "teste_wireless", "wifi", ifname, "disabled", data.state, true, true).then(function(wifiData){
console.log('updateWifi ---> data',wifiData);
if (wifiData != undefined) {
res.status(404).end();
} else {
console.log('updateDeviceInterface ', device[0].id, ' for ', ifname, ' to ', state);
Database.Services.updateWifiDeviceInterface(device[0].id, ifname, state);
res.status(200).send(wifiData);
}
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', e);
res.status(500).send(e);
});
});
}
ServicesController.prototype.uci = function(device, config, path, section, opt, value, apply, commit){
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'inicio');
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'config', config);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'path', path);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'section', section);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'option', opt);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'value', value);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'apply', apply);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'commit', commit);
var values={opt:value};
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'session', radioAccess[device.id]);
var result;
Controllers.Ubus.uciRequest('set', {"config": config, "section": section, values}, device).then(function (uciData) {
console.log('uci uciData -->', uciData); //this gets undefined
if (uciData!=null) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'depois do if do uciData' );
/*return new Promise(function (resolve, reject) {
return Controllers.Ubus.uciRequest('changes', config, device).then(function (dataAuth) {
if (dataAuth!=null) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'Invalid serial number');
res.status(400).send({error: "Cant apply changes"});
return;
}
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
reject(e);
});
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
reject(e);
});
};*/
if(commit){
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit');
return new Promise(function (resolve, reject) {
Controllers.Ubus.uciRequest('commit', {"config": config}, device).then(function (dataCommit) {
if (dataCommit) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit data', dataCommit);
if (dataCommit.result) {
if (dataCommit.result[0] == 0) {
result = dataCommit.result[1];
} else {
resolve(null);
}
} else {
resolve(null);
}
}
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
reject(e);
});
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
reject(e);
});
}
if(apply){
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
return new Promise(function (resolve, reject) {
Controllers.Ubus.fileExec(device.id, "exec", path, "restart").then(function (dataApply) {
if(dataApply){
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
if (dataApply.result) {
if (dataApply.result[0] == 0) {
result+= dataApply.result[1];
} else {
resolve(null);
}
} else {
resolve(null);
}
}
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
reject(e);
});
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
reject(e);
});
}
}
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'depois do fim do if do uciData' );
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', e);
});
}
via Catia Matos
No comments:
Post a Comment