1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #!/usr/bin/env python
- import pytz
- import sys
- import time
- from datetime import datetime as dt
- from threading import Thread
- from aman.com.DwdCrawler import DwdCrawler
- import aman.config.Weather
- class Weather(Thread):
- def __init__(self):
- Thread.__init__(self)
- self.nextUpdate = None
- self.lastUpdateTried = None
- self.stopThread = False
- self.provider = None
- def acquire(self, config : aman.config.Weather.Weather):
- self.nextUpdate = dt.utcfromtimestamp(int(time.time()))
- self.lastUpdateTried = None
- self.stopThread = False
- self.provider = None
- if 'DWD' == config.Provider.upper():
- self.provider = DwdCrawler()
- else:
- sys.stderr.write('Invalid or unknown weather-provider defined')
- sys.exit(-1)
- self.start()
- def release(self):
- self.stopThread = True
- self.join()
- def currentClock():
- clock = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
- return clock
- def run(self):
- while False == self.stopThread and None != self.provider:
- now = Weather.currentClock()
- # check if an update is required
- if None != self.provider.updateTime and self.provider.updateTime > now:
- time.sleep(1)
- continue
- if None == self.lastUpdateTried or self.lastUpdateTried <= now:
- if True == self.provider.receiveWindData():
- self.nextUpdate = self.provider.updateTime
- print('Received new wind data')
|