Weather.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. import pytz
  3. import sys
  4. import time
  5. from datetime import datetime as dt
  6. from threading import Thread
  7. from aman.com.DwdCrawler import DwdCrawler
  8. import aman.config.Weather
  9. class Weather(Thread):
  10. def __init__(self, config : aman.config.Weather.Weather):
  11. Thread.__init__(self)
  12. self.NextUpdate = dt.utcfromtimestamp(int(time.time()))
  13. self.LastUpdateTried = None
  14. self.StopThread = False
  15. self.Provider = None
  16. if 'DWD' == config.Provider.upper():
  17. self.Provider = DwdCrawler()
  18. elif 'NONE' != config.Provider.upper():
  19. sys.stderr.write('Invalid or unknown weather-provider defined')
  20. sys.exit(-1)
  21. self.setDaemon(True)
  22. self.start()
  23. def currentClock():
  24. clock = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
  25. return clock
  26. def run(self):
  27. while False == self.StopThread and None != self.Provider:
  28. now = Weather.currentClock()
  29. # check if an update is required
  30. if None != self.Provider.UpdateTime and self.Provider.UpdateTime > now:
  31. time.sleep(1)
  32. continue
  33. if None == self.LastUpdateTried or self.LastUpdateTried <= now:
  34. if True == self.Provider.receiveWindData():
  35. self.NextUpdate = self.Provider.UpdateTime
  36. print('Received new wind data')