0

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){ var values={opt:value};

return Controllers.Ubus.uciRequest('set', {"config": config, "section": section, values}, device)
  .then(function (uciData) {
    var promises = [];

    if (uciData!=null) {
        createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uci', 'depois do if do uciData' );

        if (commit){
            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit');
            var p1 = Controllers.Ubus.uciRequest('commit', {"config": config}, device)
              .then(function (dataCommit) {
                if (dataCommit && dataCommit.hasOwnProperty('result') && data.result[0] == 0) {
                  createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'commit data', dataCommit);
                  return dataCommit.result[1];
                }
              })
          promises.push(p1);
        }

        if(apply){
            createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply');
            var p2 = Controllers.Ubus.fileExec(device.id, "exec", path, "restart")
              .then(function (dataApply) {
                if (dataApply && dataApply.hasOwnProperty('result') && data.result[0] == 0) {
                  createLog('info', __dirname, __filename.slice(__dirname.length + 1, -3), null, 'uci', 'apply data', dataApply);
                  return dataApply.result[1];
                }
            })
          promises.push(p2);
        }
    }
    return Promise.all(promises);
}

My problem is with this function because it doesnt return any value but i got the ubus status code [0]

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);
    var res;
  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) {
      return 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) {
              res = data.result[1];
            return res;
          }
        }
        throw new Error("no data");
      });
     createLog('debug', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', 'dataResuult', res);
    }

    throw new Error("no dataAuth");
  }).catch(function(e) {
    createLog('error', __dirname, __filename.slice(__dirname.length + 1, -3), device.id, 'uciRequest', e);
    throw e;
  });
}

When I print in updateWifi tha data in this log console.log('updateWifi ---> data',wifiData) it returns undefined. Anyway to return value from the function ServicesController.prototype.uci ?

all Code here: http://plnkr.co/edit/5QaSxPXkz9Ts0mzCY4RE?p=preview

  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! And put a `return` in your `updateWifi` and `uci` functions – Bergi Jun 06 '17 at 12:27
  • I dont understand, my problem becomes from UbusController.prototype.uciRequest because the result is undefined and i'm trying to return value from this function and i always got undefined for uciData – Catia Matos Jun 06 '17 at 13:31
  • The code in your plunkr is not the one you posted in your question, you do `return promise` there. But still, except for `fileExec`, you should not be using the `new Promise` constructor *anywhere*. – Bergi Jun 06 '17 at 15:55
  • What can i do? instead? – Catia Matos Jun 06 '17 at 16:33
  • As I said, just `return` the promises you get from the chained `then` and `catch` calls. Also see the linked question. – Bergi Jun 06 '17 at 16:39
  • Im not understanding sorry – Catia Matos Jun 06 '17 at 16:41

0 Answers0