Skip to content

Commit

Permalink
Reduced use of unneeded threading when fetching web data.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtnorthcott committed Jan 7, 2017
1 parent 0208694 commit 104d02b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _button_listener(self):
while True:
if GPIO.input(ID_BUTTON):
self.is_red_buzzer = False
time.sleep(0.050)
time.sleep(0.10)

def lcd_out(self, string, line, justify="left"):
self.lcd.string_out(string, line, justify)
32 changes: 24 additions & 8 deletions monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import holidays
import webpage
import interface
import RPi.GPIO as GPIO
from importlib.machinery import SourceFileLoader


Expand Down Expand Up @@ -46,6 +45,7 @@
class Monitor(object):
def __init__(self, url):
self.webpage = webpage.Webpage(url)
self.has_data = False
self.update_values()

def update_values(self):
Expand All @@ -65,6 +65,7 @@ def update_spotprice(self):

self.spotprice = float(self.webpage.search(REGEX_SPOTPRICE)) + conf.ADDITIONAL_SPOTPRICE
self.time = datetime.datetime.strptime(self.webpage.search(REGEX_SPOTPRICE_TIME), "%d/%m/%Y %H:%M:%S")
self.has_data = True

def update_network_charge(self):
if self.spotprice == 0: return
Expand Down Expand Up @@ -129,6 +130,8 @@ def update_values(self):

self.weather_data = weather_data

self.has_data = True

def temp_string(self):
temp = "{}C".format(self.temperature)
humidity = "{}%".format(self.humidity)
Expand Down Expand Up @@ -157,11 +160,24 @@ def update_interface(self):
# Update LED & buzzer
[self.interface.set_green, self.interface.set_orange, self.interface.set_red][self.spotprice_mon.status()]()

spotprice_top = "No spotprice data"
spotprice_bottom = ""
weather_top = "No weather data"
weather_bottom = ""

# Update LCD
self.interface.lcd_out(self.spotprice_mon.time_string(), 0, "centre")
self.interface.lcd_out(self.spotprice_mon.price_string(), 1, "centre")
self.interface.lcd_out(self.weather_mon.temp_string(), 2, "centre")
self.interface.lcd_out(self.weather_mon.wind_string(), 3, "centre")
if self.spotprice_mon.has_data:
spotprice_top = self.spotprice_mon.time_string()
spotprice_bottom = self.spotprice_mon.price_string()

if self.weather_mon.has_data:
weather_top = self.weather_mon.temp_string()
weather_bottom = self.weather_mon.wind_string()

self.interface.lcd_out(spotprice_top, 0, "centre")
self.interface.lcd_out(spotprice_bottom, 1, "centre")
self.interface.lcd_out(weather_top, 2, "centre")
self.interface.lcd_out(weather_bottom, 3, "centre")

def mainloop(self):

Expand All @@ -171,19 +187,19 @@ def mainloop(self):
now = datetime.datetime.now()

if now.second == 0:
if now.minute % 5 == 1:
if now.minute % 5 == 0:
self.spotprice_mon.update_values()
update = True

if now.minute % 10 == 1:
if now.minute % 10 == 0:
self.weather_mon.update_values()
update = True

if update:
self.update_interface()
update = False

time.sleep(0.30)
time.sleep(0.90)


def main():
Expand Down
24 changes: 2 additions & 22 deletions webpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
# IMPORTS
import urllib.request, urllib.error
import re
import threading
import time
import queue

# GLOBALS
HEADERS = { 'User-Agent': 'Mozilla/5.0' }
Expand All @@ -32,30 +29,13 @@ def __enter__(self):
def __exit__(self, type, value, traceback):
del self

def _open(self, request, q):
def open(self):
try:
request = urllib.request.Request(self.url, headers=HEADERS)
response = urllib.request.urlopen(request).read()
except urllib.error.URLError:
response = None
q.put(response)

def open(self, timeout=10.0):
q = queue.Queue()
request = urllib.request.Request(self.url, headers=HEADERS)

thr = threading.Thread(target=self._open, args=(request, q))
thr.start()

t = 0
while t < timeout and thr.is_alive():
time.sleep(0.10)
t += 0.10

if thr.is_alive():
del thr
return False

response = q.get()
if response is None:
return False

Expand Down

0 comments on commit 104d02b

Please sign in to comment.