Processing, permutations, event detection, and generation of metadata on in-flight data, or stream processing / stream analytics, is usually where the high-value features of an IoT application are derived. That being said, deploying a service to perform stream analytics is way harder than it has to be on many application platforms. But stream analytics doesn’t have to require a degree in data science or a background in SQL queries — with scriptr.io, you just have to be able to express your objective in Javascript and push “Save”. Read on for some examples from an in-production telematics deployment.

My customer provides a fleet management, accounting, and tracking service. The assets in question are large commercial trucks, high value assets, in which their time-in-service, distance traveled, and current and historical locations are all things that we want to know about. To make this happen, we need to implement stream analytics to 1) calculate speed on the fly using GPS location (we don’t have access to the OBD bus). Then, we need to 2) understand if the asset has moved more than 1/2 mile per minute for 3 consecutive minutes. We call this a “Travel Start” event, which signifies that the asset is not just moving around the storage yard loading/fueling, but is actually out on the highway traveling. Finally, we close the trip by 3) reading if the asset has moved less than 1/2 mile for 5 consecutive minutes.

That script might look something like this:

var truck = request.body.truck; // the request object contains the data package from the device POST to this endpoint
var localTruck = storage.local[truck.id]; // the storage object is the script's local key/value store
var distanceTraveled = distanceCalculation(truck.lat, truck.lng, localTruck.lat, localTruck.lng);

if (!localTruck.traveling) { // Not in traveling state, check for distance
  
  if (distanceTraveled > 0.5) {
    localTruck.travelStartCounter += 1;
  } else {
    localTruck.travelStartCounter = 0;
  }

  if (localTruck.travelStartCounter >== 3) {
    localTruck.traveling = true;
    createEvent('Travel Start', localTruck);
  }

} else {

  if (distanceTraveled < 0.5) {
    localTruck.travelStopCounter += 1;
  } else {
    localTruck.travelStopCounter = 0;
  }

  if (localTruck.travelStopCounter >== 5) {
    localTruck.traveling = false;
    createEvent('Travel Stop', localTruck);
  } else {
    createEvent('Travel', localTruck, distanceTraveled);
}

localTruck.lat = truck.lat;
localTruck.lng = truck.lng;

You’ll notice that the Local Storage module in scriptr.io made this stream analytics tasks extremely expressive and did not require spinning up (or maintaining) any database services. Being able to easily cache and retrieve recent values like this is key and opens up tons of advanced possibilities for computations of trends and more.

What are the practical uses of this script now that it exists?

  1. The ‘createEvent’ function could potentially create a Keen.io event, allowing us to perform ‘big data’-type queries later on, like “Which 10 trucks traveled the farthest last month?”, “How far did a specific truck travel in the last 10 days?”, etc. We have a connector for that.
  2. scriptr.io provides the ability to subscribe one script to another, which is a nice way to separate concerns in a large IoT application. So, once this stream analytics job is done running, it could trigger another script that could update our mapping UI showing the location of all of the trucks.
  3. scriptr.io provides the ability to run scripts on a scheduler, so we could do some things like check if trucks have ignition on (if that data comes in the payload) but aren’t moving for a certain amount of time (idling is a huge waste of money when some trucks burn fuel at 1L/minute). We could also check if they have moved significantly but show that ignition is off (possible tampering, malfunction, or theft).