I'm using ubus to communicate with physical devides and I'm trying to set a configuration and then use that result to commit to a file (write) inside the device but i always get undefined from the uci request but inside the fuction i get ubus code [0].
ServicesController.prototype.updateWifi = function (req, res, next) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'updateWifi', 'begin');
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', 'begin');
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]);
Controllers.Ubus.uciRequest('set', {"config": config, "section": section, values}, device).then(function (uciData) {
console.log('uci uciData -->', uciData);
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) {
resolve(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) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
if (dataApply.result) {
if (dataApply.result[0] == 0) {
resolve(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);
});
}
}
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci catch', e);
reject(e);
});
}
UbusController.prototype.uciRequest = function (procedure, signature, device) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'inicio');
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'device id', device.id);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'procedure', procedure);
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'signature', signature);
return new Promise(function (resolve, reject) {
return Controllers.Ubus.getSession(device.id).then(function (dataAuth) {
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'dataAuth', dataAuth);
if (dataAuth) {
Controllers.Ubus.execCommand(device.id, "uci", procedure, signature).then(function (data) {
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'data', data);
if (data.result) {
if (data.result[0] == 0) {
resolve(data.result[1]);
} else {
resolve(null);
}
} else {
resolve(null);
}
})
}
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', e);
reject(e);
});
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'end');
}).catch(function (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'execCommand', e);
reject(e);
});
}
UbusController.prototype.fileExec = function (deviceId, procedure, path, params) {
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'fileExec', 'begin');
return new Promise(function (resolve, reject) {
var session = radioAccess[deviceId];
var access = session.access;
var options = {
host: access.address,
port: access.port,
path: '/ubus',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var callback = function (res) {
var data = '';
res.on('data', function (chunk) {
if (chunk != null)
data += chunk;
});
res.on('end', function () {
if (data != '') {
try {
resolve(JSON.parse(data));
} catch (e) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'fileExec', e);
reject(e);
}
}
});
res.on('error', function (err) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'fileExec', err);
reject(err);
});
}
if(params==undefined){
var fileData = {"jsonrpc": "2.0", "id": 1, "method": "call", "params": [session.token, "file", procedure, "{ command :"+ path+"}"]};
} else {
var signature = { command : path, params: [params]};
var fileData = {"jsonrpc": "2.0", "id": 1, "method": "call", "params": [session.token, "file", procedure, signature]};
}
createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'fileExec', 'fileData', fileData);
var req = http.request(options, callback);
req.write(JSON.stringify(fileData));
req.setTimeout(5000, function () {
req.abort();
reject({error: "request timeout"});
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'fileExec', 'request timeout');
});
req.on('error', function (err) {
createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'fileExec', err);
reject(err);
});
req.end();
createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), deviceId, 'fileExec', 'end');
});
}
via Catia Matos
No comments:
Post a Comment