Reglo
Posts: 66
Joined: Wed May 27, 2015 9:09 am
Location: USA

Device mapper script

Tue Jun 23, 2015 11:55 pm

I have been trying to create a simple device mapper script to evaluate whether the speed of a vehicle exceeds the value of a custom variable called "Speedlimit". However, it's not acting as expected. The script is as follows:

Speeding. Return type: System.Boolean.

Code: Select all

var Speed_limit = user.getCustomFieldValue ('Speedlimit');
var Current_speed = fields.get ('Speed', 0);
log ('Speed_limit: ' + Speed_limit);
log ('Current_speed: ' + Current_speed);
if (Current_speed > Speed_limit) return true;
But when I test it, using 50 as the value of "Speedlimit" and 60 as the value of "Speed", it gives the following result:

Code: Select all

log: Speed_limit: 50
log: Current_speed: 60
Speeding: False 
What am I missing?

User avatar
johan
GpsGate
Posts: 15400
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Device mapper script

Wed Jun 24, 2015 12:45 am

Speed is in meters per second. And I assume your Speed limit is in some other unit, maybe kph?

Assume it is kph, then you need to divide it with 3.6 to convert it to meters per second.

Regards,
Johan

GpsGate Support

Reglo
Posts: 66
Joined: Wed May 27, 2015 9:09 am
Location: USA

RE: Device mapper script

Wed Jun 24, 2015 12:52 am

Actually my Speed is in mph. But I don't think unit conversion is the issue -- the same result occurs if Speed is a large number such as 6000. Strangely, if I replace > with = in the script, it always returns "true," regardless of the variable values!

User avatar
johan
GpsGate
Posts: 15400
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Device mapper script

Wed Jun 24, 2015 1:01 am

Change to

if (Current_speed > Speed_limit) return true;
else return false;

And consider units.

Regards,
Johan

GpsGate Support

Reglo
Posts: 66
Joined: Wed May 27, 2015 9:09 am
Location: USA

RE: Device mapper script

Wed Jun 24, 2015 1:05 am

Just tried it - no difference.

User avatar
johan
GpsGate
Posts: 15400
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Device mapper script

Wed Jun 24, 2015 1:09 am

Make the script simpler. Start with just one row. Once you understand that row, and one more row. And so on.

Break down the problem into pieces you control and master.

Regards,
Johan

GpsGate Support

Reglo
Posts: 66
Joined: Wed May 27, 2015 9:09 am
Location: USA

RE: Device mapper script

Wed Jun 24, 2015 1:13 am

I don't see how it could be any simpler. There are two variables, each of which is logging with the expected values, but the script fails to properly evaluate which one is larger.

User avatar
johan
GpsGate
Posts: 15400
Joined: Wed Aug 04, 2004 10:40 pm
Location: Sweden
Contact: Website

RE: Device mapper script

Wed Jun 24, 2015 1:16 am

There is no magic. The script will execute exactly as you have written it.

If you want to debug and get a better understanding of how you write a script, make it smaller, and then extend it.

Just a tips (have no more tips).

Regards,
Johan

GpsGate Support

Reglo
Posts: 66
Joined: Wed May 27, 2015 9:09 am
Location: USA

RE: Device mapper script

Wed Jun 24, 2015 1:18 am

I'm thinking that maybe there is some kind of mismatch between variable types, or something like that. Because otherwise it doesn't seem to make any sense.

Reglo
Posts: 66
Joined: Wed May 27, 2015 9:09 am
Location: USA

RE: Device mapper script

Wed Jun 24, 2015 6:40 am

Well, I found a way to get the job done, by introducing an intermediate variable:

Code: Select all

var Speed_limit = user.getCustomFieldValue ('Speedlimit');
var Current_speed = fields.get ('Speed', 0);
var Speed_over = (Current_speed - Speed_limit);
if (Speed_over > 0) return true;
else return false;
Apparently the numerical comparison in the 'if' statement only works properly when comparing a variable with a number, not when comparing two variables. I suspect that when there are two variables, the script evaluates them as Boolean rather than numeric (so they are always "equal").

Machtl
Posts: 88
Joined: Sat Aug 16, 2014 2:58 pm
Location: Austria

RE: Device mapper script

Fri Jul 24, 2015 5:32 pm

in such cases you can add +0 to the variable, now its a number. :-)

Reglo
Posts: 66
Joined: Wed May 27, 2015 9:09 am
Location: USA

RE: Device mapper script

Fri Jul 24, 2015 5:58 pm

Machtl,

That is a cool trick!

Reglo

Return to “GpsGate for Developers”