You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Properties - value (state) and function (behavior) properties - of a JavaScript object ("object" === typeof objVariable) can be accessed in two ways: static and dynamic.

    Example to access a value property - read and 'write' (assign):

    1. static get and set access:

      
      2. dynamic get and set access:
      
      

      var phPropName = "phValue"; ph = sensorObject[phPropName]; sensorObject[phPropName] = 7.0;```

    Same static (hard coded) and dynamic (by variable for property name) access goes for functions (behavior). The example below uses the SensorReading prototype based object-oriented approach to have multiple (maxNumOfLastReadings) sensorReading s (objects / instances of SensorReading prototype / class) stored in sensorReadings singleton object (singleton - only one instance exists). Two sensorObjects read values, create a reading with the read values and store it in the readings in different intervals.

    var sensorModule = require("sensorModuleName");
    
    // constructor for a reading accepting a sensor id and read restTemp, ph and ec
    var SensorReading = function(id, restTemp, ph, ec) {
      this.ts = getTime(); // set current system time stamp in seconds
      this.id = sensorId; // set sensor id
      this.restTemp = restTemp; // set restTemp
      this.ph = ph; // set ph
      this.ec = ec; // set ec
    }
    
    // constructor for a sensor (prototype - or class - definition)
    var SensorObject = function(sensorId, sensorModule, connectInfo1,... ) {
      this.sensorId = sensorId;
      this.sensor = sensorModule.connect(connectInfo1, conectInfo2,...);
    }
    // method (function / behavior) to take a reading and store it in readingsObject
    SensorObject.prototype.readAndStoreIn = function(readingsObject) {
      var restTemp = this.sensor.read("temp"); // ...code detail depends on the module
      var ph = this.sensor.read("ph"); // ...code detail depends on the module
      var ec = this.sensor.read("ec"); // ...code detail depends on the module
      var reading = new SensorReading(this.sensorId, restTemp, ph, ec); // create a reading
      readingsObject.store(reading);
    };
      
    // literal 'constructor' for sensorReadings singleton object
    var sensorReadings =
    { maxNumOfLastReadings: 10 // keep max 10 readings
    , readings: [] // most recent reading is in readings[0]
    , store: function(reading) {
        if (this.readings.length >= this.maxNumOfLastReadings) { // chop oldest off
          this.readings = this.slice(0, this.maxNumOfLastReadings - 1); }
        this.readings.splice(o, 0, reading);
      }
    , clearReadings: function() { this.readings = []; }
    , getReadings: function() { return [].concat(this.readings); }
    , getReadingByIndex: function(x) { // return null if x 'outside' of readings
        return (((x>=0)&&(x<this.readings.length)) ? this.readings[x] : null); }
    , getLastReading: function() { // return most recent reading
        return this.getReadingByIndex(0); }
    , getReadingsAsJSON: function() { return JSON.stringify(this.readings); }
    , getReadingsAsHtmlTableString() {
        h  = '<table><tr><th>#</th>';
        h += '<th>ts</th><th>id</th>';
        h += '<th>restTemp</th><th>ph</th><th>ec</th>­</tr>';
        this.readings.forEach(function(r, x) { 
          h += '<tr><td>'+x+'</td>';
          h += '<td>'+r.ts+'</td><td>'+r.id+'</td>';
          h += '<td>'+r.restTemp+'</td><td>'+r.ph+'</td­><td>'+r.ec+'</td></tr>';
        }); h += '</table>';
        return h;
      }
    , getReadingsAsHtmlTableString2() { // dynamic access in loop
        var ps = ["ts","id","restTemp","ph","ec"];
        h  = '<table><tr><th>#</th>';
        ps.forEach(function(p){ h += '<th>'+p+'</th>'; });
        h += '</tr>';
        this.readings.forEach(function(r, x) { 
          h += '<tr><td>'+x+'</td>';
          ps.forEach(function(p){ h += '<th>'+r[p]+'</th>'; });
          h += '</tr>';
        }); h += '</table>';
        return h;
      }
    }
    
    var sensorAtLocation1 = new SensorObject("LocInt",<connect parmsInt>,...); 
    var sensorAtLocation2 = new SensorObject("LocOut",<connect parmsOut>,...); 
    setTimeout(function(){ 
        sensorAtLocation1.readAndStoreIn(sensorR­eadings);
      }, 15000); // every 15 seconds
    setTimeout(function()setTimeout(function­(){
        sensorAtLocation2.readAndStoreIn(sensorR­eadings);
      }, 45000); }, 5000); first time after 50 seconds, after that every 45 seconds
    

    Interval settings produce readings at 15, 30, 45, 50, 60, 75, 90, 95, 105, 120, 135, 140,... seconds (italics are LOC1 readings, bold are LOC2 readings).

    Complete above code with sensor specifics, upload / run it, and then ask readingsObjects for different things - (first) one line after the other:

    readingsObject.getLastReading();
    readingsObject.getReadings();
    readingsObject.getReadingByIndex(4);
    readingsObject.getReadingsAsJSON();
    readingsObject.getReadingsAsHtmlTableStr­ing2();
    ...
    readingsObject["getLastReading"]();
    var getReadingsMethodName = "getReadings";
    readingsObject[getReadingsMethodName]();­
    var rx = readingsObject["getReadingByIndex"](4);
    var ps = ["ts","sensorId","restTemp","ph"];
    if (rx) ps.forEach(function(p){ console.log(p+"="+rx[p]); });
    readingsObject["getReadingsAsJSON"]();
    ...
    

    PS:

    • code not fully tested (yet)
    • in 1v88, '<td>${r[p]}</td>' can replace '<th>'+r[p]+'</th>' in line 59
About

Avatar for allObjects @allObjects started