Switch to multiprocessing
This commit is contained in:
parent
fc77feedb2
commit
75cea0753a
48
chutney.py
48
chutney.py
|
@ -1,8 +1,10 @@
|
||||||
import sys
|
import sys
|
||||||
import threading
|
|
||||||
|
|
||||||
from character_display import CharacterDisplay
|
from character_display import CharacterDisplay
|
||||||
from signal import signal, SIGTERM
|
from multiprocessing import Event
|
||||||
|
from multiprocessing import Process
|
||||||
|
from signal import signal
|
||||||
|
from signal import SIGTERM
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from time_display import TimeDisplay
|
from time_display import TimeDisplay
|
||||||
from weather_display import WeatherDisplay
|
from weather_display import WeatherDisplay
|
||||||
|
@ -16,34 +18,17 @@ except ImportError:
|
||||||
unicorn.rotation(180)
|
unicorn.rotation(180)
|
||||||
|
|
||||||
|
|
||||||
WEATHER_UPDATE_INTERVAL_IN_SECONDS = 10
|
WEATHER_UPDATE_INTERVAL_IN_SECONDS = 60
|
||||||
DISPLAY_UPDATE_INTERVAL_IN_SECONDS = 0.5
|
DISPLAY_UPDATE_INTERVAL_IN_SECONDS = 0.5
|
||||||
CONFIG_FILE = 'chutney.cfg'
|
CONFIG_FILE = 'chutney.cfg'
|
||||||
|
|
||||||
|
|
||||||
def cleanExit(unicorn, haltEvent):
|
|
||||||
def _exit_(signum, frame):
|
|
||||||
unicorn.off()
|
|
||||||
haltEvent.set()
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
return _exit_
|
|
||||||
|
|
||||||
|
|
||||||
def weatherLoop(haltEvent):
|
|
||||||
weatherUpdater = WeatherUpdater(configFile=CONFIG_FILE)
|
|
||||||
|
|
||||||
while not haltEvent.is_set():
|
|
||||||
weatherUpdater.updateWeather()
|
|
||||||
sleep(WEATHER_UPDATE_INTERVAL_IN_SECONDS)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
haltEvent = threading.Event()
|
haltEvent = Event()
|
||||||
signal(SIGTERM, cleanExit(unicorn, haltEvent))
|
signal(SIGTERM, cleanExit(unicorn, haltEvent))
|
||||||
unicorn.brightness(0.3)
|
unicorn.brightness(0.3)
|
||||||
|
|
||||||
weatherThread = threading.Thread(target=weatherLoop, args=[haltEvent])
|
weatherThread = Process(target=weatherLoop, args=[haltEvent])
|
||||||
weatherThread.start()
|
weatherThread.start()
|
||||||
|
|
||||||
characterDisplay = CharacterDisplay(unicorn)
|
characterDisplay = CharacterDisplay(unicorn)
|
||||||
|
@ -56,5 +41,24 @@ def main():
|
||||||
sleep(DISPLAY_UPDATE_INTERVAL_IN_SECONDS)
|
sleep(DISPLAY_UPDATE_INTERVAL_IN_SECONDS)
|
||||||
|
|
||||||
|
|
||||||
|
def weatherLoop(haltEvent):
|
||||||
|
weatherUpdater = WeatherUpdater(configFile=CONFIG_FILE)
|
||||||
|
|
||||||
|
while not haltEvent.is_set():
|
||||||
|
weatherUpdater.updateWeather()
|
||||||
|
haltEvent.wait(timeout=WEATHER_UPDATE_INTERVAL_IN_SECONDS)
|
||||||
|
|
||||||
|
weatherUpdater.clearWeather()
|
||||||
|
|
||||||
|
|
||||||
|
def cleanExit(unicorn, haltEvent):
|
||||||
|
def _exit_(signum, frame):
|
||||||
|
unicorn.off()
|
||||||
|
haltEvent.set()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
return _exit_
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -11,15 +11,23 @@ class WeatherDisplay:
|
||||||
self.weatherFile = 'weather.txt'
|
self.weatherFile = 'weather.txt'
|
||||||
|
|
||||||
def showWeather(self):
|
def showWeather(self):
|
||||||
|
temperature = self.getTemperature()
|
||||||
|
|
||||||
|
if self.isTemperatureMissing(temperature):
|
||||||
|
self.characterDisplay.clearRow(self.topRow)
|
||||||
|
else:
|
||||||
|
self.showTemperature(list(temperature))
|
||||||
|
|
||||||
|
def getTemperature(self):
|
||||||
try:
|
try:
|
||||||
self.showTemperature(
|
with open(self.weatherFile) as file:
|
||||||
self.getTemperatureCharacters(self.getTemperature()))
|
return file.readline().strip()
|
||||||
except requests.exceptions.ConnectionError:
|
except Exception as e:
|
||||||
print('connection error', flush=True)
|
print(e, flush=True)
|
||||||
self.characterDisplay.clearRow(self.topRow)
|
return 'error'
|
||||||
except requests.exceptions.Timeout:
|
|
||||||
print('timeout', flush=True)
|
def isTemperatureMissing(self, temperature):
|
||||||
self.characterDisplay.clearRow(self.topRow)
|
return temperature == 'error' or temperature == 'init' or temperature == 'halted'
|
||||||
|
|
||||||
def showTemperature(self, temperature):
|
def showTemperature(self, temperature):
|
||||||
if temperature != self.currentTemperature:
|
if temperature != self.currentTemperature:
|
||||||
|
@ -38,16 +46,3 @@ class WeatherDisplay:
|
||||||
start = start + 4
|
start = start + 4
|
||||||
|
|
||||||
self.currentTemperature = temperature
|
self.currentTemperature = temperature
|
||||||
|
|
||||||
def getTemperature(self):
|
|
||||||
# TODO - don't use a file left around from a previous run
|
|
||||||
# TODO - clear temp on errors from updater
|
|
||||||
try:
|
|
||||||
with open(self.weatherFile) as file:
|
|
||||||
return int(file.readline().strip())
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def getTemperatureCharacters(self, temperature):
|
|
||||||
return list(str(round(temperature)))
|
|
||||||
|
|
|
@ -19,19 +19,21 @@ class WeatherUpdater:
|
||||||
self.auth = (user, password)
|
self.auth = (user, password)
|
||||||
self.weatherTempFile = 'weather.tmp'
|
self.weatherTempFile = 'weather.tmp'
|
||||||
self.weatherFile = 'weather.txt'
|
self.weatherFile = 'weather.txt'
|
||||||
|
self.persistTemperature('init')
|
||||||
# TODO - clear weather file on init and on failure
|
|
||||||
|
|
||||||
def updateWeather(self):
|
def updateWeather(self):
|
||||||
try:
|
try:
|
||||||
temperature = round(self.getTemperature())
|
temperature = round(self.getTemperature())
|
||||||
self.persistTemperature(temperature)
|
self.persistTemperature(temperature)
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError as e:
|
||||||
print('connection error', flush=True)
|
print(e, flush=True)
|
||||||
self.characterDisplay.clearRow(self.topRow)
|
self.persistTemperature('error')
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout as e:
|
||||||
print('timeout', flush=True)
|
print(e, flush=True)
|
||||||
self.characterDisplay.clearRow(self.topRow)
|
self.persistTemperature('error')
|
||||||
|
|
||||||
|
def clearWeather(self):
|
||||||
|
self.persistTemperature('halted')
|
||||||
|
|
||||||
def getTemperature(self):
|
def getTemperature(self):
|
||||||
weather = self.getWeather()
|
weather = self.getWeather()
|
||||||
|
|
Loading…
Reference in New Issue