Announcement

Collapse
No announcement yet.

Retrieving Settings from INI file

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

    #16
    Thanks Michael, but already been there and done that with no joy...

    Code:
        '************************************
        Friend Sub LoadConfigSettings()
    
            OutputHSLog(LOG_TRACE, "Entering modINI....LoadConfigSettings()....")
    
            Try
                gDebug = CBool(ReadIniValue("Settings", "Debug", "False", INIFILE))
                gDisplayMoons = CBool(ReadIniValue("Settings", "DisplayMoons", "False", INIFILE))
                gUSHolidays = CBool(ReadIniValue("Settings", "USHolidays", "True", INIFILE))
                gPreFixSpeakStr = ReadIniValue("Settings", "PreFixSpeakString", "", INIFILE)
                g1stSchedule = ReadIniValue("Settings", "FirstAnnouncement", "9:00 AM", INIFILE)
                g2ndSchedule = ReadIniValue("Settings", "SecondAnnouncement", "12:00 pM", INIFILE)
                g3rdSchedule = ReadIniValue("Settings", "ThirdAnnouncement", "3:00 PM", INIFILE)
                g4thSchedule = ReadIniValue("Settings", "FourthAnnouncement", "6:00 PM", INIFILE)
                gFromEmailAddr = ReadIniValue("Settings", "FromEmailAddr", "someone@domain.com", INIFILE)
                gEmailPassWord = ReadIniValue("Settings", "EmailPassword", "your password", INIFILE)
                gEmailServer = ReadIniValue("Settings", "EmailServer", "mail.domain.com", INIFILE)
                '----------------------------------------------------------------------
    
            Catch ex As Exception
                OutputHSLog(LOG_ERROR, "Exception error in modINI....LoadConfigSettings(): " & ex.Message)
            End Try
        End Sub
    Later in initalizing I use the following:

    Code:
            If fInfo.Exists Then    '<-- If Config file does exist then LOAD the Config file.
    
                OutputHSLog(LOG_DEBUG, "About To read config file entries")
    
                LoadConfigSettings()
    
                OutputHSLog(LOG_DEBUG, "gDebug = " & gDebug.ToString)
                OutputHSLog(LOG_DEBUG, "gDisplayMoons = " & gDisplayMoons.ToString)
                OutputHSLog(LOG_DEBUG, "gUSHolidays = " & gUSHolidays.ToString)
                OutputHSLog(LOG_DEBUG, "gPreFixSpeakStr = " & gPreFixSpeakStr)
                OutputHSLog(LOG_DEBUG, "g1stSchedule = " & g1stSchedule)
                OutputHSLog(LOG_DEBUG, "g2ndSchedule = " & g2ndSchedule)
                OutputHSLog(LOG_DEBUG, "g3rdSchedule = " & g3rdSchedule)
                OutputHSLog(LOG_DEBUG, "g4thSchedule = " & g4thSchedule)
                OutputHSLog(LOG_DEBUG, "gFromEmailAddr = " & gFromEmailAddr)
                OutputHSLog(LOG_DEBUG, "gEmailPassWord = ")
                OutputHSLog(LOG_DEBUG, "gEmailServer = " & gEmailServer)
    And the PageFactory stuff as well:

    Code:
        Private Sub InitializesettingsPages()
    
            'OutputHSLog(LOG_TRACE, "Entering Sub HSPI.InitializesettingsPages()....")
    
            Try
                'Start a PageFactory to construct the Page
                Dim settingsPage = PageFactory.CreateSettingsPage(Constants.Settings.SettingsPageId, Constants.Settings.SettingsPageName)
                '---------------------------------------------------------------------------------------------------------
                'Add a CHECKBOX to the page
                settingsPage.WithCheckBox(Constants.Settings.DebugId, Constants.Settings.DebugCheckBox, gDebug)
                '---------------------------------------------------------------------------------------------------------
                'Add a CHECKBOX to the page
                settingsPage.WithCheckBox(Constants.Settings.MoonsId, Constants.Settings.MoonsCheckBox, gDisplayMoons)
                '---------------------------------------------------------------------------------------------------------
                'Add a CHECKBOX to the page
                settingsPage.WithCheckBox(Constants.Settings.USHolidaysId, Constants.Settings.USHolidaysCheckBox, gUSHolidays)
                '---------------------------------------------------------------------------------------------------------
                'Add the page to the list of plugin settings pages
                Settings.Add(settingsPage.Page)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.PrefixSpeakStrId, Constants.Settings.PrefixSpeakStrName, HomeSeer.Jui.Types.EInputType.Text)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.FirstAnnounceId, Constants.Settings.FirstAnnounceName, HomeSeer.Jui.Types.EInputType.Text)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.SecondAnnounceId, Constants.Settings.SecondAnnounceName, HomeSeer.Jui.Types.EInputType.Text)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.ThirdAnnounceId, Constants.Settings.ThirdAnnounceName, HomeSeer.Jui.Types.EInputType.Text)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.FourthAnnounceId, Constants.Settings.FourthAnnounceName, HomeSeer.Jui.Types.EInputType.Text)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                'settingsPage.WithInput(Constants.Settings.ToEmailId, Constants.Settings.ToEmailName, HomeSeer.Jui.Types.EInputType.Email)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.FromEmailId, Constants.Settings.FromEmailName, HomeSeer.Jui.Types.EInputType.Email)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.EmailServerId, Constants.Settings.EmailServerName, HomeSeer.Jui.Types.EInputType.Text)
                '---------------------------------------------------------------------------------------------------------
                'Add a TEXT INPUTVIEW to the page
                settingsPage.WithInput(Constants.Settings.EmailPasswordId, Constants.Settings.EmailPasswordName, HomeSeer.Jui.Types.EInputType.Password)
    
                'Add the page to the list of plugin settings pages
                Settings.Add(settingsPage.Page)
    -Larry

    A member of "The HA Pioneer Group", MyWebSite

    Plugins:
    VWS, AB8SS, lrpSpeak, Calendar, Arduino, Harmony, BlueIris, Sprinklers, ZipBackup...

    Hardware:
    Intel NUC8i7BEH1 running Windows 10 Pro headless, HS3 Pro, Plex running on Synology dual High Availability DS-1815+ NAS (24Tb each), Synology Surveillance Station running on DS-416 Slim (8Tb), Samsung SmartThings, Google Home, Alexa, Hubitat Elevation, ZNET, Ubiquiti UniFi Network, Davis Vantage Pro II Weather Station. Whole house speaker system using a couple of AB8SS switches. Vantage Pro II Weather Station, Rain8Net Sprinklers, Hubitat Elevation, Google Home, Alexa, DSC Security System, Ubiquiti UniFi Network.

    Comment


      #17
      just a thought;


      is for example
      Constants.Settings.DebugId equal to "Debug"

      Comment


        #18
        Yes, but solved my issue. It was the Settings Page was using the SECTION "Settings" and my LoadConfigSettings() was using SECTION "Config". It seems to be working now.
        -Larry

        A member of "The HA Pioneer Group", MyWebSite

        Plugins:
        VWS, AB8SS, lrpSpeak, Calendar, Arduino, Harmony, BlueIris, Sprinklers, ZipBackup...

        Hardware:
        Intel NUC8i7BEH1 running Windows 10 Pro headless, HS3 Pro, Plex running on Synology dual High Availability DS-1815+ NAS (24Tb each), Synology Surveillance Station running on DS-416 Slim (8Tb), Samsung SmartThings, Google Home, Alexa, Hubitat Elevation, ZNET, Ubiquiti UniFi Network, Davis Vantage Pro II Weather Station. Whole house speaker system using a couple of AB8SS switches. Vantage Pro II Weather Station, Rain8Net Sprinklers, Hubitat Elevation, Google Home, Alexa, DSC Security System, Ubiquiti UniFi Network.

        Comment


          #19
          Originally posted by Michael McSharry View Post
          During initialization I load my settings into local variables

          Code:
          public void Load()
          {
          Location = _hs.GetINISetting("Settings", "Location", "Hubitat", Utils.IniFile);//I Like it When I can Set a Default location myself
          Location2 = _hs.GetINISetting("Settings", "Location2", "Hubitat", Utils.IniFile);
          DebugLog = bool.Parse(_hs.GetINISetting("Settings", "DebugLog", "false", Utils.IniFile));
          MakerURI = _hs.GetINISetting("Settings", "MakerURI", "", Utils.IniFile);
          }
          I then use them to initialize my settings page and also in response to the setting page save

          Code:
          private void InitializeSettingsPages()
          {
          
          //Start a PageFactory to construct the Page
          var settingsPage1 = PageFactory.CreateSettingsPage("idConfig", "Config");
          
          //Add a LabelView to the page
          settingsPage1.WithLabel("idlblMakerAPIURL", null, "Hubitat Maker API URL:"); //"Config");
          
          //Add a text InputView for the Maker API text
          settingsPage1.WithInput("idtxtMakerAPIURL", "MakerAPI URL", _settings.MakerURI, HomeSeer.Jui.Types.EInputType.Text);
          settingsPage1.WithInput("idtxtLocation", "Floor (Location2)", _settings.Location2, HomeSeer.Jui.Types.EInputType.Text);
          settingsPage1.WithInput("idtxtLocation2", "Room (Location)", _settings.Location, HomeSeer.Jui.Types.EInputType.Text);
          
          //Add a checkbox to the page for Debug
          settingsPage1.WithCheckBox("idchkDebug", "Debug", _settings.DebugLog);
          
          //Add the first page to the list of plugin settings pages HomeSeer.Jui.Views.SettingsCollection.
          Settings.Add(settingsPage1.Page);
          }
          Code:
          protected override bool OnSettingChange(string pageId, AbstractView currentView, AbstractView changedView)
          {
          Console.WriteLine("OnSettingChange");
          
          if (changedView.Id == "idtxtMakerAPIURL") _settings.MakerURI = changedView.GetStringValue();
          else if (changedView.Id == "idtxtLocation") _settings.Location = changedView.GetStringValue();
          if (changedView.Id == "idtxtLocation2") _settings.Location2 = changedView.GetStringValue();
          else if (changedView.Id == "idchkDebug") _settings.DebugLog = (changedView.GetStringValue() == "True");
          
          return true;
          }
          Michael, this is very useful one question. when you hit save the plugin sdk writes the changed settings to SettingsFileName using the id. Whereas you are reading the settings using name? Are you somehow preventing HS from saving settings and handling the save yourself?
          HS3 Pro Edition 3.0.0.435 (Windows 10 vmware)
          BLOccupied:,UltraNetCam3:,weatherXML:,RFXCOM:,Current Cost 3P:,UltraGCIR3:
          DMMQTT:,Kodi:,Z-Wave:,BLRadar:,EasyTrigger:,MySensors:,BLBackup:

          Comment


            #20
            I created mapping class, so I don't need to handle "OnSettingsChange", so adding a setting is just one-liner, it automatically updates the mapped property on user input.

            Code:
            // Toggle Log
            AddToggleView("Enable Log", dev, nameof(dev.log_enable));
            
            // Text input for IP
            AddInputView("IP Address", dev, nameof(dev.IP));
            
            // Create selection from 0 to 60 seconds (1 sec increment) for UpdateRate
            AddSelectListView<int>("Polling period", 0, 60, 1, " sec", dev, nameof(dev.UpdateRate));

            Comment


              #21
              Originally posted by alexbk66 View Post
              I created mapping class, so I don't need to handle "OnSettingsChange", so adding a setting is just one-liner, it automatically updates the mapped property on user input.

              Code:
              // Toggle Log
              AddToggleView("Enable Log", dev, nameof(dev.log_enable));
              
              // Text input for IP
              AddInputView("IP Address", dev, nameof(dev.IP));
              
              // Create selection from 0 to 60 seconds (1 sec increment) for UpdateRate
              AddSelectListView<int>("Polling period", 0, 60, 1, " sec", dev, nameof(dev.UpdateRate));
              using automapper? or another technique?
              HS3 Pro Edition 3.0.0.435 (Windows 10 vmware)
              BLOccupied:,UltraNetCam3:,weatherXML:,RFXCOM:,Current Cost 3P:,UltraGCIR3:
              DMMQTT:,Kodi:,Z-Wave:,BLRadar:,EasyTrigger:,MySensors:,BLBackup:

              Comment


                #22
                Originally posted by dmurphy View Post
                using automapper? or another technique?
                No, Automaper is different. Just Reflection - map the view to pair (target, property name)

                Code:
                PropertyInfo prop = targetType.GetProperty(propertyName)
                prop.SetValue(target, _val);

                Comment

                Working...
                X