WebDev Questions

I just initialized couple of variables and lists in a project startup script, without any function, I am able to access them as well as change them from other scripts outside it (webDev). I guess startup scripts are loaded and run irrespective of whether they are called thru a function or not.

Async files is a good idea! However I must ensure that the initialization is complete before the variables/lists/dictionaries are accessed by any external modules such as webDev, which is a fair assumption in my case.

What is the meaning of following message which is printed in wrapper.log file intermittently? Does it have anything to do with startup scripts or webDev module or its from some where else?

database “config.idb”…
INFO | jvm 1 | 2019/02/17 10:51:50 | I [P.InternalDatabase ] [05:21:50]: Created auto-backup of internal database “config.idb” in 1 seconds
INFO | jvm 1 | 2019/02/17 10:41:44 | I [P.InternalDatabase ] [05:11:44]: Creating auto-backup of internal database “config.idb”…
INFO | jvm 1 | 2019/02/17 10:41:46 | I [P.InternalDatabase ] [05:11:46]: Created auto-backup of internal database “config.idb” in 1 seconds
INFO | jvm 1 | 2019/02/17 10:43:46 | I [P.InternalDatabase ] [05:13:46]: Creating auto-backup of internal database “config.idb”…
INFO | jvm 1 | 2019/02/17 10:43:47 | I [P.InternalDatabase ] [05:13:47]: Created auto-backup of internal database “config.idb” in 1 seconds
INFO | jvm 1 | 2019/02/17 10:44:44 | I [Project ] [05:14:44]: Restarting gateway scripts… project-name=new
INFO | jvm 1 | 2019/02/17 10:45:47 | I [P.InternalDatabase ] [05:15:47]: Creating auto-backup of internal database “config.idb”…
INFO | jvm 1 | 2019/02/17 10:45:49 | I [P.InternalDatabase ] [05:15:49]: Created auto-backup of internal database “config.idb” in 1 seconds
INFO | jvm 1 | 2019/02/17 10:51:49 | I [P.InternalDatabase ] [05:21:49]: Creating auto-backup of internal database “config.idb”…
INFO | jvm 1 | 2019/02/17 10:51:50 | I [P.InternalDatabase ] [05:21:50]: Created auto-backup of internal database “config.idb” in 1 seconds

When I try to test existence of a file in startup script

if system.file.fileExists(“C:\test.txt”):
** system.gui.messageBox(“file exitst”);**

I get this error:

**INFO | jvm 1 | 2019/02/17 11:11:58 | E [W.PythonResource ] [05:41:58]: Error executing new/postjson:doPost **
INFO | jvm 1 | 2019/02/17 11:11:58 | com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO | jvm 1 | 2019/02/17 11:11:58 | File “<new/postjson:doPost>”, line 25, in doPost
INFO | jvm 1 | 2019/02/17 11:11:58 | ImportError: Error loading module NewScript: Traceback (most recent call last):
INFO | jvm 1 | 2019/02/17 11:11:58 | File “module:project.NewScript”, line 10, in
INFO | jvm 1 | 2019/02/17 11:11:58 | AttributeError: ‘com.inductiveautomation.ignition.common.script.Imm’ object has no attribute 'gui’

Are the file library functions (or any other scripting library functions for that matter) not accessible in project startup scripts?What wrong with the above code?

Got it, the gui library doesn't seem to be supported for gateway scripts (understandably). I just changed it to print, and it worked!

if system.file.fileExists(“C:\test.txt”):
print “file exitst”

I am a bit confused about accessing functions/variables from project scripts and gateway startup scripts from outside (say thru webDev get/post functions). Both scripts seem to have the same name i.e. project.NewScript by default. I am able to access functions/variables if they are in project scripts, whereas when I copy the same code into gateway startup scripts thru script playground, I am unable to access the same from outside.

When do project scripts get executed? When ever I access them from outside ? My requirement is that they should get executed when project is saved or started. So i thought gateway startup scripts will be the right place, but if I put the same code there, I am unable to access the variables from there! Whereas when I have code in project scripts, I am able to access.

Basically I am doing some initialization stuff there. I want the startup script to run every time I save the project or a manual user event.

The project.* and shared.* namespaces are exposed everywhere in project scope by default. The namespaces in event scripts of all kinds are purely local to that event, and are re-created from scratch on every event. A project or shared script is “run” once after startup or edit when it is imported, which occurs when some other code (an event) calls one of its functions or references one of its variables. The functions within the project or shared script then run when called, but with script-global objects that were established on import.

Experimenting is the best way to get comfortable with all of this.

Ok. I am just trying to initialize a global variable say a dictionary (json) variable once at the beginning from a file. This works fine in a project script, but it doesn’t update when I want it to say for example after I save the project. Anyway let me experiment further. Hopefully I get a proper solution. But right now its working fine, but not very predictably. will update on this post.

Got the hang of the above problem and found a solution.

I have have one more question. How to import external (but own) python modules from disk in to scripts.

Place them in your user-lib/pylib folder of your gateway's install folder.

All of a sudden I started getting 405 error in webdev post method! It was working fine, I just played with the post code , such as moving it in pylib, and accessing it from there. Then I rolled back the code to previous condition but it still is giving the same error

http://laptop-rlsqd170:8088/system/webdev/new/postjson 405 (HTTP method POST is not supported by this URL)

Got it, I had inadvertently introduced a bug in the post routine while rolling back, so the error invalidated the the post function and I got this error! Its fine now after the correction!

I tried that but doesn't seem to work! I included import statement in the dopost function. Hope the import will take the module from user-lib/pylib without having to give any path names etc! Can I put the module files to be imported into any other folder and file the path name in import statement!?

You should not need nor can you put a path name in your import statement. Others have noted problems with the from whatever import something syntax, if you are using that. Designers and clients may need to be restarted.

No sir! This error cropped up again when I deleted my library from pylib and put code back in postjson, removing the import statement! Does it have anything to do with your following observation/comment? I tried restarting the designer and client, but it didn't help! Perhaps I should restart ignition gateway and see!

I did restart the gateway after its trial period of 2 hours. The postjson didn't work immediately after that, but yes it started working after few repeated trials for a few minutes. I don't know how it started working but there seems to be some connection with importing modules and deleting modules! Unpredictable behavior.!

Basically what I am trying to do is to obfuscate my py code in my scripts by putting them in pyc files in pylib but that’s not working . How can I do that to protect my IP?

You can't protect your python IP in the Ignition platform. Write all such code in Java and expose the functions/classes to jython through your module hook.

It may not be worth the effort in that case if I have to write everything in JAVA that is written using few lines of python code using the builtin rich python library. My value addition is outside python.

Is there a way I can check if ignition is licensed version or not using scripts? I don’t see any corresponding scripting functions in the documentation.

You need a reference to the gateway context (there are some floating on this forum already involving the SRContext class, though they are guaranteed to break in 8.0), from which you would call getLicenseManager to get a reference to the license manager, from which you can call various methods to get the system state re: licensing: LicenseManager (ignition 7.9.5 API)