重启后,在Python中保存和恢复On / Off开关

时间:2014-04-19 10:17:47

标签: python

我用Python编写了一个程序,运行XBMC,显示一些按钮来控制中继板。如果我打开继电器然后关闭程序,继电器仍将打开。但是当我打开程序时,按钮将被设置为关闭。那么如何保存按钮的状态呢? 这是代码:

# HusLjus 1.0.3
# Arvid Widerberg

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(1000, 600, 8, 5)
    self.set_info_controls()
    self.set_active_controls()
    self.set_navigation()
    # Tillbaka med backspace
    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(self):
    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('Tillbaka!')
    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 :(得分:-1)

为什么不为此使用pilight API?

已经提供了python的示例: https://github.com/pilight/pilight/blob/master/clients/process.py

API参考: http://www.pilight.org/development/api/

如果您可以编写一个pilight XBMC GUI插件,那将会很棒。所以在XBMC中呈现了一个基本的导游gui。