Skip to content

Model Transformer

simen edited this page May 4, 2017 · 4 revisions

Freebird uses two unified data models Device Class and Gadget Class to represent the real-world device and gadget respectively. Thus, a netcore implementer has responsibility to transform any of the raw device and gadget(application) data into the unified model.

<< Important >>:

Freebird leaves two template methods nc._cookRawDev and nc._cookRawGad for the implementers to provide the transformer implementations. The following descriptions describe the signature and behavior of these two methods.



nc._cookRawDev(dev, rawDev, done)

Fill the data required by the dev instance with netInfoObj and devAttrsObj via dev.set() method, where netInfoObj and devAttrsObj can be built from the rawDev data and some additional requests to the remote device.

When transform procedure accomplishes, call done(err, dev) to pass the fullfilled dev instance to the framework.

Note:
rawDev is the raw device data that you commit by calling nc.commitDevIncoming(), therefore the shape of rawDev is actually planned by you.


Arguments:

  1. dev (Object): Deivce instance.
  2. rawDev (Object): Raw device data.
  3. done (Function): function (err, dev) {}. Pass the fullfilled dev to the framework.

Returns:

  • none

Examples:

// An simple implementation of the device model transformer

nc._cookRawDev = function (dev, rawDev, done) {
    dev.set('net', {
        role: 'router',
        maySleep: false,
        address: {  // Required
            permanent: rawDev.ieeeAddr,
            dynamic: rawDev.nwkAddr,
        }
    });

    dev.set('attrs', {
        manufacturer: rawDev.manufacturerName,
        model: rawDev.modelNum
    });

    done(null, dev);
};



nc._cookRawGad(gad, rawGad, done)

Fill the data required by the gad instance with pannelInfoObj and gadAttrsObj via gad.set() method, where pannelInfoObj and gadAttrsObj can be built from the rawGad data and some additional requests to the remote gadget.

When transform procedure accomplishes, call done(err, gad) to pass the fullfilled gad instance to the framework.

Note:
rawGad is the raw gadget data that you commit by calling nc.commitGadIncoming(), therefore the shape of rawGad is actually planned by you.


Arguments:

  1. gad (Object): Gadget instance.
  2. rawGad (Object): Raw gadget data.
  3. done (Function): function (err, gad) {}. Pass the fullfilled gad to the framework.

Returns:

  • none

Examples:

// An simple implementation of the gadget model transformer

nc._cookRawGad = function (gad, rawGad, done) {
    gad.set('panel', {
        profile: 'home',
        classId: 'presence'
    });

    gad.set('attrs', {
        dInState: rawGad.resources.dInState,
        counter: rawGad.resources.counter,
    });

    done(null, gad);
};