Author Topic: Add mote sketch informational messages to log/terminal view  (Read 631 times)

wmljames

  • NewMember
  • *
  • Posts: 3
Add mote sketch informational messages to log/terminal view
« on: January 01, 2018, 01:18:25 PM »
I built a custom mote based on the garage mote that is designed to open a small (dog kennel) gate using a stepper motor.  I used eeprom on the mote sketch to set and save step motor configurations.   The mote sketch sends informational messages that start with "nI-" where n ranges from 1 to 9.

I wanted to send commands to the mote and view any informational messages on my smart phone using the log/terminal options (when I am outside).  The kluge custom code I added to gateway.js sends the informational message (io.sockets.emit) when the metric is an informational message (nI-), where n ranges from 1 to 9. 

My kluge solution is working but I'd like to define a custom metric that allows for informational messages without modifying the gateway.js code.  Any suggestions?     

Regards,
Bill
 
Output from gateway.sys.log
Code: [Select]
[01-01-18_09:11:47.707] [LOG]   NODEACTION: {"nodeId":"12","action":"Cfg"}
[01-01-18_09:11:47.734] [LOG]   >: ACK:OK
[01-01-18_09:11:47.841] [LOG]   >: [12] 2I-Enter CFG, CFG option, or CFG opt value    [RSSI:-28][ACK-sent by gateway]
[01-01-18_09:11:48.049] [LOG]   >: [12] 2I-Build Date Mar 10 2017-20:51:02   [RSSI:-15][ACK-sent by gateway]
[01-01-18_09:11:48.713] [LOG]   >: [12] 2I-1. Test step forward and reverse directions.    [RSSI:-28][ACK-sent by gateway]
[01-01-18_09:11:48.913] [LOG]   >: [12] 2I-2. Flip 1/8 microstep mode: off   [RSSI:-15][ACK-sent by gateway]
[01-01-18_09:11:49.116] [LOG]   >: [12] 2I-4. Flip default Request Type: Open   [RSSI:-15][ACK-sent by gateway]
[01-01-18_09:11:50.219] [LOG]   >: [12] 2I-5. Set steps per loop: 5000   [RSSI:-28][ACK-sent by gateway]
[01-01-18_09:11:51.262] [LOG]   >: [12] 2I-6. Stepper RPM: 1.500    [RSSI:-28][ACK-sent by gateway]
[01-01-18_09:11:51.459] [LOG]   >: [12] 2I-7. Steps per Revolution: 5400   [RSSI:-14][ACK-sent by gateway]
[01-01-18_09:12:33.156] [LOG]   NODEACTION: {"nodeId":"12","action":"Cfg 4"}
[01-01-18_09:12:33.185] [LOG]   >: ACK:OK
[01-01-18_09:12:33.275] [LOG]   >: [12] 3I-Request Type set:Close   [RSSI:-28][ACK-sent by gateway]

Code snippet from gateway.js (see new lines with Bill in comments)

Code: [Select]
      msgHistory[id] = msgTokens;
      existingNode._id = id;
      existingNode.updated = Date.now(); //update timestamp we last heard from this node, regardless of any matches
      if (existingNode.metrics == undefined) existingNode.metrics = {};

      var regexpTokens = /[\w\:\.\$\!\\\'\"\?\[\]\-\(\)@%^&#+\/<>*~=,|]+/ig; //match (almost) any non space human readable character
      while (match = regexpTokens.exec(msgTokens)) //extract each token/value pair from the message and process it
      {
        var matchingMetric;
        //                                                                                                                       // Bill
        // search for informational messages (nI-) and send them to client log/terminal        // Bill
        //                                                                                                                       // Bill
        var istr = match[0].substr(1,2);                                                                           // Bill
        if (istr  == "I-") {                                                                                                // Bill
                io.sockets.emit("LOG", "Info-" + data);                                                         // Bill
                return;                                                                                                       // Bill                                                                           
        }

        //try to match a metric definition
 
« Last Edit: January 01, 2018, 04:21:18 PM by Felix »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6571
  • Country: us
    • LowPowerLab
Re: Add mote sketch informational messages to log/terminal view
« Reply #1 on: January 02, 2018, 02:40:57 PM »
Bill,
Are you just trying to transition from your injected code into a user metric?
Then you can define a metric under the userMetrics directory (see examples for that).

For the metric itself you can define it as you'd like, here's a quick attempt at me doing it according to your definition:

Code: [Select]
DD : { name:'DD', regexp:/\b(\d)I-\b/i, value:'', duplicateInterval:1, unit:'', pin:0, },

This would create a metric for your nI- tokens, with a value of n. Add graph=1 if you want it logged & graphed.

Then you need an event, which you can enable or disable from the node itself (BTW first you need to add it to your node (with the +Event button from the node's details page). Here's a sample such event:

Code: [Select]
ddInfo: { label:'DogDoor info', icon:'info', descr:'Send a log message when dog door reports info tokens', serverExecute:function(node) { io.sockets.emit('LOG', 'Info-'+node.metrics['DD'].value); } },

I haven't tried it but let me know if it works.
« Last Edit: January 03, 2018, 11:44:35 AM by Felix »

wmljames

  • NewMember
  • *
  • Posts: 3
Re: Add mote sketch informational messages to log/terminal view
« Reply #2 on: January 02, 2018, 11:07:24 PM »
Thanks for guiding me in the right direction; I am very new to javascript/JSON.  I do want to transition to the metrics definitions since there are future capabilities I would like to graph.  I can always change the mote sketch to align to the syntax metric:value but this looks to be a great option for the informational messages.

I'll try it and let you know how it works.