# -*- coding: utf-8 -*- from Plugins.Plugin import PluginDescriptor from Screens.Screen import Screen from Screens.MessageBox import MessageBox from Components.Button import Button from Components.ActionMap import ActionMap from Components.ConfigList import ConfigListScreen from Components.config import config, ConfigSubsection, ConfigDateTime, ConfigClock, ConfigText, getConfigListEntry, ConfigInteger, ConfigSequence, ConfigSelection import enigma from enigma import eTimer, iServiceInformation from datetime import datetime, time as dt_time from os import system import gettext import time, httplib from Screens.Console import Console import os from os import popen config.plugins.SetClock = ConfigSubsection() config.plugins.SetClock.HostName = ConfigText(default='vniiftri.ru', fixed_size=False) #config.plugins.SetClock.NTPName01 = ConfigText(default='vniiftri2.khv.ru', fixed_size=False) #config.plugins.SetClock.NTPName02 = ConfigText(default='vniiftri.khv.ru', fixed_size=False) #config.plugins.SetClock.NTPName03 = ConfigText(default='ntp2.niiftri.irkutsk.ru', fixed_size=False) #config.plugins.SetClock.NTPName04 = ConfigText(default='ntp1.niiftri.irkutsk.ru', fixed_size=False) #config.plugins.SetClock.NTPName05 = ConfigText(default='ntp21.vniiftri.ru', fixed_size=False) #config.plugins.SetClock.NTPName06 = ConfigText(default='ntp4.vniiftri.ru', fixed_size=False) #config.plugins.SetClock.NTPName07 = ConfigText(default='ntp3.vniiftri.ru', fixed_size=False) #config.plugins.SetClock.NTPName08 = ConfigText(default='ntp1.vniiftri.ru', fixed_size=False) #config.plugins.SetClock.NTPName09 = ConfigText(default='ntp2.vniiftri.ru', fixed_size=False) config.plugins.SetClock.NTPName01 = ConfigText(default='ntp2.vniiftri.ru', fixed_size=False) config.plugins.SetClock.timeout = ConfigInteger(default = 20, limits = (0,120)) config.plugins.SetClock.syncDVBtime = ConfigSelection(default = "1", choices = [("1", _("Нажмите OK"))]) ver = '1.1' #def _(txt): #t = gettext.gettext(txt) #return t #def Plugins(**kwargs): def Plugins(path, **kwargs): global plugin_path plugin_path = path 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)] def main(session, servicereference = None, **kwargs): #try: session.open(SetClockMain) #except: #print '[SetClock] Pluginexecution failed' def autostart(reason, **kwargs): global session if reason == 0 and kwargs.has_key('session'): session = kwargs['session'] session.open(AutoCorrTime) def get_time_str(res_time): replace_months = { 'Jan': '1', 'Feb': '2', 'Mar': '3', 'Apr': '4', 'May': '5', 'June': '6', 'Jun': '6', 'July': '7', 'Jul': '7', 'Aug': '8', 'Sept': '9', 'Sep': '9', 'Oct': '10', 'Nov': '11', 'Dec': '12' } gmts = [ 'UTC', 'GMT' ] _res_time = res_time try: time_tuple = time.strptime(res_time, '%a, %d %b %Y %H:%M:%S %Z') except ValueError: for m1, m2 in replace_months.items(): res_time = res_time.replace(m1, m2) for gmt in gmts: res_time = res_time.replace(gmt, '') res_time = res_time.split(', ')[1].strip() try: time_tuple = time.strptime(res_time, '%d %m %Y %H:%M:%S') #open('/usr/lib/enigma2/python/Plugins/Extensions/SetClock/log.txt', 'a').write(_res_time) except ValueError: open('/usr/lib/enigma2/python/Plugins/Extensions/SetClock/log.txt', 'a').write(_res_time) raise t = time.localtime(time.mktime(time_tuple) - time.timezone) return time.strftime('%Y%m%d%H%M.%S', t) class AutoCorrTime(Screen): skin = ''' ''' def __init__(self, session): Screen.__init__(self, session) self.session = session if time.localtime().tm_year == 2000: self.limit = 0 self.cursorTimer = eTimer() self.cursorTimer.callback.append(self.timer) self.cursorTimer.start(75, False) def timer(self): if time.localtime().tm_year > 2000: refstr = dtt = 'n/a' try: service = self.session.nav.getCurrentService() if service: info = service.info() if info: refstr = info.getInfoString(iServiceInformation.sServiceref) except: refstr = 'ref error' if refstr == '1:0:1:1:65:64:3840000:0:0:0:': tc = time.time() - 189391980 dtt = datetime.fromtimestamp(tc).strftime('%Y%m%d%H%M') enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False) try: system('/bin/date -s %s' % dtt) print '[SetClock] Set clock!' except: pass self.cursorTimer.stop() print '[SetClock] Stop timer.' self.limit = self.limit + 1 if self.limit > 1200: self.cursorTimer.stop() print '[SetClock] Stop timer (limit).' class SetClockMain(ConfigListScreen, Screen): skin = ''' ''' #def __init__(self, session, args = 0): def __init__(self, session, servicelist = None, args = None): self.servicelist = servicelist self.skin = SetClockMain.skin self.session = session Screen.__init__(self, session) self.list = [] self.date_en = ConfigDate(default=time.time()) self.entryDate = getConfigListEntry(_('Дата :'), self.date_en) self.time_en = ConfigClock(default=time.time()) self.entryTime = getConfigListEntry(_('Время :'), self.time_en) self.list.append(self.entryDate) self.list.append(self.entryTime) self.list.append(getConfigListEntry('HTTP хост :', config.plugins.SetClock.HostName)) #self.list.append(getConfigListEntry('NTP сервер № 01 :', config.plugins.SetClock.NTPName01)) #self.list.append(getConfigListEntry('NTP сервер № 02 :', config.plugins.SetClock.NTPName02)) #self.list.append(getConfigListEntry('NTP сервер № 03 :', config.plugins.SetClock.NTPName03)) #self.list.append(getConfigListEntry('NTP сервер № 04 :', config.plugins.SetClock.NTPName04)) #self.list.append(getConfigListEntry('NTP сервер № 05 :', config.plugins.SetClock.NTPName05)) #self.list.append(getConfigListEntry('NTP сервер № 06 :', config.plugins.SetClock.NTPName06)) #self.list.append(getConfigListEntry('NTP сервер № 07 :', config.plugins.SetClock.NTPName07)) #self.list.append(getConfigListEntry('NTP сервер № 08 :', config.plugins.SetClock.NTPName08)) #self.list.append(getConfigListEntry('NTP сервер № 09 :', config.plugins.SetClock.NTPName09)) self.list.append(getConfigListEntry('NTP сервер № 01 :', config.plugins.SetClock.NTPName01)) self.list.append(getConfigListEntry(_('NTP сервер таймаут [сек] :'), config.plugins.SetClock.timeout)) #self.list.append(getConfigListEntry(_('Синхронизировать сейчас с текущего транспондера'), config.plugins.SetClock.syncDVBtime)) #self.list.append(getConfigListEntry(_('Синхронизация сейчас с текущего транспондера'), config.plugins.SetClock.syncDVBtime)) #self.list.append(getConfigListEntry(_('Синхронизация с транспондера :'), config.plugins.SetClock.syncDVBtime)) self.list.append(getConfigListEntry(_('Синхронизация с текущего транспондера :'), config.plugins.SetClock.syncDVBtime)) self['key_red'] = Button(_('Отмена')) self['key_green'] = Button(_('HTTP SET')) self['key_yellow'] = Button(_('NTP SET')) self['key_blue'] = Button(_('Установить!')) ConfigListScreen.__init__(self, self.list, session=session) self['setupActions'] = ActionMap(['OkCancelActions', 'ColorActions'], {'red': self.exit, 'green': self.inet_set, 'yellow': self.NTP_set, #'blue': self.set, 'blue': self.keyBlue, 'cancel': self.exit, #'ok': self.pressed_ok, 'ok': self.keyGo}, -2) self['config'].list = self.list self['config'].l.setList(self.list) self["config"].onSelectionChanged.append(self.configPosition) #def pressed_ok(self): #sel = self["config"].getCurrent() and self["config"].getCurrent()[0] #if sel == _("HTTP хост :"): #config.plugins.SetClock.HostName.save() #if sel == _("NTP сервер № 01 :"): #config.plugins.SetClock.NTPName01.save() #config.plugins.SetClock.NTPName02.save() #config.plugins.SetClock.NTPName03.save() #config.plugins.SetClock.NTPName04.save() #config.plugins.SetClock.NTPName05.save() #config.plugins.SetClock.NTPName06.save() #config.plugins.SetClock.NTPName07.save() #config.plugins.SetClock.NTPName08.save() #config.plugins.SetClock.NTPName09.save() #if sel == _("NTP сервер таймаут [сек] :"): #config.plugins.SetClock.timeout.save() #self.set() #self.close() #self.session.open(MessageBox, _("Настройки сохранены !"), MessageBox.TYPE_INFO) ##def pressed_ok1(self): ##config.plugins.SetClock.HostName.save() #config.plugins.SetClock.NTPName01.save() #config.plugins.SetClock.NTPName02.save() #config.plugins.SetClock.NTPName03.save() #config.plugins.SetClock.NTPName04.save() #config.plugins.SetClock.NTPName05.save() #config.plugins.SetClock.NTPName06.save() #config.plugins.SetClock.NTPName07.save() #config.plugins.SetClock.NTPName08.save() #config.plugins.SetClock.NTPName09.save() #config.plugins.SetClock.timeout.save() #self.set() #self.close() ##self.session.open(MessageBox, _("Настройки сохранены !"), MessageBox.TYPE_INFO) #self.close() ##self.exit() ##def pressed_ok2(self): #config.plugins.SetClock.HostName.save() ##config.plugins.SetClock.NTPName01.save() #config.plugins.SetClock.NTPName02.save() #config.plugins.SetClock.NTPName03.save() #config.plugins.SetClock.NTPName04.save() #config.plugins.SetClock.NTPName05.save() #config.plugins.SetClock.NTPName06.save() #config.plugins.SetClock.NTPName07.save() #config.plugins.SetClock.NTPName08.save() #config.plugins.SetClock.NTPName09.save() #config.plugins.SetClock.timeout.save() #self.set() #self.close() ##self.session.open(MessageBox, _("Настройки сохранены !"), MessageBox.TYPE_INFO) #self.close() ##self.exit() ##def pressed_ok3(self): #config.plugins.SetClock.HostName.save() #config.plugins.SetClock.NTPName01.save() #config.plugins.SetClock.NTPName02.save() #config.plugins.SetClock.NTPName03.save() #config.plugins.SetClock.NTPName04.save() #config.plugins.SetClock.NTPName05.save() #config.plugins.SetClock.NTPName06.save() #config.plugins.SetClock.NTPName07.save() #config.plugins.SetClock.NTPName08.save() #config.plugins.SetClock.NTPName09.save() ##config.plugins.SetClock.timeout.save() #self.set() #self.close() ##self.session.open(MessageBox, _("Настройки сохранены !"), MessageBox.TYPE_INFO) #self.close() ##self.exit() def set(self, dtt = 0): if self.date_en.value[2] >= 2038 and self.date_en.value[1] >= 1 and self.date_en.value[0] >= 19 and self.time_en.value[0] >= 6 and self.time_en.value[1] >= 14 and self.time_en.value[2] > 5: self.date_en.value[2] = 2038 self.date_en.value[1] = 1 self.date_en.value[0] = 19 self.time_en.value[0] = 6 self.time_en.value[1] = 14 self.time_en.value[2] = 5 dtt = '%d%02d%02d%02d%02d.%02d' % (self.date_en.value[2], self.date_en.value[1], self.date_en.value[0], self.time_en.value[0], self.time_en.value[1], self.time_en.value[2]) enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False) try: system('/bin/date -s %s' % dtt) except: pass self.close() #print '[SetClock] SET! ** %s' % time.strftime('%X %x (%Y) %Z') def inet_set(self): config.plugins.SetClock.HostName.save() conn = httplib.HTTPConnection(config.plugins.SetClock.HostName.value) try: conn.request('HEAD', '/favicon.ico') t_rtt = time.clock() res_time = conn.getresponse().getheader('date') except: self.session.open(MessageBox, 'Error: No server responce: ' + config.plugins.SetClock.HostName.value, MessageBox.TYPE_ERROR, timeout=3) return enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False) try: system('/bin/date -s %s' % get_time_str(res_time)) except: pass self.close() #print '[SetClock] inet SET! ** %s' % time.strftime('%X %x (%Y) %Z') #def cancel(self): #self.close() def save(self): for x in self['config'].list: x[1].save() self.close() def exit(self): for x in self['config'].list: x[1].cancel() self.close() def NTP_set(self): #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 == ""): #self.session.open(MessageBox, _("no timeserver configured!"), MessageBox.TYPE_INFO) #else: if os.path.exists(os.path.join(plugin_path, "ntpdate")): #Lite_OutPut_In_Console_1_stroke #cmd = '/usr/bin/ntpdate -b -t %s %s && echo "\n"' % (config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName.value) #cmd = '%s%s %s %s && echo "\n"' % (plugin_path, "/ntpdate -b -t ", config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName01.value) #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) #Extended_OutPut_In_Console_2_stroke #cmd = '/usr/bin/ntpdate -v -u %s %s && echo "\n"' % (config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName.value) cmd = '%s%s %s %s && echo "\n"' % (plugin_path, "/ntpdate -v -u ", config.plugins.SetClock.timeout.value, config.plugins.SetClock.NTPName01.value) #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) res = popen(cmd).read() if res == "": cmd = "ls -l %s%s" % (plugin_path, "/ntpdate") res = popen(cmd).read() if res[3]!="x": cmd = "chmod 755 %s%s" % (plugin_path, "/ntpdate") res = popen(cmd).read() self.session.open(MessageBox, _("ntpdate problem: attributes for 'ntpdate' have not been correct! Fixed now! Try again!\n%s" % res), MessageBox.TYPE_INFO) else: self.session.open(MessageBox, _("ntpdate problem: Internet connection ok? Time server ok?"), MessageBox.TYPE_INFO) else: self.session.open(MyConsole, _("Системное время с NTP ..."), [cmd]) self.close() self.close() else: self.session.open(MessageBox, _("'ntpdate' не установлен !"), MessageBox.TYPE_ERROR) #self.close() #print '[SetClock] NTP SET! ** %s' % time.strftime('%X %x (%Y) %Z') #self.close() #print '[SetClock] NTP SET! ** %s' % time.strftime('%X %x (%Y) %Z') #def keyLeft(self): def keyGo(self): sel = self["config"].getCurrent() and self["config"].getCurrent()[0] if sel == _("Синхронизация с текущего транспондера :"): #if os.path.exists("/usr/bin/dvbdate"): if os.path.exists(os.path.join(plugin_path, "dvbdate")): #cmd = '/usr/bin/dvbdate -p -s -f && echo "\n"' cmd = '%s%s && echo "\n"' % (plugin_path, "/dvbdate -p -s -f") res = popen(cmd).read() if res == "": cmd = "ls -l %s%s" % (plugin_path, "/dvbdate") res = popen(cmd).read() if res[3]!="x": cmd = "chmod 755 %s%s" % (plugin_path, "/dvbdate") res = popen(cmd).read() self.session.open(MessageBox, _("dvbdate problem: attributes for 'dvbdate' have not been correct! Fixed now! Try again!\n%s" % res), MessageBox.TYPE_INFO) else: self.session.open(MessageBox, _("dvbdate problem: Satellite connection ok?"), MessageBox.TYPE_INFO) else: self.session.open(MyConsole, _("Системное время с DVB ..."), [cmd]) self.close() self.close() else: self.session.open(MessageBox, _("'dvbdate' не установлен !"), MessageBox.TYPE_ERROR) if sel == _("Дата :"): self.set() if sel == _("Время :"): self.set() if sel == _("HTTP хост :"): #self.pressed_ok1() #self.pressed_ok() config.plugins.SetClock.HostName.save() self.session.open(MessageBox, _("Настройки сохранены !"), MessageBox.TYPE_INFO) self.exit() #self.close() #self.close() if sel == _("NTP сервер № 01 :"): #self.pressed_ok2() #self.pressed_ok() config.plugins.SetClock.NTPName01.save() self.session.open(MessageBox, _("Настройки сохранены !"), MessageBox.TYPE_INFO) self.exit() #self.close() #self.close() if sel == _("NTP сервер таймаут [сек] :"): #self.pressed_ok3() #self.pressed_ok() config.plugins.SetClock.timeout.save() self.session.open(MessageBox, _("Настройки сохранены !"), MessageBox.TYPE_INFO) self.exit() #self.close() #self.close() #else: #self.pressed_ok() def configPosition(self): self["key_blue"].setText("") #idx = self["config"].getCurrent()[1] #if idx == self.ST.syncDVBtime: sel = self["config"].getCurrent() and self["config"].getCurrent()[0] #sel = self["config"].getCurrent() # Do Not Work #self["config"].getCurrent()[0] # Do Not Work #if sel == ((_("Дата")) or (_("Время"))): #self["key_blue"].setText(_("Установить!")) if sel == _("Дата :"): self["key_blue"].setText(_("Установить!")) if sel == _("Время :"): self["key_blue"].setText(_("Установить!")) #if sel == ((_("HTTP хост :")) or (_("NTP сервер № 01 :")) or (_("NTP сервер таймаут [сек] :"))): #self["key_blue"].setText(_("Сохранить")) if sel == _("HTTP хост :"): self["key_blue"].setText(_("Сохранить")) if sel == _("NTP сервер № 01 :"): self["key_blue"].setText(_("Сохранить")) if sel == _("NTP сервер таймаут [сек] :"): self["key_blue"].setText(_("Сохранить")) if sel == _("Синхронизация с текущего транспондера :"): self["key_blue"].setText(_("Выбор транспондера")) #else: #self["key_blue"].setText(_("Установить!")) def keyBlue(self): if self["key_blue"].getText() == _("Выбор транспондера"): if self.servicelist is None: for (dlg,flag) in self.session.dialog_stack: if dlg.__class__.__name__ == "InfoBar": self.servicelist = dlg.servicelist break if not self.servicelist is None: self.session.execDialog(self.servicelist) #if self["key_blue"].getText() == _("Установить!"): #self.set() else: self.keyGo() class MyConsole(Console): skin = ''' ''' def __init__(self, session, title = "My Console...", cmdlist = None): Console.__init__(self, session, title, cmdlist) date_limits = [(1, 31), (1, 12), (1970, 2090)] class ConfigDate(ConfigSequence): def __init__(self, default): d = time.localtime(default) ConfigSequence.__init__(self, seperator=".", limits=date_limits, default=[d.tm_mday, d.tm_mon, d.tm_year]) time_limits = [(0, 23), (0, 59), (0, 59)] class ConfigClock(ConfigSequence): def __init__(self, default): d = time.localtime(default) ConfigSequence.__init__(self, seperator=":", limits=time_limits, default=[d.tm_hour, d.tm_min, d.tm_sec])