Skip to content

Instantly share code, notes, and snippets.

@germanviscuso
Created October 15, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save germanviscuso/5230047770090dea613b to your computer and use it in GitHub Desktop.
Save germanviscuso/5230047770090dea613b to your computer and use it in GitHub Desktop.
function update_trackers(params, context, done) {
// Available params are:
// params.thingID
// params.vendorThingID
// params.uri
// params.userID
// params.groupID
// params.values
// params.expected
console.log("Running update_trackers extension...");
var admin = context.getAppAdminContext();
var baseUrl = "https://dweet.io:443";
var createAction = "/dweet/for/";
admin.loadThingWithThingID(params.objectScope.thingID, {
success: function(thing) {
console.log("Thing loaded!");
//console.log(thing);
// Refresh sensor log entry
var sensorEntry = admin.objectWithURI(params.uri);
sensorEntry.refresh({
success: function(theObject) {
console.log("Sensor log entry refreshed!");
//console.log(theObject);
var latitude = null; //latitude, signed / degrees / float
var longitude = null; //longitude, signed / degrees / float
var speed = null; //gps speed / m/s / float
var altitude = null; //altitude, signed / meters / float
var accuracy = null; //accuracy / meters / float
var long_accel = null; //Longitudinal acceleration signed / g / float
var lat_accel = null; //Lateral acceleration, signed / g / float
var vert_accel = null; //Vertical acceleration, signed / g / float
var impact = null; //Impact, signed / g / float
var temperature = null; //Temperature, signed / C / float
var humidity = null; //Humidity / % / float
var light = null; //Ambient light / lux / float
var pressure = null; //Air pressure / hPa / float
var battery = null; //Current battery level / % / float
var charging = null; //Is connected to charger / boolean / boolean (0, 1)
var heading = null; //Heading 0– 360 degrees / degrees / float
var yaw = null; //Yaw, signed / degrees / float
var pitch = null; //Pitch, signed / degrees / float
var roll = null; //Roll, signed / degrees / float
var senses = theObject.get("senses");
for(var i=0; i<senses.length; i++) {
var item = senses[i];
if(item.sId == "0x00010100") {
latitude = item.val; // [-90,90]
} else
if(item.sId == "0x00010200") {
longitude = item.val; // [-180,180]
} else
if(item.sId == "0x00010300") {
altitude = item.val; // [-10000,10000]
} else
if(item.sId == "0x00010400") {
accuracy = item.val; // [0,1000]
} else
if(item.sId == "0x00020100") {
speed = item.val; // [0,1000]
} else
if(item.sId == "0x00030200") {
battery = item.val; // [0,100]
} else
if(item.sId == "0x00030400") {
charging = item.val; // [0,1]
} else
if(item.sId == "0x00040100") {
heading = item.val; // [0,360]
} else
if(item.sId == "0x00040200") {
yaw = item.val; // [-180,180]
} else
if(item.sId == "0x00040300") {
pitch = item.val; // [-90,90]
} else
if(item.sId == "0x00040400") {
roll = item.val; // [-180,180]
} else
if(item.sId == "0x00050100") {
long_accel = item.val; // [-4,4]
} else
if(item.sId == "0x00050200") {
lat_accel = item.val; // [-4,4]
} else
if(item.sId == "0x00050300") {
vert_accel = item.val; // [-4,4]
} else
if(item.sId == "0x00050400") {
impact = item.val; // [0,5.656]
} else
if(item.sId == "0x00060100") {
temperature = item.val; // [-40,85]
} else
if(item.sId == "0x00060200") {
humidity = item.val; // [0,100]
} else
if(item.sId == "0x00060300") {
light = item.val; // [0,187269]
} else
if(item.sId == "0x00060400") {
pressure = item.val; // [260,1260]
}
}
// Load application scope bucket for trackers
var appBucket = admin.bucketWithName("trackers");
// Create the conditions for the query
var clause = KiiClause.equals("thingID", thing.getThingID());
// Build the query with the clauses and some other parameters
var query = KiiQuery.queryWithClause(clause);
query.setLimit(1);
// Define the callbacks
var queryCallbacks = {
success: function(queryPerformed, resultSet, nextQuery) {
var globalTracker;
if(resultSet.length == 0) {
globalTracker = appBucket.createObject();
globalTracker.set("thingID", thing.getThingID());
globalTracker.set("vendorThingID", thing.getVendorThingID());
}
else
globalTracker = resultSet[0];
// Copy tracker properties
if(latitude != null) globalTracker.set("latitude", latitude); //latitude, signed / degrees / float
if(longitude != null) globalTracker.set("longitude", longitude); //longitude, signed / degrees / float
if(speed != null) globalTracker.set("speed", speed); //gps speed / m/s / float
if(altitude != null) globalTracker.set("altitude", altitude); //altitude, signed / meters / float
if(accuracy != null) globalTracker.set("accuracy", accuracy); //accuracy / meters / float
if(long_accel != null) globalTracker.set("long_accel", long_accel); //Longitudinal acceleration signed / g / float
if(lat_accel != null) globalTracker.set("lat_accel", lat_accel); //Lateral acceleration, signed / g / float
if(vert_accel != null) globalTracker.set("vert_accel", vert_accel); //Vertical acceleration, signed / g / float
if(impact != null) globalTracker.set("impact", impact); //Impact, signed / g / float
if(temperature != null) globalTracker.set("temperature", temperature); //Temperature, signed / C / float
if(humidity != null) globalTracker.set("humidity", humidity); //Humidity / % / float
if(light != null) globalTracker.set("light", light); //Ambient light / lux / float
if(pressure != null) globalTracker.set("pressure", pressure); //Air pressure / hPa / float
if(battery != null) globalTracker.set("battery", battery); //Current battery level / % / float
if(charging != null) globalTracker.set("charging", charging); //Is connected to charger / boolean / boolean (0, 1)
if(heading != null) globalTracker.set("heading", heading); //Heading 0– 360 degrees / degrees / float
if(yaw != null) globalTracker.set("yaw", yaw); //Yaw, signed / degrees / float
if(pitch != null) globalTracker.set("pitch", pitch); //Pitch, signed / degrees / float
if(roll != null) globalTracker.set("roll", roll); //Roll, signed / degrees / float
console.log("All properties of global tracker set. Saving to cloud...");
// Save global tracker
globalTracker.save({
success: function(theObject) {
console.log("Global tracker saved!");
//console.log(theObject);
var thingVendorId = theObject.get("vendorThingID");
if(thingVendorId == null)
console.log("Error: could not retrieve thing vendor id for visualization");
else {
targetUrl = baseUrl + createAction + thingVendorId;
var string = null;
$.each(theObject, function(key, val){
if(key == "_customInfo")
string = JSON.stringify(val);
});
if (string == null)
console.log("Error: can't retrieve thing custom data for visualization");
else {
console.log("Posting to " + targetUrl + " the JSON string " + string);
// Send visualization data
$.ajax({
url: targetUrl,
type: "POST",
data: string,
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(body) {
console.log("OK posting: " + JSON.stringify(body));
},
error: function(msg) {
console.log("Error posting:" + JSON.stringify(msg));
}
});
}
}
done("SUCCESS!");
},
failure: function(theObject, errorString) {
console.log("Error saving global tracker: " + errorString);
done("Error saving global tracker: " + errorString);
}
});
},
failure: function(queryPerformed, anErrorString) {
console.log("Error fetching global tracker: " + anErrorString);
done("Error fetching global tracker: " + anErrorString);
}
}
// Execute the query
appBucket.executeQuery(query, queryCallbacks);
},
failure: function(error) {
console.log("Error refreshing sensor log entry: " + error);
done("Error refreshing sensor log entry: " + error);
}
});
},
failure: function(error) {
console.log("Can't load Thing from sensor object: " + error);
done("Can't load Thing from sensor object: " + error);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment