Author Topic: Automatic NODE REQUEST via event once per day  (Read 418 times)

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Automatic NODE REQUEST via event once per day
« on: May 16, 2021, 02:12:33 PM »
I have been able to be the Request functionality working on a couple of my nodes and have been playing around with it for a while.  Something that would be really useful would be to setup a recurring request at a given time of day.  This would be useful as some of my nodes keep track of daily information and rely on a mote with a real time clock to send reset requests at midnight every day.  If the Pi Gateway could do this that would make things simpler.  One other thought about this would be to setup an event that would scheduled run on a regular basis that would then be able to send a request to the gateway and create a request for a given node.
« Last Edit: May 27, 2021, 12:11:23 PM by Felix »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6605
  • Country: us
    • LowPowerLab
Re: Pi Gateway Request functionality enhancement
« Reply #1 on: May 17, 2021, 02:55:02 PM »
I think you mean having some kind of event that runs apart of any node?

About the second part - running a request to the Gateway - wouldn't that need to be executed from outside? Not that it would be impossible to run a request on itself but it makes little sense. Or maybe I'm misunderstanding.

Do you have a more concise use case for each of these requests?

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: Pi Gateway Request functionality enhancement
« Reply #2 on: May 18, 2021, 09:14:02 AM »
I think this would be easier if I left out the notion of events.

Here is the use case.  I have a mote that measures rainfall it has no way of keeping track of time.  It sends data to the Pi Gateway every 10min.  It will accumulate the amount of rain detected throughout the day.  At  midnight every day I would like to have a request that is queued up by the Pi Gateway so when the rain mote checks in after midnight it will receive the request in the response from the Pi Gateway.  This request will be a string that indicates to the mote that it should reset it's rain accumulator.  The rain mote will then be ready to start accumulating rain for the day.

Hope that makes sense.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6605
  • Country: us
    • LowPowerLab
Re: Pi Gateway Request functionality enhancement
« Reply #3 on: May 18, 2021, 01:37:34 PM »
What you are after is queuing a NODE REQUEST to the end node, but in an automatic manner, at a given time (once per day, after midnight).

I see this being done with an event tied to that particular node. The event would then run at the given time, and queue a REQUEST on your behalf. So this is not an enhancement but rather some custom coding using existing functionality.

You'd need to run the PiGateway sketch that supports the REQUEST queuing, and follow the protocol to queue that request. I can say this is an advanced feature and will require some coding in the form of that event which you'd then enable on the node. In addition, the node would have to listen to ACKs and parse any ACK for the special "RESET-MY-RAIN-LEVEL" message/token (which is the mechanism used by the PiGateway to send these NODE REQUESTS.
Sorry I don't have specific code examples to share here.

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: Pi Gateway Request functionality enhancement
« Reply #4 on: May 18, 2021, 05:01:40 PM »
I am running the PiGateway sketch you reference and it is working now, after I changed to Arduino IDE 1.8.6.  I have a couple of motes that I can send a PGM request to.  When they receive the request they go into wireless programming mode and I am able to update the firmware.

I think I understand where you are going with setting up a recurring reset event on a given node.  The event would need setup a properly formatted request string and call sendMessageToGateway(requestString);
If I am understanding correctly.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6605
  • Country: us
    • LowPowerLab
Re: Pi Gateway Request functionality enhancement
« Reply #5 on: May 18, 2021, 09:14:15 PM »
That's exactly right, you'd queue something very similar to your "PGM" node REQUEST, just in time to get picked up after midnight by the target node.
You will just need to create that event that fires once per day at 12:00:01 AM.

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: Pi Gateway Request functionality enhancement
« Reply #6 on: May 20, 2021, 11:43:37 AM »
I was able to create an event that is scheduled to fire at midnight and a request is successfully sent to the Gateway sketch.  The Gateway sketch is able to relay the request in the ack and the node is able to receive the ack data and send the ack data back.  The problem is that the request is never cleared out queue so the request is sent over and over again by the Gateway sketch.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6605
  • Country: us
    • LowPowerLab
Re: Pi Gateway Request functionality enhancement
« Reply #7 on: May 20, 2021, 12:57:51 PM »
Good work.
Are you able to verify the node sends back another ACK to the REQUEST so the PiGateway app would then know to mark the REQUEST as complete and ask the PiGateway to flush that request from the queue?

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: Pi Gateway Request functionality enhancement
« Reply #8 on: May 20, 2021, 03:38:38 PM »
When I queue up a request using the web UI only the one rest request is sent and a green check box shows up.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6605
  • Country: us
    • LowPowerLab
Re: Pi Gateway Request functionality enhancement
« Reply #9 on: May 20, 2021, 06:27:19 PM »
only the one rest request is sent
Not sure what you mean there?

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: Pi Gateway Request functionality enhancement
« Reply #10 on: May 23, 2021, 11:11:49 AM »
Sorry for the confusion.   ;D

What I was trying to get across is that when I use the web interface to create a reset request for a node everything works correctly.  I see the reset request come through in the logs only once and the node does respond with the RST string like it should.  When I create a request using an event the reset request is continually sent by the Pi Gateway sketch and the node does respond with the proper RST response.  But like I said the request is sent in every ack by the Pi Gateway sketch.

Here is the code for the event I created:

Code: [Select]
exports.events = {
  resetDaily : {
    label: 'Schedule reset request on a node.',
    icon: 'clock',
    descr: 'Make a request to a node to reset daily values at midnight.',
    nextSchedule:function(node) {
      return timeoutOffset(08, 44); },
    scheduledExecute:function(node) {
      var reqValue = null;

      // Void out any outstanding requests before sending new reset request.
      requestString = node._id + ':VOID:' + reqName + (reqValue?':'+reqValue.tri
m():'') + '\n';;
      requestString += 'RQ:' + node._id + ':' + 'RST' + (reqValue?':'+reqValue.t
rim():'') + '\n';

      sendMessageToGateway(requestString);

      console.info('REQUEST SENT: ' + requestString.replaceNewlines());
  }
 }
};

When I look at the the Pi Gateway sketch it looks like it relies on the Gateway app to send out a Void command to clean up the request.  Is that correct?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6605
  • Country: us
    • LowPowerLab
Re: Pi Gateway Request functionality enhancement
« Reply #11 on: May 24, 2021, 04:06:32 PM »
Look at how the following socket function handler does it, it should be done similarly:

socket.on('SUBMITNODEREQUEST'

IOW you have to add that request to the pending requests of that node. Otherwise it's going to be queued with the hardware gateway, but the PiGateway will have no record of it. So it won't know it has to be cancelled.

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: Pi Gateway Request functionality enhancement
« Reply #12 on: May 24, 2021, 10:16:21 PM »
I understand the socket function handler up until the following two lines:

        socket.emit('LOG', 'NODE ['+nodeId+'] Request '+(isNew?'Added':'Updated')+': ' + reqName);
        io.sockets.emit('UPDATENODE', dbNode);

What is needed there?

Thanks for all the help

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6605
  • Country: us
    • LowPowerLab
Re: Pi Gateway Request functionality enhancement
« Reply #13 on: May 25, 2021, 09:53:32 AM »
The LOG is just logging what happened.
The sockets emit sends a message to all connected clients updating that particular node.

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: Pi Gateway Request functionality enhancement
« Reply #14 on: May 27, 2021, 10:20:16 AM »
I was able to successfully schedule a reset event and upon successful receipt of the request the node sends a Void command.  I still need to test things a bit more.  Here is the code for anyone who might be interested:

Code: [Select]
exports.events = {
  resetDaily : {
    label: 'Schedule reset request on a node.',
    icon: 'clock',
    descr: 'Ask a node to reset daily values at midnight.',
    nextSchedule:function(node) {
      return timeoutOffset(00, 00);
    },
    scheduledExecute:function(node) {
      var dbNode = node;
      if (dbNode.requests == undefined) dbNode.requests = {};

      isNew = false;
      if (dbNode.requests[reqName] == null) {
        dbNode.requests[reqName] = {};
        isNew = true;
      }

      dbNode.requests[reqName].name = reqName;
      dbNode.requests[reqName].value = null;
      dbNode.requests[reqName].updated = Date.now();
      dbNode.requests[reqName].status = 'PENDING';
      dbNode.requests[reqName].timeout = requestTimeout;

      requestString='';
      if (!isNew) //VOID previous request value before queuing adding latest value
        requestString += node._id + ':VOID:' + reqName + (reqValue?':'+reqValue.trim():'') + '\n';

      requestString += 'RQ:' + node._id + ':' + reqName + (reqValue?':'+reqValue.trim():'') + '\n';
      sendMessageToGateway(requestString);
      console.info('REQUEST SENT: ' + requestString.replaceNewlines());
      db.update({ _id: dbNode._id }, { $set : dbNode}, {}, function (err, numReplaced) { console.info('SUBMITNODEREQUEST DB
-Replaced:' + numReplaced); });
      console.info(`   [${node._id}] ${(isNew?'Added':'Updated')} request:${reqName}`);
      io.sockets.emit('UPDATENODE', dbNode);
    }
  }
};