从文件存储和加载状态

时间:2015-01-15 22:41:18

标签: python xbmc

所以我有这个用于XBMC的UI,我希望能够在应用程序退出然后加载时存储/加载开/关按钮的状态。我认为最简单的方法是将状态存储在一个简单的txt文件中并在启动时加载它们。因此,第一步是在启动时从列表中加载状态。如何将值发送到按钮?有没有办法让我的代码更有效率?

import os
import xbmc, xbmcaddon, xbmcgui
from pyxbmct.addonwindow import *

_addon = xbmcaddon.Addon()
_addon_path = _addon.getAddonInfo('path').decode('utf-8')


class MyAddon(AddonDialogWindow):

    def __init__(self, title=''):
        super(MyAddon, self).__init__(title)
        self.setGeometry(700, 450, 8, 5)
        self.set_info_controls()
        self.set_active_controls()
        self.set_navigation()
        # Connect a key action (Backspace) to close the window.
        self.connect(ACTION_NAV_BACK, self.close)

    def set_info_controls(self):
        # Labels
        no_int_label = Label('Inomhus', alignment=ALIGN_CENTER)
        self.placeControl(no_int_label, 0, 0, 1, 2)
        #L1
        l1 = Label('Taklampan')
        self.placeControl(l1, 1, 0)
        #L2
        l2 = Label('Pentry')
        self.placeControl(l2, 2, 0)
        #L3
        l3 = Label('Ljuslist')
        self.placeControl(l3, 3, 0)
        #L4
        l4 = Label('Stereo')
        self.placeControl(l4, 4, 0)
        #L5
        l5 = Label('Lampa 5')
        self.placeControl(l5, 1, 3)
        #L6
        l6 = Label('Lampa 6')
        self.placeControl(l6, 2, 3)
        #L7
        l7 = Label('Lampa 7')
        self.placeControl(l7, 3, 3)
        #L8
        l8 = Label('Lampa 8')
        self.placeControl(l8, 4,3)

    def set_active_controls(currentState):
        int_label = Label('Utomhus', alignment=ALIGN_CENTER)
        self.placeControl(int_label, 0, 3, 1, 2)

        # R1
        self.radiobutton1 = RadioButton('Off')
        self.placeControl(self.radiobutton1, 1, 1)
        self.connect(self.radiobutton1, self.radio_update)
        # R2
        self.radiobutton2 = RadioButton('Off')
        self.placeControl(self.radiobutton2, 2, 1)
        self.connect(self.radiobutton2, self.radio_update)
        # R3
        self.radiobutton3 = RadioButton('Off')
        self.placeControl(self.radiobutton3, 3, 1)
        self.connect(self.radiobutton3, self.radio_update)
        # R4
        self.radiobutton4 = RadioButton('Off')
        self.placeControl(self.radiobutton4, 4, 1)
        self.connect(self.radiobutton4, self.radio_update)
        # R5
        self.radiobutton5 = RadioButton('Off')
        self.placeControl(self.radiobutton5, 1, 4)
        self.connect(self.radiobutton5, self.radio_update)
        # R6
        self.radiobutton6 = RadioButton('Off')
        self.placeControl(self.radiobutton6, 2, 4)
        self.connect(self.radiobutton6, self.radio_update)
        # R7
        self.radiobutton7 = RadioButton('Off')
        self.placeControl(self.radiobutton7, 3, 4)
        self.connect(self.radiobutton7, self.radio_update)
        # R8
        self.radiobutton8 = RadioButton('Off')
        self.placeControl(self.radiobutton8, 4, 4)
        self.connect(self.radiobutton8, self.radio_update)

        # Button
        self.button = Button('Close')
        self.placeControl(self.button, 7, 2)
        self.connect(self.button, self.close)

    def set_navigation(self):
        self.setFocus(self.button)

    def radio_update(self):
        # Update radiobutton caption on toggle
        if self.radiobutton1.isSelected():
            self.radiobutton1.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r1 -s on")
        else:
            self.radiobutton1.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r1 -s off")
        if self.radiobutton2.isSelected():
            self.radiobutton2.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r2 -s on")
        else:
            self.radiobutton2.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r2 -s off")
        if self.radiobutton3.isSelected():
            self.radiobutton3.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r3 -s on")
        else:
            self.radiobutton3.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r3 -s off")
        if self.radiobutton4.isSelected():
            self.radiobutton4.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r4 -s on")
        else:
            self.radiobutton4.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r4 -s off")
        if self.radiobutton5.isSelected():
            self.radiobutton5.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r5 -s on")
        else:
            self.radiobutton5.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r5 -s off")
        if self.radiobutton6.isSelected():
            self.radiobutton6.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r6 -s on")
        else:
            self.radiobutton6.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r6 -s off")
        if self.radiobutton7.isSelected():
            self.radiobutton7.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r7 -s on")
        else:
            self.radiobutton7.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r7 -s off")
        if self.radiobutton8.isSelected():
            self.radiobutton8.setLabel('On')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r8 -s on")
        else:
            self.radiobutton8.setLabel('Off')
            os.system("/usr/local/sbin/pilight-control -l husvagn -d r8 -s off")

    def setAnimation(self, control):
        # Set fade animation for all add-on window controls
        control.setAnimations([('WindowOpen', 'effect=fade start=0 end=100 time=500',),
                                ('WindowClose', 'effect=fade start=100 end=0 time=500',)])



def main():


    window = MyAddon('HusLjus')
    window.doModal()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

我自己做这个与当前(当前夜间)事件库的pilight。小片段:

{
    "devices": {
        "speakers": {
            "protocol": [ "kaku_switch" ],
            "id": [{
                "id": 12345,
                "unit": 0
            }],
            "state": "off"
        },
        "xbmcControls": {
            "protocol": [ "xbmc" ],
            "id": [{
                "server": "127.0.0.1",
                "port": 9090
            }],
            "action": "home",
            "media": "none"
        }
    },
    "rules": {
        "speakers-on": {
            "rule": "IF xbmcControls.action IS play THEN switch DEVICE speakers TO on",
            "active": 1
        },
        "speakers-off": {
            "rule": "IF xbmcControls.action IS pause OR xbmcControls.action IS home THEN switch DEVICE speakers TO off",
            "active": 1
        }
    }
...
}

一切都很完美。