def tick(self, _): """The application ticks once per second for updating clock needles and emitting snowflakes""" self.day = datetime.today().weekday() now = datetime.now() # set day or night mode now_time_string = "%d:%d" % (now.hour, now.minute) now_time = datetime.strptime(now_time_string, '%H:%M') start_time = datetime.strptime( self.config.get( 'night', 'start_time'), '%H:%M') stop_time = datetime.strptime( self.config.get( 'night', 'stop_time'), '%H:%M') if now_time >= stop_time and now_time <= start_time: if self.mode != "day": self.set_mode("day") else: if self.mode != "night": self.set_mode("night") # calculate angles of clock needles, with precise hour hour = now.hour if hour > 12: hour = hour - 12 self.hours_needle_angle = 360 - (0.5 * (60 * hour + now.minute)) self.minutes_needle_angle = 360 - (6 * now.minute) self.seconds_needle_angle = 360 - (6 * now.second) # set approx hour minute = now.minute puoli = False if minute > 15: hour = hour + 1 if minute < 45: puoli = True if hour > 12: hour = hour - 12 if hour == 0: hour = 12 self.hour = hour self.puoli = puoli # emit a snowflake for each tick if self.snowflakes is None: self.snowflakes = Snowflakes() self.snowflakes.init(self.layout) self.snowflakes.emit()
class ClockApp(App): #pylint:disable=too-many-public-methods,too-many-instance-attributes """Defines the main class for Ensikello""" use_kivy_settings = False layout = None snowflakes = None audioplayer = None clocksettings = None backgrounds = [] hours_needle_angle = NumericProperty(0) minutes_needle_angle = NumericProperty(0) seconds_needle_angle = NumericProperty(0) microseconds_needle_angle = NumericProperty(0) day = NumericProperty(0) mode = "not_set" # shall be "day" or "night" hour = None puoli = False def say_day(self, *args): """Plays queued wav files to indicate the current day of the week""" if self.layout.ids.week.collide_point(*args[1].pos): #pylint:disable=star-args if self.config.get(self.mode, 'audio_enabled') == '0' or \ self.audioplayer.playing is not None: return self.audioplayer.reset() self.audioplayer.queued_play("%s/audio/tanaan_on.wav" % RESOURCES) self.audioplayer.queued_play( "%s/audio/d%d.wav" % (RESOURCES, self.day)) def say_time(self, *args): """Plays queued wav files to indicate time (nearest 30 minutes)""" if self.layout.ids.clock.collide_point(*args[1].pos): #pylint:disable=star-args if self.config.get(self.mode, 'audio_enabled') == '0' or \ self.audioplayer.playing is not None: return self.audioplayer.reset() self.audioplayer.queued_play("%s/audio/kello_on.wav" % RESOURCES) if self.puoli: self.audioplayer.queued_play("%s/audio/puoli.wav" % RESOURCES) self.audioplayer.queued_play( "%s/audio/%d.wav" % (RESOURCES, self.hour)) return True def set_mode(self, mode): """Sets the mode of the application to night or day""" self.mode = mode self.layout.ids.bg.source = "%s/background/%s" % ( RESOURCES, self.config.get(mode, 'background')) self.layout.ids.moon.source = "%s/clock/%s" % (RESOURCES, "moon2.png") self.layout.ids.frame.source = "%s/clock/%sclock_bg.png" % ( RESOURCES, mode) self.layout.ids.hour.source = "%s/clock/%sclock_hour_needle.png" % ( RESOURCES, mode) self.layout.ids.minute.source = "%s/clock/%sclock_minute_needle.png" % ( RESOURCES, mode) self.layout.ids.second.source = "%s/clock/%sclock_second_needle.png" % ( RESOURCES, mode) self.update_layout() def update_layout(self): """Updates layout based on the current mode""" add_clock = remove_clock = add_moon = remove_moon = False clock_in_layout = self.layout.ids.clock.parent == self.layout if clock_in_layout == False and self.config.get( self.mode, 'clock_visible') == '1': add_clock = True elif clock_in_layout and self.config.get( self.mode, 'clock_visible') == '0': remove_clock = True moon_in_layout = self.layout.ids.moon.parent == self.layout if self.mode == "day" and moon_in_layout: remove_moon = True elif self.mode == "night": if moon_in_layout == False and self.config.get( self.mode, 'moon_visible') == '1': add_moon = True elif moon_in_layout and self.config.get( self.mode, 'moon_visible') == '0': remove_moon = True if add_clock: self.layout.add_widget(self.layout.ids.clock) if remove_clock: self.layout.remove_widget(self.layout.ids.clock) if add_moon: self.layout.add_widget(self.layout.ids.moon) if remove_moon: self.layout.remove_widget(self.layout.ids.moon) def tick(self, _): """The application ticks once per second for updating clock needles and emitting snowflakes""" self.day = datetime.today().weekday() now = datetime.now() # set day or night mode now_time_string = "%d:%d" % (now.hour, now.minute) now_time = datetime.strptime(now_time_string, '%H:%M') start_time = datetime.strptime( self.config.get( 'night', 'start_time'), '%H:%M') stop_time = datetime.strptime( self.config.get( 'night', 'stop_time'), '%H:%M') if now_time >= stop_time and now_time <= start_time: if self.mode != "day": self.set_mode("day") else: if self.mode != "night": self.set_mode("night") # calculate angles of clock needles, with precise hour hour = now.hour if hour > 12: hour = hour - 12 self.hours_needle_angle = 360 - (0.5 * (60 * hour + now.minute)) self.minutes_needle_angle = 360 - (6 * now.minute) self.seconds_needle_angle = 360 - (6 * now.second) # set approx hour minute = now.minute puoli = False if minute > 15: hour = hour + 1 if minute < 45: puoli = True if hour > 12: hour = hour - 12 if hour == 0: hour = 12 self.hour = hour self.puoli = puoli # emit a snowflake for each tick if self.snowflakes is None: self.snowflakes = Snowflakes() self.snowflakes.init(self.layout) self.snowflakes.emit() def build_settings(self, settings): """Called when creating settings. Implemented in clocksettings.py""" clocksettings.build_settings(settings, self.config, self.backgrounds) def on_config_change(self, config, section, key, value): """Called when settings have changed. Updates layout if needed""" if config is self.config: token = (section, key) if token == (self.mode, 'clock_visible'): self.update_layout() elif token == (self.mode, 'moon_visible'): self.update_layout() elif token == (self.mode, 'background'): self.layout.ids.bg.source = "%s/background/%s" % ( RESOURCES, self.config.get(self.mode, 'background')) self.update_layout() def build_config(self, config): """Called to create a config file. Implemented in clocksettings.py""" clocksettings.build_config(config) def update_backgrounds(self): """Populates background file list again from file system""" bgpath = "%s/background" % RESOURCES self.backgrounds = [] for _, _, files in os.walk(bgpath): for bgfile in files: self.backgrounds.append(bgfile) self.backgrounds = sorted(self.backgrounds) def build(self): """Called in application start""" self.update_backgrounds() # initialize objects self.layout = ClockLayout() self.layout.ids.drag_area.app = self self.audioplayer = AudioPlayer() # start ticking Clock.schedule_interval(self.tick, 1 / 1.) return self.layout