Best way for multiple dynamic settings?
#1
So, I have a service add-on where you set a Pin and Function and it will trigger that function when the pin rises or falls.

I want to extend it to be able to have multiple pins to monitor.

How would I accomplish this in the settings.xml?

My thinking is:

<setting id="pin-1" type="slider" label="30000" default="0" range="0,1,40" option="int" />
<setting id="edge-1" type="select" label="30001" default="0" value="0,1" lvalues="30101|30100" />
<setting id="function-1" type="text" label="30002" default="ShutDown" />
<setting label="32033" type="action" action="RunScript(my.addon.id, removepin-1)"/>
<setting label="32032" type="action" action="RunScript(my.addon.id, addpin)"/>

The addon would then write the below to the settings.xml file and then reload the settings window

<setting id="pin-2" type="slider" label="30000" default="0" range="0,1,40" option="int" />
<setting id="edge-2" type="select" label="30001" default="0" value="0,1" lvalues="30101|30100" />
<setting id="function-2" type="text" label="30002" default="ShutDown" />
<setting label="32033" type="action" action="RunScript(my.addon.id, removepin-2)"/>

So, the new settings file would like look

<setting id="pin-1" type="slider" label="30000" default="0" range="0,1,40" option="int" />
<setting id="edge-1" type="select" label="30001" default="0" value="0,1" lvalues="30101|30100" />
<setting id="function-1" type="text" label="30002" default="ShutDown" />
<setting label="32033" type="action" action="RunScript(my.addon.id, removepin-1)"/>

<setting id="pin-2" type="slider" label="30000" default="0" range="0,1,40" option="int" />
<setting id="edge-2" type="select" label="30001" default="0" value="0,1" lvalues="30101|30100" />
<setting id="function-2" type="text" label="30002" default="ShutDown" />
<setting label="32033" type="action" action="RunScript(my.addon.id, removepin-2)"/>

<setting label="32032" type="action" action="RunScript(my.addon.id, addpin)"/>


Thoughts?

Or, would it be best to keep these settings out of settings.xml and have own GUI for creating / removing them and store them in a simply JSON file or DB?
If so, what's the best way to do GUIS these days that are skin neutral?

Update, here is what I have gone with

settings.xml

Code:
<?xml version="1.0" ?>
<settings>                        
  <category label="30100">
    <setting action="RunScript(service.gpio.monitor, addpin)" label="30010" option="close" type="action"/>
    <setting default="true" id="notifications" label="30011" type="bool"/>
  </category>
</settings>

default.py

Code:
import os, sys
from xml.dom.minidom import parse, parseString
import xbmc, xbmcaddon

__addon__           = xbmcaddon.Addon()
__addonid__         = __addon__.getAddonInfo('id').decode( 'utf-8' )
__settings_file__   = os.path.join(__addon__.getAddonInfo('path').decode("utf-8"), 'resources', 'settings.xml')

if __name__ == '__main__':
    if len(sys.argv) > 1:
        method = sys.argv[1]
    else:
        method = None

    settings = parse(__settings_file__)

    last_id = __addon__.getSetting('last_id')
    if not last_id:
        last_id = 0

    if method == 'addpin':
        last_id = int(last_id) + 1

        category = parseString("""
        <category id="{0}" label="30101">
            <setting default="0" id="pin-{0}" label="30000" option="int" range="0,1,40" type="slider"/>
            <setting default="0" id="edge-{0}" label="30001" lvalues="30003|30004" type="select" value="0,1"/>
            <setting default="ShutDown" id="function-{0}" label="30002" type="text"/>
            <setting action="RunScript({1}, removepin, {0})" label="30005" type="action" option="close"/>
        </category>
        """.format(last_id, __addonid__)).documentElement

        settings.childNodes[0].appendChild(category)

    elif method == 'removepin':
        pin = sys.argv[2]
        for elem in settings.getElementsByTagName('category'):
            if elem.getAttribute('id') == pin:
                settings.childNodes[0].removeChild(elem)
                __addon__.setSetting('pin-%s' % pin, "0")
                break

    with open(__settings_file__, 'w') as f:
        f.write(settings.toxml())

    __addon__.setSetting('last_id', str(last_id))
    __addon__.openSettings()
Reply
#2
Hmm.
Seems KODI caches or something

Sometimes when it opens the addon settings after modifying the xml - it still shows the old values
..
Any way to force KODI to reload the settings.xml file??
Reply
#3
If you need an event-driven UI it's better to create your own using WindowDialog or WindowXMLDialog. If you don't need anything too fancy, I'd recommend my PyXBMCt.
Reply
#4
I can get a PIN from the following terminal command:
Code:
curl -s -k -d="value=Get a new PIN" "https://offshoreplugins.com/pin/boom/" | grep "PIN: " | sed 's/.*PIN: //' | sed 's/<.*//'
Shouldn't be too hard to convert to a python one-liner function for your needs.
Reply

Logout Mark Read Team Forum Stats Members Help
Best way for multiple dynamic settings?0