===== Loading a JavaScript Functions File ===== The load() function allows you to import a file of functions you can use in your script. This file must reside in the same directory where your application is running to be loaded. For MIStudio development this file should be put into the devices folder of the project so it is available at development time as well as used for the deployed project. For TransSECS projects, this file should be put into the resources of the project so it is copied to the deployment. Example: Create a file called test.js which contains: //simple load() test //function to double a number function aTest(number){ number=number*2; return number; } ====Test in MIStudio==== Place the script file where it will be available for testing in MIStudio, into the MIStudio directory. Also put it into the userfiles of the project so it will be deployed with your MIX build. Start an MIStudio project. Add a Javascript bean to the diagram and a SwingTextField to the design. Connect the SwingTextField to the Javascript bean in the diagram. Connect the output of the Javascript to an Annunciator. Enter this into the Javascript's script: try { load("test.js"); x=incomingValue.getDoubleValue(); y=aTest(x); print("result is: "+ y); } catch (e) { print("Error in script: \n"+e.stack); print(e); } Errors: You may see an error such as this: javax.script.ScriptException: TypeError: Cannot load script from test.js in at line number 3 at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470) ... This error occurs if the script "test.js" cannot be found. You may need to close and re-open the MIStudio project if you have just newly added the script. ====Test in TransSECS Devices==== TransSECS Devices allows scripts on the incoming message beans. The script can only be tested in the deployed project so copy the test.js into your project's resource directory. Use the PLCTool for testing and select a message bean, such as HostCommandSTART, and enter this script: try { load("test.js"); y=aTest(15); print(y); } catch (e) { print("Error in script: \n"+e.stack); print(e); } Build, deploy, and run your Devices project. When the host sends the host command "START" this script will be executed. You can make changes to test.js to change what the aTest function calculates and the changes will be in effect in real time (no need to rebuild the project). ====Notes==== ===Loading vs. Script in Application=== The difference in using load() in the script to add a function compared to entering this same code in the script directly is that the function(s) in the .js file are read from the file each time the script is triggered and executed if called, without any optimizations. This should be a consideration when deploying a script using load(). If the function code is entered in the script and built with the deployment, this code (as well as all the other script code) is JIT compiled at runtime so that optimizations are in effect (so the second time the code is run there is not a delay while the code is loaded and executed). ===Referencing Servers in the Project and in the Devices Node=== Scripts loaded from the files do not have the internal shortcuts that MIStudio and TransSECS use to reference data sources in the project but these servers can still be used by using "bsf.lookupBean()" with the fully qualified path to the server in the (). As an example, in MIStudio you can access a server in the "Main" application by using "newValue=/Main/ServerName->getIntValue()". In the script loaded using load() this must be done a little differently: server=bsf.lookupBean("/Main/FloatFluxServer"); newValue=server.getDoubleValue(); server.setTimebase(2.0); Similarly, if you have added servers to the "Devices" node, these can be accessed using a similar technique: server=bsf.lookupBean("/Devices/DemoServers_Servers/IncrementServer"); currentValue = sever.getIntValue();