Skip to content

Commit

Permalink
lichen-community-systemsgh-3: Implements basic logic for converting p…
Browse files Browse the repository at this point in the history
…rimitive-valued event parameters.
  • Loading branch information
colinbdclark committed May 26, 2017
1 parent edc5cea commit a0ac66d
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/js/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"env": {
"browser": "true",
"node": "true"
}
}
107 changes: 107 additions & 0 deletions src/js/phet-converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* PhET-OSC Bridge PhET Event Converter
* http://github.com/fluid-studios/phet-osc-bridge
*
* Copyright 2017, OCAD University
* Licensed under the New BSD license.
*/

"use strict";

var fluid = fluid || require("infusion"),
phetosc = phetosc || fluid.registerNamespace("phetosc");

fluid.defaults("phetosc.converter", {
gradeNames: "fluid.component",

shouldBundleParams: true,

addressTemplate: "/%phetioID/%eventType/%event/",

jsToOSCTypes: {
"number": "d",
"string": "s",
"boolean": "b"
},

invokers: {
toOSC: "phetosc.converter.toOSC({arguments}.0, {that}.options)"
}
});

phetosc.converter.toOSC = function (phetEvent, options) {
var packets = [];


return phetosc.converter.eventToOSCMessage(phetEvent, options, packets);
};

phetosc.converter.visitPhETEvents = function (phetEvent) {

};

phetosc.converter.eventToOSCMessage = function (phetEvent, options, packets) {
var messageCollection,
togo;

if (options.shouldBundleParams) {
var bundle = phetosc.converter.createBundleForEvent(phetEvent, packets);
messageCollection = bundle.packets;
togo = bundle;
} else {
messageCollection = packets;
togo = messageCollection;
}

var addressPrefix = phetosc.converter.messageAddressPrefix(phetEvent, options);
phetosc.converter.parametersToMessages(phetEvent, addressPrefix, options, messageCollection);

return togo;
};

phetosc.converter.createBundleForEvent = function (phetEvent, packets) {
var bundle = {
packets: []
};

packets.push(bundle);

return bundle;
};

phetosc.converter.messageAddressPrefix = function (phetEvent, options) {
return fluid.stringTemplate(options.addressTemplate, phetEvent);
};

phetosc.converter.parametersToMessages = function (phetEvent, addressTemplate, options, packets) {
fluid.each(phetEvent.parameters, function (val, key) {
var message = phetosc.converter.parameterToMessage(val, key, addressTemplate, options);
packets.push(message);
});
};

phetosc.converter.parameterToMessage = function (val, key, addressTemplate, options) {
var message = {
address: addressTemplate + key,
args: [
{
type: phetosc.converter.oscTypeForArgument(val, options.jsToOSCTypes),
value: val
}
]
};

return message;
};

phetosc.converter.oscTypeForArgument = function (val, jsToOSCTypes) {
var jsType = typeof val,
oscType = jsToOSCTypes[jsType];

if (!oscType) {
throw new Error("An invalid JavaScript value type was found. Can't convert parameter to an OSC messagee. Parameter value was: " +
fluid.prettyPrintJSON(val));
}

return oscType;
};
10 changes: 10 additions & 0 deletions tests/data/osc/osc-particle-count-changed.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
[
{
"packets": [
{
"address": "/buildAnAtom.atomScreen.model.particleAtom.particleCountProperty/model/changed/oldValue",
"args": [
{
"type": "d",
"value": 0
}
]
},

{
"address": "/buildAnAtom.atomScreen.model.particleAtom.particleCountProperty/model/changed/newValue",
"args": [
Expand Down
3 changes: 3 additions & 0 deletions tests/html/phet-converter-tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<script type="text/javascript" src="../../node_modules/infusion/tests/lib/qunit/js/qunit.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/dist/infusion-all.js"></script>
<script type="text/javascript" src="../../node_modules/infusion/tests/test-core/jqUnit/js/jqUnit.js"></script>

<script type="text/javascript" src="../../src/js/phet-converter.js"></script>

<script type="text/javascript" src="../js/conversion-tester.js"></script>
<script type="text/javascript" src="../js/phet-converter-tests.js"></script>
</head>
Expand Down
16 changes: 10 additions & 6 deletions tests/js/conversion-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ fluid.defaults("phetosc.conversionTester", {
phetPath: "",
oscPath: "",

invokers: {
runTests: {
funcName: "fluid.mustBeOverridden",
args: ["{that}", "{arguments}.0", "{arguments}.1"]
}
},

events: {
onPhETFileLoaded: null,
onOSCFileLoaded: null,
Expand All @@ -27,7 +34,8 @@ fluid.defaults("phetosc.conversionTester", {
events: {
phet: "{that}.events.onPhETFileLoaded",
osc: "{that}.events.onOSCFileLoaded"
}
},
args: ["{arguments}.phet.0", "{arguments}.osc.0"]
}
},

Expand Down Expand Up @@ -63,11 +71,7 @@ fluid.defaults("phetosc.conversionTester", {

"onFilesLoaded.runTests": {
priority: "after:defineModule",
func: "{that}.runTests",
args: [
"{arguments}.phet.0",
"{arguments}.osc.0"
]
func: "{that}.runTests"
}
}
});
Expand Down
17 changes: 14 additions & 3 deletions tests/js/phet-converter-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@ fluid.defaults("phetosc.tests.converter.primitiveValueChanged", {
phetPath: "../data/phet/phet-particle-count-changed.json",
oscPath: "../data/osc/osc-particle-count-changed.json",

components: {
converter: {
type: "phetosc.converter"
}
},

invokers: {
runTests: "phetosc.tests.converter.runTests"
runTests: {
funcName: "phetosc.tests.converter.runTests",
args: ["{that}", "{arguments}.0.0", "{arguments}.1.0"]
}
}
});

phetosc.tests.converter.runTests = function (phetEvent, expected) {
phetosc.tests.converter.runTests = function (that, phetEvent, expected) {
jqUnit.test("Primitive value changed", function () {
jqUnit.assertTrue("Test not implemented!", expected);
var actual = that.converter.toOSC(phetEvent);
jqUnit.assertDeepEq("PhET event was correctly converted to an OSC bundle",
expected, actual);
});
};

Expand Down
1 change: 1 addition & 0 deletions tests/node-all-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"use strict";

var fluid = require("infusion");
fluid.require("%phetosc/src/js/phet-converter.js");
fluid.require("%phetosc/tests/js/conversion-tester.js");

require("./js/phet-converter-tests.js");

0 comments on commit a0ac66d

Please sign in to comment.