class Settings(): def __init__(self, onChange=None): self.loaded = False self._tzcache = None self._ip2loccache = None self.hardcodedLocation = False self.onChange = onChange self.load() def _needsIp2LocCacheRefresh(self, datetime): if not self._ip2loccache: return True return self._ip2loccache['date'] < (datetime - IP2LOC_CACHE_LIFETIME) def _needsTzCacheRefresh(self, datetime): if not self._tzcache: return True if self._tzcache['coordinates'] != self.coordinates: return True return self._tzcache['date'] < (datetime - TZ_CACHE_LIFETIME) def _getGoogleTimezoneData(self, timestamp): url = TZ_URL.format(self.coordinates['latitude'], self.coordinates['longitude'], timestamp) response = urllib.urlopen(url, None, 2) result = response.read() if (pyVersion == 3): result = result.decode('utf-8') return json.loads(result) def getTimeZone(self): now = datetime.utcnow() try: if not(self.hardcodedLocation) and self._needsIp2LocCacheRefresh(now): self.loadIPCoordinates() self._ip2loccache = { 'date': now } if self._needsTzCacheRefresh(now): result = self._getGoogleTimezoneData(calendar.timegm(now.timetuple())) self._tzcache = { 'date': now, 'coordinates': self.coordinates, 'name': result['timeZoneName'], 'offset': result['dstOffset'] + result['rawOffset'] } logToConsole('Using {0}'.format(result['timeZoneName'])) return FixedOffset(self._tzcache['offset'] / 60, self._tzcache['name']) except Exception: return LocalTimezone() def load(self): settings = sublime.load_settings(PACKAGE + '.sublime-settings') settings.clear_on_change(PACKAGE) settings.add_on_change(PACKAGE, self.load) if not settings.has('day'): raise KeyError('SunCycle: missing day setting') if not settings.has('night'): raise KeyError('SunCycle: missing night setting') self.day = settings.get('day') self.night = settings.get('night') self.coordinates = {'latitude': settings.get('latitude', 0), 'longitude': settings.get('longitude', 0)} self.sun = Sun(self.coordinates) if self.coordinates['latitude'] != 0 or self.coordinates['longitude'] != 0: self.hardcodedLocation = True now = datetime.now(tz=self.getTimeZone()) logToConsole('Sunrise at {0}'.format(self.sun.sunrise(now))) logToConsole('Sunset at {0}'.format(self.sun.sunset(now))) if self.loaded and self.onChange: self.onChange() self.loaded = True def loadIPCoordinates(self): try: response = urllib.urlopen(IP2LOC_URL, None, 2) result = response.read() if (pyVersion == 3): result = result.decode('utf-8') lookup = json.loads(result) self.coordinates['latitude'] = lookup['latitude'] self.coordinates['longitude'] = lookup['longitude'] logToConsole('Lookup lat & lng through IP: {0}'.format(self.coordinates)) return self.coordinates except Exception: logToConsole('Failed to lookup lat & lng through IP')
is_night = False is_night_time = False prev_leds = [] while running: d = timezone.localize(datetime.now()) utc = d.astimezone(pytz.utc) cur = d.timetuple() if loc.located(): night_b = d.replace(hour=night[0], minute=0, second=0) night_e = d.replace(hour=night[1], minute=0, second=0) sun = Sun(lat=loc.lat, long=loc.lng) sunrise = sun.sunrise(utc) sunset = sun.sunset(utc) if d > sunrise: sunrise = sun.sunrise(utc + timedelta(days=1)) else: sunset = sun.sunset(utc - timedelta(days=1)) if d > night_e: night_e = night_e + timedelta(days=1) else: night_b = night_b - timedelta(days=1) is_night = sunset < d < sunrise night_length = sunrise - sunset is_night_time = night_b < d < night_e night_time_length = night_e - night_b