Weather.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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):
  11. Thread.__init__(self)
  12. self.nextUpdate = None
  13. self.lastUpdateTried = None
  14. self.stopThread = False
  15. self.provider = None
  16. def acquire(self, config : aman.config.Weather.Weather):
  17. self.nextUpdate = dt.utcfromtimestamp(int(time.time()))
  18. self.lastUpdateTried = None
  19. self.stopThread = False
  20. self.provider = None
  21. if 'DWD' == config.Provider.upper():
  22. self.provider = DwdCrawler()
  23. else:
  24. sys.stderr.write('Invalid or unknown weather-provider defined')
  25. sys.exit(-1)
  26. self.start()
  27. def release(self):
  28. self.stopThread = True
  29. self.join()
  30. def currentClock():
  31. clock = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
  32. return clock
  33. def run(self):
  34. while False == self.stopThread and None != self.provider:
  35. now = Weather.currentClock()
  36. # check if an update is required
  37. if None != self.provider.updateTime and self.provider.updateTime > now:
  38. time.sleep(1)
  39. continue
  40. if None == self.lastUpdateTried or self.lastUpdateTried <= now:
  41. if True == self.provider.receiveWindData():
  42. self.nextUpdate = self.provider.updateTime
  43. print('Received new wind data')