Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
linkController: Make sure that the server can only store a link copy once.
hardwareInterface: reformating
nodeUtilities: init
recorder: bugfix for complex data recording
utilities: forEachLink function
server: sceneGraph support for nodes and logicBlocks
  • Loading branch information
valentinptc committed Dec 15, 2020
1 parent 8a3f524 commit fb31de9
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 43 deletions.
22 changes: 18 additions & 4 deletions controllers/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getLinkData(fullEntry, wasAdded) {
var linkAddedData = null;

if (fullEntry) {
console.log('getLinkData', fullEntry);
// console.log('getLinkData', fullEntry);

var linkObjectA = fullEntry['objectA'];
var linkObjectB = fullEntry['objectB'];
Expand Down Expand Up @@ -65,6 +65,7 @@ function getLinkData(fullEntry, wasAdded) {
* @param {Link} body
*/
const newLink = function (objectID, frameID, linkID, body) {

var updateStatus = 'nothing happened';

var foundFrame = utilities.getFrame(objects, objectID, frameID);
Expand All @@ -77,9 +78,22 @@ const newLink = function (objectID, frameID, linkID, body) {
body.frameA === body.frameB &&
body.nodeA === body.nodeB);

foundFrame.links[linkID] = body;
utilities.forEachLinkInFrame(utilities.getFrame(objects, body.objectA, body.frameA), function (thisLink) {
if (!body.loop) {
console.log('link already exists');
body.loop = (body.objectA === thisLink.objectA &&
body.objectB === thisLink.objectB &&
body.frameA === thisLink.frameA &&
body.frameB === thisLink.frameB &&
body.nodeA === thisLink.nodeA &&
body.nodeB === thisLink.nodeB
);
}
});


if (!body.loop) {
foundFrame.links[linkID] = body;
console.log('added link: ' + linkID);
// write the object state to the permanent storage.
utilities.writeObjectToFile(objects, objectID, objectsPath, globalVariables.saveToDisk);
Expand Down Expand Up @@ -230,7 +244,7 @@ const deleteLinkLock = function (objectKey, frameKey, linkKey, password) {
return updateStatus;
};

const setup = function(objects_, knownObjects_, socketArray_, globalVariables_, hardwareAPI_, objectsPath_, socketUpdater_) {
const setup = function (objects_, knownObjects_, socketArray_, globalVariables_, hardwareAPI_, objectsPath_, socketUpdater_) {
objects = objects_;
knownObjects = knownObjects_;
socketArray = socketArray_;
Expand All @@ -246,4 +260,4 @@ module.exports = {
addLinkLock: addLinkLock,
deleteLinkLock: deleteLinkLock,
setup: setup
};
};
14 changes: 7 additions & 7 deletions libraries/hardwareInterfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ exports.getKnownObjects = function () {
return knownObjects;
};

exports.getSceneGraph = function() {
exports.getSceneGraph = function () {
// send acyclic version of sceneGraph by removing linkedVehicles if needed
sceneGraphReference.recomputeGraph();
let sceneGraphCopy = {};
Expand All @@ -252,26 +252,26 @@ exports.getSceneGraph = function() {
return sceneGraphCopy;
};

exports.getWorldGraph = function() {
exports.getWorldGraph = function () {
if (!worldGraphReference) {
return {};
}
return worldGraphReference.compile().getSerializableCopy();
};

exports.getDistanceOneToMany = function(id1, ids) {
exports.getDistanceOneToMany = function (id1, ids) {
sceneGraphReference.recomputeGraph();
let distances = {};
distances[id1] = {};
ids.forEach(function(id2) {
ids.forEach(function (id2) {
let computedDistance = sceneGraphReference.getDistanceBetween(id1, id2);
console.log(computedDistance);
distances[id1][id2] = computedDistance;
});
return distances;
};

exports.onSceneGraphUpdated = function(callback) {
exports.onSceneGraphUpdated = function (callback) {
sceneGraphReference.onUpdate(callback);
};

Expand Down Expand Up @@ -733,7 +733,7 @@ let setHardwareInterfaceSettingsImpl = null;
* @param {Array.<string>} limitToKeys - if provided, only affects the properties of settings whose keys are included in this array
* @param {successCallback} callback
*/
exports.setHardwareInterfaceSettings = function(interfaceName, settings, limitToKeys, callback) {
exports.setHardwareInterfaceSettings = function (interfaceName, settings, limitToKeys, callback) {
return setHardwareInterfaceSettingsImpl(interfaceName, settings, limitToKeys, callback);
};

Expand All @@ -745,7 +745,7 @@ exports.setHardwareInterfaceSettings = function(interfaceName, settings, limitTo
* Complement to setup() which is necessary due to the unique positioning of
* the setHardwareInterfaceSettings function
*/
exports.setHardwareInterfaceSettingsImpl = function(setHardwareInterfaceSettings) {
exports.setHardwareInterfaceSettingsImpl = function (setHardwareInterfaceSettings) {
setHardwareInterfaceSettingsImpl = setHardwareInterfaceSettings;
};

Expand Down
48 changes: 33 additions & 15 deletions libraries/nodeUtilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ const Link = require('../models/Link');
var linkController;
// Pointers populated from server.js with setup()
var objects = {};
var sceneGraph = {};
var knownObjects = {};
var socketArray = {};
var globalVariables = {};
var hardwareAPI = {};
var objectsPath = {};

exports.setup = function (_objects, _knownObjects, _socketArray, _globalVariables, _hardwareAPI, _objectsPath, _linkController) {
exports.setup = function (_objects, _sceneGraph, _knownObjects, _socketArray, _globalVariables, _hardwareAPI, _objectsPath, _linkController) {
objects = _objects;
sceneGraph = _sceneGraph;
knownObjects = _knownObjects;
socketArray = _socketArray;
globalVariables = _globalVariables;
Expand All @@ -21,33 +23,45 @@ exports.setup = function (_objects, _knownObjects, _socketArray, _globalVariable

exports.deepCopy = utilities.deepCopy;

exports.searchNodeByType = function (nodeType, object, tool, node, callback) {

let thisObject = utilities.getObject(objects, object);

exports.searchNodeByType = function (nodeType, _object, tool, node, callback) {
let thisObjectKey = _object;
if (!(_object in objects)) {
thisObjectKey = utilities.getObjectIdFromTargetOrObjectFile(_object, objectsPath);
}
let thisObject = utilities.getObject(objects, thisObjectKey);
if (!tool && !node) {
utilities.forEachFrameInObject(thisObject, function (thisTool, toolKey) {
utilities.forEachNodeInFrame(thisTool, function (thisNode, nodeKey) {
if (thisNode.type === nodeType) callback(object, toolKey, nodeKey);
if (thisNode.type === nodeType) callback(thisObjectKey, toolKey, nodeKey);
});
});
} else if (!node) {
let thisTool = utilities.getFrame(objects, object, tool);
let thisTool = utilities.getFrame(objects, thisObjectKey, tool);
if (!thisTool) {
thisTool = utilities.getFrame(objects, thisObjectKey, thisObjectKey + tool);
}
utilities.forEachNodeInFrame(thisTool, function (thisNode, nodeKey) {
if (thisNode.type === nodeType) callback(object, tool, nodeKey);
if (thisNode.type === nodeType) {
callback(thisObjectKey, tool, nodeKey);
}
});

} else if (!tool) {
utilities.forEachFrameInObject(thisObject, function (tool, toolKey) {
let thisNode = utilities.getFrame(objects, toolKey, node);
let thisNode = utilities.getFrame(objects, thisObjectKey, toolKey, node);
if (!thisNode) {
if (thisNode.type === nodeType) callback(object, toolKey, node);
if (thisNode.type === nodeType) callback(thisObjectKey, toolKey, node);
}
});
}
};

exports.createLink = function (originObject, originTool, originNode, destinationObject, destinationTool, destinationNode) {
exports.createLink = function (originObject, _originTool, originNode, destinationObject, destinationTool, destinationNode) {

let originTool = _originTool;
if (!utilities.getFrame(objects, originObject, _originTool)) {
originTool = originObject + _originTool;
}
var linkBody = new Link();
linkBody.objectA = originObject;
linkBody.frameA = originTool;
Expand All @@ -63,12 +77,16 @@ exports.deleteLink = function (object, tool, link) {
};

exports.getWorldObject = function (object) {
//todo @Ben how do I find out what world object is assosiated with the object?
let thisObject = utilities.getObject(objects, object);
if (thisObject) {
if (thisObject.hasOwnProperty('worldId')) {
return thisObject.worldId;
}
}
return null;
};

exports.getWorldLocation = function (object) {
//todo @Ben how do I find out the world location
return null;
exports.getWorldLocation = function (objectID) {
return sceneGraph.getWorldPosition(objectID);
};

2 changes: 2 additions & 0 deletions libraries/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ recorder.recurse = function (obj, objectInTime, keyString) {

recorder.getItemFromArray = function (object, array) {
let item = object;
if (!item) return null;
let returnItem = {};
array.forEach(function (data) {
if (data !== '') {
Expand All @@ -97,6 +98,7 @@ recorder.getItemFromArray = function (object, array) {
}
// let newItem = item[data];
returnItem = item;
if(item[data])
item = item[data];
}
});
Expand Down
38 changes: 34 additions & 4 deletions libraries/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ exports.createFolder = function (folderVar, objectsPath, debug) {
}
});
}
/**
if (!fs.existsSync(firstFrame)) {
/**
if (!fs.existsSync(firstFrame)) {
fs.mkdirSync(firstFrame, '0766', function (err) {
if (err) {
console.error(err);
Expand All @@ -134,7 +134,7 @@ exports.createFolder = function (folderVar, objectsPath, debug) {
}
// writeObjectToFile(tempFolderName);
}
**/
**/
};


Expand Down Expand Up @@ -703,6 +703,7 @@ exports.actionSender = function (action, timeToLive, beatport) {
function doesObjectExist(objects, objectKey) {
return objects.hasOwnProperty(objectKey);
}

exports.doesObjectExist = doesObjectExist;

function getObject(objects, objectKey) {
Expand All @@ -711,6 +712,7 @@ function getObject(objects, objectKey) {
}
return null;
}

exports.getObject = getObject;

function doesFrameExist(objects, objectKey, frameKey) {
Expand All @@ -722,6 +724,7 @@ function doesFrameExist(objects, objectKey, frameKey) {
}
return false;
}

exports.doesFrameExist = doesFrameExist;

function getFrame(objects, objectKey, frameKey) {
Expand All @@ -733,6 +736,7 @@ function getFrame(objects, objectKey, frameKey) {
}
return null;
}

exports.getFrame = getFrame;

function doesNodeExist(objects, objectKey, frameKey, nodeKey) {
Expand All @@ -744,6 +748,7 @@ function doesNodeExist(objects, objectKey, frameKey, nodeKey) {
}
return false;
}

exports.doesNodeExist = doesNodeExist;

function getNode(objects, objectKey, frameKey, nodeKey) {
Expand All @@ -755,6 +760,7 @@ function getNode(objects, objectKey, frameKey, nodeKey) {
}
return null;
}

exports.getNode = getNode;

/**
Expand All @@ -769,6 +775,7 @@ function getObjectAsync(objects, objectKey, callback) {
var object = objects[objectKey];
callback(null, object);
}

exports.getObjectAsync = getObjectAsync;

/**
Expand All @@ -790,6 +797,7 @@ function getFrameAsync(objects, objectKey, frameKey, callback) {
callback(null, object, frame);
});
}

exports.getFrameAsync = getFrameAsync;

/**
Expand All @@ -812,6 +820,7 @@ function getNodeAsync(objects, objectKey, frameKey, nodeKey, callback) {
callback(null, object, frame, node);
});
}

exports.getNodeAsync = getNodeAsync;

/**
Expand Down Expand Up @@ -841,6 +850,7 @@ function getFrameOrNode(objects, objectKey, frameKey, nodeKey, callback) {
callback(null, object, frame, node);
});
}

exports.getFrameOrNode = getFrameOrNode;

function forEachObject(objects, callback) {
Expand All @@ -849,24 +859,41 @@ function forEachObject(objects, callback) {
callback(objects[objectKey], objectKey);
}
}

exports.forEachObject = forEachObject;

function forEachFrameInObject(object, callback) {
if (!object) return;
for (var frameKey in object.frames) {
if (!object.frames.hasOwnProperty(frameKey)) continue;
callback(object.frames[frameKey], frameKey);
}
}

exports.forEachFrameInObject = forEachFrameInObject;

function forEachNodeInFrame(frame, callback) {
if (!frame) return;
for (var nodeKey in frame.nodes) {
if (!frame.nodes.hasOwnProperty(nodeKey)) continue;
callback(frame.nodes[nodeKey], nodeKey);
}
}

exports.forEachNodeInFrame = forEachNodeInFrame;


function forEachLinkInFrame(frame, callback) {
if (!frame) return;
for (var nodeKey in frame.links) {
if (!frame.links.hasOwnProperty(nodeKey)) continue;
callback(frame.links[nodeKey], nodeKey);
}
}

exports.forEachLinkInFrame = forEachLinkInFrame;


/**
* Helper function to return the absolute path to the directory that should contain all
* video files for the provided object name. (makes dir if necessary)
Expand All @@ -888,17 +915,20 @@ function getVideoDir(objectsPath, identityFolderName, isMobile, objectName) {

return videoDir;
}

exports.getVideoDir = getVideoDir;

// Ensures id is alphanumeric or -_
function isValidId(id) {
return id.match(/^[A-Za-z0-9_-]+$/);
}

exports.isValidId = isValidId;

function goesUpDirectory(path) {
return path.match(/\.\./);
}

exports.goesUpDirectory = goesUpDirectory;

function deepCopy(item) {
Expand All @@ -922,7 +952,7 @@ function deepCopy(item) {

exports.deepCopy = deepCopy;

exports.httpGet = function(url) {
exports.httpGet = function (url) {
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
if (error) reject(error);
Expand Down
Loading

0 comments on commit fb31de9

Please sign in to comment.