Announcement

Collapse
No announcement yet.

Reading counts of sections of INI settings

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

    Reading counts of sections of INI settings

    Hello Homeseerians,

    A question for the programming guru's, I am looking at the best way to be able to not hard code in data through my scripts, and wanted to move towards using INI files to store the data, and then retrieve it (relating to IP addresses and so on).

    I understand with the ini settings, one could have something like below in their own ini file (in the config folder)

    [setting1]
    IP=127.0.0.1
    PORT=80

    [setting2]
    IP=127.0.0.2
    PORT=81

    [setting3]
    IP=127.0.0.3
    PORT=82

    My question is, how could I determine the number of setting sections, it would be nice to be able to have this as a semi-array, or at least be able to know the number of clients or settings sections I have when they are variable, how are others handling this variability, and would you have some tips you are willing to share on how to identify the number of clients / sections within an ini file,

    Thanks
    HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

    Facebook | Twitter | Flickr | Google+ | Website | YouTube

    #2
    I think the way I did it was to keep a track of how many clients I thought were connected by placing a variable say in the [Settings] key that was NoOfClients=5 so the loop was then something like:

    For i = 1 to CInt(hs.getinisetting("Settings", "NoOfClients", "1", INIFile)

    IPAddress = hs.getinisetting("Client" & i, "IPAddress", "127.0.0.1", INIFile)

    Next

    Comment


      #3
      Thanks for the tip, I thought it would be a simple case but I was not sure if it would involve looping through the file to look for entries, or something else.

      I did have a question though, well strictly speaking actually two:

      1. How to you handle accidental deletion of an entry in the config / ini file whereby the number of clients gets split, so for example the user has client 1 - 5 but deletes 4, and the number of clients gets set to 4, so presumably when it does a for next loop it would have an issue as it goes through Client = 1, 2, 3, 5 (misses four and errors).
      2. I couldn't find on the forum, but i believe you did the HSTouchTrig (3P)?, if so would you be able to point me in the right direction for code examples that let you create a web page to populate an ini file much like your settings page?

      Thanks!
      HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

      Facebook | Twitter | Flickr | Google+ | Website | YouTube

      Comment


        #4
        Originally posted by travisdh View Post
        How to you handle accidental deletion of an entry in the config / ini file whereby the number of clients gets split, so for example the user has client 1 - 5 but deletes 4, and the number of clients gets set to 4, so presumably when it does a for next loop it would have an issue as it goes through Client = 1, 2, 3, 5 (misses four and errors).
        I handle this a bit differently in weatherXML for the locations and the speak items.

        [setting]
        IP1=127.0.0.1
        PORT1=80
        IP2=127.0.0.2
        PORT2=81
        IP4=127.0.0.3
        PORT4=82

        Rather than loop over x = 1 to maxItems
        I use hs.GetSection. That returns all of the items in that section as a string that you can split into an array and you will get every key and value so you don't have to know what the max number of IPs are.


        So that I don't have to worry about a deleted item still being there I set the value to Nothing.
        For Ex
        Rather than leave this in the ini file:
        IP3=
        when I delete the value I use

        clr=Nothing
        hs.SaveINISetting("Setting","IP3",clr,"inifilename.ini")

        That will clear the whole line so you don't have to worry about hs.GetSection including it.
        --
        Jeff Farmer
        HS 3, HSPhone
        My HS3 Plugins: CFHSExtras, Random, Restart, Tracker, WeatherXML, PanaBluRay
        Other Plugins In Use: APCUPSD, BLOnkyo, Device History, EasyTrigger, HSTouch Server, PHLocation2, Pushover, RFXCom, UltraGCIR3, UltraMon3, UltraPioneerAVR3, X10, Z-Wave

        Hardware: GoControl Irrigation Controler, Schlage Lever Lock, Schlage Deadbolt, Way2Call Hi-Phone, RFXCom RFXrec433 Receiver, WGL 800, TI-103, Z-Net, Pioneer 1120, Pioneer 1021, Pioneer LX302, Panasonic BDT-110, Panasonic BDT-210 x2

        Comment


          #5
          I suppose in example two, then the number (e.g. IP1=,IP2=) is really more about differentiating between entries than actually having any meaning, so as long as you use hs.getsections, then put the values into an array you can use them appropriately. and if an entry is blank, it does not matter since you are not using a for loop specifically.

          Is there any value in multi-dimensional arrays over single arrays in that case. I am looking at this for the plugin idea / script (at the moment) i am working on, and want to store the client connection parameters and use them through script.

          Would it therefore be better to use a multi-dimensional array like so:

          IP(,0) PORT(,1) USER(,2) PASS(,3)
          127.0.0.1 81 xbmc xbmc
          127.0.0.2 81 xbmc xbmc

          then call them through some script of whatever, or is it more efficient to use a few different arrays?

          I guess in using hs.getinisection I could feed the results into an array and use it from there in scripting etc.
          HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

          Facebook | Twitter | Flickr | Google+ | Website | YouTube

          Comment


            #6
            Originally posted by travisdh View Post
            I suppose in example two, then the number (e.g. IP1=,IP2=) is really more about differentiating between entries than actually having any meaning, so as long as you use hs.getsections, then put the values into an array you can use them appropriately. and if an entry is blank, it does not matter since you are not using a for loop specifically.

            Is there any value in multi-dimensional arrays over single arrays in that case. I am looking at this for the plugin idea / script (at the moment) i am working on, and want to store the client connection parameters and use them through script.

            Would it therefore be better to use a multi-dimensional array like so:

            IP(,0) PORT(,1) USER(,2) PASS(,3)
            127.0.0.1 81 xbmc xbmc
            127.0.0.2 81 xbmc xbmc

            then call them through some script of whatever, or is it more efficient to use a few different arrays?

            I guess in using hs.getinisection I could feed the results into an array and use it from there in scripting etc.
            Yeah, you can load the ini section into an array and keep it in memory.
            I use multi dimensional arrays in a lot of places.
            You can loop over an array easily.

            for x = 0 to aList.GetUpperBound(1)
            IP = aList(0,x)
            Next

            You would want the first dimension to be the set one though

            IP(0,) PORT(1,) USER(2,) PASS(3,)
            127.0.0.1 81 xbmc xbmc
            127.0.0.2 81 xbmc xbmc

            The reason it matters is if you need to add a new IP, you can change the value of the right dimension.
            Dim aList(3,0) as string
            This would have 1 IP.

            Then when you add another IP
            ReDim Preserve aList(3,1)

            The ReDim tells the script that you are chaning the rows in the array.
            The Preserve tells the script to keep what is already in the array.
            If you don't use Preserve you will lose all the values already in the array.
            --
            Jeff Farmer
            HS 3, HSPhone
            My HS3 Plugins: CFHSExtras, Random, Restart, Tracker, WeatherXML, PanaBluRay
            Other Plugins In Use: APCUPSD, BLOnkyo, Device History, EasyTrigger, HSTouch Server, PHLocation2, Pushover, RFXCom, UltraGCIR3, UltraMon3, UltraPioneerAVR3, X10, Z-Wave

            Hardware: GoControl Irrigation Controler, Schlage Lever Lock, Schlage Deadbolt, Way2Call Hi-Phone, RFXCom RFXrec433 Receiver, WGL 800, TI-103, Z-Net, Pioneer 1120, Pioneer 1021, Pioneer LX302, Panasonic BDT-110, Panasonic BDT-210 x2

            Comment


              #7
              So based on that, on my script / plugin initialization I would scan the Ini file, and populate the array as a global array. Then possibly have a timer event or something similar, which would rescan the array and redim it, or would there be a better way of making the clients available to the script / plugin always, but also update the array every x minutes (or through event etc).
              HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

              Facebook | Twitter | Flickr | Google+ | Website | YouTube

              Comment

              Working...
              X