Announcement

Collapse
No announcement yet.

Help creating persistent class objects for scripts for HS3

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Help creating persistent class objects for scripts for HS3

    I am trying to use a class to store audio zone information. This class is initialized by reading an XML file with configuration data and state data of the audio zones. This class object is used by script functions that control the audio system. The problem I have is not knowing how to instantiate the object and have it persist, when the initialization script ends the object will be destroyed. It does not seem that assigning the obect to a global variable will eliminate this problem. Does anyone have experience or advice on this?

    Thanks, Mike

    #2
    Serialization?

    Originally posted by mhiliger View Post
    I am trying to use a class to store audio zone information. This class is initialized by reading an XML file with configuration data and state data of the audio zones. This class object is used by script functions that control the audio system. The problem I have is not knowing how to instantiate the object and have it persist, when the initialization script ends the object will be destroyed. It does not seem that assigning the obect to a global variable will eliminate this problem. Does anyone have experience or advice on this?

    Thanks, Mike
    Hi Mike,

    I'm by no means an expert, but you should be able to serialize/de-serialize the object as a means of persistence. This will dump members (and state?) to an XML file locally, but then with a single call you can read that back in the next time the script is called and re-instantiate it.

    Does that make any sense? I don't have any examples off-hand.

    hjk
    ---

    Comment


      #3
      I would suggest you save/read your data to an ini file (hs.GetIniSetting & hs.SaveIniSetting). That way your data is available to you even after a reboot or restart of HS3.
      Jon

      Comment


        #4
        Originally posted by mhiliger View Post
        I am trying to use a class to store audio zone information. This class is initialized by reading an XML file with configuration data and state data of the audio zones. This class object is used by script functions that control the audio system. The problem I have is not knowing how to instantiate the object and have it persist, when the initialization script ends the object will be destroyed. It does not seem that assigning the obect to a global variable will eliminate this problem. Does anyone have experience or advice on this?

        Thanks, Mike
        I subscribed to this thread because I am also interested in best practices on how to manage state and complex objects within HS. However, global variables should persist after the initialization script ends. What led you to believe they wouldn't?

        Comment


          #5
          Hjk: I was not aware of the serialize / de-serialize functionality. I quickly looked at the documentation and it looks like it could be very handy. I will play around a bit with this to understand how it works. The thing I am not sure I like about this is the overhead to load and unload the object each time it is used. I also currently implement an array of a zone class so I will be interested to see how it may or may not handle an array. Thanks for the idea.

          Tgpaul: Glad you are interested. I am not concerned that the global variable will not persist, but my understanding is that this global variable will just be a pointer to the object. The pointer /global variable can be set by my initialization routine, but the storage behind the object that is pointed to will be destroyed when the routine exits. I will play with this some more to make sure my thinking on this is right.

          Comment


            #6
            I know this is not necessarily what you asked but you may be able to store data in a device if your script has any, I have not tried using this function in a script context but it could be something to consider http://homeseer.com/support/homeseer...gextradata.htm

            Comment


              #7
              Originally posted by mhiliger View Post
              Tgpaul: Glad you are interested. I am not concerned that the global variable will not persist, but my understanding is that this global variable will just be a pointer to the object. The pointer /global variable can be set by my initialization routine, but the storage behind the object that is pointed to will be destroyed when the routine exits. I will play with this some more to make sure my thinking on this is right.
              Thanks for the ideas…
              Granted, I'm new to playing around with scripting in HS, but if you're running a .NET script, the behavior I would expect is that the global variable does indeed maintain a pointer to the managed object, and said object therefore would be safe from garbage collection until all references went out of scope (basically until the global variable is deleted (DeleteVar). I would consider deviation from this behavior to be a defect in the code--global variables would otherwise serve no purpose of holding a reference to a type "Object".

              Give it a try--let us know.

              Comment


                #8
                Originally posted by mhiliger View Post
                I am not concerned that the global variable will not persist, but my understanding is that this global variable will just be a pointer to the object. The pointer /global variable can be set by my initialization routine, but the storage behind the object that is pointed to will be destroyed when the routine exits. I will play with this some more to make sure my thinking on this is right.
                Have you experienced this as an issue? Does not seem correct to me, would make the concept of Global Variables useless.

                tenholde
                tenholde

                Comment


                  #9
                  Global Variable Persist BUT can they be used to pass classes/structures?

                  I have 2 scripts:

                  First:

                  Structure TestStruct
                  Public _Key As String
                  Public _Msg As String
                  Public _Duration As Integer

                  Public Sub New(ByVal pKey As String, ByVal pMsg As String)
                  _Key = pKey
                  _Msg = pMsg
                  _Duration = 0
                  End Sub
                  End Structure

                  Sub Main(ByVal parm as Object)
                  Dim sTest As TestStruct

                  hs.CreateVar("test")
                  sTest = New TestStruct("Key", "Message")
                  hs.SaveVar("test", sTest )
                  hs.writelog ("Test", "Done" )
                  End Sub

                  The Second:

                  Structure TestStruct
                  Public _Key As String
                  Public _Msg As String
                  Public _Duration As Integer

                  Public Sub New(ByVal pKey As String, ByVal pMsg As String)
                  _Key = pKey
                  _Msg = pMsg
                  _Duration = 0
                  End Sub
                  End Structure

                  Sub Main(parm as object)
                  Dim sTest As TestStruct

                  sTest = DirectCast(hs.GetVar("test"), TestStruct)
                  hs.writelog ("test", "Key: " & sTest._Key & " Msg: " & sTest._Msg )

                  hs.writelog ("Test", "Done" )
                  End Sub

                  The first script creates a Global Variable and saves the structure TestStruct into it. The second script retrieves that structure from the Global Variable. The problem is that the Global Variable is created with a of 'type scriptcode229.VBWrapper+TestStruct' and in the second script the TestStruct is of type 'scriptcode230.VBWrapper+TestStruct'. I have not been able to figure out how to cast the Global Variable into the script TestStruct. I get the error "Running script C:\Program Files\HomeSeer HS3\scripts\temp.vb :Exception has been thrown by the target of an invocation.Specified cast is not valid. "

                  Can this be done? I've know I can write the data to a file and read it but if this can be done it would be more efficient. Thanks!

                  Comment

                  Working...
                  X