1276b1e3fecff6f8d6474bcc9a0425f2cfc46e24
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / WOLSetup / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2
3 from Screens.Screen import Screen
4 from Components.ActionMap import ActionMap
5 from Components.Label import Label
6 from Components.Sources.StaticText import StaticText
7
8 from Screens.ChoiceBox import ChoiceBox
9 from Screens.MessageBox import MessageBox
10 from Screens.Standby import TryQuitMainloop
11
12 from Components.PluginComponent import plugins
13
14 from Components.ConfigList import ConfigListScreen
15 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigYesNo, ConfigSelection
16
17 import os
18
19 _deviseWOL = "/proc/stb/fp/wol"
20
21 _flagForceEnable = False
22 _flagSupportWol = _flagForceEnable and True or os.path.exists(_deviseWOL)
23
24 _tryQuitTable = {"deepstandby":1, "reboot":2, "guirestart":3}
25
26 _ethDevice = "eth0"
27
28 config.plugins.wolconfig = ConfigSubsection()
29 config.plugins.wolconfig.activate = ConfigYesNo(default = False)
30 config.plugins.wolconfig.location = ConfigSelection(default = "menu", choices = [("menu", _("Show on the Standby Menu")), ("deepstandby", _("Run at the Deep Standby"))])
31
32 import socket
33 class NetTool:
34         @staticmethod
35         def GetHardwareAddr(ethname):
36                 macaddr = ""
37                 try:
38                         sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
39                         sock.bind((ethname, 9999))
40
41                         macaddr = ":".join(["%02X" % ord(x) for x in sock.getsockname()[-1]])
42                 except Exception, Message:
43                         print Message
44                         macaddr = "Unknown"
45                 return macaddr
46
47 class WOLSetup(ConfigListScreen, Screen):
48         skin =  """
49                 <screen name="WOLSetup" position="center,120" size="600,390" title="WakeOnLan Setup">
50                         <ePixmap pixmap="skin_default/buttons/red.png" position="5,0" size="140,40" alphatest="on" />
51                         <ePixmap pixmap="skin_default/buttons/green.png" position="155,0" size="140,40" alphatest="on" />
52                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="305,0" size="140,40" alphatest="on" />
53                         <ePixmap pixmap="skin_default/buttons/blue.png" position="455,0" size="140,40" alphatest="on" />
54
55                         <widget source="key_red" render="Label" position="5,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
56                         <widget source="key_green" render="Label" position="155,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
57                         <widget source="key_yellow" render="Label" position="305,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" foregroundColor="#ffffff" transparent="1" />
58                         <widget source="key_blue" render="Label" position="455,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" foregroundColor="#ffffff" transparent="1" />
59
60                         <widget name="config" position="5,70" size="590,260" scrollbarMode="showOnDemand" />
61                         <widget name="introduction" position="5,345" size="590,40" font="Regular;24" halign="center" />
62                 </screen>
63                 """
64         def __init__(self, session):
65                 self.configlist = []
66                 self.session = session
67                 Screen.__init__(self, session)
68                 ConfigListScreen.__init__(self, self.configlist)
69
70                 self["actions"]  = ActionMap(["OkCancelActions", "ColorActions", "WizardActions"], {
71                         "cancel": self.OnKeyCancel,
72                         "green" : self.OnKeyGreen,
73                         "red"   : self.OnKeyRed,
74                         "blue"  : self.OnKeyBlue,
75                 }, -1)
76
77                 self["key_red"]    = StaticText(_("Close"))
78                 self["key_green"]  = StaticText(_("Save"))
79                 self["key_yellow"] = StaticText(_(" "))
80                 self["key_blue"]   = StaticText(_("Default"))
81                 self["introduction"] = Label(" ")
82
83                 self.default = {"activate":False, "location":"menu"}
84                 self.backup = {
85                         "activate":config.plugins.wolconfig.activate.value,
86                         "location":config.plugins.wolconfig.location.value,
87                         }
88
89                 self.MakeConfigList()
90
91         def MakeConfigList(self):
92                 self.UpdateConfigList()
93
94         def UpdateConfigList(self):
95                 self.configlist = []
96                 if _flagSupportWol:
97                         macaddr = " "
98                         self.configlist.append(getConfigListEntry(_("WakeOnLan Enable"), config.plugins.wolconfig.activate))
99                         if config.plugins.wolconfig.activate.value:
100                                 self.configlist.append(getConfigListEntry(_("Location"), config.plugins.wolconfig.location))
101                                 macaddr = "HWaddr of %s is %s" % (_ethDevice, NetTool.GetHardwareAddr(_ethDevice))
102                         else:   macaddr = "Wake on Lan disabled"
103                         self["introduction"].setText(macaddr)
104
105                 self["config"].list = self.configlist
106                 self["config"].l.setList(self.configlist)
107
108         def Save(self):
109                 config.plugins.wolconfig.activate.save()
110                 config.plugins.wolconfig.location.save()
111                 config.plugins.wolconfig.save()
112                 config.plugins.save()
113                 config.save()
114
115         def Restore(self):
116                 print "[WOLSetup] Restore :", self.backup
117                 config.plugins.wolconfig.activate.value = self.backup["activate"]
118                 config.plugins.wolconfig.location.value = self.backup["location"]
119
120         def OnKeyGreenCB(self, answer):
121                 answer = answer and answer[1]
122                 if answer == "n":
123                         self.Restore()
124                         return
125                 self.Save()
126                 self.close()
127
128         def OnKeyGreen(self):
129                 if config.plugins.wolconfig.activate.value == self.backup["activate"] and config.plugins.wolconfig.location.value == self.backup["location"]:
130                         self.close()
131                         return
132                 if not config.plugins.wolconfig.activate.value:
133                         WOLSetup.ActivateWOL(False, True)
134                         self.OnKeyBlue()
135                         self.Save()
136                         self.close()
137                         return
138                 message = _("If WOL is enabled, power consumption will be around 2W.\nErP Standby Power Regulation (<0.5W at standby) cannot be met.\nProceed?")
139                 self.session.openWithCallback(self.OnKeyGreenCB, ChoiceBox, title=message, list=((_("Yes"), "y"), (_("No"), "n"),))
140
141         def OnKeyCancel(self):
142                 self.Restore()
143                 self.close()
144
145         def OnKeyRed(self):
146                 self.OnKeyCancel()
147
148         def OnKeyBlue(self):
149                 print "[WOLSetup] Set Default :", self.default
150                 config.plugins.wolconfig.activate.value = self.default["activate"]
151                 config.plugins.wolconfig.location.value = self.default["location"]
152                 self.UpdateConfigList()
153
154         def keyLeft(self):
155                 ConfigListScreen.keyLeft(self)
156                 self.UpdateConfigList()
157
158         def keyRight(self):
159                 ConfigListScreen.keyRight(self)
160                 self.UpdateConfigList()
161
162         @staticmethod
163         def ActivateWOL(self=None, enable=True, writeDevice=False):
164                 print "[WOLSetup] Support :", _flagSupportWol, " Enable :", enable ," WriteDevice :", writeDevice
165                 if not _flagSupportWol:
166                         return
167                 if writeDevice:
168                         os.system('echo "%s" > %s' % (enable and "enable" or "disble",_deviseWOL))
169                 os.system("ethtool -s %s wol %s" % (_ethDevice, enable and "g" or "d"))
170
171         @staticmethod
172         def DeepStandbyNotifierCB(self=None):
173                 if config.plugins.wolconfig.activate.value:
174                         if config.plugins.wolconfig.location.value == "deepstandby":
175                                 print "[WOLSetup] Deep Standby with WOL."
176                                 WOLSetup.ActivateWOL(writeDevice=True)
177                         else:   print "[WOLSetup] Deep Standby without WOL."
178                 else:   print "[WOLSetup] Nomal Deep Standby."
179
180 def SessionStartMain(session, **kwargs):
181         config.misc.DeepStandbyOn.addNotifier(WOLSetup.DeepStandbyNotifierCB, initial_call=False)
182
183 def PluginMain(session, **kwargs):
184         session.open(WOLSetup)
185
186 def DeepStandbyWOLMain(session, **kwargs):
187         WOLSetup.ActivateWOL(session, writeDevice=True)
188         session.open(TryQuitMainloop, _tryQuitTable["deepstandby"])
189
190 def MenuSelected(selected, **kwargs):
191         if selected == "system":
192                 return [(_("WakeOnLan Setup"), PluginMain, "wolconfig", 80)]
193         if selected == "shutdown" and config.plugins.wolconfig.activate.value and config.plugins.wolconfig.location.value == "menu":
194                 return [(_("Deep Standby with WOL"), DeepStandbyWOLMain, "deep_standby_wol", 80)]
195         return []
196
197 def Plugins(**kwargs):
198         if not _flagSupportWol: return []
199         l = []
200         l.append(PluginDescriptor(name=_("WakeOnLan Setup"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, needsRestart=True, fnc=PluginMain))
201         l.append(PluginDescriptor(where=PluginDescriptor.WHERE_SESSIONSTART, fnc=SessionStartMain))
202         l.append(PluginDescriptor(where=PluginDescriptor.WHERE_MENU, fnc=MenuSelected))
203         return l
204