Skip to content

Instantly share code, notes, and snippets.

@push-gists
Created May 19, 2016 14:27
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 push-gists/e4ed5cf8e7247d0d57ac735584e8e2a2 to your computer and use it in GitHub Desktop.
Save push-gists/e4ed5cf8e7247d0d57ac735584e8e2a2 to your computer and use it in GitHub Desktop.
File snippets from the Horse Race demonstration.
// Connect to Diffusion
function connectToDiffusion() {
console.log("Connecting to Diffusion...");
diffusion.connect({
principal : diffusionUser,
credentials : diffusionPassword,
host : diffusionHost,
port : diffusionPort,
secure : false,
}).then(onConnectSuccess, onConnectError);
// Create the JSON topic
session.topics.add(diffusionTopic, diffusion.topics.TopicType.JSON).then(
function(result) {
console.log('JSON Topic Added : ' + result.topic);
// Start the updates to the topic
updateTopic(session);
},
function(error) {
console.log('JSON Topic Add Failed : ' + error);
});
function updateTopic(session) {
// Update the topic with the data from the current row
var json = JSON.parse(horseData[fileRowNum]);
session.topics.update(diffusionTopic, json).then(onComplete, onError);
fileRowNum = fileRowNum + 1;
if (fileRowNum == horseData.length-1) {
fileRowNum = 0;
}
// Delay until the next record is published
setTimeout(function(){updateTopic(session)}, delay);
function onComplete(status) {
}
function onError(error) {
console.log("onError!" + error);
}
}
// Connection established
var datatype = diffusion.datatypes.json();
session.subscribe(diffusionTopic).asType(datatype).on({
subscribe : function(topic) {
// Subscribed to a particular topic
},
unsubscribe : function(topic, reason) {
// Unsubscribed from a particular topic
},
value : function(topic, specification, newValue, oldValue) {
// Get the JSON object and plot it on the race course
var jsObj = newValue.get();
// Convert the x and y position to absolute longitude and latitude from data origin. The identifier
// of the horse is the last two digits of the I field.
var horseId = parseInt(jsObj.I.substr(-2), 10);
var horseX = jsObj.X * cosmologicalConstant;
var horseY = jsObj.Y * cosmologicalConstant;
var radiusEarth = 6378137; // In meters
var horseLatitude = dataOriginLat + ( horseY / radiusEarth) * (180 / Math.PI);
var horseLongitude = dataOriginLong + ( horseX / radiusEarth) * (180 / Math.PI) / Math.cos(dataOriginLat * Math.PI/180);
// Place a marker for the horse on the map at the new location, creating a new one on the fly if it does not already exist
if( horses[horseId - 1] === undefined) {
// Create the new marker and append it to the array
var image = "icons/horse" + horseId + ".png";
var marker = new google.maps.Marker({
position: new google.maps.LatLng(horseLatitude, horseLongitude),
map: map,
icon : image
});
horses[horseId - 1] = marker;
numberOfHorses = numberOfHorses + 1;
}
else {
// We already had a marker for this horse, so move it...
horses[horseId - 1].setMap(null);
horses[horseId - 1].setPosition(new google.maps.LatLng(horseLatitude, horseLongitude));
horses[horseId - 1].setMap(map);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment