From 721413cd52345a52cf5aab442c64b99fc061eeec Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 30 Apr 2024 12:30:44 +0400 Subject: [PATCH] Add Solar Cycle and Uptimerobot 2 widgets --- community/solar-cycle-widget.lua | 79 ++++++++++++++++++++++++++++++ community/uptimerobot-2-widget.lua | 69 ++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 community/solar-cycle-widget.lua create mode 100644 community/uptimerobot-2-widget.lua diff --git a/community/solar-cycle-widget.lua b/community/solar-cycle-widget.lua new file mode 100644 index 0000000..1cbb017 --- /dev/null +++ b/community/solar-cycle-widget.lua @@ -0,0 +1,79 @@ +-- name = "Solar Cycle" +-- description = "Shows Sunrise Sunset at your location" +-- data_source = "https://api.sunrise-sunset.org/" +-- type = "widget" +-- author = "Sriram S V, Will Hall" +-- version = "1.0" +-- foldable = "false" + +local json = require "json" +local md_colors = require "md_colors" + +function on_alarm() + local location=system:location() + url="https://api.sunrise-sunset.org/json?lat="..location[1].."&lng="..location[2].."&formatted=0" + http:get(url) +end + + +function on_network_result(result) + local t = json.decode(result) + + local times_table = { + { + gen_icon("red_900","↦"), + gen_icon("orange_900", "↗"), + gen_icon("yellow_900", "☀"), + gen_icon("orange_900", "↘"), + gen_icon("red_900", "⇥"), + }, + { + time_from_utc(t.results.civil_twilight_begin), + time_from_utc(t.results.sunrise), + time_from_utc(t.results.solar_noon), + time_from_utc(t.results.sunset), + time_from_utc(t.results.civil_twilight_end), + } + } + + ui:show_table(times_table, 0, true) +end + +function time_from_utc(utc) + return utc_to_local(parse_iso8601_datetime(utc)) +end + +function gen_icon(md_color, icon) + return ""..icon.."" +end + +function parse_iso8601_datetime(json_date) + local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-]?)(%d?%d?)%:?(%d?%d?)" + local year, month, day, hour, minute, + seconds, offsetsign, offsethour, offsetmin = json_date:match(pattern) + local timestamp = os.time{year = year, month = month, + day = day, hour = hour, min = minute, sec = seconds} + local offset = 0 + if offsetsign ~= '' and offsetsign ~= 'Z' then + offset = tonumber(offsethour) * 60 + tonumber(offsetmin) + if xoffset == "-" then offset = offset * -1 end + end + + return timestamp + offset * 60 +end + +function utc_to_local(utctime) + local local_time_str = os.date("%H:%M", utctime) + local utc_time_str = os.date("!%H:%M", utctime) + + local function time_to_seconds(timestr) + local hour, minute = timestr:match("(%d+):(%d+)") + return tonumber(hour) * 3600 + tonumber(minute) * 60 + end + + local local_seconds = time_to_seconds(local_time_str) + local utc_seconds = time_to_seconds(utc_time_str) + local delta = local_seconds - utc_seconds + + return os.date("%H:%M", utctime + delta) +end diff --git a/community/uptimerobot-2-widget.lua b/community/uptimerobot-2-widget.lua new file mode 100644 index 0000000..722d528 --- /dev/null +++ b/community/uptimerobot-2-widget.lua @@ -0,0 +1,69 @@ +-- name = "Uptimerobot V2" +-- description = "Shows uptime information from uptimerobot.com. Needs API key. Button-based Version." +-- data_source = "uptimerobot.com" +-- type = "widget" +-- author = "Evgeny Zobnin (zobnin@gmail.com), Will Hall (hello@willhall.uk)" +-- version = "1.0" +-- arguments_help = "Enter your API key" + +local json = require "json" +local md_colors = require "md_colors" + +-- constants +local api_url = "https://api.uptimerobot.com/v2/" +local base_click_url = "https://uptimerobot.com/dashboard#" +local media_type = "application/x-www-form-urlencoded" +local status_colors = { "grey_500", "green_500", "red_500", "red_500", "red_500", "red_500", "red_500", "orange_500", "red_500" } + +-- monitors +local monitor_ids = {} + +function on_alarm() + if (next(settings:get()) == nil) then + ui:show_text("Tap to enter API key") + return + end + + local key = settings:get()[1] + local body = "api_key="..key.."&format=json" + + http:post(api_url.."getMonitors", body, media_type) +end + +function on_click(i) + if (next(settings:get()) == nil) then + settings:show_dialog() + else + if(monitor_ids[i] ~= nil) then + system:open_browser(base_click_url..monitor_ids[i]) + else + system:open_browser(base_click_url.."mainDashboard") + end + end +end + +function on_network_result(result, code) + if (code >= 400) then + ui:show_text("Error: "..code) + return + end + + local parsed = json.decode(result) + + if (parsed.stat ~= "ok") then + ui:show_text("Error: "..parsed.error.message) + return + end + + local names = {} + local colours = {} + + for k,v in pairs(parsed.monitors) do + monitor_ids[k] = v.id + names[k] = v.friendly_name + colours[k] = md_colors[status_colors[v.status]] or md_colors["grey_500"] + end + + ui:show_buttons(names, colours) + +end \ No newline at end of file