mirror of
https://github.com/zoriya/noctalia-shell.git
synced 2026-08-15 18:43:59 +00:00
Switched to qmlformat.
This commit is contained in:
@@ -37,20 +37,20 @@ Singleton {
|
||||
}
|
||||
|
||||
onLoadFailed: {
|
||||
cacheAdapter.cachedEvents = ([])
|
||||
cacheAdapter.cachedCalendars = ([])
|
||||
cacheAdapter.lastUpdate = ""
|
||||
cacheAdapter.cachedEvents = ([]);
|
||||
cacheAdapter.cachedCalendars = ([]);
|
||||
cacheAdapter.lastUpdate = "";
|
||||
}
|
||||
|
||||
onLoaded: {
|
||||
loadFromCache()
|
||||
loadFromCache();
|
||||
}
|
||||
}
|
||||
|
||||
Component.onCompleted: {
|
||||
Logger.i("Calendar", "Service started")
|
||||
loadFromCache()
|
||||
checkAvailability()
|
||||
Logger.i("Calendar", "Service started");
|
||||
loadFromCache();
|
||||
checkAvailability();
|
||||
}
|
||||
|
||||
// Save cache with debounce
|
||||
@@ -61,23 +61,23 @@ Singleton {
|
||||
}
|
||||
|
||||
function saveCache() {
|
||||
saveDebounce.restart()
|
||||
saveDebounce.restart();
|
||||
}
|
||||
|
||||
// Load events and calendars from cache
|
||||
function loadFromCache() {
|
||||
if (cacheAdapter.cachedEvents && cacheAdapter.cachedEvents.length > 0) {
|
||||
root.events = cacheAdapter.cachedEvents
|
||||
Logger.d("Calendar", `Loaded ${cacheAdapter.cachedEvents.length} cached event(s)`)
|
||||
root.events = cacheAdapter.cachedEvents;
|
||||
Logger.d("Calendar", `Loaded ${cacheAdapter.cachedEvents.length} cached event(s)`);
|
||||
}
|
||||
|
||||
if (cacheAdapter.cachedCalendars && cacheAdapter.cachedCalendars.length > 0) {
|
||||
root.calendars = cacheAdapter.cachedCalendars
|
||||
Logger.d("Calendar", `Loaded ${cacheAdapter.cachedCalendars.length} cached calendar(s)`)
|
||||
root.calendars = cacheAdapter.cachedCalendars;
|
||||
Logger.d("Calendar", `Loaded ${cacheAdapter.cachedCalendars.length} cached calendar(s)`);
|
||||
}
|
||||
|
||||
if (cacheAdapter.lastUpdate) {
|
||||
Logger.d("Calendar", `Cache last updated: ${cacheAdapter.lastUpdate}`)
|
||||
Logger.d("Calendar", `Cache last updated: ${cacheAdapter.lastUpdate}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,43 +93,42 @@ Singleton {
|
||||
// Core functions
|
||||
function checkAvailability() {
|
||||
if (Settings.data.location.showCalendarEvents) {
|
||||
availabilityCheckProcess.running = true
|
||||
availabilityCheckProcess.running = true;
|
||||
} else {
|
||||
root.available = false
|
||||
root.available = false;
|
||||
}
|
||||
}
|
||||
|
||||
function loadCalendars() {
|
||||
listCalendarsProcess.running = true
|
||||
listCalendarsProcess.running = true;
|
||||
}
|
||||
|
||||
function loadEvents(daysAhead = 31, daysBehind = 14) {
|
||||
if (!Settings.data.location.showCalendarEvents) {
|
||||
root.loading = false
|
||||
root.events = []
|
||||
return
|
||||
root.loading = false;
|
||||
root.events = [];
|
||||
return;
|
||||
}
|
||||
if (loading)
|
||||
return
|
||||
return;
|
||||
loading = true;
|
||||
lastError = "";
|
||||
|
||||
loading = true
|
||||
lastError = ""
|
||||
const now = new Date();
|
||||
const startDate = new Date(now.getTime() - (daysBehind * 24 * 60 * 60 * 1000));
|
||||
const endDate = new Date(now.getTime() + (daysAhead * 24 * 60 * 60 * 1000));
|
||||
|
||||
const now = new Date()
|
||||
const startDate = new Date(now.getTime() - (daysBehind * 24 * 60 * 60 * 1000))
|
||||
const endDate = new Date(now.getTime() + (daysAhead * 24 * 60 * 60 * 1000))
|
||||
loadEventsProcess.startTime = Math.floor(startDate.getTime() / 1000);
|
||||
loadEventsProcess.endTime = Math.floor(endDate.getTime() / 1000);
|
||||
loadEventsProcess.running = true;
|
||||
|
||||
loadEventsProcess.startTime = Math.floor(startDate.getTime() / 1000)
|
||||
loadEventsProcess.endTime = Math.floor(endDate.getTime() / 1000)
|
||||
loadEventsProcess.running = true
|
||||
|
||||
Logger.d("Calendar", `Loading events (${daysBehind} days behind, ${daysAhead} days ahead): ${startDate.toLocaleDateString()} to ${endDate.toLocaleDateString()}`)
|
||||
Logger.d("Calendar", `Loading events (${daysBehind} days behind, ${daysAhead} days ahead): ${startDate.toLocaleDateString()} to ${endDate.toLocaleDateString()}`);
|
||||
}
|
||||
|
||||
// Helper to format date/time
|
||||
function formatDateTime(timestamp) {
|
||||
const date = new Date(timestamp * 1000)
|
||||
return Qt.formatDateTime(date, "yyyy-MM-dd hh:mm")
|
||||
const date = new Date(timestamp * 1000);
|
||||
return Qt.formatDateTime(date, "yyyy-MM-dd hh:mm");
|
||||
}
|
||||
|
||||
// Process to check for evolution-data-server libraries
|
||||
@@ -140,15 +139,15 @@ Singleton {
|
||||
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
const result = text.trim()
|
||||
root.available = result === "available"
|
||||
const result = text.trim();
|
||||
root.available = result === "available";
|
||||
|
||||
if (root.available) {
|
||||
Logger.i("Calendar", "EDS libraries available")
|
||||
loadCalendars()
|
||||
Logger.i("Calendar", "EDS libraries available");
|
||||
loadCalendars();
|
||||
} else {
|
||||
Logger.w("Calendar", "EDS libraries not available: " + result)
|
||||
root.lastError = "Evolution Data Server libraries not installed"
|
||||
Logger.w("Calendar", "EDS libraries not available: " + result);
|
||||
root.lastError = "Evolution Data Server libraries not installed";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,9 +155,9 @@ Singleton {
|
||||
stderr: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (text.trim()) {
|
||||
Logger.d("Calendar", "Availability check error: " + text)
|
||||
root.available = false
|
||||
root.lastError = "Failed to check library availability"
|
||||
Logger.d("Calendar", "Availability check error: " + text);
|
||||
root.available = false;
|
||||
root.lastError = "Failed to check library availability";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,24 +172,24 @@ Singleton {
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
try {
|
||||
const result = JSON.parse(text.trim())
|
||||
root.calendars = result
|
||||
cacheAdapter.cachedCalendars = result
|
||||
saveCache()
|
||||
const result = JSON.parse(text.trim());
|
||||
root.calendars = result;
|
||||
cacheAdapter.cachedCalendars = result;
|
||||
saveCache();
|
||||
|
||||
Logger.d("Calendar", `Found ${result.length} calendar(s)`)
|
||||
Logger.d("Calendar", `Found ${result.length} calendar(s)`);
|
||||
|
||||
// Auto-load events after discovering calendars
|
||||
// Only load if we have calendars and no cached events
|
||||
if (result.length > 0 && root.events.length === 0) {
|
||||
loadEvents()
|
||||
loadEvents();
|
||||
} else if (result.length > 0) {
|
||||
// If we already have cached events, load in background
|
||||
loadEvents()
|
||||
loadEvents();
|
||||
}
|
||||
} catch (e) {
|
||||
Logger.d("Calendar", "Failed to parse calendars: " + e)
|
||||
root.lastError = "Failed to parse calendar list"
|
||||
Logger.d("Calendar", "Failed to parse calendars: " + e);
|
||||
root.lastError = "Failed to parse calendar list";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -198,8 +197,8 @@ Singleton {
|
||||
stderr: StdioCollector {
|
||||
onStreamFinished: {
|
||||
if (text.trim()) {
|
||||
Logger.d("Calendar", "List calendars error: " + text)
|
||||
root.lastError = text.trim()
|
||||
Logger.d("Calendar", "List calendars error: " + text);
|
||||
root.lastError = text.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,24 +215,24 @@ Singleton {
|
||||
|
||||
stdout: StdioCollector {
|
||||
onStreamFinished: {
|
||||
root.loading = false
|
||||
root.loading = false;
|
||||
|
||||
try {
|
||||
const result = JSON.parse(text.trim())
|
||||
root.events = result
|
||||
cacheAdapter.cachedEvents = result
|
||||
cacheAdapter.lastUpdate = new Date().toISOString()
|
||||
saveCache()
|
||||
const result = JSON.parse(text.trim());
|
||||
root.events = result;
|
||||
cacheAdapter.cachedEvents = result;
|
||||
cacheAdapter.lastUpdate = new Date().toISOString();
|
||||
saveCache();
|
||||
|
||||
Logger.d("Calendar", `Loaded ${result.length} event(s)`)
|
||||
Logger.d("Calendar", `Loaded ${result.length} event(s)`);
|
||||
} catch (e) {
|
||||
Logger.d("Calendar", "Failed to parse events: " + e)
|
||||
root.lastError = "Failed to parse events"
|
||||
Logger.d("Calendar", "Failed to parse events: " + e);
|
||||
root.lastError = "Failed to parse events";
|
||||
|
||||
// Fall back to cached events if available
|
||||
if (cacheAdapter.cachedEvents.length > 0) {
|
||||
root.events = cacheAdapter.cachedEvents
|
||||
Logger.d("Calendar", "Using cached events")
|
||||
root.events = cacheAdapter.cachedEvents;
|
||||
Logger.d("Calendar", "Using cached events");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -241,16 +240,16 @@ Singleton {
|
||||
|
||||
stderr: StdioCollector {
|
||||
onStreamFinished: {
|
||||
root.loading = false
|
||||
root.loading = false;
|
||||
|
||||
if (text.trim()) {
|
||||
Logger.d("Calendar", "Load events error: " + text)
|
||||
root.lastError = text.trim()
|
||||
Logger.d("Calendar", "Load events error: " + text);
|
||||
root.lastError = text.trim();
|
||||
|
||||
// Fall back to cached events if available
|
||||
if (cacheAdapter.cachedEvents.length > 0) {
|
||||
root.events = cacheAdapter.cachedEvents
|
||||
Logger.d("Calendar", "Using cached events due to error")
|
||||
root.events = cacheAdapter.cachedEvents;
|
||||
Logger.d("Calendar", "Using cached events due to error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ Singleton {
|
||||
enabled: Settings.data.colorSchemes.schedulingMode == "location"
|
||||
function onWeatherChanged() {
|
||||
if (LocationService.data.weather !== null) {
|
||||
const changes = root.collectWeatherChanges(LocationService.data.weather)
|
||||
const changes = root.collectWeatherChanges(LocationService.data.weather);
|
||||
if (!root.initComplete) {
|
||||
root.initComplete = true
|
||||
root.applyCurrentMode(changes)
|
||||
root.initComplete = true;
|
||||
root.applyCurrentMode(changes);
|
||||
}
|
||||
root.scheduleNextMode(changes)
|
||||
root.scheduleNextMode(changes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,140 +29,145 @@ Singleton {
|
||||
target: Settings.data.colorSchemes
|
||||
enabled: Settings.data.colorSchemes.schedulingMode == "manual"
|
||||
function onManualSunriseChanged() {
|
||||
const changes = root.collectManualChanges()
|
||||
root.applyCurrentMode(changes)
|
||||
root.scheduleNextMode(changes)
|
||||
const changes = root.collectManualChanges();
|
||||
root.applyCurrentMode(changes);
|
||||
root.scheduleNextMode(changes);
|
||||
}
|
||||
function onManualSunsetChanged() {
|
||||
const changes = root.collectManualChanges()
|
||||
root.applyCurrentMode(changes)
|
||||
root.scheduleNextMode(changes)
|
||||
const changes = root.collectManualChanges();
|
||||
root.applyCurrentMode(changes);
|
||||
root.scheduleNextMode(changes);
|
||||
}
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: Settings.data.colorSchemes
|
||||
function onSchedulingModeChanged() {
|
||||
root.init()
|
||||
root.init();
|
||||
}
|
||||
}
|
||||
|
||||
Timer {
|
||||
id: timer
|
||||
onTriggered: {
|
||||
Settings.data.colorSchemes.darkMode = root.nextDarkModeState
|
||||
Settings.data.colorSchemes.darkMode = root.nextDarkModeState;
|
||||
if (LocationService.data.weather !== null) {
|
||||
const changes = root.collectWeatherChanges(LocationService.data.weather)
|
||||
root.scheduleNextMode(changes)
|
||||
const changes = root.collectWeatherChanges(LocationService.data.weather);
|
||||
root.scheduleNextMode(changes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
Logger.i("DarkModeService", "Service started")
|
||||
Logger.i("DarkModeService", "Service started");
|
||||
|
||||
if (Settings.data.colorSchemes.schedulingMode == "manual") {
|
||||
const changes = collectManualChanges()
|
||||
initComplete = true
|
||||
applyCurrentMode(changes)
|
||||
scheduleNextMode(changes)
|
||||
const changes = collectManualChanges();
|
||||
initComplete = true;
|
||||
applyCurrentMode(changes);
|
||||
scheduleNextMode(changes);
|
||||
}
|
||||
|
||||
if (Settings.data.colorSchemes.schedulingMode == "location" && LocationService.data.weather) {
|
||||
const changes = collectWeatherChanges(LocationService.data.weather)
|
||||
initComplete = true
|
||||
applyCurrentMode(changes)
|
||||
scheduleNextMode(changes)
|
||||
const changes = collectWeatherChanges(LocationService.data.weather);
|
||||
initComplete = true;
|
||||
applyCurrentMode(changes);
|
||||
scheduleNextMode(changes);
|
||||
}
|
||||
}
|
||||
|
||||
function parseTime(timeString) {
|
||||
const parts = timeString.split(":").map(Number)
|
||||
const parts = timeString.split(":").map(Number);
|
||||
return {
|
||||
"hour": parts[0],
|
||||
"minute": parts[1]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function collectManualChanges() {
|
||||
const sunriseTime = parseTime(Settings.data.colorSchemes.manualSunrise)
|
||||
const sunsetTime = parseTime(Settings.data.colorSchemes.manualSunset)
|
||||
const sunriseTime = parseTime(Settings.data.colorSchemes.manualSunrise);
|
||||
const sunsetTime = parseTime(Settings.data.colorSchemes.manualSunset);
|
||||
|
||||
const now = new Date()
|
||||
const year = now.getFullYear()
|
||||
const month = now.getMonth()
|
||||
const day = now.getDate()
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = now.getMonth();
|
||||
const day = now.getDate();
|
||||
|
||||
const yesterdaysSunset = new Date(year, month, day - 1, sunsetTime.hour, sunsetTime.minute)
|
||||
const todaysSunrise = new Date(year, month, day, sunriseTime.hour, sunriseTime.minute)
|
||||
const todaysSunset = new Date(year, month, day, sunsetTime.hour, sunsetTime.minute)
|
||||
const tomorrowsSunrise = new Date(year, month, day + 1, sunriseTime.hour, sunriseTime.minute)
|
||||
const yesterdaysSunset = new Date(year, month, day - 1, sunsetTime.hour, sunsetTime.minute);
|
||||
const todaysSunrise = new Date(year, month, day, sunriseTime.hour, sunriseTime.minute);
|
||||
const todaysSunset = new Date(year, month, day, sunsetTime.hour, sunsetTime.minute);
|
||||
const tomorrowsSunrise = new Date(year, month, day + 1, sunriseTime.hour, sunriseTime.minute);
|
||||
|
||||
return [{
|
||||
"time": yesterdaysSunset.getTime(),
|
||||
"darkMode": true
|
||||
}, {
|
||||
"time": todaysSunrise.getTime(),
|
||||
"darkMode": false
|
||||
}, {
|
||||
"time": todaysSunset.getTime(),
|
||||
"darkMode": true
|
||||
}, {
|
||||
"time": tomorrowsSunrise.getTime(),
|
||||
"darkMode": false
|
||||
}]
|
||||
return [
|
||||
{
|
||||
"time": yesterdaysSunset.getTime(),
|
||||
"darkMode": true
|
||||
},
|
||||
{
|
||||
"time": todaysSunrise.getTime(),
|
||||
"darkMode": false
|
||||
},
|
||||
{
|
||||
"time": todaysSunset.getTime(),
|
||||
"darkMode": true
|
||||
},
|
||||
{
|
||||
"time": tomorrowsSunrise.getTime(),
|
||||
"darkMode": false
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function collectWeatherChanges(weather) {
|
||||
const changes = []
|
||||
const changes = [];
|
||||
|
||||
if (Date.now() < Date.parse(weather.daily.sunrise[0])) {
|
||||
// The sun has not risen yet
|
||||
changes.push({
|
||||
"time": Date.now() - 1,
|
||||
"darkMode": true
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
for (var i = 0; i < weather.daily.sunrise.length; i++) {
|
||||
changes.push({
|
||||
"time": Date.parse(weather.daily.sunrise[i]),
|
||||
"darkMode": false
|
||||
})
|
||||
});
|
||||
changes.push({
|
||||
"time": Date.parse(weather.daily.sunset[i]),
|
||||
"darkMode": true
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
return changes
|
||||
return changes;
|
||||
}
|
||||
|
||||
function applyCurrentMode(changes) {
|
||||
const now = Date.now()
|
||||
const now = Date.now();
|
||||
|
||||
// changes.findLast(change => change.time < now) // not available in QML...
|
||||
let lastChange = null
|
||||
let lastChange = null;
|
||||
for (var i = 0; i < changes.length; i++) {
|
||||
if (changes[i].time < now) {
|
||||
lastChange = changes[i]
|
||||
lastChange = changes[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (lastChange) {
|
||||
Settings.data.colorSchemes.darkMode = lastChange.darkMode
|
||||
Logger.d("DarkModeService", `Reset: darkmode=${lastChange.darkMode}`)
|
||||
Settings.data.colorSchemes.darkMode = lastChange.darkMode;
|
||||
Logger.d("DarkModeService", `Reset: darkmode=${lastChange.darkMode}`);
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleNextMode(changes) {
|
||||
const now = Date.now()
|
||||
const nextChange = changes.find(change => change.time > now)
|
||||
const now = Date.now();
|
||||
const nextChange = changes.find(change => change.time > now);
|
||||
if (nextChange) {
|
||||
root.nextDarkModeState = nextChange.darkMode
|
||||
timer.interval = nextChange.time - now
|
||||
timer.restart()
|
||||
Logger.d("DarkModeService", `Scheduled: darkmode=${nextChange.darkMode} in ${timer.interval} ms`)
|
||||
root.nextDarkModeState = nextChange.darkMode;
|
||||
timer.interval = nextChange.time - now;
|
||||
timer.restart();
|
||||
Logger.d("DarkModeService", `Scheduled: darkmode=${nextChange.darkMode} in ${timer.interval} ms`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,19 +27,19 @@ Singleton {
|
||||
printErrors: false
|
||||
onAdapterUpdated: saveTimer.start()
|
||||
onLoaded: {
|
||||
Logger.d("Location", "Loaded cached data")
|
||||
Logger.d("Location", "Loaded cached data");
|
||||
// Initialize stable properties on load
|
||||
if (adapter.latitude !== "" && adapter.longitude !== "" && adapter.weatherLastFetch > 0) {
|
||||
root.stableLatitude = adapter.latitude
|
||||
root.stableLongitude = adapter.longitude
|
||||
root.stableName = adapter.name
|
||||
root.coordinatesReady = true
|
||||
Logger.i("Location", "Coordinates ready")
|
||||
root.stableLatitude = adapter.latitude;
|
||||
root.stableLongitude = adapter.longitude;
|
||||
root.stableName = adapter.name;
|
||||
root.coordinatesReady = true;
|
||||
Logger.i("Location", "Coordinates ready");
|
||||
}
|
||||
updateWeather()
|
||||
updateWeather();
|
||||
}
|
||||
onLoadFailed: function (error) {
|
||||
updateWeather()
|
||||
updateWeather();
|
||||
}
|
||||
|
||||
JsonAdapter {
|
||||
@@ -57,11 +57,11 @@ Singleton {
|
||||
// Helper property for UI components (outside JsonAdapter to avoid binding loops)
|
||||
readonly property string displayCoordinates: {
|
||||
if (!root.coordinatesReady || root.stableLatitude === "" || root.stableLongitude === "") {
|
||||
return ""
|
||||
return "";
|
||||
}
|
||||
const lat = parseFloat(root.stableLatitude).toFixed(4)
|
||||
const lon = parseFloat(root.stableLongitude).toFixed(4)
|
||||
return `${lat}, ${lon}`
|
||||
const lat = parseFloat(root.stableLatitude).toFixed(4);
|
||||
const lon = parseFloat(root.stableLongitude).toFixed(4);
|
||||
return `${lat}, ${lon}`;
|
||||
}
|
||||
|
||||
// Every 20s check if we need to fetch new weather
|
||||
@@ -71,7 +71,7 @@ Singleton {
|
||||
running: Settings.data.location.weatherEnabled || Settings.data.colorSchemes.schedulingMode == "location"
|
||||
repeat: true
|
||||
onTriggered: {
|
||||
updateWeather()
|
||||
updateWeather();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,195 +86,194 @@ Singleton {
|
||||
function init() {
|
||||
// does nothing but ensure the singleton is created
|
||||
// do not remove
|
||||
Logger.i("Location", "Service started")
|
||||
Logger.i("Location", "Service started");
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function resetWeather() {
|
||||
Logger.i("Location", "Resetting weather data")
|
||||
Logger.i("Location", "Resetting weather data");
|
||||
|
||||
// Mark as changing to prevent UI updates
|
||||
root.coordinatesReady = false
|
||||
root.coordinatesReady = false;
|
||||
// Reset stable properties
|
||||
root.stableLatitude = ""
|
||||
root.stableLongitude = ""
|
||||
root.stableName = ""
|
||||
root.stableLatitude = "";
|
||||
root.stableLongitude = "";
|
||||
root.stableName = "";
|
||||
|
||||
// Reset core data
|
||||
adapter.latitude = ""
|
||||
adapter.longitude = ""
|
||||
adapter.name = ""
|
||||
adapter.weatherLastFetch = 0
|
||||
adapter.weather = null
|
||||
adapter.latitude = "";
|
||||
adapter.longitude = "";
|
||||
adapter.name = "";
|
||||
adapter.weatherLastFetch = 0;
|
||||
adapter.weather = null;
|
||||
|
||||
// Try to fetch immediately
|
||||
updateWeather()
|
||||
updateWeather();
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function updateWeather() {
|
||||
if (!Settings.data.location.weatherEnabled) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
if (isFetchingWeather) {
|
||||
Logger.w("Location", "Weather is still fetching")
|
||||
return
|
||||
Logger.w("Location", "Weather is still fetching");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((adapter.weatherLastFetch === "") || (adapter.weather === null) || (adapter.latitude === "") || (adapter.longitude === "") || (adapter.name !== Settings.data.location.name) || (Time.timestamp >= adapter.weatherLastFetch + weatherUpdateFrequency)) {
|
||||
getFreshWeather()
|
||||
getFreshWeather();
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function getFreshWeather() {
|
||||
isFetchingWeather = true
|
||||
isFetchingWeather = true;
|
||||
|
||||
// Check if location name has changed
|
||||
const locationChanged = data.name !== Settings.data.location.name
|
||||
const locationChanged = data.name !== Settings.data.location.name;
|
||||
if (locationChanged) {
|
||||
root.coordinatesReady = false
|
||||
Logger.d("Location", "Location changed from", adapter.name, "to", Settings.data.location.name)
|
||||
root.coordinatesReady = false;
|
||||
Logger.d("Location", "Location changed from", adapter.name, "to", Settings.data.location.name);
|
||||
}
|
||||
|
||||
if ((adapter.latitude === "") || (adapter.longitude === "") || locationChanged) {
|
||||
|
||||
_geocodeLocation(Settings.data.location.name, function (latitude, longitude, name, country) {
|
||||
Logger.d("Location", "Geocoded", Settings.data.location.name, "to:", latitude, "/", longitude)
|
||||
Logger.d("Location", "Geocoded", Settings.data.location.name, "to:", latitude, "/", longitude);
|
||||
|
||||
// Save location name
|
||||
adapter.name = Settings.data.location.name
|
||||
adapter.name = Settings.data.location.name;
|
||||
|
||||
// Save GPS coordinates
|
||||
adapter.latitude = latitude.toString()
|
||||
adapter.longitude = longitude.toString()
|
||||
adapter.latitude = latitude.toString();
|
||||
adapter.longitude = longitude.toString();
|
||||
|
||||
root.stableName = `${name}, ${country}`
|
||||
root.stableName = `${name}, ${country}`;
|
||||
|
||||
_fetchWeather(latitude, longitude, errorCallback)
|
||||
}, errorCallback)
|
||||
_fetchWeather(latitude, longitude, errorCallback);
|
||||
}, errorCallback);
|
||||
} else {
|
||||
_fetchWeather(adapter.latitude, adapter.longitude, errorCallback)
|
||||
_fetchWeather(adapter.latitude, adapter.longitude, errorCallback);
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function _geocodeLocation(locationName, callback, errorCallback) {
|
||||
Logger.d("Location", "Geocoding location name")
|
||||
var geoUrl = "https://assets.noctalia.dev/geocode.php?city=" + encodeURIComponent(locationName) + "&language=en&format=json"
|
||||
var xhr = new XMLHttpRequest()
|
||||
Logger.d("Location", "Geocoding location name");
|
||||
var geoUrl = "https://assets.noctalia.dev/geocode.php?city=" + encodeURIComponent(locationName) + "&language=en&format=json";
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
try {
|
||||
var geoData = JSON.parse(xhr.responseText)
|
||||
var geoData = JSON.parse(xhr.responseText);
|
||||
if (geoData.lat != null) {
|
||||
callback(geoData.lat, geoData.lng, geoData.name, geoData.country)
|
||||
callback(geoData.lat, geoData.lng, geoData.name, geoData.country);
|
||||
} else {
|
||||
errorCallback("Location", "could not resolve location name")
|
||||
errorCallback("Location", "could not resolve location name");
|
||||
}
|
||||
} catch (e) {
|
||||
errorCallback("Location", "Failed to parse geocoding data: " + e)
|
||||
errorCallback("Location", "Failed to parse geocoding data: " + e);
|
||||
}
|
||||
} else {
|
||||
errorCallback("Location", "Geocoding error: " + xhr.status)
|
||||
errorCallback("Location", "Geocoding error: " + xhr.status);
|
||||
}
|
||||
}
|
||||
}
|
||||
xhr.open("GET", geoUrl)
|
||||
xhr.send()
|
||||
};
|
||||
xhr.open("GET", geoUrl);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function _fetchWeather(latitude, longitude, errorCallback) {
|
||||
Logger.d("Location", "Fetching weather from api.open-meteo.com")
|
||||
var url = "https://api.open-meteo.com/v1/forecast?latitude=" + latitude + "&longitude=" + longitude + "¤t_weather=true¤t=relativehumidity_2m,surface_pressure&daily=temperature_2m_max,temperature_2m_min,weathercode,sunset,sunrise&timezone=auto"
|
||||
var xhr = new XMLHttpRequest()
|
||||
Logger.d("Location", "Fetching weather from api.open-meteo.com");
|
||||
var url = "https://api.open-meteo.com/v1/forecast?latitude=" + latitude + "&longitude=" + longitude + "¤t_weather=true¤t=relativehumidity_2m,surface_pressure&daily=temperature_2m_max,temperature_2m_min,weathercode,sunset,sunrise&timezone=auto";
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
try {
|
||||
var weatherData = JSON.parse(xhr.responseText)
|
||||
var weatherData = JSON.parse(xhr.responseText);
|
||||
//console.log(JSON.stringify(weatherData))
|
||||
|
||||
// Save core data
|
||||
data.weather = weatherData
|
||||
data.weatherLastFetch = Time.timestamp
|
||||
data.weather = weatherData;
|
||||
data.weatherLastFetch = Time.timestamp;
|
||||
|
||||
// Update stable display values only when complete and successful
|
||||
root.stableLatitude = data.latitude = weatherData.latitude.toString()
|
||||
root.stableLongitude = data.longitude = weatherData.longitude.toString()
|
||||
root.coordinatesReady = true
|
||||
root.stableLatitude = data.latitude = weatherData.latitude.toString();
|
||||
root.stableLongitude = data.longitude = weatherData.longitude.toString();
|
||||
root.coordinatesReady = true;
|
||||
|
||||
isFetchingWeather = false
|
||||
Logger.i("Location", "Cached weather to disk - stable coordinates updated")
|
||||
isFetchingWeather = false;
|
||||
Logger.i("Location", "Cached weather to disk - stable coordinates updated");
|
||||
} catch (e) {
|
||||
errorCallback("Location", "Failed to parse weather data")
|
||||
errorCallback("Location", "Failed to parse weather data");
|
||||
}
|
||||
} else {
|
||||
errorCallback("Location", "Weather fetch error: " + xhr.status)
|
||||
errorCallback("Location", "Weather fetch error: " + xhr.status);
|
||||
}
|
||||
}
|
||||
}
|
||||
xhr.open("GET", url)
|
||||
xhr.send()
|
||||
};
|
||||
xhr.open("GET", url);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function errorCallback(module, message) {
|
||||
Logger.e(module, message)
|
||||
isFetchingWeather = false
|
||||
Logger.e(module, message);
|
||||
isFetchingWeather = false;
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function weatherSymbolFromCode(code) {
|
||||
if (code === 0)
|
||||
return "weather-sun"
|
||||
return "weather-sun";
|
||||
if (code === 1 || code === 2)
|
||||
return "weather-cloud-sun"
|
||||
return "weather-cloud-sun";
|
||||
if (code === 3)
|
||||
return "weather-cloud"
|
||||
return "weather-cloud";
|
||||
if (code >= 45 && code <= 48)
|
||||
return "weather-cloud-haze"
|
||||
return "weather-cloud-haze";
|
||||
if (code >= 51 && code <= 67)
|
||||
return "weather-cloud-rain"
|
||||
return "weather-cloud-rain";
|
||||
if (code >= 71 && code <= 77)
|
||||
return "weather-cloud-snow"
|
||||
return "weather-cloud-snow";
|
||||
if (code >= 71 && code <= 77)
|
||||
return "weather-cloud-snow"
|
||||
return "weather-cloud-snow";
|
||||
if (code >= 85 && code <= 86)
|
||||
return "weather-cloud-snow"
|
||||
return "weather-cloud-snow";
|
||||
if (code >= 95 && code <= 99)
|
||||
return "weather-cloud-lightning"
|
||||
return "weather-cloud"
|
||||
return "weather-cloud-lightning";
|
||||
return "weather-cloud";
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function weatherDescriptionFromCode(code) {
|
||||
if (code === 0)
|
||||
return "Clear sky"
|
||||
return "Clear sky";
|
||||
if (code === 1)
|
||||
return "Mainly clear"
|
||||
return "Mainly clear";
|
||||
if (code === 2)
|
||||
return "Partly cloudy"
|
||||
return "Partly cloudy";
|
||||
if (code === 3)
|
||||
return "Overcast"
|
||||
return "Overcast";
|
||||
if (code === 45 || code === 48)
|
||||
return "Fog"
|
||||
return "Fog";
|
||||
if (code >= 51 && code <= 67)
|
||||
return "Drizzle"
|
||||
return "Drizzle";
|
||||
if (code >= 71 && code <= 77)
|
||||
return "Snow"
|
||||
return "Snow";
|
||||
if (code >= 80 && code <= 82)
|
||||
return "Rain showers"
|
||||
return "Rain showers";
|
||||
if (code >= 95 && code <= 99)
|
||||
return "Thunderstorm"
|
||||
return "Unknown"
|
||||
return "Thunderstorm";
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
function celsiusToFahrenheit(celsius) {
|
||||
return 32 + celsius * 1.8
|
||||
return 32 + celsius * 1.8;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,66 +16,66 @@ Singleton {
|
||||
function apply() {
|
||||
// If using LocationService, wait for it to be ready
|
||||
if (!params.forced && params.autoSchedule && !LocationService.coordinatesReady) {
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
var command = buildCommand()
|
||||
var command = buildCommand();
|
||||
|
||||
// Compare with previous command to avoid unecessary restart
|
||||
if (JSON.stringify(command) !== JSON.stringify(lastCommand)) {
|
||||
lastCommand = command
|
||||
runner.command = command
|
||||
lastCommand = command;
|
||||
runner.command = command;
|
||||
|
||||
// Set running to false so it may restarts below if still enabled
|
||||
runner.running = false
|
||||
runner.running = false;
|
||||
}
|
||||
runner.running = params.enabled
|
||||
runner.running = params.enabled;
|
||||
}
|
||||
|
||||
function buildCommand() {
|
||||
var cmd = ["wlsunset"]
|
||||
var cmd = ["wlsunset"];
|
||||
if (params.forced) {
|
||||
// Force immediate full night temperature regardless of time
|
||||
// Keep distinct day/night temps but set times so we're effectively always in "night"
|
||||
cmd.push("-t", `${params.nightTemp}`, "-T", `${params.dayTemp}`)
|
||||
cmd.push("-t", `${params.nightTemp}`, "-T", `${params.dayTemp}`);
|
||||
// Night spans from sunset (00:00) to sunrise (23:59) covering almost the full day
|
||||
cmd.push("-S", "23:59") // sunrise very late
|
||||
cmd.push("-s", "00:00") // sunset at midnight
|
||||
cmd.push("-S", "23:59"); // sunrise very late
|
||||
cmd.push("-s", "00:00"); // sunset at midnight
|
||||
// Near-instant transition
|
||||
cmd.push("-d", 1)
|
||||
cmd.push("-d", 1);
|
||||
} else {
|
||||
cmd.push("-t", `${params.nightTemp}`, "-T", `${params.dayTemp}`)
|
||||
cmd.push("-t", `${params.nightTemp}`, "-T", `${params.dayTemp}`);
|
||||
if (params.autoSchedule) {
|
||||
cmd.push("-l", `${LocationService.stableLatitude}`, "-L", `${LocationService.stableLongitude}`)
|
||||
cmd.push("-l", `${LocationService.stableLatitude}`, "-L", `${LocationService.stableLongitude}`);
|
||||
} else {
|
||||
cmd.push("-S", params.manualSunrise)
|
||||
cmd.push("-s", params.manualSunset)
|
||||
cmd.push("-S", params.manualSunrise);
|
||||
cmd.push("-s", params.manualSunset);
|
||||
}
|
||||
cmd.push("-d", 60 * 15) // 15min progressive fade at sunset/sunrise
|
||||
cmd.push("-d", 60 * 15); // 15min progressive fade at sunset/sunrise
|
||||
}
|
||||
return cmd
|
||||
return cmd;
|
||||
}
|
||||
|
||||
// Observe setting changes and location readiness
|
||||
Connections {
|
||||
target: Settings.data.nightLight
|
||||
function onEnabledChanged() {
|
||||
apply()
|
||||
apply();
|
||||
// Toast: night light toggled
|
||||
const enabled = !!Settings.data.nightLight.enabled
|
||||
ToastService.showNotice(I18n.tr("settings.display.night-light.section.label"), enabled ? I18n.tr("toast.night-light.enabled") : I18n.tr("toast.night-light.disabled"), enabled ? "nightlight-on" : "nightlight-off")
|
||||
const enabled = !!Settings.data.nightLight.enabled;
|
||||
ToastService.showNotice(I18n.tr("settings.display.night-light.section.label"), enabled ? I18n.tr("toast.night-light.enabled") : I18n.tr("toast.night-light.disabled"), enabled ? "nightlight-on" : "nightlight-off");
|
||||
}
|
||||
function onForcedChanged() {
|
||||
apply()
|
||||
apply();
|
||||
if (Settings.data.nightLight.enabled) {
|
||||
ToastService.showNotice(I18n.tr("settings.display.night-light.section.label"), Settings.data.nightLight.forced ? I18n.tr("toast.night-light.forced") : I18n.tr("toast.night-light.normal"), Settings.data.nightLight.forced ? "nightlight-forced" : "nightlight-on")
|
||||
ToastService.showNotice(I18n.tr("settings.display.night-light.section.label"), Settings.data.nightLight.forced ? I18n.tr("toast.night-light.forced") : I18n.tr("toast.night-light.normal"), Settings.data.nightLight.forced ? "nightlight-forced" : "nightlight-on");
|
||||
}
|
||||
}
|
||||
function onNightTempChanged() {
|
||||
apply()
|
||||
apply();
|
||||
}
|
||||
function onDayTempChanged() {
|
||||
apply()
|
||||
apply();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ Singleton {
|
||||
target: LocationService
|
||||
function onCoordinatesReadyChanged() {
|
||||
if (LocationService.coordinatesReady) {
|
||||
apply()
|
||||
apply();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,10 +93,10 @@ Singleton {
|
||||
id: runner
|
||||
running: false
|
||||
onStarted: {
|
||||
Logger.i("NightLight", "Wlsunset started:", runner.command)
|
||||
Logger.i("NightLight", "Wlsunset started:", runner.command);
|
||||
}
|
||||
onExited: function (code, status) {
|
||||
Logger.i("NightLight", "Wlsunset exited:", code, status)
|
||||
Logger.i("NightLight", "Wlsunset exited:", code, status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user