Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weather alerts #82

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install Dependencies
run: |
apt update
apt install -y libaccountsservice-dev libdbus-1-dev libgranite-dev libgeoclue-2-dev libfwupd-dev meson valac
apt install -y libaccountsservice-dev libdbus-1-dev libgranite-dev libgeoclue-2-dev libfwupd-dev libsoup-3.0-dev libjson-glib-dev meson valac
- name: Build
env:
DESTDIR: out
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fwupd_dep = dependency('fwupd')
gio_dep = dependency ('gio-2.0')
glib_dep = dependency('glib-2.0')
granite_dep = dependency('granite', version: '>= 5.3.0')
json_dep = dependency('json-glib-1.0')
soup_dep = dependency('libsoup-3.0')
i18n = import('i18n')
gettext_name = meson.project_name()

Expand Down
4 changes: 4 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public sealed class SettingsDaemon.Application : Gtk.Application {

private Backends.Housekeeping housekeeping;

private Backends.WeatherAlerts weather_alerts;

private const string FDO_ACCOUNTS_NAME = "org.freedesktop.Accounts";
private const string FDO_ACCOUNTS_PATH = "/org/freedesktop/Accounts";

Expand Down Expand Up @@ -52,6 +54,8 @@ public sealed class SettingsDaemon.Application : Gtk.Application {

housekeeping = new Backends.Housekeeping ();

weather_alerts = new Backends.WeatherAlerts ();

var check_firmware_updates_action = new GLib.SimpleAction ("check-firmware-updates", null);
check_firmware_updates_action.activate.connect (check_firmware_updates);
add_action (check_firmware_updates_action);
Expand Down
123 changes: 123 additions & 0 deletions src/Backends/WeatherAlerts.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
public class SettingsDaemon.Backends.WeatherAlerts : Object {
private const string API_KEY = "9d9c2fa1c0024a4b9fc152215232108";

private double latitude;
private double longitude;

construct {
init_location.begin (() => {
update_alerts.begin ();
});

Timeout.add_seconds (3600, () => {
update_alerts.begin ();
return Source.CONTINUE;
});
}

private async void init_location () {
try {
var simple = yield new GClue.Simple (Build.PROJECT_NAME, GClue.AccuracyLevel.CITY, null);

simple.notify["location"].connect (() => {
latitude = simple.location.latitude;
longitude = simple.location.longitude;
update_alerts.begin ();
});

latitude = simple.location.latitude;
longitude = simple.location.longitude;
} catch (Error e) {
warning ("Failed to connect to GeoClue2 service: %s", e.message);
return;
}
}

private async void update_alerts () {
var session = new Soup.Session ();

var msg = new Soup.Message (
"GET",
"http://api.weatherapi.com/v1/forecast.json?key=%s&q=%s,%s&days=1&aqi=no&alerts=yes".printf (
API_KEY,
latitude.to_string (),
longitude.to_string ()
)
);

msg.finished.connect (() => {
if (msg.status_code != Soup.Status.OK) {
warning ("Failed with status code %u (%s)", msg.status_code, Soup.Status.get_phrase (msg.status_code));
}
});

try {
var bytes = yield session.send_and_read_async (msg, Priority.DEFAULT, null);
var json_parser = new Json.Parser ();
try {
string data = (string) bytes.get_data ();
json_parser.load_from_data (data);
var node = json_parser.get_root ();
if (node == null) {
warning ("No JSON root node found!");
return;
}

unowned var obj = node.get_object ();
if (obj == null) {
warning ("No root object found!");
return;
}

unowned var location_obj = obj.get_object_member ("location");
if (location_obj != null) {
debug (
"Getting alerts for %s, %s, %s",
location_obj.get_string_member ("name"),
location_obj.get_string_member ("region"),
location_obj.get_string_member ("country")
);
}

unowned var alerts = obj.get_object_member ("alerts");
if (alerts == null) {
warning ("No alerts object found!");
return;
}

unowned var array = alerts.get_array_member ("alert");
if (array == null) {
warning ("No alerts array found!");
return;
}

unowned var app = GLib.Application.get_default ();
array.foreach_element ((array, index, element_node) => {
unowned var alert_obj = element_node.get_object ();
if (alert_obj == null) {
warning ("No alert object found!");
return;
}

var start_time = new DateTime.from_iso8601 (alert_obj.get_string_member ("effective"), null);

var notification = new Notification (
"%s starting %s on %s".printf (
alert_obj.get_string_member ("event"),
start_time.format (Granite.DateTime.get_default_time_format ()),
start_time.format (Granite.DateTime.get_default_date_format ())
)
);
notification.set_icon (new ThemedIcon ("dialog-error"));
notification.set_body (alert_obj.get_string_member ("headline"));

app.send_notification (alert_obj.get_string_member ("headline"), notification);
});
} catch (Error e) {
warning ("Failed to parse response as json: %s", e.message);
}
} catch (Error e) {
critical ("Failed to send soup message: %s", e.message);
}
}
}
3 changes: 3 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sources = files(
'Backends/MouseSettings.vala',
'Backends/NightLightSettings.vala',
'Backends/PrefersColorSchemeSettings.vala',
'Backends/WeatherAlerts.vala',
'Utils/SunriseSunsetCalculator.vala',
)

Expand All @@ -19,8 +20,10 @@ executable(
gio_dep,
glib_dep,
granite_dep,
json_dep,
libgeoclue_dep,
m_dep,
soup_dep
],
install: true,
)