def __init__(self, config: Configurations): self.window = Window7in5('resources') self.events = GoogleCalendarEvents(config.google_credentials) for calendar_id in config.selected_calendars: self.events.select_calendar(calendar_id) self.weather = OpenWeatherMapModel(config.owm_token, config.city_id) self.weather.temperature_unit = config.units # Avoid importing non existing package (RPi.GPIO etc.) if config.is_debug: from view.hardware.mock import EPD, ButtonAndLed else: EPD = __import__('view.hardware.epd7in5', fromlist=['EPD']).EPD ButtonAndLed = __import__('view.hardware.button_and_led', fromlist=['ButtonAndLed']).ButtonAndLed self.epd = EPD(config) self.button_and_led = ButtonAndLed(self) self.updating_flag = False self.hour_counter = 0 if config.show_borders: self.window.show_widget_border(True)
class Controller: def __init__(self, config: Configurations): self.window = Window7in5('resources') self.events = GoogleCalendarEvents(config.google_credentials) for calendar_id in config.selected_calendars: self.events.select_calendar(calendar_id) self.weather = OpenWeatherMapModel(config.owm_token, config.city_id) self.weather.temperature_unit = config.units # Avoid importing non existing package (RPi.GPIO etc.) if config.is_debug: from view.hardware.mock import EPD, ButtonAndLed else: EPD = __import__('view.hardware.epd7in5', fromlist=['EPD']).EPD ButtonAndLed = __import__('view.hardware.button_and_led', fromlist=['ButtonAndLed']).ButtonAndLed self.epd = EPD(config) self.button_and_led = ButtonAndLed(self) self.updating_flag = False self.hour_counter = 0 def update_calendar(self): self.window.calender.clear_selection() self.window.calender.set_month(get_month_str()) days, selection = get_calendar_days() self.window.calender.set_dates(days) self.window.calender.set_select_date(selection[0], selection[1], True) def update_weather(self): weather_id, low, high, humidity = self.weather.get_current_weather() self.window.weather.set_weather(weather_id) self.window.weather.set_temp_range(low, high) self.window.weather.set_humidity(humidity) forecasts = self.weather.get_daily_forecast() forecasts = list(map(lambda forecast: forecast[:-1], forecasts)) self.window.weather.set_forecast(forecasts) def update_events(self): events = self.events.get_sorted_events() self.window.events.set_events(events) def _update_all(self): self.update_events() self.update_weather() self.update_calendar() def _render_and_display(self): image = self.window.render() self.epd.init() self.epd.display(self.epd.get_buffer(image)) self.epd.sleep() def update_and_redraw(self): if self.updating_flag: return self.updating_flag = True self.button_and_led.led_on() self._update_all() self._render_and_display() self.button_and_led.led_off() self.updating_flag = False def run(self): try: while True: if self.hour_counter == 24: self.hour_counter = 0 self.epd.init() self.epd.clear(0xFE) self.update_and_redraw() logger.info('Periodic update of the screen') time.sleep(3600) self.hour_counter += 1 except KeyboardInterrupt: logger.info('Clearing screen on exit') self.epd.init() self.epd.clear(0xFE) self.epd.sleep() self.button_and_led.exit()
def load_or_create_config(): parser = argparse.ArgumentParser('EInk Smart Calendar') parser.add_argument('-c', '--config', type=str, help='Path for the config file') parser.add_argument('-d', '--debug', type=str, help='Path for generating debug images') parser.add_argument('-s', '--show_border', action='store_true', default=False, help='Path for generating debug images') args = parser.parse_args() if args.config is not None and os.path.isfile(args.config): config = configparser.ConfigParser() with open(args.config, 'r') as file: config_str = file.read() config.read_string(config_str) config_obj = Configurations(config) else: config_obj = Configurations(configparser.ConfigParser()) config_obj.owm_token = input('Paste in the Open Weather Map Token: \n') print('To generate Google API tokens, see the video' + ' https://www.youtube.com/watch?v=hfWe1gPCnzc') config_obj.google_token = input('Paste in the Access Token: \n') config_obj.google_refresh_token = input( 'Paste in the Refresh Token: \n') config_obj.google_client_id = input('Paste in the Client ID: \n') config_obj.google_client_secrete = input( 'Paste in the Client Secrete: \n') print('Retrieving calendars ...') credentials = config_obj.google_credentials google_calendar = GoogleCalendarEvents(credentials) list_calendars = google_calendar.list_calendars() for i in range(len(list_calendars)): print('%d) %s' % (i, list_calendars[i][1])) selected_calendars = [] prompt = ('Select one or more calendars by listing out' + ' their index. Separated by \',\'\n') while True: selections = input(prompt) selections = map(lambda s: int(s.strip()), selections.split(',')) for index in selections: if index >= len(list_calendars): prompt = 'Invalid index. Try again \n' break selected_calendars.append(list_calendars[index][0]) else: break selected_calendars = [] for selected_calendar in selected_calendars: config_obj.add_selected_calendars(selected_calendar) city_id = int(input('Paste in the city id for retrieving weather.' + ' The city id could be found on Open Weather' + ' Map website: \n')) config_obj.city_id = city_id prompt = ('Now select the unit for temperature.' + ' Either "fahrenheit" or "celsius" \n') while True: units = input(prompt) if units != 'fahrenheit' and units != 'celsius': prompt = 'Invalid selection. Try again' else: break config_obj.units = units saving_path = input('Now provide a path for saving the config \n') config_obj.save(saving_path) abs_path = os.path.abspath(saving_path) print(('Congratulations, configuration is done. The file has been saved' + ' to %s. Later runs should specify the arguments:' + ' -c %s') % (abs_path, abs_path)) if args.debug is not None: config_obj.debug_save_path = args.debug config_obj.show_borders = args.show_border return config_obj