ledbelly2142 Posted January 31, 2012 Share Posted January 31, 2012 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 More sharing options...
AzeoTech Posted January 31, 2012 Share Posted January 31, 2012 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 More sharing options...
ledbelly2142 Posted February 1, 2012 Author Share Posted February 1, 2012 Looks really good, working on it now with lots to redo. Would have been easier to start off my config this way. Thank your for this solution, it is much more elegant than how I thought it might be done. Thanks, Greg Link to comment Share on other sites More sharing options...
tamm Posted October 24, 2013 Share Posted October 24, 2013 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 More sharing options...
AzeoTech Posted October 24, 2013 Share Posted October 24, 2013 It sounds like the variable is being declared as a number. Can you post your .ctl, or at least your class declaration and json load/save code? Link to comment Share on other sites More sharing options...
tamm Posted October 30, 2013 Share Posted October 30, 2013 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 More sharing options...
AzeoTech Posted October 31, 2013 Share Posted October 31, 2013 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 More sharing options...
tamm Posted October 31, 2013 Share Posted October 31, 2013 That's somehow what I suspected. Great that it can be fixed easily. Is there a time estimation for the release of 5.90.7? Curious because it will contain some bug fixes relevant for me :-) Link to comment Share on other sites More sharing options...
AzeoTech Posted October 31, 2013 Share Posted October 31, 2013 I expect very soon, probably next week. Link to comment Share on other sites More sharing options...
tamm Posted December 5, 2013 Share Posted December 5, 2013 Hi again. Now, it's 5 weeks since your last post expecting the new 5.90.7 "soon". How is work going? I really appreciate your work and support! Link to comment Share on other sites More sharing options...
AzeoTech Posted December 5, 2013 Share Posted December 5, 2013 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.