def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "time.first_day", self.first_day.isoformat()) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used( self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 return True else: return (date_timestamp <= last_day)
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "time_slot.first_day", self.first_day.isoformat()) ret = MTimeSlot.load_sessions( self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day), self.slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] return True else: return (date_timestamp <= last_day)
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "traffic.first_day", self.first_day.isoformat()) ret = MTraffic.load_traffic(self.ntm.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] return True else: return (date_timestamp <= last_day)
def update_main_gui(self): if self.gtkb == None: print("MMTime.update_main_gui(): Gui not builded.\n") return total_used_sec = self.used_sec + self.this_slot_sec (h, m, s) = ntmtools.sec_to_hms(total_used_sec) out_str = """<span color='#900000'>{0}h{1}'<small>{2}"</small></span>""".format( h, m, s) self.gui_main_used_sec.set_markup(out_str) time_diff = self.total_sec - total_used_sec if (time_diff < 0): time_diff = -time_diff sign = "-" else: sign = "" (h, m, s) = ntmtools.sec_to_hms(time_diff) out_str = """<span color='#009000'>{0}{1}h{2}'<small>{3}"</small></span>""".format( sign, h, m, s) self.gui_main_left_sec.set_markup(out_str) (h, m, s) = ntmtools.sec_to_hms(self.total_sec) out_str = """{0}h{1}'<small>{2}"</small>""".format(h, m, s) self.gui_main_total_sec.set_markup(out_str) total_used_sec = self.used_sec + self.this_slot_sec if self.total_sec == 0: print _("Warning! mtime.py: total sec is 0.") per = 100.0 else: per = 100.0 * total_used_sec / self.total_sec last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.now != None: period_len_total = ntmtools.timedelta2sec( ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day)) period_len_used = ntmtools.timedelta2sec( self.now - ntmtools.date_to_datetime_start(self.first_day)) if period_len_used != 0: estimate = total_used_sec * period_len_total / period_len_used (h, m, s) = ntmtools.sec_to_hms(estimate) estimate_str = '''{0}h{1}'{2}"'''.format(h, m, s) else: estimate_str = "--" else: estimate_str = "--" text = "<b>" + _("Time") + "</b>" text += """ <small>({0:.1f}%) [{1}]</small>""".format( per, estimate_str) self.gui_time_topLabel.set_markup(text) #self.gui_time_topLabel.set_markup("""<small>({0:.1f}%) [{1}]</small>""".format(per, estimate_str)) self.gui_main_period.set_markup( "<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day))
def update_main_gui(self): if self.gtkb == None: print("MTimeSlot.update_main_gui() - Gui not builded.\n") return self.gui_main_slots_used.set_text(str(self.slotsUsed)) self.gui_main_slots_left.set_text(str(self.totalSlots - self.slotsUsed)) self.gui_main_slots_total.set_text(str(self.totalSlots)) self.gui_main_thisSlot_timeUsed.set_text(ntmtools.format_time(self.tsTime)) secLeft = self.slotLength - self.tsTime self.gui_main_thisSlot_timeLeft.set_text(ntmtools.format_time(secLeft)) self.gui_main_thisSlot_timeTotal.set_text(ntmtools.format_time(self.slotLength)) totalSec = self.totalSlots * self.slotLength per = 100.0 * (self.slotsUsed * self.slotLength - secLeft) / totalSec last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.now != None: period_len_total = ntmtools.timedelta2sec(ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day)) period_len_used = ntmtools.timedelta2sec(self.now - ntmtools.date_to_datetime_start(self.first_day)) if period_len_used != 0: estimate = self.slotsUsed * period_len_total / period_len_used estimate_str = '{0}sl.'.format(int(round(estimate))) else: estimate_str = "--" else: estimate_str = "--" text = "<b>" + _("Time Slot") + "</b>" text += " <small>({0:.1f}%) [{1}]</small>".format(per, estimate_str) self.gui_timeslot_topLabel.set_markup(text) self.gui_main_period.set_markup( "<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day) )
def check_limit(self): if (not self.active): return if ((self.slotsUsed > self.totalSlots) or ( (self.slotsUsed == self.totalSlots) and (self.tsTime + 10 >= self.slotLength) )): if (self.last_state == 1) and (not self.disc_msgDialog): self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Timeslot Limit Reached') + ", {0}. ".format(self.totalSlots) + _('Disconnection is done!!') + "\n" + _("Period") + (_(": {0} to {1}")).format(self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() showDialog = True else: msg = _('Timeslot Limit Reached') + ", {0}. ".format(self.totalSlots) + '\n' + _("Period") + (_(": {0} to {1}")).format(self.first_day.isoformat(), last_day.isoformat()) showDialog = not self.dialog_showed if showDialog: dialog = gtk.Dialog( "NTM - Message", None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK) ) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.dialog_showed = True self.disc_msgDialog = False
def check_limit(self): if (not self.active): return if ( (self.used_sec + self.this_slot_sec) >= self.total_sec): if (self.last_state == 1) and (not self.disc_msgDialog): self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Time Limit Reached') + ', {0}. '.format(ntmtools.formatTime(self.total_sec)) + _('Disconnection is done!!\nPeriod: {0} to {1}').format(self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() else: msg = _('Time Limit Reached') + ', {0}.\n'.format(ntmtools.formatTime(self.total_sec)) + _('Period: {0} to {1}').format(self.first_day.isoformat(), last_day.isoformat()) dialog = gtk.Dialog( _("NTM - Message"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK) ) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_BUTTON) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.disc_msgDialog = False
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): if (not self.active): return self.now = timestamp if not self.update_period(session_start): return if (self.last_state == 0): if (conn_state == 1): # offline -> online last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.loadTimeUsed(self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 else: # last_state is online self.this_slot_sec = ntmtools.timedelta2sec(self.now - self.connStartTime) if (conn_state == 0): # online -> offline pass self.last_state = conn_state self.update_main_gui() if self.logUpdate: print('Time: {0} of {1}\n'. format( self.used_sec + self.this_slot_sec, self.total_sec) ) self.check_limit()
def check_limit(self): if (not self.active): return total = self.rec_traffic + self.tra_traffic if (total >= self.traffic_limit) and (self.last_state == 1): # print("bbb") # self.last_speed = 0 # self.update_main_gui() if not self.disc_msgDialog: self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Traffic Limit Reached') + ", {0}".format(ntmtools.formatBytes(self.traffic_limit)) + ". " + _("Disconnection is done") + "!!\n" + _("Period: {0} to {1}").format(self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() else: msg = _('Traffic Limit Reached') + ", {0}".format(ntmtools.formatBytes(self.traffic_limit)) + ".\n" + _('Period: {0} to {1}').format(self.first_day.isoformat(), last_day.isoformat()) if self.logTraffic: print(msg) dialog = gtk.Dialog( _("NTM - Message"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK) ) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_BUTTON) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.disc_msgDialog = False
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): ntmtools.dbg_msg("MTime.update_h - conn_state:{0}".format(conn_state)) if (not self.active): return self.now = timestamp if not self.update_period(session_start): return if (self.last_state == 0): if (conn_state == 1): # offline -> online self.dialog_showed = False last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used( self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 else: # last_state is online self.this_slot_sec = ntmtools.timedelta2sec(self.now - self.connStartTime) if (conn_state == 0): # online -> offline pass self.last_state = conn_state self.update_main_gui() if self.logUpdate: print('Time: {0} of {1}\n'.format( self.used_sec + self.this_slot_sec, self.total_sec)) self.check_limit()
def reload_time_used(self): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) used_sec = MTime.load_time_used( self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.used_sec = used_sec self.update_main_gui()
def reloadTraffic(self): print("reloadTraffic(self)") last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.loadTraffic(self.db_conn, self.first_day, last_day) print("Days: {0} - {1}".format(self.first_day, last_day)) print("Traffic : {0}".format(ret)) self.rec_traffic = ret[0] self.tra_traffic = ret[1] self.update_main_gui()
def reload_sessions(self): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTimeSlot.load_sessions( self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day), self.slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] self.update_main_gui()
def reload_traffic(self): ntmtools.dbg_msg("reload_traffic(self)") last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.load_traffic(self.db_conn, self.first_day, last_day) ntmtools.dbg_msg("Days: {0} - {1}".format(self.first_day, last_day)) ntmtools.dbg_msg("Traffic : {0}".format(ret)) self.rec_traffic = ret[0] self.tra_traffic = ret[1] self.update_main_gui()
def setPreferences(self, active, totalSlots, slotLength, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): update_timeslot = False if (self.active != active): self.active = active ntmtools.setDBVar(self.db_conn, "time_slot.active", ntmtools.boolToStrInt(active)) update_timeslot = True if (self.totalSlots != totalSlots): self.totalSlots = totalSlots ntmtools.setDBVar(self.db_conn, "time_slot.total_slots", str(int(totalSlots))) if (self.slotLength != slotLength): self.slotLength = slotLength ntmtools.setDBVar(self.db_conn, "time_slot.slot_length", str(int(slotLength))) update_timeslot = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.setDBVar(self.db_conn, "time_slot.auto_disconnect", ntmtools.boolToStrInt(auto_disconnect)) update_timeslot = True if (self.period_length != period_length): self.period_length = period_length ntmtools.setDBVar(self.db_conn, "time_slot.period_length", str(int(period_length))) update_timeslot = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.setDBVar(self.db_conn, "time_slot.custom_days", str(int(custom_days))) if (period_length == 0): update_timeslot = True if (self.first_day != first_day): self.first_day = first_day ntmtools.setDBVar(self.db_conn, "time_slot.first_day", first_day.isoformat()) update_timeslot = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.setDBVar(self.db_conn, "time_slot.period_autoupdate", ntmtools.boolToStrInt(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_timeslot = True if (update_timeslot): last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.loadSessions(self.db_conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def setPreferences(self, active, traffic_limit, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): #print("mtraffic.setPreferences({0},{1},{2},{3},{4})\n".format(active, traffic_limit, auto_disconnect, period_length, first_day)) update = False if (self.active != active): self.active = active ntmtools.setDBVar(self.db_conn, "traffic.active", ntmtools.boolToStrInt(active)) update = True if (self.traffic_limit != traffic_limit): self.traffic_limit = traffic_limit ntmtools.setDBVar(self.db_conn, "traffic.limit", str(int(traffic_limit))) update = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.setDBVar(self.db_conn, "traffic.auto_disconnect", ntmtools.boolToStrInt(auto_disconnect)) update_traffic = False if (self.period_length != period_length): self.period_length = period_length ntmtools.setDBVar(self.db_conn, "traffic.period_length", str(int(period_length))) update_traffic = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.setDBVar(self.db_conn, "traffic.custom_days", str(int(custom_days))) update_traffic = True if (self.first_day != first_day): self.first_day = first_day ntmtools.setDBVar(self.db_conn, "traffic.first_day", first_day.isoformat()) update_traffic = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.setDBVar(self.db_conn, "traffic.period_autoupdate", ntmtools.boolToStrInt(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_traffic = True if update_traffic: last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.loadTraffic(self.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] update = True if update: if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def set_preferences(self, active, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): self.dialog_showed = False update_time = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time.active", ntmtools.bool_to_str_int(active)) update_time = True if (self.total_sec != total_sec): self.total_sec = total_sec ntmtools.set_db_var(self.db_conn, "time.total_sec", str(total_sec)) if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time.period_length", str(int(period_length))) update_time = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time.custom_days", str(int(custom_days))) #if (period_length == 0): update_timeslot = True update_time = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time.first_day", first_day.isoformat()) update_time = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) update_time = True if (update_time): if self.period_autoupdate: self.update_period(self.now) else: last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used(self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.set_db_var(self.db_conn, "time_slot.first_day", self.first_day.isoformat()) ret = MTimeSlot.load_sessions(self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day), self.slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] return True else: return (date_timestamp <= last_day)
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.setDBVar(self.db_conn, "traffic.first_day", self.first_day.isoformat()) ret = MTraffic.loadTraffic(self.ntm.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] return True else: return (date_timestamp <= last_day)
def update_period(self, timestamp): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) date_timestamp = timestamp.date() if (date_timestamp < self.first_day): return False if self.period_autoupdate: ch = False while date_timestamp > last_day: self.first_day = last_day + datetime.timedelta(1) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ch = True if ch: ntmtools.setDBVar(self.db_conn, "time.first_day", self.first_day.isoformat()) last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.loadTimeUsed(self.ntm.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.connStartTime = timestamp self.this_slot_sec = 0 return True else: return (date_timestamp <= last_day)
def update_main_gui(self): if self.gtkb == None: print("MMTime.update_main_gui(): Gui not builded.\n") return total_used_sec = self.used_sec + self.this_slot_sec (h, m, s) = ntmtools.sec_to_hms(total_used_sec) out_str = """<span color='#900000'>{0}h{1}'<small>{2}"</small></span>""".format(h, m, s) self.gui_main_used_sec.set_markup(out_str) time_diff = self.total_sec - total_used_sec if (time_diff < 0): time_diff = -time_diff sign = "-" else: sign = "" (h, m, s) = ntmtools.sec_to_hms(time_diff) out_str = """<span color='#009000'>{0}{1}h{2}'<small>{3}"</small></span>""".format(sign, h, m, s) self.gui_main_left_sec.set_markup(out_str) (h, m, s) = ntmtools.sec_to_hms(self.total_sec) out_str = """{0}h{1}'<small>{2}"</small>""".format(h, m, s) self.gui_main_total_sec.set_markup(out_str) total_used_sec = self.used_sec + self.this_slot_sec if self.total_sec == 0: print _("Warning! mtime.py: total sec is 0.") per = 100.0 else: per = 100.0 * total_used_sec / self.total_sec last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.now != None: period_len_total = ntmtools.timedelta2sec(ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day)) period_len_used = ntmtools.timedelta2sec(self.now - ntmtools.date_to_datetime_start(self.first_day)) if period_len_used != 0: estimate = total_used_sec * period_len_total / period_len_used (h, m, s) = ntmtools.sec_to_hms(estimate) estimate_str = '''{0}h{1}'{2}"'''.format(h, m, s) else: estimate_str = "--" else: estimate_str = "--" text = "<b>" + _("Time") + "</b>" text += """ <small>({0:.1f}%) [{1}]</small>""".format(per, estimate_str) self.gui_time_topLabel.set_markup(text) #self.gui_time_topLabel.set_markup("""<small>({0:.1f}%) [{1}]</small>""".format(per, estimate_str)) self.gui_main_period.set_markup("<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day))
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): ntmtools.dbg_msg('mtraffic.update_h') #ntmtools.dbg_msg('update_h(self, {0}, {1}, {2}, {3})\n'.format(timestamp, last_rec_traffic, last_tra_traffic, conn_state)) if (not self.active): return self.now = timestamp if not self.update_period(session_start): return if (self.last_state == 0): if (conn_state == 1): self.dialog_showed = False last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.load_traffic(self.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] else: self.rec_traffic += last_rec_traffic self.tra_traffic += last_tra_traffic if (conn_state == 1): self.last_rec_traffic = last_rec_traffic self.last_tra_traffic = last_tra_traffic else: self.last_rec_traffic = 0 self.last_tra_traffic = 0 self.last_state = conn_state self.last_update_interval = update_interval self.last_speed = (self.last_rec_traffic + self.last_tra_traffic) / self.last_update_interval self.update_main_gui() if self.logTraffic: speed = (last_rec_traffic + last_tra_traffic) / update_interval / 1024 total = (self.rec_traffic + self.tra_traffic) / 1024 if speed == 0: bar = '|' else: bar = '#' * (1 + int(speed)) print('{0}\t{1}\t{2}\t{3:.3f}\t\t'.format( total / 1024, self.last_rec_traffic / 1024, self.last_tra_traffic / 1024, speed) + bar) self.check_limit()
def update_main_gui(self): if self.gtkb == None: print("MTimeSlot.update_main_gui() - Gui not builded.\n") return self.gui_main_slots_used.set_text(str(self.slotsUsed)) self.gui_main_slots_left.set_text(str(self.totalSlots - self.slotsUsed)) self.gui_main_slots_total.set_text(str(self.totalSlots)) self.gui_main_thisSlot_timeUsed.set_text( ntmtools.format_time(self.tsTime)) secLeft = self.slotLength - self.tsTime self.gui_main_thisSlot_timeLeft.set_text(ntmtools.format_time(secLeft)) self.gui_main_thisSlot_timeTotal.set_text( ntmtools.format_time(self.slotLength)) totalSec = self.totalSlots * self.slotLength per = 100.0 * (self.slotsUsed * self.slotLength - secLeft) / totalSec last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.now != None: period_len_total = ntmtools.timedelta2sec( ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day)) period_len_used = ntmtools.timedelta2sec( self.now - ntmtools.date_to_datetime_start(self.first_day)) if period_len_used != 0: estimate = self.slotsUsed * period_len_total / period_len_used estimate_str = '{0}sl.'.format(int(round(estimate))) else: estimate_str = "--" else: estimate_str = "--" text = "<b>" + _("Time Slot") + "</b>" text += " <small>({0:.1f}%) [{1}]</small>".format(per, estimate_str) self.gui_timeslot_topLabel.set_markup(text) self.gui_main_period.set_markup( "<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day))
def check_limit(self): if (not self.active): return total = self.rec_traffic + self.tra_traffic if (total >= self.traffic_limit) and (self.last_state == 1): if not self.disc_msgDialog: self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Traffic Limit Reached') + ", {0}".format( ntmtools.format_bytes(self.traffic_limit) ) + ". " + _("Disconnection is done!!") + "\n" + _( "Period: {0} to {1}").format( self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() showDialog = True else: msg = _('Traffic Limit Reached') + ", {0}".format( ntmtools.format_bytes(self.traffic_limit) ) + ".\n" + _('Period: {0} to {1}').format( self.first_day.isoformat(), last_day.isoformat()) showDialog = not self.dialog_showed if self.logTraffic: print(msg) if showDialog: dialog = gtk.Dialog( _("NTM - Message"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK)) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.dialog_showed = True self.disc_msgDialog = False
def check_limit(self): ntmtools.dbg_msg("MTime.check_limit") if (not self.active): return if ((self.used_sec + self.this_slot_sec) >= self.total_sec): if (self.last_state == 1) and (not self.disc_msgDialog): self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Time Limit Reached') + ', {0}. '.format( ntmtools.format_time(self.total_sec) ) + _( 'Disconnection is done!!\nPeriod: {0} to {1}').format( self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() showDialog = True else: msg = _('Time Limit Reached') + ', {0}.\n'.format( ntmtools.format_time( self.total_sec)) + _('Period: {0} to {1}').format( self.first_day.isoformat(), last_day.isoformat()) showDialog = not self.dialog_showed if showDialog: dialog = gtk.Dialog( _("NTM - Message"), None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK)) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.dialog_showed = True self.disc_msgDialog = False
def check_limit(self): if (not self.active): return if ((self.slotsUsed > self.totalSlots) or ((self.slotsUsed == self.totalSlots) and (self.tsTime + 10 >= self.slotLength))): if (self.last_state == 1) and (not self.disc_msgDialog): self.disc_msgDialog = True last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.auto_disconnect: msg = _('Timeslot Limit Reached') + ", {0}. ".format( self.totalSlots ) + _('Disconnection is done!!') + "\n" + _("Period") + ( _(": {0} to {1}")).format(self.first_day.isoformat(), last_day.isoformat()) if self.disconnect_handler != None: self.disconnect_handler() showDialog = True else: msg = _('Timeslot Limit Reached') + ", {0}. ".format( self.totalSlots) + '\n' + _("Period") + (_( ": {0} to {1}")).format(self.first_day.isoformat(), last_day.isoformat()) showDialog = not self.dialog_showed if showDialog: dialog = gtk.Dialog( "NTM - Message", None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK)) icon = gtk.Window().render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG) dialog.set_icon(icon) label = gtk.Label(msg) label.set_padding(8, 8) dialog.vbox.pack_start(label) dialog.show_all() result = dialog.run() dialog.destroy() self.dialog_showed = True self.disc_msgDialog = False
def update_h(self, timestamp, session_start, update_interval, last_rec_traffic, last_tra_traffic, conn_state): #print('update_h(self, {0}, {1}, {2}, {3})\n'.format(timestamp, last_rec_traffic, last_tra_traffic, conn_state)) if (not self.active): return self.now = timestamp if not self.update_period(session_start): return if (self.last_state == 0): if (conn_state == 1): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.loadTraffic(self.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] else: self.rec_traffic += last_rec_traffic self.tra_traffic += last_tra_traffic if (conn_state == 1): self.last_rec_traffic = last_rec_traffic self.last_tra_traffic = last_tra_traffic else: self.last_rec_traffic = 0 self.last_tra_traffic = 0 self.last_state = conn_state self.last_update_interval = update_interval self.last_speed = (self.last_rec_traffic + self.last_tra_traffic) / self.last_update_interval self.update_main_gui() if self.logTraffic: speed = (last_rec_traffic + last_tra_traffic) / update_interval / 1024 total = (self.rec_traffic + self.tra_traffic) / 1024 if speed == 0: bar = '|' else: bar = '#' * (1 + int(speed)) print( '{0}\t{1}\t{2}\t{3:.3f}\t\t'. format( total/1024, self.last_rec_traffic / 1024, self.last_tra_traffic / 1024, speed) + bar ) self.check_limit()
def update_main_gui(self): # print("mtraffic.update_main_gui(self)") if self.gtkb == None: print("MTraffic.update_main_gui(*): Gui not builded.\n") return mbyte = (1024.0*1024.0) traf_tot = (self.rec_traffic + self.tra_traffic) per = 100.0 * traf_tot / self.traffic_limit self.gui_main_left.set_text("{0:.3f}".format((self.traffic_limit - traf_tot)/mbyte)) self.gui_main_used.set_text("{0:.3f}".format(traf_tot/mbyte)) self.gui_main_in.set_text("{0:.3f}".format(self.rec_traffic / mbyte)) self.gui_main_out.set_text("{0:.3f}".format(self.tra_traffic / mbyte)) self.gui_main_total.set_text("{0:.0f}".format(self.traffic_limit/mbyte)) if self.last_speed != None: while self.last_speed > self.bwu_max_speed: self.bwu_max_speed = self.bwu_max_speed + 10.0 self.gui_main_bwu.speed = self.last_speed self.gui_main_bwu.max_speed = self.bwu_max_speed self.gui_main_bwu.queue_draw() last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.now != None: period_len_total = ntmtools.timedelta2sec(ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day)) period_len_used = ntmtools.timedelta2sec(self.now - ntmtools.date_to_datetime_start(self.first_day)) if period_len_used != 0: estimate_str = ntmtools.formatBytes(traf_tot * period_len_total / period_len_used) else: estimate_str = "--" else: estimate_str = "--" self.gui_main_per.set_markup("<small>({0:.1f}%) [{1}]</small>".format(per, estimate_str)) self.gui_main_period.set_markup("<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day))
def set_preferences(self, active, totalSlots, slotLength, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate ): self.dialog_showed = False update_timeslot = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time_slot.active", ntmtools.bool_to_str_int(active)) update_timeslot = True if (self.totalSlots != totalSlots): self.totalSlots = totalSlots ntmtools.set_db_var(self.db_conn, "time_slot.total_slots", str(int(totalSlots))) if (self.slotLength != slotLength): self.slotLength = slotLength ntmtools.set_db_var(self.db_conn, "time_slot.slot_length", str(int(slotLength))) update_timeslot = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time_slot.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) update_timeslot = True if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time_slot.period_length", str(int(period_length))) update_timeslot = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time_slot.custom_days", str(int(custom_days))) if (period_length == 0): update_timeslot = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time_slot.first_day", first_day.isoformat()) update_timeslot = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time_slot.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_timeslot = True if (update_timeslot): last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions(self.db_conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] if (self.alert_active != alert_active): self.alert_active = alert_active ntmtools.set_db_var(self.db_conn, "time_slot.alert.active", ntmtools.bool_to_str_int(alert_active)) if (self.alert_predate != alert_predate): self.alert_predate = alert_predate ntmtools.set_db_var(self.db_conn, "time_slot.alert.predate", str(int(alert_predate))) if self.notify_ok: self.notifyalert_predate = pynotify.Notification (_("NTM - Time Slot"), "{0} ".format(self.alert_predate) + _("seconds before timeslot end!")) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def reload_sessions(self): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTimeSlot.load_sessions(self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day), self.slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] self.update_main_gui()
def make_from_db(ntm): conn = ntm.db_conn val_str = ntmtools.read_db_var(conn, "time_slot.active") try: active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") active = True ntmtools.set_db_var(conn, "time_slot.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.total_slots") try: total_slots = ntmtools.str_to_int(val_str, 200) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.total_slots' " + _("or is not stored. Default value") + " '200'") total_slots = 200 ntmtools.set_db_var(conn, "time_slot.total_slots", total_slots) val_str = ntmtools.read_db_var(conn, "time_slot.slot_length") try: slot_length = ntmtools.str_to_int(val_str, 15 * 60) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.slot_length' " + _("or is not stored. Default value") + " 15'") slot_length = 15 * 60 ntmtools.set_db_var(conn, "time_slot.slot_length", slot_length) val_str = ntmtools.read_db_var(conn, "time_slot.auto_disconnect") try: auto_disconnect = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.auto_disconnect' " + _("or is not stored. Default value") + " '" + _("True") + "'") auto_disconnect = True ntmtools.set_db_var(conn, "time_slot.auto_disconnect", "1") val_str = ntmtools.read_db_var(conn, "time_slot.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.set_db_var(conn, "time_slot.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "time_slot.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.custom_days' " + _("or is not stored. Default value") + " 30") custom_days = 30 ntmtools.set_db_var(conn, "time_slot.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "time_slot.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "time_slot.first_day", first_day) val_str = ntmtools.read_db_var(conn, "time_slot.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "time_slot.period_autoupdate", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.active") try: alert_active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.alert.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") alert_active = True ntmtools.set_db_var(conn, "time_slot.alert.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.predate") try: alert_predate = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time_slot.alert.predate' " + _("or is not stored. Default value") + ' 30"') alert_predate = 30 ntmtools.set_db_var(conn, "time_slot.alert.predate", str(int(alert_predate))) last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions( conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slot_length) slots_used = ret[0] ts_time = ret[1] return MTimeSlot(ntm, active, slots_used, total_slots, ts_time, slot_length, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate)
def make_from_db(ntm): conn = ntm.db_conn res = ntmtools.read_db_var( conn, "traffic.active") # 0:Disable; Other:Enable; try: active = (int(res) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.active' " + _("or is not stored. Default value") + " '1'") ntmtools.set_db_var(conn, "traffic.active", "1") active = True res = ntmtools.read_db_var(conn, "traffic.limit") # bytes try: traffic_limit = float(res) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.limit' " + _("or is not stored. Default value") + " '98MB'") ntmtools.set_db_var(conn, "traffic.limit", str(98 * 1024 * 1024)) traffic_limit = 98 * 1024 * 1024 res = ntmtools.read_db_var( conn, "traffic.auto_disconnect") # 0:Disable; Other:Enable; try: auto_disconnect = (int(res) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.auto_disconnect' " + _("or is not stored. Default value") + " '1'") ntmtools.set_db_var(conn, "traffic.auto_disconnect", "1") auto_disconnect = True val_str = ntmtools.read_db_var(conn, "traffic.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.period_length' " + _("or is not stored. Default value") + " '" + _("Daily") + "'") period_length = 0 # 0 -> Daily ntmtools.set_db_var(conn, "traffic.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "traffic.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.custom_days' " + _("or is not stored. Default value") + " 30") custom_days = 30 ntmtools.set_db_var(conn, "traffic.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "traffic.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "traffic.first_day", first_day) val_str = ntmtools.read_db_var(conn, "traffic.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'traffic.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "traffic.period_autoupdate", "1") last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTraffic.load_traffic(conn, first_day, last_day) return MTraffic(ntm, active, ret[0], ret[1], traffic_limit, auto_disconnect, period_length, custom_days, first_day, period_autoupdate)
def update_main_gui(self): ntmtools.dbg_msg("MTraffic.update_main_gui") if self.gtkb == None: ntmtools.dbg_msg("MTraffic.update_main_gui : Gui not builded.\n", 100) return mbyte = (1024.0 * 1024.0) traf_tot = (self.rec_traffic + self.tra_traffic) per = 100.0 * traf_tot / self.traffic_limit self.gui_main_left.set_text("{0:.3f}".format( (self.traffic_limit - traf_tot) / mbyte)) self.gui_main_left.set_tooltip_text("{0} bytes".format( int(self.traffic_limit - traf_tot))) self.gui_main_used.set_text("{0:.3f}".format(traf_tot / mbyte)) self.gui_main_used.set_tooltip_text("{0} bytes".format(int(traf_tot))) self.gui_main_in.set_text("{0:.3f}".format(self.rec_traffic / mbyte)) self.gui_main_in.set_tooltip_text("{0} bytes".format( int(self.rec_traffic))) self.gui_main_out.set_text("{0:.3f}".format(self.tra_traffic / mbyte)) self.gui_main_out.set_tooltip_text("{0} bytes".format( int(self.tra_traffic))) self.gui_main_total.set_text("{0:.0f}".format(self.traffic_limit / mbyte)) self.gui_main_total.set_tooltip_text("{0} bytes".format( int(self.traffic_limit))) if self.last_speed != None: while self.last_speed > self.bwu_max_speed: self.bwu_max_speed = self.bwu_max_speed + 10.0 self.gui_main_bwu.speed = self.last_speed self.gui_main_bwu.max_speed = self.bwu_max_speed self.gui_main_bwu.queue_draw() last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) if self.now != None: period_len_total = ntmtools.timedelta2sec( ntmtools.date_to_datetime_end(last_day) - ntmtools.date_to_datetime_start(self.first_day)) period_len_used = ntmtools.timedelta2sec( self.now - ntmtools.date_to_datetime_start(self.first_day)) if period_len_used != 0: estimate_str = ntmtools.format_bytes( traf_tot * period_len_total / period_len_used) else: estimate_str = "--" else: estimate_str = "--" if self.gui_expander.get_expanded(): text = "<b>{0}</b> <small>({1:.1f}%) [{2}]</small>".format( _("Traffic"), per, estimate_str) else: text = "<b>{0}</b> {1}<small> ({2:.1f}%)</small>".format( _("Traffic"), ntmtools.format_bytes(traf_tot), per) self.gui_main_top_label.set_markup(text) self.gui_main_period.set_markup( "<small><small>" + _("Period") + ": {0} - {1}</small></small>".format(self.first_day, last_day))
def makeFromDb(ntm): conn = ntm.db_conn res = ntmtools.readDBVar(conn, "traffic.active") # 0:Disable; Other:Enable; try: active = (int(res) != 0) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'traffic.active' " + _("or is not stored. Default value") + " '1'") ntmtools.setDBVar(conn, "traffic.active", "1") active = True res = ntmtools.readDBVar(conn, "traffic.limit") # bytes try: traffic_limit = float(res) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'traffic.limit' " + _("or is not stored. Default value") + " '98MB'") ntmtools.setDBVar(conn, "traffic.limit", str(98*1024*1024)) traffic_limit = 98*1024*1024 res = ntmtools.readDBVar(conn, "traffic.auto_disconnect") # 0:Disable; Other:Enable; try: auto_disconnect = (int(res) != 0) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'traffic.auto_disconnect' " + _("or is not stored. Default value") + " '1'") ntmtools.setDBVar(conn, "traffic.auto_disconnect", "1") auto_disconnect = True val_str = ntmtools.readDBVar(conn, "traffic.period_length") try: period_length = int(val_str) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'traffic.period_length' " + _("or is not stored. Default value") + " '" + _("Daily") + "'") period_length = 0 # 0 -> Daily ntmtools.setDBVar(conn, "traffic.period_length", str(int(period_length))) val_str = ntmtools.readDBVar(conn, "traffic.custom_days") try: custom_days = int(val_str) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'traffic.custom_days' " + _("or is not stored. Default value") + " 30") custom_days = 30 ntmtools.setDBVar(conn, "traffic.custom_days", str(int(custom_days))) val_str = ntmtools.readDBVar(conn, "traffic.first_day") try: first_day = ntmtools.strToDate(val_str) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'traffic.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.setDBVar(conn, "traffic.first_day", first_day) val_str = ntmtools.readDBVar(conn, "traffic.period_autoupdate") try: period_autoupdate = (ntmtools.strToInt(val_str, 1) != 0) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'traffic.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.setDBVar(conn, "traffic.period_autoupdate", "1") last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTraffic.loadTraffic(conn, first_day, last_day) return MTraffic(ntm, active, ret[0], ret[1], traffic_limit, auto_disconnect, period_length, custom_days, first_day, period_autoupdate)
def makeFromDb(ntm): conn = ntm.db_conn val_str = ntmtools.readDBVar(conn, "time.active") try: active = (ntmtools.strToInt(val_str, 1) != 0) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'time.active' " + _("or is not stored. Default value") + " 'True'") active = True ntmtools.setDBVar(conn, "time.active", "1") val_str = ntmtools.readDBVar(conn, "time.total_sec") try: total_sec = ntmtools.strToInt(val_str, 30*60*60) # 30h except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'time.total_sec' " + _("or is not stored. Default value") + " '30h'") total_sec = 30*60*60 ntmtools.setDBVar(conn, "time.total_sec", total_sec) val_str = ntmtools.readDBVar(conn, "time.auto_disconnect") try: auto_disconnect = (ntmtools.strToInt(val_str, 1) != 0) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'time.auto_disconnect' " + _("or is not stored. Default value") + " '" +_("True") + "'") auto_disconnect = True ntmtools.setDBVar(conn, "time.auto_disconnect", "1") val_str = ntmtools.readDBVar(conn, "time.period_length") try: period_length = int(val_str) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'time.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.setDBVar(conn, "time.period_length", str(int(period_length))) val_str = ntmtools.readDBVar(conn, "time.custom_days") try: custom_days = int(val_str) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'time.custom_days' " + _("or is not stored. Default value") + " '30'") custom_days = 30 ntmtools.setDBVar(conn, "time.custom_days", str(int(custom_days))) val_str = ntmtools.readDBVar(conn, "time.first_day") try: first_day = ntmtools.strToDate(val_str) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'time.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.setDBVar(conn, "time.first_day", first_day) val_str = ntmtools.readDBVar(conn, "time.period_autoupdate") try: period_autoupdate = (ntmtools.strToInt(val_str, 1) != 0) except: ntmtools.dbgMsg(_("Wrong value for the param") + " 'time.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.setDBVar(conn, "time.period_autoupdate", "1") last_day = ntmtools.get_last_day(first_day, period_length, custom_days) used_sec = MTime.loadTimeUsed(conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day)) return MTime(ntm, active, used_sec, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate)
def make_from_db(ntm): conn = ntm.db_conn val_str = ntmtools.read_db_var(conn, "time.active") try: active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.active' " + _("or is not stored. Default value") + " 'True'") active = True ntmtools.set_db_var(conn, "time.active", "1") val_str = ntmtools.read_db_var(conn, "time.total_sec") try: total_sec = ntmtools.str_to_int(val_str, 30 * 60 * 60) # 30h except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.total_sec' " + _("or is not stored. Default value") + " '30h'") total_sec = 30 * 60 * 60 ntmtools.set_db_var(conn, "time.total_sec", total_sec) val_str = ntmtools.read_db_var(conn, "time.auto_disconnect") try: auto_disconnect = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.auto_disconnect' " + _("or is not stored. Default value") + " '" + _("True") + "'") auto_disconnect = True ntmtools.set_db_var(conn, "time.auto_disconnect", "1") val_str = ntmtools.read_db_var(conn, "time.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.set_db_var(conn, "time.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "time.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.custom_days' " + _("or is not stored. Default value") + " '30'") custom_days = 30 ntmtools.set_db_var(conn, "time.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "time.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "time.first_day", first_day) val_str = ntmtools.read_db_var(conn, "time.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg( _("Wrong value for the param") + " 'time.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "time.period_autoupdate", "1") last_day = ntmtools.get_last_day(first_day, period_length, custom_days) used_sec = MTime.load_time_used( conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day)) return MTime(ntm, active, used_sec, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate)
def set_preferences(self, active, totalSlots, slotLength, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate): self.dialog_showed = False update_timeslot = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time_slot.active", ntmtools.bool_to_str_int(active)) update_timeslot = True if (self.totalSlots != totalSlots): self.totalSlots = totalSlots ntmtools.set_db_var(self.db_conn, "time_slot.total_slots", str(int(totalSlots))) if (self.slotLength != slotLength): self.slotLength = slotLength ntmtools.set_db_var(self.db_conn, "time_slot.slot_length", str(int(slotLength))) update_timeslot = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time_slot.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) update_timeslot = True if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time_slot.period_length", str(int(period_length))) update_timeslot = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time_slot.custom_days", str(int(custom_days))) if (period_length == 0): update_timeslot = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time_slot.first_day", first_day.isoformat()) update_timeslot = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time_slot.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_timeslot = True if (update_timeslot): last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions( self.db_conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slotLength) self.slotsUsed = ret[0] self.tsTime = ret[1] if (self.alert_active != alert_active): self.alert_active = alert_active ntmtools.set_db_var(self.db_conn, "time_slot.alert.active", ntmtools.bool_to_str_int(alert_active)) if (self.alert_predate != alert_predate): self.alert_predate = alert_predate ntmtools.set_db_var(self.db_conn, "time_slot.alert.predate", str(int(alert_predate))) if self.notify_ok: self.notifyalert_predate = pynotify.Notification( _("NTM - Time Slot"), "{0} ".format(self.alert_predate) + _("seconds before timeslot end!")) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def reloadTimeUsed(self): last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) used_sec = MTime.loadTimeUsed(self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) self.used_sec = used_sec self.update_main_gui()
def set_preferences(self, active, traffic_limit, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): #ntmtools.dbg_msg("mtraffic.setPreferences({0},{1},{2},{3},{4})\n".format(active, traffic_limit, auto_disconnect, period_length, first_day)) update = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "traffic.active", ntmtools.bool_to_str_int(active)) update = True if (self.traffic_limit != traffic_limit): self.traffic_limit = traffic_limit ntmtools.set_db_var(self.db_conn, "traffic.limit", str(int(traffic_limit))) update = True if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "traffic.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) update_traffic = False if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "traffic.period_length", str(int(period_length))) update_traffic = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "traffic.custom_days", str(int(custom_days))) update_traffic = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "traffic.first_day", first_day.isoformat()) update_traffic = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "traffic.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) if self.period_autoupdate: self.update_period(self.now) update_traffic = True if update_traffic: last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) ret = MTraffic.load_traffic(self.db_conn, self.first_day, last_day) self.rec_traffic = ret[0] self.tra_traffic = ret[1] update = True if update: if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()
def make_from_db(ntm): conn = ntm.db_conn val_str = ntmtools.read_db_var(conn, "time_slot.active") try: active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") active = True ntmtools.set_db_var(conn, "time_slot.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.total_slots") try: total_slots = ntmtools.str_to_int(val_str, 200) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.total_slots' " + _("or is not stored. Default value") + " '200'") total_slots = 200 ntmtools.set_db_var(conn, "time_slot.total_slots", total_slots) val_str = ntmtools.read_db_var(conn, "time_slot.slot_length") try: slot_length = ntmtools.str_to_int(val_str, 15*60) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.slot_length' " + _("or is not stored. Default value") + " 15'") slot_length = 15*60 ntmtools.set_db_var(conn, "time_slot.slot_length", slot_length) val_str = ntmtools.read_db_var(conn, "time_slot.auto_disconnect") try: auto_disconnect = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.auto_disconnect' " + _("or is not stored. Default value") + " '" + _("True") + "'") auto_disconnect = True ntmtools.set_db_var(conn, "time_slot.auto_disconnect", "1") val_str = ntmtools.read_db_var(conn, "time_slot.period_length") try: period_length = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.period_length' " + _("or is not stored. Default value") + " '" + _("Month") + "'") period_length = 3 # 3 -> Month ntmtools.set_db_var(conn, "time_slot.period_length", str(int(period_length))) val_str = ntmtools.read_db_var(conn, "time_slot.custom_days") try: custom_days = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.custom_days' " + _("or is not stored. Default value") + " 30") custom_days = 30 ntmtools.set_db_var(conn, "time_slot.custom_days", str(int(custom_days))) val_str = ntmtools.read_db_var(conn, "time_slot.first_day") try: first_day = ntmtools.str_to_date(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.first_day' " + _("or is not stored. Default value") + " '2009-10-01'") first_day = datetime.date(2009, 10, 01) ntmtools.set_db_var(conn, "time_slot.first_day", first_day) val_str = ntmtools.read_db_var(conn, "time_slot.period_autoupdate") try: period_autoupdate = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.period_autoupdate' " + _("or is not stored. Default value") + " '" + _("True") + "'") period_autoupdate = True ntmtools.set_db_var(conn, "time_slot.period_autoupdate", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.active") try: alert_active = (ntmtools.str_to_int(val_str, 1) != 0) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.alert.active' " + _("or is not stored. Default value") + " '" + _("True") + "'") alert_active = True ntmtools.set_db_var(conn, "time_slot.alert.active", "1") val_str = ntmtools.read_db_var(conn, "time_slot.alert.predate") try: alert_predate = int(val_str) except: ntmtools.dbg_msg(_("Wrong value for the param") + " 'time_slot.alert.predate' " + _("or is not stored. Default value") + ' 30"') alert_predate = 30 ntmtools.set_db_var(conn, "time_slot.alert.predate", str(int(alert_predate))) last_day = ntmtools.get_last_day(first_day, period_length, custom_days) ret = MTimeSlot.load_sessions(conn, ntmtools.date_to_datetime_start(first_day), ntmtools.date_to_datetime_end(last_day), slot_length) slots_used = ret[0] ts_time = ret[1] return MTimeSlot(ntm, active, slots_used, total_slots, ts_time, slot_length, auto_disconnect, period_length, custom_days, first_day, period_autoupdate, alert_active, alert_predate)
def set_preferences(self, active, total_sec, auto_disconnect, period_length, custom_days, first_day, period_autoupdate): self.dialog_showed = False update_time = False if (self.active != active): self.active = active ntmtools.set_db_var(self.db_conn, "time.active", ntmtools.bool_to_str_int(active)) update_time = True if (self.total_sec != total_sec): self.total_sec = total_sec ntmtools.set_db_var(self.db_conn, "time.total_sec", str(total_sec)) if (self.auto_disconnect != auto_disconnect): self.auto_disconnect = auto_disconnect ntmtools.set_db_var(self.db_conn, "time.auto_disconnect", ntmtools.bool_to_str_int(auto_disconnect)) if (self.period_length != period_length): self.period_length = period_length ntmtools.set_db_var(self.db_conn, "time.period_length", str(int(period_length))) update_time = True if (self.custom_days != custom_days): self.custom_days = custom_days ntmtools.set_db_var(self.db_conn, "time.custom_days", str(int(custom_days))) #if (period_length == 0): update_timeslot = True update_time = True if (self.first_day != first_day): self.first_day = first_day ntmtools.set_db_var(self.db_conn, "time.first_day", first_day.isoformat()) update_time = True if (self.period_autoupdate != period_autoupdate): self.period_autoupdate = period_autoupdate ntmtools.set_db_var(self.db_conn, "time.period_autoupdate", ntmtools.bool_to_str_int(period_autoupdate)) update_time = True if (update_time): if self.period_autoupdate: self.update_period(self.now) else: last_day = ntmtools.get_last_day(self.first_day, self.period_length, self.custom_days) self.used_sec = MTime.load_time_used( self.db_conn, ntmtools.date_to_datetime_start(self.first_day), ntmtools.date_to_datetime_end(last_day)) if self.active: self.gui_main.show() self.update_main_gui() self.check_limit() else: self.gui_main.hide()