Skip to content

Commit

Permalink
Implemented coordinate lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderfrangos committed Dec 12, 2023
1 parent 91df170 commit 461f4b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/components/SettingsWindow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ export default class SettingsWindow extends PureComponent {
<p><a className="button" onClick={this.addAdjustmentTime}>+ {T.t("SETTINGS_TIME_ADD")}</a></p>
</div>
<div className="pageSection">
<SettingsOption title={"Coordinates for sun position"} description={"To use \"sun position\" for time adjustments, enter your current latitude and longitude so the correct times can be determined. Twinkle Tray does not detect this for you."} expandable={true}>
<SettingsOption title={"Coordinates for sun position"} description={"To use \"sun position\" for time adjustments, enter your current latitude and longitude so the correct times can be determined."} expandable={true}>
<SettingsChild>
<div style={{ "display": "flex" }}>
<div style={{ marginRight: "6px", flex: 1 }}>
Expand All @@ -1088,6 +1088,8 @@ export default class SettingsWindow extends PureComponent {
<label style={{ "textTransform": "capitalize" }}>Longitude</label>
<input type="number" min="-180" max="180" value={window.settings.adjustmentTimeLongitude * 1} onChange={(e) => this.setSetting("adjustmentTimeLongitude", e.target.value)} style={{width: "100%", boxSizing: "border-box"}} />
</div>
{/* I'll write better CSS later, I promise. */}
<div><label style={{opacity:0}}>Get coordinates</label><input type="button" className="button" onClick={() => window.ipc.send("get-coordinates")} value="Get coordinates" style={{lineHeight:"1.3",padding:(document.body.dataset.isWin11 === 'true' ? "9px" : "8px"),marginLeft:"6px"}} /></div>
</div>
</SettingsChild>
</SettingsOption>
Expand Down
39 changes: 39 additions & 0 deletions src/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -3769,6 +3769,45 @@ function handleBackgroundUpdate(force = false) {
}, 1000)
}

let lastCoordCheck = { value: { lat: 0, long: 0}, ts: 0 }
async function getUserCoordinates() {
if(Date.now() - 10000 < lastCoordCheck.ts) return lastCoordCheck.value;
try {
const fetch = require('node-fetch')
if (isAppX === false) {
console.log("Getting geolocation...")
const response = await fetch("https://geo.twinkletray.com/")
if(response.status === 200) {
const coordinates = {
lat: response.headers.get("X-Geo-Lat"),
long: response.headers.get("X-Geo-Long")
}
if(typeof coordinates.lat === "string" && typeof coordinates.long === "string") {
console.log("Coordinates: ", coordinates)
lastCoordCheck.value = coordinates
lastCoordCheck.ts = Date.now()
return coordinates
}
throw("Couldn't get coordinates. Returned: " . JSON.stringify(coordinates))
}
}
} catch (e) {
console.log(e)
}
Utils.unloadModule("node-fetch")
}

async function getAndApplyUserCoordinates() {
try {
const coordinates = await getUserCoordinates()
writeSettings({adjustmentTimeLongitude: coordinates.long, adjustmentTimeLatitude: coordinates.lat}, true, true)
} catch(e) {
console.log(e)
}
}

ipcMain.on('get-coordinates', getAndApplyUserCoordinates)

/*
Handle input from second process command line. One monitor argument and one brightness argument is required. Multiple arguments will override each other.
Expand Down

0 comments on commit 461f4b4

Please sign in to comment.