Metric value transformation [solved]

Started by Lukapple, September 26, 2017, 09:34:08 AM

Lukapple

Felix,
could you maybe add a value transformation(defined in metrics file) option functionality to the gateway?
I've following scenario:
I'm using a sonar mote for measuring oil level in tanker. Sonar mote is attached at top of the tanker and returns values in "cm".
Now I need to convert those values to liters.

I know that I could:
- do the math on sonar mote, but I don't want to waste the battery for math and I want that sonar mote returns distance in "cm" (maybe someday I'll use it for measuring something else)
- do the math on gateway mote/ mightyhat, but I don't want to modify original sketch with some exceptions and "IF" mess. Gateway mote should just pass messages forward to Pi.
- modify gateway.js script, but I don't want to do that, because it will be overwritten on next gateway updates

So it would be great if we could define a value transform function in metrics file, something like that:
//we have a tanker with dimensions: W x H x D: 148 x 140 x 75
//liters = ((H - node_CM_value) * W * D) / 1000
sonar : { 
  name:'CM', 
  regexp:/\b([\d\.]+)cm?\b/i, 
  value:'',
  valueTransformer: {transform:  function(cm) {return (((140-cm) * 148 * 75) / 1000)}}
//and "unit transfer" from cm to liters
  unit:'l', 
  pin:1, 
  graph:1,  
  graphOptions: { legendLbl:'Litri', 
                  lines: { lineWidth:1 }, 
                  colors:['#09c'], 
                  yaxis: {transform:  function(cm) {return (((140-cm) * 148 * 75) / 1000)}} //doesn't work?
                }
} 

Something similar is already possible to do with graphs, see "yaxis:...", but it's only for plotting graphs and I didn't get it to work yet.


Thanks!

Felix

This is already doable with the valuation() function, look at the FH metric:

FH : { name:'F', regexp:/\bF\:(-?\d+)\b/i, value:'', duplicateInterval:3600, valuation:function(value) {return value/100;}, ... }

Lukapple

And it works perfect, thanks.
I Can't find the word "valuation" in gateway.js, where is it implemented?

Felix

In metrics.js:

determineValue()
and
determineGraphValue()

Lukapple