Announcement

Collapse
No announcement yet.

Retrieving Settings from INI file

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

  • alexbk66
    replied
    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);

    Leave a comment:


  • dmurphy
    replied
    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?

    Leave a comment:


  • alexbk66
    replied
    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));

    Leave a comment:


  • dmurphy
    replied
    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?

    Leave a comment:


  • lpitman
    replied
    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.

    Leave a comment:


  • dcorsus
    replied
    just a thought;


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

    Leave a comment:


  • lpitman
    replied
    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)

    Leave a comment:


  • Michael McSharry
    replied
    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;
            }

    Leave a comment:


  • lpitman
    replied
    Settings Page does save settings in INI file, but does not load them on startup. I have to toggle off then toggle on things like Debug Checkbox and other configuration items.

    Leave a comment:


  • dcorsus
    replied
    Originally posted by lpitman View Post

    Thats the same as I'm using?
    Check the parameters. Parameter 3 is a value and 4 is some type. I don't see you setting the value and in my code I don't set the type which is optional

    HTML Code:
                settingsPage1.WithDropDownSelectList(Constants.Settings.Sp1DebugLevelOptionsId, Constants.Settings.Sp1DebugLevelOptionsName, Constants.Settings.Sp1DebugLevelOptions, DebugLevel.dlErrorsOnly)
                settingsPage1.WithDropDownSelectList(Constants.Settings.Sp1UPNPDebugLevelOptionsId, Constants.Settings.Sp1UPNPDebugLevelOptionsName, Constants.Settings.Sp1UPNPDebugLevelOptions, DebugLevel.dlErrorsOnly)
                settingsPage1.WithToggle(Constants.Settings.Sp1DebugToggleLogtoDiskToggleId, Constants.Settings.Sp1LogtoDiskToggleName)
    
                'Add the Music DB Setting Label to the page
                settingsPage1.WithLabel(Constants.Settings.Sp1MusicDBLabelId, Nothing, Constants.Settings.Sp1MusicDBLabelValue)
    
                settingsPage1.WithToggle(Constants.Settings.Sp1AutoUpdateFlagId, Constants.Settings.Sp1AutoUpdateFlagName, True)
                settingsPage1.WithToggle(Constants.Settings.Sp1ImmediateUpdateFlagId, Constants.Settings.Sp1ImmediateUpdateFlagName, False)
                settingsPage1.WithToggle(Constants.Settings.Sp1LearnRadioStationsFlagId, Constants.Settings.Sp1LearnRadioStationsFlagName, True)
                settingsPage1.WithInput(Constants.Settings.Sp1AutoUpdateTimeId, Constants.Settings.Sp1AutoUpdateTimeInputBoxName, "02:10")

    Leave a comment:


  • lpitman
    replied
    Originally posted by dcorsus View Post

    I store booleans, text and integers as part of the settings page. They do require use of different kind of:

    withToggle
    withInput
    withDropDownSelect

    .....

    Seems to work for me but text is simple not chapters long with potential conflict in characters like "=" or crlf
    Thats the same as I'm using?

    '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.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)

    Leave a comment:


  • alexbk66
    replied
    Originally posted by rmasonjr View Post
    I am writing a new plugin that will 'cache' the INI settings instead of reading them for every device event. I've even thought about a built-in timer to get any new settings every 'x' minutes or so.
    That's how I do it for PED, but you can also apply it to ini settings

    Code:
    public bool use_status_device
    {
        get
        {
            if(_use_status_device == null)
                _use_status_device = GetPED<bool>(Utils.FuncName());
            return (bool)_use_status_device;
        }
        set
        {
            _use_status_device = value;
            SavePED(Utils.FuncName(), _use_status_device);
        }
    }
    
    bool? _use_status_device;

    Leave a comment:


  • dcorsus
    replied
    Originally posted by lpitman View Post

    1.) GetIniSetting works great to retrieve settings for my subs/functions.

    2.) But the settings page doesn't read anything in except toggles, and seems to ignore my text input.

    3.) The settings page does save any changes I make to the correct pluginname.ini file, but on startup it only reads the toggles???

    So I still am forced to use my routine to retrieve the data reliably.
    I store booleans, text and integers as part of the settings page. They do require use of different kind of:

    withToggle
    withInput
    withDropDownSelect

    .....

    Seems to work for me but text is simple not chapters long with potential conflict in characters like "=" or crlf

    Leave a comment:


  • lpitman
    replied
    Originally posted by dcorsus View Post
    Use HomeseerSystem.GetIniSetting
    The section is “Settings” but you can see that if you open the ini file
    1.) GetIniSetting works great to retrieve settings for my subs/functions.

    2.) But the settings page doesn't read anything in except toggles, and seems to ignore my text input.

    3.) The settings page does save any changes I make to the correct pluginname.ini file, but on startup it only reads the toggles???

    So I still am forced to use my routine to retrieve the data reliably.

    Leave a comment:


  • lpitman
    replied
    Originally posted by mnsandler View Post
    do you guys trusts the hs3/4 built-in ini routines? i gave up on them in HS3, and implemented a third party ini class. My HS3 ini file kept getting truncated/corrupted when hs3 crashed or was shutdown in correctly
    I used my own and not HS3's

    Leave a comment:

Working...
X