From 95997eae52772b87d7f359eba030276aef78656b Mon Sep 17 00:00:00 2001 From: Ace Date: Mon, 24 Dec 2018 21:13:58 +0100 Subject: [PATCH] Preparing for v1.4! --- Calendar/stable.py | 112 +++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 45 deletions(-) diff --git a/Calendar/stable.py b/Calendar/stable.py index 8742acf..9a8a4c5 100755 --- a/Calendar/stable.py +++ b/Calendar/stable.py @@ -8,7 +8,7 @@ If you have any questions, feel free to open an issue at Github. Copyright by Ace-Laboratory """ -print('importing modules'+'\n') +print('importing modules') from settings import * from icon_positions_locations import * @@ -17,9 +17,13 @@ import calendar, pyowm from ics import Calendar, Event from datetime import datetime from time import sleep -from urllib.request import urlopen +try: + from urllib.request import urlopen +except Exception as e: + print('It seems the network is offline :(') + pass import arrow - +print('modules imported successfully!'+'\n') if display_colours == "bwr": import epd7in5b @@ -31,29 +35,28 @@ if display_colours == "bw": epd = epd7in5.EPD() from calibration_bw import calibration -font = ImageFont.truetype(path+'Assistant-Bold.ttf', 18) c = Calendar(urlopen(url).read().decode('UTF-8')) e = Event() EPD_WIDTH = 640 EPD_HEIGHT = 384 - +font = ImageFont.truetype(path+'Assistant-Bold.ttf', 18) def main(): while True: - + time = datetime.now() hour = int(time.strftime("%-H")) - + for i in range(1): """At the following hours (midnight, midday and 6 pm), perform a calibration of the display's colours""" if (hour is 0) or (hour is 12) or (hour is 18): - print('performing calibration now') + print('performing calibration for colours now') calibration() print('Current date:',time.strftime('%a %-d %b %y')) print('Current time:', time.strftime('%H:%M')+'\n') - + """Create a blank page""" image = Image.new('L', (EPD_WIDTH, EPD_HEIGHT), 255) draw = (ImageDraw.Draw(image)).bitmap @@ -96,70 +99,92 @@ def main(): draw(positions['f'+str(cal[5].index(i)+1)] ,Image.open(dpath+str(i)+'.bmp')) except IndexError: pass - + + + def write_text(a,b, text, c,d):#a,b box-size #c,d box position + w, h = font.getsize(text) + if (w, h) > (a, b): + raise ValueError('Sorry, your text is too big for the box') + else: + x = int((a / 2) - (w / 2)) + y = int((b / 2) - (h / 2)) + space = Image.new('1', (a,b), color=255) + ImageDraw.Draw(space).text((x, y), text, fill=0 ,font=font) + image.paste(space.rotate(270, expand=1), (c,d)) + + """ Handling Openweathermap API""" - try: + owm = pyowm.OWM(api_key) + if owm.is_API_online() is True: #test server connection print("Preparing to fetch data from openweathermap API") - owm = pyowm.OWM(api_key) observation = owm.weather_at_place(location) print("Fetching weather data...") weather = observation.get_weather() weathericon = weather.get_weather_icon_name() - + Temperature = str(int(weather.get_temperature(unit='celsius')['temp'])) Humidity = str(weather.get_humidity()) - print('temperature: '+Temperature +' °C') + print('temperature: '+Temperature +' °C') print('humidity: '+Humidity+'%') print('fetched icon code: '+weathericon) print('equivalent to icon: '+weathericons[weathericon]+'\n') - + windspeed= str(int(weather.get_wind()['speed'])) + sunrisetime = str(datetime.fromtimestamp(int(weather.get_sunrise_time(timeformat='unix'))).strftime('%H:%M')) + sunsettime = str(datetime.fromtimestamp(int(weather.get_sunset_time(timeformat='unix'))).strftime('%H:%M')) + cloudstatus = str(weather.get_clouds()) + weather_description = (str(weather.get_status())) + print('Current wind speed: '+windspeed+ 'km/h') + print('The sunrise today took take place place at: '+sunrisetime) + print('The sunset today will take place place at: '+sunsettime) + print('The current Cloud condition is: ' + cloudstatus + '%') + print('Weather: '+ weather_description) + """Drawing the fetched weather icon""" - draw(wiconplace, open(wpath+weathericons[weathericon]+'.bmp')) + draw(wiconplace, open(wpath+weathericons[weathericon] + '.bmp')) """Drawing the fetched temperature""" - space2 = Image.new('1', (50,35), color=255) - temperature = ImageDraw.Draw(space2) - temperature.text((2, 8), (Temperature + " °C"), fill=0 ,font=font) - rotate2 = space2.rotate(270, expand=1) - image.paste(rotate2, (605,334)) + draw(tempplace, tempicon) + write_text(50,35, Temperature + " °C", 605, 334) """Drawing the fetched humidity""" - space3 = Image.new('1', (50,35), color=255) - humidity = ImageDraw.Draw(space3) - humidity.text((4, 8), (Humidity +'%'), fill=0 ,font=font) - rotate3 = space3.rotate(270, expand=1) - image.paste(rotate3, (570,334)) - - except Exception as e: - template = "An exception of type {0} occurred. Arguments:\n{1!r}" - message = template.format(type(ex).__name__, ex.args) - print(message) - print("************ OWM DID NOT RESPOND *************") - print("Drawing the 'no-response' icon on the display now") + draw(humplace, humicon) + write_text(50,35, Humidity + " %", 570, 334) + + """Drawing the fetched sunrise time""" + draw(sunriseplace, sunriseicon) + write_text(50,35, sunrisetime, 605, 250) + + """Drawing the fetched sunset time""" + draw(sunsetplace, sunseticon) + write_text(50,35, sunsettime, 570, 250) + + """Drawing the fetched wind speed""" + draw(windiconspace, windicon) + write_text(75, 35, windspeed+" km/h", 605,135) + + """Write a short weather description""" + write_text(100,35, weather_description, 570, 100) + + else: draw(wiconplace, no_response) pass - """Drawing today's date at the top left corner""" - """Uncomment this section (the following 5 lines) to hide date at top left corner""" - space1=Image.new('1', (115,25), color=255) - date = ImageDraw.Draw(space1) - date.text((2, 3), (time.strftime('%a %-d %b %y')), font=font, fill=0) - rotate1 = space1.rotate(270, expand=1) - image.paste(rotate1, (595,20)) - """Sort the Events in your iCalendar""" print('Fetching upcoming events from your calendar') elist = [] + eventstoday = [] for events in c.events: if time.year <= int((events.begin).format('YYYY')): if time.month == int((events.begin).format('M')): elist.append(int((events.begin).format('D'))) + """Uncomment the next 4 lines to print your events on the console""" # if time.day <= int((events.begin).format('D')): # print(events.name+' starts on '+events.begin.format('D '+'MMM '+'YYYY')) # if time.month < int((events.begin).format('M')): # print(events.name+' starts on '+events.begin.format('D '+'MMM '+'YYYY')) + """Draw circles on any days which include an Event""" for x in elist: if x in cal[0]: @@ -178,7 +203,6 @@ def main(): except IndexError: pass - """Draw a square with round corners on the today's date""" today = time.day if today in cal[0]: @@ -197,9 +221,6 @@ def main(): except IndexError: pass - draw(tempplace, tempicon) - draw(humplace, humicon) - print('\n'+'initialising E-Paper Display') epd.init() sleep(5) @@ -208,6 +229,7 @@ def main(): # delete the list so deleted events can be removed from the list del elist[:] + del eventstoday[:] print('data sent successfully'+'\n') print('letting the display sleep until the next hour') epd.sleep()