Author Topic: Custom node/metrics  (Read 4356 times)

HeneryH

  • Full Member
  • ***
  • Posts: 229
Re: Custom node/metrics
« Reply #15 on: October 16, 2017, 09:49:02 AM »
Until I find the error in the code posted immediately prior to this post, I will try to use the node.metrics['UID'].logValue rather than the string based node.metrics['UID'].value.  While it won't read as well, if I can get it to at least work, it will be making progress.

HeneryH

  • Full Member
  • ***
  • Posts: 229
Re: Custom node/metrics
« Reply #16 on: October 17, 2017, 09:34:15 AM »
The following code is not executing as expected.  Can anyone spot the error?

If I set the hardcoded  "  || 1"  to force the expression to be true, then the doorbell sound rings. 

If I set the hardcoded  "  || 0" to have the evaluator check the UID of the card, it does not work.

The gateway is receiving the following message from the RFID Mot
Code: [Select]
[10-14-17_17:58:18.724] [LOG]   >: [22] RFID UID:90a3e116 BAT:0.98v   [RSSI:-49][ACK-sent]
[10-14-17_17:58:18.826] [LOG]   post: /home/pi/gateway/data/db/0022_V.bin[1508003898,0.98]
[10-14-17_17:58:18.852] [LOG]   post: /home/pi/gateway/data/db/0022_RSSI.bin[1508003898,-49]
[10-14-17_17:58:19.008] [LOG]      [22] DB-Updates:1

So the gateway is getting the UID of 90a3e116 as expected.

The metric file should be getting that and mapping the value to "Card 01" then evaluating the event to ring the doorbell when there is a match for Card 01.

Code: [Select]
exports.metrics = {
  RFID_UID : { name:'UID', regexp:/UID:90a3e116/i, value:'Card 01', logValue:1},
  RFID_UID : { name:'UID', regexp:/UID:8008e616/i, value:'Card 02', logValue:2},
  RFID_UID : { name:'UID', regexp:/UID:xxxx/i, value:'Card 03', logValue:3},
  RFID_UID : { name:'UID', regexp:/UID:[0-9a-fA-F]+/i, value:"Unknown Card", logValue:0},
};

//example of overriding an event
exports.events = {

  rfidSound : { label:'RFID : Xxx ', icon:'audio', descr:'Play either grant or denysound when RFID card is detected', serverExecute:function(node) { if (node.metrics['UID'] && (node.metrics['UID'].value == 'Card 01' || 0 )) { io.sockets.emit('PLAYSOUND', 'sounds/doorbell.wav'); }; } },

};

So the either the mapping of the value to "Card 01" is not happening or the evaluation of node.metrics['UID'].value == 'Card 01'  is not triggering as TRUE.

I was thinking that maybe the string compare wasn't behaving properly with strings so I tried switching to strcmp(a,b) but that syntax wasn't accepted.  I tried double-quotes instead of single quotes without success.  I'm not 100% on the syntax being used here so if there is any advice I would greatly appreciate it.

It is definitely the pattern matching that is not working correctly.

Can anyone spot an error in this code?  All card scans return "Unknown Card" as the value.
Code: [Select]
exports.metrics = {
  RFID_UID : { name:'UID', regexp:/UID:90a3e116/i, value:'Card 01', logValue:1},
  RFID_UID : { name:'UID', regexp:/UID:8008e616/i, value:'Card 02', logValue:2},
  RFID_UID : { name:'UID', regexp:/UID:xxxx/i, value:'Card 03', logValue:3},
  RFID_UID : { name:'UID', regexp:/UID:[0-9a-fA-F]+/i, value:"Unknown Card", logValue:0},
};

Here is the output of the server log
Code: [Select]
[10-14-17_17:58:18.724] [LOG]   >: [22] RFID UID:90a3e116 BAT:0.98v   [RSSI:-49][ACK-sent]
[10-14-17_17:58:18.826] [LOG]   post: /home/pi/gateway/data/db/0022_V.bin[1508003898,0.98]
[10-14-17_17:58:18.852] [LOG]   post: /home/pi/gateway/data/db/0022_RSSI.bin[1508003898,-49]
[10-14-17_17:58:19.008] [LOG]      [22] DB-Updates:1

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Custom node/metrics
« Reply #17 on: October 17, 2017, 10:14:36 AM »
The patterns work. You can see it work here.
The problem is you are overriding them. Look at the others in metrics.js where they are the same metric, you will notice the variable names are different, otherwise only the last one would stand.
Use a different variable name each time you declare a new variant.

Code: [Select]
exports.metrics = {
  RFID_UID1 : { name:'UID', regexp:/UID:90a3e116/i, value:'Card 01', logValue:1},
  RFID_UID2 : { name:'UID', regexp:/UID:8008e616/i, value:'Card 02', logValue:2},
  RFID_UID3 : { name:'UID', regexp:/UID:xxxx/i, value:'Card 03', logValue:3},
  RFID_UID4 : { name:'UID', regexp:/UID:[0-9a-fA-F]+/i, value:"Unknown Card", logValue:0},
};

Makes sense?

HeneryH

  • Full Member
  • ***
  • Posts: 229
Re: Custom node/metrics
« Reply #18 on: October 17, 2017, 10:25:42 AM »
Sorry, I may be trying something that just wasn't meant to be.

I want one variable that gets set according to the text string received, then test that string in the event rule. 

Sounds like that might not be possible.


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Custom node/metrics
« Reply #19 on: October 17, 2017, 11:24:49 AM »
Just use my adjusted version of your metrics.

HeneryH

  • Full Member
  • ***
  • Posts: 229
Re: Custom node/metrics
« Reply #20 on: October 19, 2017, 09:18:52 AM »
Can someone tell me the syntax of the serverExecute() function and in particular the semi-colon placement?  I'm used to c++ and when I try to put an 'else' clause in my function it doesn't work.  I think the semicolon matters here even though it doesn't in c++.

Thanks

Code: [Select]
                                         serverExecute:function(node) { 
                                                if (node.metrics['UID'] && (
                                                           node.metrics['UID'].value == 'Card 01' ||
                                                           node.metrics['UID'].value == 'Card 02' ||
                                                           node.metrics['UID'].value == 'Card 03' ||
                                                           node.metrics['UID'].value == 'Card 04'
                                                           ) {
                                                      io.sockets.emit('PLAYSOUND', 'sounds/access_granted.wav');
                                                } else {
                                                      io.sockets.emit('PLAYSOUND', 'sounds/access_denied.wav');
                                               };                  <-- what is syntax requirements for thiss emicolon? 
                                         }

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Custom node/metrics
« Reply #21 on: October 19, 2017, 01:08:59 PM »
Javascript != c++  :D

You can omit that semicolon, and in fact you can omit the other in the main if as well.