# @name SunEvent # @desc Odliczanie do wschodu i zachodu słońca # @author strusic # @version 1.5 # @config lat number "Latitude" default=50.2584 min=-90 max=90 step=0.0001 # @config lon number "Longitude" default=19.0275 min=-180 max=180 step=0.0001 # @config minutes number "Alert przed (min)" default=30 min=5 max=120 # @config duration number "Czas wyświetlania (ms)" default=10000 min=1000 max=60000 # @config icon_rise text "Ikona - Wschód" default="2893" # @config icon_set text "Ikona - Zachód" default="53384" # @config test_mode bool "Tryb testowy (pokaż teraz)" default=false class SunEvent var sunrise_ts, sunset_ts, active, label, current_icon, ticks, check_ticks, alert_mins, dur, icon_rise, icon_set, test_mode def init() self.sunrise_ts = store.get("sunrise_ts", 0) self.sunset_ts = store.get("sunset_ts", 0) self.active = false self.label = "" self.current_icon = "" self.ticks = 0 self.check_ticks = 0 self.alert_mins = int(store.get("minutes")) self.dur = int(store.get("duration")) self.icon_rise = store.get("icon_rise") self.icon_set = store.get("icon_set") self.test_mode = store.get("test_mode") end def duration() return self.dur end def on_body(body, status) if body == nil return end # Naprawiony regex - wyłapuje pierwszą liczbę po znaku nawiasu kwadratowego "[" var sr = re.search("\"sunrise\":\\s*\\[\\s*([0-9]+)", body) var ss = re.search("\"sunset\":\\s*\\[\\s*([0-9]+)", body) if sr != nil && ss != nil self.sunrise_ts = int(sr[1]) self.sunset_ts = int(ss[1]) store.set("sunrise_ts", self.sunrise_ts) store.set("sunset_ts", self.sunset_ts) end end def loop() self.test_mode = store.get("test_mode") if self.test_mode self.label = "15M" self.current_icon = self.icon_set self.active = true return end if self.ticks <= 0 self.ticks = 3600 # Dodany parametr &forecast_days=1 odchudza JSON-a do dzisiejszego dnia var url = "http://api.open-meteo.com/v1/forecast?latitude=" + str(store.get("lat")) + "&longitude=" + str(store.get("lon")) + "&daily=sunrise,sunset&timeformat=unixtime&timezone=auto&forecast_days=1" http.get(url, / b, st -> self.on_body(b, st), {'find': "\"daily\":", 'keep': 256}) end self.ticks -= 1 if self.check_ticks <= 0 self.check_ticks = 60 var now = epoch_ms() / 1000 if now >= 0 && self.sunrise_ts > 0 var threshold = self.alert_mins * 60 var diff_sr = self.sunrise_ts - now if diff_sr >= 0 && diff_sr <= threshold var m = int(diff_sr / 60) self.label = str(m) + "M" self.current_icon = self.icon_rise self.active = true return end var diff_ss = self.sunset_ts - now if diff_ss >= 0 && diff_ss <= threshold var m = int(diff_ss / 60) self.label = str(m) + "M" self.current_icon = self.icon_set self.active = true return end end self.active = false end self.check_ticks -= 1 end def should_show() return self.active end def draw() clear() if !icon(self.current_icon, 0, 0) circle_fill(4, 4, 3, 0xFF8800) end var text_w = text_ink_width(self.label) var x = 8 + int((24 - text_w) / 2) text(x, 6, self.label, 0xFFFFFF) end end return SunEvent()