Advertisement
SharkyEXE

Untitled

Aug 15th, 2018
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.43 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from Plugins.Plugin import PluginDescriptor
  3. from Screens.Screen import Screen
  4. from Screens.MessageBox import MessageBox
  5. from Components.Button import Button
  6. from Components.ActionMap import ActionMap
  7. from Components.ConfigList import ConfigListScreen
  8. from Components.config import config, ConfigSubsection, ConfigDateTime, ConfigClock, ConfigText, getConfigListEntry, ConfigInteger
  9. import enigma
  10. from enigma import eTimer, iServiceInformation
  11. from datetime import datetime, time as dt_time
  12. from os import system
  13. import gettext
  14. import time, httplib
  15. from Screens.Console import Console
  16. import os
  17. from os import popen
  18.  
  19. config.plugins.SetClock = ConfigSubsection()
  20. config.plugins.SetClock.HostName = ConfigText(default='vniiftri.ru', fixed_size=False)
  21. #config.plugins.SetClock.NTPName01 = ConfigText(default='vniiftri2.khv.ru', fixed_size=False)
  22. #config.plugins.SetClock.NTPName02 = ConfigText(default='vniiftri.khv.ru', fixed_size=False)
  23. #config.plugins.SetClock.NTPName03 = ConfigText(default='ntp2.niiftri.irkutsk.ru', fixed_size=False)
  24. #config.plugins.SetClock.NTPName04 = ConfigText(default='ntp1.niiftri.irkutsk.ru', fixed_size=False)
  25. #config.plugins.SetClock.NTPName05 = ConfigText(default='ntp21.vniiftri.ru', fixed_size=False)
  26. #config.plugins.SetClock.NTPName06 = ConfigText(default='ntp4.vniiftri.ru', fixed_size=False)
  27. #config.plugins.SetClock.NTPName07 = ConfigText(default='ntp3.vniiftri.ru', fixed_size=False)
  28. #config.plugins.SetClock.NTPName08 = ConfigText(default='ntp1.vniiftri.ru', fixed_size=False)
  29. #config.plugins.SetClock.NTPName09 = ConfigText(default='ntp2.vniiftri.ru', fixed_size=False)
  30. config.plugins.SetClock.NTPName01 = ConfigText(default='ntp2.vniiftri.ru', fixed_size=False)
  31. config.plugins.SetClock.timeout = ConfigInteger(default = 20, limits = (0,120))
  32. ver = '1.1'
  33.  
  34. def _(txt):
  35. t = gettext.gettext(txt)
  36. return t
  37.  
  38.  
  39. #def Plugins(**kwargs):
  40. def Plugins(path, **kwargs):
  41. global plugin_path
  42. plugin_path = path
  43. return [PluginDescriptor(name='SetClock', description='SetClock plug-in v.' + ver + ' (c)2011 by SatCat', where=PluginDescriptor.WHERE_PLUGINMENU, icon='plugin.png', fnc=main), PluginDescriptor(name='SetClock', description='SetClock plug-in v.' + ver + '(c)2011 by SatCat', where=[PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc=autostart)]
  44.  
  45.  
  46. def main(session, servicereference = None, **kwargs):
  47. try:
  48. session.open(SetClockMain)
  49. except:
  50. print '[SetClock] Pluginexecution failed'
  51.  
  52.  
  53. def autostart(reason, **kwargs):
  54. global session
  55. if reason == 0 and kwargs.has_key('session'):
  56. session = kwargs['session']
  57. session.open(AutoCorrTime)
  58.  
  59.  
  60. def get_time_str(res_time):
  61. replace_months = {
  62. 'Jan': '1',
  63. 'Feb': '2',
  64. 'Mar': '3',
  65. 'Apr': '4',
  66. 'May': '5',
  67. 'June': '6',
  68. 'Jun': '6',
  69. 'July': '7',
  70. 'Jul': '7',
  71. 'Aug': '8',
  72. 'Sept': '9',
  73. 'Sep': '9',
  74. 'Oct': '10',
  75. 'Nov': '11',
  76. 'Dec': '12'
  77. }
  78. gmts = [
  79. 'UTC',
  80. 'GMT'
  81. ]
  82. _res_time = res_time
  83. try:
  84. time_tuple = time.strptime(res_time, '%a, %d %b %Y %H:%M:%S %Z')
  85. except ValueError:
  86. for m1, m2 in replace_months.items():
  87. res_time = res_time.replace(m1, m2)
  88. for gmt in gmts:
  89. res_time = res_time.replace(gmt, '')
  90. res_time = res_time.split(', ')[1].strip()
  91. try:
  92. time_tuple = time.strptime(res_time, '%d %m %Y %H:%M:%S')
  93. #open('/usr/lib/enigma2/python/Plugins/Extensions/SetClock/log.txt', 'a').write(_res_time)
  94. except ValueError:
  95. open('/usr/lib/enigma2/python/Plugins/Extensions/SetClock/log.txt', 'a').write(_res_time)
  96. raise
  97. t = time.localtime(time.mktime(time_tuple) - time.timezone)
  98. return time.strftime('%Y%m%d%H%M.%S', t)
  99.  
  100. class AutoCorrTime(Screen):
  101. skin = '''
  102. <screen position="100,100" size="280,300" title="SetClock" >
  103. </screen>'''
  104.  
  105. def __init__(self, session):
  106. Screen.__init__(self, session)
  107. self.session = session
  108. if time.localtime().tm_year == 2000:
  109. self.limit = 0
  110. self.cursorTimer = eTimer()
  111. self.cursorTimer.callback.append(self.timer)
  112. self.cursorTimer.start(75, False)
  113.  
  114. def timer(self):
  115. if time.localtime().tm_year > 2000:
  116. refstr = dtt = 'n/a'
  117. try:
  118. service = self.session.nav.getCurrentService()
  119. if service:
  120. info = service.info()
  121. if info:
  122. refstr = info.getInfoString(iServiceInformation.sServiceref)
  123. except:
  124. refstr = 'ref error'
  125.  
  126. if refstr == '1:0:1:1:65:64:3840000:0:0:0:':
  127. tc = time.time() - 189391980
  128. dtt = datetime.fromtimestamp(tc).strftime('%Y%m%d%H%M')
  129. enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False)
  130. try:
  131. system('/bin/date -s %s' % dtt)
  132. print '[SetClock] Set clock!'
  133. except:
  134. pass
  135.  
  136. self.cursorTimer.stop()
  137. print '[SetClock] Stop timer.'
  138. self.limit = self.limit + 1
  139. if self.limit > 1200:
  140. self.cursorTimer.stop()
  141. print '[SetClock] Stop timer (limit).'
  142.  
  143.  
  144. class SetClockMain(ConfigListScreen, Screen):
  145. skin = '''
  146. <screen position="center,200" size="560,365" title="SetClock" >
  147. <widget name="config" position="0,0" size="560,365" scrollbarMode="showOnDemand" />
  148. <widget name="key_red" position="0,325" size="140,40" valign="center" halign="center" zPosition="4" foregroundColor="white" font="Regular;20" transparent="1"/>
  149. <ePixmap name="red" position="0,325" size="140,40" zPosition="2" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
  150. <widget name="key_green" position="140,325" size="140,40" valign="center" halign="center" zPosition="4" foregroundColor="white" font="Regular;20" transparent="1"/>
  151. <ePixmap name="green" position="140,325" size="140,40" zPosition="2" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
  152. <widget name="key_yellow" position="280,325" size="140,40" valign="center" halign="center" zPosition="4" backgroundColor="#a08500" font="Regular;20" transparent="1"/>
  153. <ePixmap name="yellow" position="280,325" size="140,40" zPosition="2" pixmap="skin_default/buttons/yellow.png" transparent="1" alphatest="on" />
  154. <widget name="key_blue" position="420,325" size="140,40" valign="center" halign="center" zPosition="4" backgroundColor="#a08500" font="Regular;20" transparent="1"/>
  155. <ePixmap name="blue" position="420,325" size="140,40" zPosition="2" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
  156. </screen>'''
  157.  
  158. def __init__(self, session, args = 0):
  159. self.skin = SetClockMain.skin
  160. self.session = session
  161. Screen.__init__(self, session)
  162. self.list = []
  163. self.date_en = ConfigDateTime(default=time.time(), formatstring='%d.%m.%Y')
  164. self.entryDate = getConfigListEntry(_('Date'), self.date_en)
  165. self.time_en = ConfigClock(default=time.time())
  166. self.entryTime = getConfigListEntry(_('Time'), self.time_en)
  167. self.list.append(self.entryDate)
  168. self.list.append(self.entryTime)
  169. #self.list.append(getConfigListEntry('HTTP host:', config.plugins.SetClock.HostName))
  170. #self.list.append(getConfigListEntry('NTP Server № 01:', config.plugins.SetClock.NTPName01))
  171. #self.list.append(getConfigListEntry('NTP Server № 02:', config.plugins.SetClock.NTPName02))
  172. #self.list.append(getConfigListEntry('NTP Server № 03:', config.plugins.SetClock.NTPName03))
  173. #self.list.append(getConfigListEntry('NTP Server № 04:', config.plugins.SetClock.NTPName04))
  174. #self.list.append(getConfigListEntry('NTP Server № 05:', config.plugins.SetClock.NTPName05))
  175. #self.list.append(getConfigListEntry('NTP Server № 06:', config.plugins.SetClock.NTPName06))
  176. #self.list.append(getConfigListEntry('NTP Server № 07:', config.plugins.SetClock.NTPName07))
  177. #self.list.append(getConfigListEntry('NTP Server № 08:', config.plugins.SetClock.NTPName08))
  178. #self.list.append(getConfigListEntry('NTP Server № 09:', config.plugins.SetClock.NTPName09))
  179. self.list.append(getConfigListEntry('NTP Server № 01:', config.plugins.SetClock.NTPName01))
  180. self.list.append(getConfigListEntry('NTP Server Timeout [s]:', config.plugins.SetClock.timeout))
  181. self['key_red'] = Button(_('Установить!'))
  182. self['key_green'] = Button(_('HTTP SET'))
  183. self['key_yellow'] = Button(_('NTP SET'))
  184. self['key_blue'] = Button(_('Cancel'))
  185. ConfigListScreen.__init__(self, self.list, session=session)
  186. self['setupActions'] = ActionMap(['OkCancelActions', 'ColorActions'], {'red': self.set,
  187. 'green': self.inet_set,
  188. 'yellow': self.NTP_set,
  189. 'blue': self.cancel,
  190. 'save': self.set,
  191. 'cancel': self.cancel,
  192. 'ok': self.pressed_ok}, -2)
  193. self['config'].list = self.list
  194. self['config'].l.setList(self.list)
  195.  
  196. def pressed_ok(self):
  197. config.plugins.SetClock.HostName.save()
  198. config.plugins.SetClock.NTPName.save()
  199. self.set()
  200.  
  201. def set(self, dtt = 0):
  202. d = time.localtime(self.date_en.value)
  203. dtt = '%d%02d%02d%02d%02d' % (d.tm_year,
  204. d.tm_mon,
  205. d.tm_mday,
  206. self.time_en.value[0],
  207. self.time_en.value[1])
  208. enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False)
  209. try:
  210. system('/bin/date -s %s' % dtt)
  211. except:
  212. pass
  213.  
  214. self.close()
  215. print '[SetClock] SET! ** %s' % time.strftime('%X %x (%Y) %Z')
  216.  
  217. def inet_set(self):
  218. config.plugins.SetClock.HostName.save()
  219. conn = httplib.HTTPConnection(config.plugins.SetClock.HostName.value)
  220. try:
  221. conn.request('HEAD', '/favicon.ico')
  222. t_rtt = time.clock()
  223. res_time = conn.getresponse().getheader('date')
  224. except:
  225. self.session.open(MessageBox, 'Error: No server responce: ' + config.plugins.SetClock.HostName.value, MessageBox.TYPE_ERROR, timeout=3)
  226. return
  227.  
  228. enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False)
  229.  
  230. try:
  231. system('/bin/date -s %s' % get_time_str(res_time))
  232. except:
  233. pass
  234.  
  235. self.close()
  236. print '[SetClock] inet SET! ** %s' % time.strftime('%X %x (%Y) %Z')
  237.  
  238. def cancel(self):
  239. self.close()
  240.  
  241. def NTP_set(self):
  242. #if (config.plugins.SetClock.NTPName01.value == "" and config.plugins.SetClock.NTPName02.value == "" and config.plugins.SetClock.NTPName03.value == "" and config.plugins.SetClock.NTPName04.value == "" and config.plugins.SetClock.NTPName05.value == "" and config.plugins.SetClock.NTPName06.value == "" and config.plugins.SetClock.NTPName07.value == "" and config.plugins.SetClock.NTPName08.value == "" and config.plugins.SetClock.NTPName09.value == ""):
  243. #self.session.open(MessageBox, _("no timeserver configured!"), MessageBox.TYPE_INFO)
  244. #else:
  245. if os.path.exists(os.path.join(plugin_path, "ntpdate")):
  246. #Lite_OutPut_In_Console_1_stroke
  247. #cmd = '/usr/bin/ntpdate -b -t %s %s && echo "\n"' % (config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName.value)
  248. #cmd = '%s%s %s %s && echo "\n"' % (plugin_path, "/ntpdate -b -t ", config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName01.value)
  249. #cmd = '%s%s %s %s %s %s %s %s %s %s %s %s && echo "\n"' % (plugin_path, "/ntpdate -b -t ", config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName01.value, config.plugins.SetClock.NTPName02.value, config.plugins.SetClock.NTPName03.value, config.plugins.SetClock.NTPName04.value, config.plugins.SetClock.NTPName05.value, config.plugins.SetClock.NTPName06.value, config.plugins.SetClock.NTPName07.value, config.plugins.SetClock.NTPName08.value, config.plugins.SetClock.NTPName09.value)
  250. #Extended_OutPut_In_Console_2_stroke
  251. #cmd = '/usr/bin/ntpdate -v -u %s %s && echo "\n"' % (config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName.value)
  252. cmd = '%s%s %s %s && echo "\n"' % (plugin_path, "/ntpdate -v -u ", config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName01.value)
  253. #cmd = '%s%s %s %s %s %s %s %s %s %s %s %s && echo "\n"' % (plugin_path, "/ntpdate -v -u ", config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName01.value, config.plugins.SetClock.NTPName02.value, config.plugins.SetClock.NTPName03.value, config.plugins.SetClock.NTPName04.value, config.plugins.SetClock.NTPName05.value, config.plugins.SetClock.NTPName06.value, config.plugins.SetClock.NTPName07.value, config.plugins.SetClock.NTPName08.value, config.plugins.SetClock.NTPName09.value)
  254. res = popen(cmd).read()
  255. if res == "":
  256. cmd = "ls -l %s%s" % (plugin_path, "/ntpdate")
  257. res = popen(cmd).read()
  258. if res[3]!="x":
  259. cmd = "chmod 755 %s%s" % (plugin_path, "/ntpdate")
  260. res = popen(cmd).read()
  261. self.session.open(MessageBox, _("ntpdate problem: attributes for 'ntpdate' have not been correct! Fixed now! Try again!\n%s" % res), MessageBox.TYPE_INFO)
  262. else:
  263. self.session.open(MessageBox, _("ntpdate problem: Internet connection ok? Time server ok?"), MessageBox.TYPE_INFO)
  264. else:
  265. self.session.open(MyConsole, _("Системное время с NTP ..."), [cmd])
  266. else:
  267. self.session.open(MessageBox, _("'ntpdate' не установлен !"), MessageBox.TYPE_ERROR)
  268.  
  269. self.close()
  270. print '[SetClock] NTP SET! ** %s' % time.strftime('%X %x (%Y) %Z')
  271.  
  272. self.close()
  273. print '[SetClock] NTP SET! ** %s' % time.strftime('%X %x (%Y) %Z')
  274.  
  275. class MyConsole(Console):
  276. skin = '''
  277. <screen position="center,245" size="485,260" title="Command execution..." >
  278. <widget name="text" position="0,0" size="485,260" font="Regular;20" />
  279. </screen>'''
  280.  
  281. def __init__(self, session, title = "My Console...", cmdlist = None):
  282. Console.__init__(self, session, title, cmdlist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement