LowPowerLab Forum

Software support => Pi Gateway => Topic started by: Lukapple on September 26, 2017, 09:34:08 AM

Title: Metric value transformation [solved]
Post by: Lukapple on September 26, 2017, 09:34:08 AM
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!
Title: Re: Value transformers
Post by: Felix on September 26, 2017, 10:27:08 AM
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;}, ... }
Title: Re: Value transformers
Post by: Lukapple on September 27, 2017, 02:10:55 AM
And it works perfect, thanks.
I Can't find the word "valuation" in gateway.js, where is it implemented?
Title: Re: Value transformers
Post by: Felix on September 27, 2017, 11:28:17 AM
In metrics.js:

determineValue()
and
determineGraphValue()
Title: Re: Metric value transformation
Post by: Lukapple on September 27, 2017, 04:05:16 PM
Ah, now I see, thanks!