1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-03 20:32:07 -04:00

weather: fix fallback temporarily

This commit is contained in:
bbedward
2026-02-27 22:37:10 -05:00
parent 7bea6b4a62
commit b9e9da579f
6 changed files with 86 additions and 21 deletions

View File

@@ -3,12 +3,23 @@ package geolocation
import "github.com/AvengeMedia/DankMaterialShell/core/internal/log" import "github.com/AvengeMedia/DankMaterialShell/core/internal/log"
func NewClient() Client { func NewClient() Client {
if geoclueClient, err := newGeoClueClient(); err != nil { geoclueClient, err := newGeoClueClient()
if err != nil {
log.Warnf("Failed to initialize GeoClue2 client: %v", err) log.Warnf("Failed to initialize GeoClue2 client: %v", err)
} else { log.Info("Falling back to IP location")
return newIpClient()
}
loc, _ := geoclueClient.GetLocation()
if loc.Latitude != 0 || loc.Longitude != 0 {
return geoclueClient return geoclueClient
} }
log.Info("Falling back to IP location") log.Info("GeoClue2 has no fix yet, seeding with IP location")
return newIpClient() ipClient := newIpClient()
if ipLoc, err := ipClient.GetLocation(); err == nil {
geoclueClient.SeedLocation(ipLoc)
}
return geoclueClient
} }

View File

@@ -222,6 +222,13 @@ func (c *GeoClueClient) notifySubscribers() {
}) })
} }
func (c *GeoClueClient) SeedLocation(loc Location) {
c.locationMutex.Lock()
defer c.locationMutex.Unlock()
c.currLocation.Latitude = loc.Latitude
c.currLocation.Longitude = loc.Longitude
}
func (c *GeoClueClient) GetLocation() (Location, error) { func (c *GeoClueClient) GetLocation() (Location, error) {
c.locationMutex.RLock() c.locationMutex.RLock()
defer c.locationMutex.RUnlock() defer c.locationMutex.RUnlock()

View File

@@ -31,10 +31,8 @@ func newIpClient() *IpClient {
func (c *IpClient) Subscribe(id string) chan Location { func (c *IpClient) Subscribe(id string) chan Location {
ch := make(chan Location, 1) ch := make(chan Location, 1)
if location, err := c.GetLocation(); err != nil { if location, err := c.GetLocation(); err == nil {
ch <- location ch <- location
} else {
close(ch)
} }
return ch return ch

View File

@@ -14,8 +14,9 @@ func NewManager(client geolocation.Client) (*Manager, error) {
} }
m := &Manager{ m := &Manager{
client: client, client: client,
dirty: make(chan struct{}), dirty: make(chan struct{}),
stopChan: make(chan struct{}),
state: &State{ state: &State{
Latitude: currLocation.Latitude, Latitude: currLocation.Latitude,

View File

@@ -10,14 +10,17 @@ Singleton {
id: root id: root
readonly property bool locationAvailable: DMSService.isConnected && (DMSService.capabilities.length === 0 || DMSService.capabilities.includes("location")) readonly property bool locationAvailable: DMSService.isConnected && (DMSService.capabilities.length === 0 || DMSService.capabilities.includes("location"))
readonly property bool valid: latitude !== 0 || longitude !== 0
property var latitude: 0.0 property var latitude: 0.0
property var longitude: 0.0 property var longitude: 0.0
signal locationChanged(var data) signal locationChanged(var data)
readonly property var lowPriorityCmd: ["nice", "-n", "19", "ionice", "-c3"]
readonly property var curlBaseCmd: ["curl", "-sS", "--fail", "--connect-timeout", "3", "--max-time", "6", "--limit-rate", "100k", "--compressed"]
Component.onCompleted: { Component.onCompleted: {
console.info("LocationService: Initializing...");
getState(); getState();
} }
@@ -25,27 +28,68 @@ Singleton {
target: DMSService target: DMSService
function onLocationStateUpdate(data) { function onLocationStateUpdate(data) {
if (locationAvailable) { if (!locationAvailable)
handleStateUpdate(data); return;
} handleStateUpdate(data);
} }
} }
function handleStateUpdate(data) { function handleStateUpdate(data) {
root.latitude = data.latitude; const lat = data.latitude;
root.longitude = data.longitude; const lon = data.longitude;
if (lat === 0 && lon === 0)
return;
root.locationChanged(data) root.latitude = lat;
root.longitude = lon;
root.locationChanged(data);
} }
function getState() { function getState() {
if (!locationAvailable) if (!locationAvailable) {
fetchIPLocation();
return; return;
}
DMSService.sendRequest("location.getState", null, response => { DMSService.sendRequest("location.getState", null, response => {
if (response.result) { if (response.result && (response.result.latitude !== 0 || response.result.longitude !== 0)) {
handleStateUpdate(response.result); handleStateUpdate(response.result);
return;
} }
fetchIPLocation();
}); });
} }
function fetchIPLocation() {
if (root.valid)
return;
ipLocationFetcher.running = true;
}
Process {
id: ipLocationFetcher
command: root.lowPriorityCmd.concat(root.curlBaseCmd).concat(["http://ip-api.com/json/"])
running: false
stdout: StdioCollector {
onStreamFinished: {
const raw = text.trim();
if (!raw || raw[0] !== "{")
return;
try {
const data = JSON.parse(raw);
if (data.status === "fail")
return;
const lat = parseFloat(data.lat);
const lon = parseFloat(data.lon);
if (isNaN(lat) || isNaN(lon) || (lat === 0 && lon === 0))
return;
root.handleStateUpdate({ latitude: lat, longitude: lon });
} catch (e) {}
}
}
}
} }

View File

@@ -512,6 +512,8 @@ Singleton {
} }
function getLocationFromService() { function getLocationFromService() {
if (!LocationService.valid)
return;
getLocationFromCoords(LocationService.latitude, LocationService.longitude); getLocationFromCoords(LocationService.latitude, LocationService.longitude);
} }
@@ -829,9 +831,11 @@ Singleton {
target: LocationService target: LocationService
function onLocationChanged(data) { function onLocationChanged(data) {
if (SettingsData.useAutoLocation) { if (!SettingsData.useAutoLocation)
root.getLocationFromCoords(data.latitude, data.longitude) return;
} if (data.latitude === 0 && data.longitude === 0)
return;
root.getLocationFromCoords(data.latitude, data.longitude);
} }
} }