Saving user settings in global variables over a restart


ledbelly2142

Recommended Posts

Is there a way to save user settings through a system (computer) restart? I have a need to restart the computer that DAQFactory is on, once a day (scheduled restart). Is there a way to save user settings for global variables over to a restart?

My thoughts are that the global user settings can be logged to a file, then recalled in a sequence on start up, kind of like a recipe. Has anyone done this?

I would prefer not to use the registry, I would like to use a file that can include any number of global variables to be able to add them as/if needed.

Regards,

Greg

Link to comment
Share on other sites

The easiest, most organized way is to put all your globals in class. Something like:

class CGlobals
   local firstSetting = 5
   local string secondSetting = "myName"
  //   etc.
endclass

Make sure this code runs before you do any of the below!

Then instantiate this class once:

global g = new(CGlobals)

And access your globals by putting a g. in front:

g.firstSetting

Of course you'll want to use different names than "firstSetting" and "secondSetting". Make sure you put defaults in as I did.

Then to persist to disk, use the File.open() function to create a file, then use the toJson() function on the object to create a string representation of that object:

private handle = file.open("c:\mySettings.json",0,1,0,0)
file.write(handle, g.toJson())
file.close(handle)

You can optionally pass a 1 to the toJson() function to have the resulting string formatted nicer (but less compactly).

Then, to restore the file, simply read it and use fromJson() to recreate it:

private handle = file.open("c:\mySettings.json",1,0,0,0)
private string dataIn = file.read(handle,file.getLength(handle))
file.close(handle)
g = fromJson(dataIn)

You'll probably want some error handling around all this of course.

The nice part about using this method is that everything stays organized and you don't have to worry about file formatting. Also, you can have arrays and they'll be saved automatically. You can even have nested objects, or even arrays of objects, and the entire tree will be restored, provided the classes exist.

Link to comment
Share on other sites

  • 1 year later...

Sorry for reactivating an old topic. I am using the JSON technique described above for saving my user configuration, including both numbers and strings. Everything seems to work as expected:

- data gets written to file with tojson

- everything gets restored with fromjson

 

However, if I then (after restoring the global g variable from file) try to modify one of the string variables, it gets filled with NaN instead:

?g.myvar[0]
Old_content
g.myvar[0]="test"
?g.myvar[0]
NaN

I do not quite understand what is going on. Doing the same sequence on a "fresh" global g (i.e. without writing to file and recovering), g.myvar[0] will behave as expected and accept the new string content. So backing up and restoring the settings basically corrupts my string variables in a way that they cannot be modified any more.

Tested in DF 5.9 build 2127.

Link to comment
Share on other sites

In the meantime, I have implemented a workaround storing all my string settings in a separate file without using the json commands. So the old code is not there any more.

So I have tried to recreate the case. On my first try, all worked as it should, using the example posted by you earlier. No problems with the string. But then, I tried the same thing with arrays, and it seems as if this is where the bug is. As soon I have more than one element in a string variable, I get the behaviour described earlier, not being able to change string values in that variable.

I attach the file used for testing, it is basically your own example extended to use arrays and with some Variable Components.

After storing the variable to file, that file has the following content:

{"_ClassName":"cglobals","intSetting":[5.0,15.0],"strSetting":["String1","String2"]}

test.ctl

Link to comment
Share on other sites

Thanks!  I was able to reproduce the problem and chase it down.  The issue was that when creating the variable for "strSetting" it was looking at the json data type, which works fine if strSetting is just a string.  But in your case, its an array, so the program was defaulting to numeric type.  So, we added a little code to see if a variable was an array, and to instead look at the first element of the array to determine the DAQFactory data type.  The fix will be included in 5.90.7.

Link to comment
Share on other sites

  • 1 month later...

I know.  We held off to try and reinstate and improve on the web server capability.  It won't be available until the beginning of January officially.  The holidays and all.  But you are certainly welcome to try it now, just email us.  Its really just lacking proper documentation for the new features, which include:

 

Ability to save just a graph to a jpeg, as well as a binary format that you can rebuild the graph from

Multi page PDF's

Change events for many of the edit controls

Blobs for ODBC databases plus helper functions for this

Script access to alarm priority

Fullscreen changable froms cript

LabJackM function calls

Modified and improved webserver including the ability to parse files like PHP does using DAQFactory script.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.