Implement cache and work on weather
This commit is contained in:
parent
0d46227f8b
commit
d001b6bc80
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
||||
/__pycache__/
|
||||
/test*.png
|
||||
/cache.sqlite
|
||||
|
||||
BIN
7.3inch-1.bmp
BIN
7.3inch-1.bmp
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 MiB |
BIN
7.3inch-2.bmp
BIN
7.3inch-2.bmp
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 MiB |
BIN
7.3inch-3.bmp
BIN
7.3inch-3.bmp
Binary file not shown.
|
Before Width: | Height: | Size: 1.1 MiB |
44
bom.py
44
bom.py
@ -1,15 +1,41 @@
|
||||
from ftplib import FTP
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
ftp = FTP('ftp.bom.gov.au')
|
||||
file = []
|
||||
try:
|
||||
def fetch_forecast():
|
||||
file = []
|
||||
with FTP('ftp.bom.gov.au') as ftp:
|
||||
ftp.login()
|
||||
ftp.cwd('anon/gen/fwo')
|
||||
ftp.retrbinary('RETR IDV17000.xml', file.append)
|
||||
finally:
|
||||
ftp.quit()
|
||||
ftp.retrlines('RETR anon/gen/fwo/IDV10450.xml', file.append)
|
||||
return ''.join(file)
|
||||
|
||||
def parse_forecast(xml):
|
||||
root = ET.fromstring(xml)
|
||||
[today] = root.findall('./forecast/area[@aac="VIC_PT042"]/forecast-period[@index="0"]')
|
||||
return today
|
||||
|
||||
import cache
|
||||
import datetime
|
||||
with cache.open() as cache:
|
||||
print(parse_forecast(cache.get('weather', datetime.timedelta(minutes=10), fetch_forecast)))
|
||||
|
||||
file = []
|
||||
with FTP('ftp.bom.gov.au') as ftp:
|
||||
ftp.login()
|
||||
ftp.retrlines('RETR anon/gen/fwo/IDV60920.xml', file.append)
|
||||
root = ET.fromstringlist(file)
|
||||
print(root)
|
||||
print([e.attrib['description'] for e in root.findall('./forecast/area')])
|
||||
stations = root.findall('./observations/station')
|
||||
|
||||
from math import cos, asin, sqrt, pi
|
||||
|
||||
def distance(lat1, lon1, lat2, lon2):
|
||||
r = 6371 # km
|
||||
p = pi / 180
|
||||
|
||||
a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2
|
||||
return 2 * r * asin(sqrt(a))
|
||||
def dist(station):
|
||||
lat, lon = [float(station.attrib[k]) for k in ['lat', 'lon']]
|
||||
return distance(lat, lon, -37.82635, 145.09653)
|
||||
stations.sort(key=dist)
|
||||
for station in stations:
|
||||
print(station.attrib['stn-name'], dist(station))
|
||||
|
||||
32
cache.py
Normal file
32
cache.py
Normal file
@ -0,0 +1,32 @@
|
||||
import contextlib
|
||||
import datetime
|
||||
import sqlite3
|
||||
|
||||
class Cache:
|
||||
def __init__(self, db):
|
||||
self.db = db
|
||||
|
||||
def get(self, key, timeout, f):
|
||||
now = datetime.datetime.now()
|
||||
earliest_acceptable = now - timeout
|
||||
match self.db.execute(
|
||||
'SELECT value FROM cache WHERE key = ? AND timestamp >= ?',
|
||||
(key, earliest_acceptable.isoformat()),
|
||||
).fetchall():
|
||||
case [(value,)]:
|
||||
self.db.commit()
|
||||
return value
|
||||
|
||||
value = f()
|
||||
self.db.execute(
|
||||
'INSERT INTO cache (key, timestamp, value) VALUES (?, ?, ?) ' +
|
||||
'ON CONFLICT(key) DO UPDATE SET timestamp = excluded.timestamp, value = excluded.value',
|
||||
(key, now.isoformat(), value),
|
||||
)
|
||||
return value
|
||||
|
||||
@contextlib.contextmanager
|
||||
def open():
|
||||
with sqlite3.connect('cache.sqlite', isolation_level=None) as db:
|
||||
db.execute('CREATE TABLE IF NOT EXISTS cache (key TEXT PRIMARY KEY, timestamp TEXT NOT NULL, value TEXT NOT NULL)')
|
||||
yield Cache(db)
|
||||
66
demo.py
66
demo.py
@ -1,66 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import sys
|
||||
import os
|
||||
picdir = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
import logging
|
||||
import epd7in3g
|
||||
import time
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
try:
|
||||
logging.info("epd7in3g Demo")
|
||||
|
||||
epd = epd7in3g
|
||||
logging.info("init and Clear")
|
||||
epd.init()
|
||||
#epd.clear()
|
||||
font24 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 24)
|
||||
font18 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18)
|
||||
font40 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 40)
|
||||
|
||||
# Drawing on the image
|
||||
logging.info("1.Drawing on the image...")
|
||||
h_image = Image.new('RGB', (epd.width, epd.height), epd.WHITE) # 255: clear the frame
|
||||
draw = ImageDraw.Draw(h_image)
|
||||
draw.text((5, 0), 'hello world', font = font18, fill = epd.RED)
|
||||
draw.text((5, 20), '7.3inch e-Paper', font = font24, fill = epd.YELLOW)
|
||||
draw.text((5, 45), u'微雪电子', font = font40, fill = epd.BLACK)
|
||||
draw.text((5, 85), u'微雪电子', font = font40, fill = epd.YELLOW)
|
||||
draw.text((5, 125), u'微雪电子', font = font40, fill = epd.RED)
|
||||
|
||||
draw.line((5, 170, 80, 245), fill = epd.RED)
|
||||
draw.line((80, 170, 5, 245), fill = epd.YELLOW)
|
||||
draw.rectangle((5, 170, 80, 245), outline = epd.BLACK)
|
||||
draw.rectangle((90, 170, 165, 245), fill = epd.YELLOW)
|
||||
draw.arc((5, 250, 80, 325), 0, 360, fill = epd.BLACK)
|
||||
draw.chord((90, 250, 165, 325), 0, 360, fill = epd.RED)
|
||||
epd.display(epd.get_buffer(h_image))
|
||||
time.sleep(3)
|
||||
|
||||
## read bmp file
|
||||
#logging.info("2.read bmp file")
|
||||
#h_image = Image.open(os.path.join(picdir, '7.3inch-1.bmp'))
|
||||
#epd.display(epd.get_buffer(h_image))
|
||||
#time.sleep(3)
|
||||
|
||||
#logging.info("3.read bmp file")
|
||||
#h_image = Image.open(os.path.join(picdir, '7.3inch-2.bmp'))
|
||||
#epd.display(epd.get_buffer(h_image))
|
||||
#time.sleep(3)
|
||||
|
||||
#logging.info("4.read bmp file")
|
||||
#h_image = Image.open(os.path.join(picdir, '7.3inch-3.bmp'))
|
||||
#epd.display(epd.getbuffer(h_image))
|
||||
#time.sleep(3)
|
||||
|
||||
logging.info("Clear...")
|
||||
epd.clear()
|
||||
|
||||
logging.info("Goto Sleep...")
|
||||
epd.sleep()
|
||||
|
||||
finally:
|
||||
epd7in3g.epdconfig.module_exit(cleanup=True)
|
||||
BIN
reuben.webp
BIN
reuben.webp
Binary file not shown.
|
Before Width: | Height: | Size: 22 KiB |
9704
routes.json
9704
routes.json
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
Before Width: | Height: | Size: 100 KiB |
Loading…
Reference in New Issue
Block a user