Import javascript library
-
@phildunlap said in Import javascript library:
Meta point
Hi Thanks for the reply. I wanted to create a Meta Point. Inside the Meta point write a script. I was hoping to be able to to reference a 3rd party javascript library something like:
var imported = document.createElement('script');
imported.src = '/path/to/imported/script';
document.head.appendChild(imported);the script i want to reference inside my meta point script is about 6mb and i cant simply copy and paste it.
So generally, what can i do? I simply want to write a script to use few point names and pass them to my 3rd party library t do some calculation and set the result into a new Meta Point, -
Oh I was wrong!
It looks like the load() function is what you're looking for. Give
load("https://wherever");
orload("/local/path/to/whatever.js");
a try.Edit: I would still recommend storing the file locally, lest the point be dependent on the internet and it do that load every single time the point runs. Perhaps put the load into the global script.
-
@phildunlap Thanks. I tried it and it instantly made my Mango Server to crawl to the point that i had to unplug and plug t back. It wasnt anything overly complicated. Something like:
load('http://whatever.js'); //about 7mb
var S=Module.SomeFunction('x','y','z'); //this is a function defined in the js i just loadedQuestion 1: I suspect it was because i was referencing the the javascript over http and because of its size it was taking to long to load. Any thought?
Question 2: If i want to print the value of S in the log file, can i simply say print(S)? I couldnt try it as the script was not running properly.
Thanks.
-
Hi arvand,
I couldn't tell you what performance you should expect from an unknown library. Local loading might help. You can wget it or whatever you like and then load it locally and test.
My inclination for a slow run would be to wonder... 1) How much memory was available before the JavaScript ran? How much did it consume? What is the library trying to do? 2) How often is the meta point running? Did you only test it with the validate button, or was it running
0/1 * * * * ?
?The print() function will only work in the browser. If you want it logged, you have to use the LOG object as described: https://help.infiniteautomation.com/about-mango-java-script/#logging
-
Thanks a lot. You have been very helpful. Now when i try to run the script (either through validate button or running in on a cron job) i get a memory error in ma.log.
java.lang.IllegalStateException: Timer already cancelled.
....
com.serotonin.m2m2.web.dwr.util.ExceptionDetectionFilter.doFilter:38) - DWR invocation exception
java.lang.OutOfMemoryError: GC overhead limit exceeded
...
ERROR 2017-07-10 16:43:13,852 (com.serotonin.m2m2.web.dwr.util.ExceptionDetectionFilter.doFilter:38) - DWR invocation exception
java.lang.OutOfMemoryError: Java heap spaceMy box has 1gb of memory allocated (memory-small.sh is enabled). When not running my script, under internal metrics, i see my available memory is 855 and jvm max available is 889. Also when i ssh into the box, i see this info about the memory:
Ram Info:
total used free shared buffers cached
1.9G 1.7G 203M 99M 31M 304Min my script, im not even trying to do anything. Just loading a coolprop library (http://www.coolprop.org/coolprop/wrappers/Javascript/index.html#javascript) and it gets the memory error. coolprop is about 6mb.
LOG.trace('Start');
load('/opt/mango/web/resources/coolprop.js');
LOG.trace('End');Any thoughts? Thanks.
-
Huh.
Testing it on my machine, here's what I get as a profile of my Mango. Nothing is enabled, I just hit the script validate button on a single line loading coolprop.js
One thing I noticed is that coolprop threw an exception when it did load, so you'll have to do something more to get the script as it will need to be (no read function was the error I was seeing). But, check out this memory chart:
The first massive jump is the first time I loaded coolprop.js. Then I hit validate a few more times, and no real change. Then I triggered a manual garbage collection in the deep valley in the middle, then hit validate again. It looks like you options will be to try and figure out why it is so massive (tough to say from what I'm seeing.... looks like it just assigns tons and tons of variable names, which seems like it should never get to the size it is), or try to allocate enough memory to avoid the issue.
-
@phildunlap Thank you. At the moment is not possible for me to increase my memory. I need to get a bit creative here and see how I can go around this and do my calculation. One option i can think of is to create an external webpage (residing on the same server) and have the javascript to run there. I need to be able to pass data and retrieve data from it and I know that XMLHttpRequest is not supported. So instead of using script, i guess i have to use an http receiver data source to call my page url.
- Is there anyway to simply pass data points to this url? if not, then, within my webpage, i have to connect to the mango database and retrieve the data i need and do the required calculation. Is this even possible?
- It seems to be much easier to send the data back to Mango. using the above http receiver i can setup data points to capture the returned data from the webpage response
Am i even on the right track? What other things can i do here? Thanks.
-
What you're talking about is possible,
but not especially clean(having something like node.js makes plenty of sense, opening a browser doesn't).If you had a webpage open, and it had loaded coolprop and somehow no memory issues occurred in that version despite being the same device opening the webpage (edit: or you're using some other JavaScript engine, or node.js, etc), you could write that webpage to subscribe to a point value web socket (or just poll a data point if you prefer) and then return the computed points as a PUT to /rest/v1/point-values (you can explore the endpoints in swagger, but I suspect you'd want the multiple update endpoint). So, both the retrieval and the return of values should be taken care of in that webpage through the API.
Other alternatives are figuring out how to prevent it from allocating such a chunk of memory when loaded (but alas I couldn't find a non-minified version of the script), or using another of their libraries and seeing if that works, For instance, you could use the python library on the server, then use the SSH data source when you need to invoke the library.
-
@phildunlap said in Import javascript library:
point value web socket
Do you have an example of a webpage that interacts with Mango point value using a web socket? Thanks.
-
@arvand.owji
Lots of the Mango UI pages do this. e.g.Data Point Details
orWatch List
-
So i created an internal data source and also added a data point for that data source. Now when i try to edit that data point using MangoAPI.defaultApi.putValue(xid, pvt) (**running on a seperate webpage but on the same server)i get an error that the data point is not settable. inside the DP, all i see for "set permission" is blank, Admin and super admin. How can i set me DP permission to be able to set it from outside? Am i even right to use an internal data source for this purpose? All i need is an arbitrary datapoint that i can be able to set it from outside. Maybe i have to use http data source instead?
-
Hi Arvand,
You would rather use a virtual data source for that purpose. The internal data source is for gathering information on how Mango or the system are running.
-
@phildunlap said in Import javascript library:
Hi Arvand,
You would rather use a virtual data source for that purpose. The internal data source is for gathering information on how Mango or the system are running.
Thanks and it worked. I can now Get data and then set them in my webpage.
Im encountering a new problem now. I created a http data retriever and passed my webpage address as the data source url . I set the update time to 5 minutes and when I save/enable the data source i get the following error:
INFO 2017-07-17 14:41:34,313 (com.serotonin.m2m2.rt.RuntimeManager.stopDataSource:383) - Data source 'CoolPropRet' stopped
INFO 2017-07-17 14:45:03,419 (com.serotonin.m2m2.rt.RuntimeManager.initializeDataSourceStartup:351) - Data source 'CoolPropRet' initialized
INFO 2017-07-17 14:45:03,423 (com.serotonin.m2m2.rt.RuntimeManager.initializeDataSourceStartup:356) - Data source 'CoolPropRet' took 33.553523ms to start
ERROR 2017-07-17 14:45:03,424 (com.serotonin.m2m2.web.dwr.util.ExceptionDetectionFilter.doFilter:38) - DWR invocation exception
java.lang.IllegalStateException: Timer already cancelled. -
Hi All,
Any chance we know why i'm getting the above error? Thanks. -
Hi Arvand,
Off hand I'm not quite sure. Can you possibly share the JSON for the HTTP Retriever data source that you are experiencing this with? Export it from the row in the table of data sources on /data_sources.shtm so that it also exports its points.
-
@phildunlap said in Import javascript library:
Hi Arvand,
Off hand I'm not quite sure. Can you possibly share the JSON for the HTTP Retriever data source that you are experiencing this with? Export it from the row in the table of data sources on /data_sources.shtm so that it also exports its points.
Maybe im not using the right tool. I wrote a webpage that uses mango api to retrieve bunch of DPs and and then set some DPs. What i'm trying to do is to use the HTTP retriever to act like a cron job and simply send a request to that url every X minutes. I dont really care whats being oasses ot that url or what that url returns. Here is the datasrouce export. I removed the website name from the url but i have validated that the mango box has access to the url (i can ping the host from command prompt.
{
"dataSources":[
{
"xid":"DS_608664",
"name":"CoolPropRet",
"enabled":true,
"type":"HTTP_RETRIEVER",
"alarmLevels":{
"DATA_RETRIEVAL_FAILURE":"URGENT",
"POLL_ABORTED":"URGENT",
"SET_POINT_FAILURE":"URGENT",
"PARSE_EXCEPTION":"URGENT"
},
"purgeType":"YEARS",
"updatePeriodType":"MINUTES",
"quantize":false,
"retries":2,
"setPointUrl":"",
"timeoutSeconds":30,
"updatePeriods":2,
"url":"http://www.MYWEBSITE.COM:85/coolprop4.html",
"editPermission":"",
"purgeOverride":false,
"purgePeriod":1
}
],
"dataPoints":[
]
} -
I also just tried http://192.168.1.125/coolprop4.html as the url and i got the same error.
-
Hmm. Can you try clearing your browser cache (ctrl + shift + delete in Chrome) and deleting your Mango/work/jsp directory, then trying again?
-
@phildunlap said in Import javascript library:
Hmm. Can you try clearing your browser cache (ctrl + shift + delete in Chrome) and deleting your Mango/work/jsp directory, then trying again?
ia have bunch of stuff in /opt/mango/work/jsp. is it ok to delete them?
-
Yeah those are compiled JSPs. It will recompile them when the pages are loaded if you delete that.