0

I'm sure this is a simple syntax thing that I'm just not getting but here's my code. The purpose of which is to have an object that is shared between two other scripts.

The object saved as canStat.js

class cCan {
  constructor() {
    this.tmp = 0;
    this.hum = 0;
    this.isRelay1 = false;
    this.isRelay2 = false;
    this.isRelay3 = false;
    this.isRelay4 = false;
  }
}

module.exports = cCan;

next door to that file I have a script listening for data to be received, server.js which it is successfully over UDP, that should be saved to that object as follows.

const dgram = require('dgram');
const listener = dgram.createSocket('udp4');

const cCan = require('./canStat');

var ceres1 = new cCan();

listener.on('message', (msg, rinfo) => {

  if (msg.readUInt8(0) == 1 ) {

    console.log("Received Packet");
    console.log("IP: ", `${rinfo.address}`);
    console.log("Port: ", `${rinfo.port}`);
    console.log("Byte Size: ", `${rinfo.size}`);
    console.log("Humidity: ", msg.readUInt8(2), "%");
    console.log("Temperature: ", msg.readUInt8(1), "C");

    ceres1.tmp = msg.readUInt8(1);
    ceres1.hum = msg.readUInt8(2);


    if (msg.readUInt8(3) == 0) {
      ceres1.isRelay1 = true;
      console.log("Cooler Relay is On");
    } else {
      ceres1.isRelay1 = false;
      console.log("Cooler Relay is Off")
    }

    if (msg.readUInt8(4) == 0) {
      ceres1.isRelay2 = true;
      console.log("Humidifier Relay is On");
    } else {
      ceres1.isRelay2 = false;
      console.log("Humidifier Relay is Off")
    }

    if (msg.readUInt8(5) == 0) {
      ceres1.isRelay3 = true;
      console.log("Fan Relay is On");
    } else {
      ceres1.isRelay3 = false;
      console.log("Fan Relay is Off")
    }

    if (msg.readUInt8(6) == 0) {
      ceres1.isRelay4 = true;
      console.log("Lighting Relay is On");
    } else {
      ceres1.isRelay4 = false;
      console.log("Lighting Relay is Off")
    }

    console.log(
      ceres1.tmp,
      ceres1.hum,
      ceres1.isRelay1,
      ceres1.isRelay2,
      ceres1.isRelay3,
      ceres1.isRelay4
    );

  }

});

module.exports = ceres1;

Two folders down from these two (within the master "app" folder) is located (app/api/routes/test.js), test.js

var ceres1 = require('../../server');

// Get data
function getData() {

  console.log(
    ceres1.tmp,
    ceres1.hum,
    ceres1.isRelay1,
    ceres1.isRelay2,
    ceres1.isRelay3,
    ceres1.isRelay4
  );

}

getData();

However this is doesn't appear to be saving to a common class object and is returning the constructor function values (0, 0, false, false, false, false), where as the first console.log in the server.js reports that the values are being saved to the object ceres1.

What do I have to do to have to have these two scripts, server.js & test.js, to share the same class object? Thanks!

AustinFoss
  • 385
  • 5
  • 13
  • Are you positive you're running `getData()` *after* the server received the message? –  Dec 12 '18 at 22:11
  • Woops, I started with the title intended as a different question, over the course of writing this the title isn't accurate anymore. Would like to change to "Javascript: sharing a class object between scripts" Don't know how to change it. That is my bad. – AustinFoss Dec 12 '18 at 22:13
  • Click on "edit" and edit the title. – Barmar Dec 12 '18 at 22:14
  • Thanks!, still semi-new to learning where everything is located in the UX – AustinFoss Dec 12 '18 at 22:14
  • As @ChrisG suggested, the problem is that you're updating `ceres1` asynchronously, but `test.js` is printing it synchronously. – Barmar Dec 12 '18 at 22:15
  • I used to have a `get stats() {}` method within the constructor and was getting errors. That was my original starting point and be playing with this question for an hour so I'm now taking this approach. – AustinFoss Dec 12 '18 at 22:16
  • Also, how exactly are you running this? If you do `node server.js` in one terminal window, and `node test.js` in another, they won't share data. –  Dec 12 '18 at 22:22
  • I'm not sure my question has been correctly marked as duplicate. The response posted by the moderator doesn't seem relevant. The code in test.js shouldn't have to be run asynchronous with the server.js file because it's a manual trigger. The server is simply looping and saving updated data to the class object every 5seconds. The test.js file is simply meant to ask what values are saved to that object every time I trigger it manually. – AustinFoss Dec 12 '18 at 22:23
  • @Chris G, yes, two terminal windows...... As I read your question there it became exceedingly obvious why this would be. Derp – AustinFoss Dec 12 '18 at 22:24
  • Do you want me to reopen this? Or are you done with the question for now? –  Dec 12 '18 at 22:39

0 Answers0