Merge commit 'dm/experimental' into vuplus_experimental
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / Fancontrol / plugin.py
1 from Screens.Screen import Screen
2 from Screens.MessageBox import MessageBox
3 from Plugins.Plugin import PluginDescriptor
4 from Components.ActionMap import ActionMap,NumberActionMap
5 from Components.config import config
6 from Components.config import config, getConfigListEntry, ConfigInteger, ConfigSubsection, ConfigSelection
7 from Components.ConfigList import ConfigListScreen
8 from Components.ActionMap import ActionMap
9 from Components.Sources.StaticText import StaticText
10
11
12 config.plugins.fancontrols = ConfigSubsection()
13 config.plugins.fancontrols.standbymode = ConfigSelection(default = "off", choices = [
14         ("off", _("off")), ("on", _("on"))])
15 config.plugins.fancontrols.usetimer = ConfigSelection(default = "off", choices = [
16         ("off", _("no")), ("on", _("yes"))])
17 config.plugins.fancontrols.fanontime = ConfigInteger(default = 5, limits = (1, 100))
18 config.plugins.fancontrols.fanofftime = ConfigInteger(default = 60, limits = (1, 100))
19
20 class FancontrolConfiguration(Screen, ConfigListScreen):
21         skin = """
22                 <screen name="FancontrolConfiguration" position="center,center" size="560,300" title="Standbymode Fancontrol settings" >
23                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
24                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
25                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" foregroundColor="#ececec" backgroundColor="#9f1313" transparent="1" />
26                         <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" foregroundColor="#ececec" backgroundColor="#1f771f" transparent="1" />
27                         <widget name="config" zPosition="2" position="5,50" size="550,200" scrollbarMode="showOnDemand" transparent="1" />
28                 </screen>"""
29         def __init__(self, session):
30                 Screen.__init__(self, session)
31                 self.session = session
32                 self.standbyEntry = None
33                 self.usetimerEntry = None
34                 self.fanontimeEntry = None
35                 self.fanofftimeEntry = None
36
37                 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
38                 {
39                         "ok": self.keySave,
40                         "cancel": self.keyCancel,
41                         "red": self.keyCancel,
42                         "green": self.keySave,
43                 }, -2)
44
45                 self.list = []
46                 ConfigListScreen.__init__(self, self.list,session = self.session)
47 #               self.getFaninfo()
48                 self.createSetup()
49
50                 self["key_red"] = StaticText(_("Close"))
51                 self["key_green"] = StaticText(_("Save"))
52
53         def keyLeft(self):
54                 ConfigListScreen.keyLeft(self)
55                 self.newConfig()
56
57         def keyRight(self):
58                 ConfigListScreen.keyRight(self)
59                 self.newConfig()
60
61         def getFaninfo(self):
62                 try:
63                         value=int(open('/proc/stb/system/standby_fan_off','r').read())
64                         if value is 0:
65                                 config.plugins.fancontrols.standbymode.value="on"
66                         else:
67                                 config.plugins.fancontrols.standbymode.value="off"
68                         value=int(open('/proc/stb/system/use_fan_timer','r').read())
69                         if value is 0:
70                                 config.plugins.fancontrols.usetimer.value = "off"
71                         else:
72                                 config.plugins.fancontrols.usetimer.value = "on"
73                         time=int(open('/proc/stb/system/fan_on_time','r').read())
74                         if time > 0 and time < 101:
75                                 config.plugins.fancontrols.fanontime.value = time
76                         else:
77                                 config.plugins.fancontrols.fanontime.value = 1
78                         time=int(open('/proc/stb/system/fan_off_time','r').read())
79                         if time > 0 and time < 101:
80                                 config.plugins.fancontrols.fanofftime.value = time
81                         else:
82                                 config.plugins.fancontrols.fanofftime.value = 1
83                 except:
84                         print 'Error read proc of fan'
85         
86
87         def createSetup(self):
88                 self.list = []
89                 self.standbyEntry = getConfigListEntry(_("Fan basic action"), config.plugins.fancontrols.standbymode)
90                 self.usetimerEntry = getConfigListEntry(_("Use Fan timer"), config.plugins.fancontrols.usetimer)
91                 self.fanontimeEntry = getConfigListEntry(_("Fan on duration time"), config.plugins.fancontrols.fanontime)
92                 self.fanofftimeEntry = getConfigListEntry(_("Fan off duration time"), config.plugins.fancontrols.fanofftime)
93
94                 self.list.append( self.standbyEntry )
95                 if config.plugins.fancontrols.standbymode.value is "off":
96                         self.list.append( self.usetimerEntry )
97                         if config.plugins.fancontrols.usetimer.value is not "off":
98                                 self.list.append( self.fanontimeEntry )
99                                 self.list.append( self.fanofftimeEntry )
100                 
101                 self["config"].list = self.list
102                 self["config"].l.setList(self.list)
103                 if not self.selectionChanged in self["config"].onSelectionChanged:
104                         self["config"].onSelectionChanged.append(self.selectionChanged)
105
106         def newConfig(self):
107                 if self["config"].getCurrent() == self.usetimerEntry or self["config"].getCurrent() == self.standbyEntry:
108                         self.createSetup()
109
110         def selectionChanged(self):
111                 current = self["config"].getCurrent()
112                 print current
113
114         def cancelConfirm(self, result):
115                 if not result:
116                         return
117                 for x in self["config"].list:
118                         x[1].cancel()
119                 self.close()
120
121                         
122         def keyCancel(self):
123                 print "cancel"
124                 if self["config"].isChanged():
125                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
126                 else:
127                         self.close()
128
129         def keySave(self):
130                 ConfigListScreen.keySave(self)
131                 try:
132                         if config.plugins.fancontrols.standbymode.value is "on":
133                                 open('/proc/stb/system/standby_fan_off','w').write('0')
134                         else:
135                                 open('/proc/stb/system/standby_fan_off','w').write('1')
136                                 if config.plugins.fancontrols.usetimer.value is "off":
137                                         open('/proc/stb/system/use_fan_timer','w').write('0')
138                                 else:
139                                         open('/proc/stb/system/use_fan_timer','w').write('1')
140                                         open('/proc/stb/system/fan_on_time','w').write('%s'%config.plugins.fancontrols.fanontime.value)
141                                         open('/proc/stb/system/fan_off_time','w').write('%s'%config.plugins.fancontrols.fanofftime.value)
142                 except:
143                         print 'Error write proc of fan'
144                 
145         
146 def openconfig(session, **kwargs):
147         session.open(FancontrolConfiguration)
148
149 def selSetup(menuid, **kwargs):
150         if menuid != "system":
151                 return [ ]
152
153         return [(_("Fan Control"), openconfig, "fancontrol_config", 70)]
154
155 def setfancontrol(reason, **kwargs):
156         try:
157                 if config.plugins.fancontrols.standbymode.value is "on":
158                         open('/proc/stb/system/standby_fan_off','w').write('0')
159                 else:
160                         open('/proc/stb/system/standby_fan_off','w').write('1')
161                         if config.plugins.fancontrols.usetimer.value is "off":
162                                 open('/proc/stb/system/use_fan_timer','w').write('0')
163                         else:
164                                 open('/proc/stb/system/use_fan_timer','w').write('1')
165                                 open('/proc/stb/system/fan_on_time','w').write('%s'%config.plugins.fancontrols.fanontime.value)
166                                 open('/proc/stb/system/fan_off_time','w').write('%s'%config.plugins.fancontrols.fanofftime.value)
167         except:
168                 print 'Error to set fan control'
169
170 def Plugins(**kwargs):
171         return [PluginDescriptor(name = "Fancontrols", description = "check Fan Control settings", where = PluginDescriptor.WHERE_AUTOSTART, fnc = setfancontrol),
172         PluginDescriptor(name=_("Fan control"), description="Fan Control", where = PluginDescriptor.WHERE_MENU, fnc=selSetup)]