config: dynamic service confs
This commit is contained in:
parent
f561b77ee3
commit
2d97968445
8 changed files with 82 additions and 29 deletions
20
config.ts
20
config.ts
|
|
@ -138,28 +138,28 @@ const config = {
|
||||||
},
|
},
|
||||||
// Services
|
// Services
|
||||||
math: {
|
math: {
|
||||||
maxHistory: 100,
|
maxHistory: s(100),
|
||||||
},
|
},
|
||||||
updates: {
|
updates: {
|
||||||
interval: 900000,
|
interval: s(900000),
|
||||||
},
|
},
|
||||||
weather: {
|
weather: {
|
||||||
interval: 600000,
|
interval: s(600000),
|
||||||
key: "assets/weather-api-key.txt", // Path to file containing api key relative to the base directory. To get a key, visit https://weatherapi.com/
|
key: s("assets/weather-api-key.txt"), // Path to file containing api key relative to the base directory. To get a key, visit https://weatherapi.com/
|
||||||
location: "", // Location as a string or empty to autodetect
|
location: s(""), // Location as a string or empty to autodetect
|
||||||
imperial: false,
|
imperial: s(false),
|
||||||
},
|
},
|
||||||
cpu: {
|
cpu: {
|
||||||
interval: 2000,
|
interval: s(2000),
|
||||||
},
|
},
|
||||||
gpu: {
|
gpu: {
|
||||||
interval: 2000,
|
interval: s(2000),
|
||||||
},
|
},
|
||||||
memory: {
|
memory: {
|
||||||
interval: 5000,
|
interval: s(5000),
|
||||||
},
|
},
|
||||||
storage: {
|
storage: {
|
||||||
interval: 5000,
|
interval: s(5000),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,10 @@ export default class Cpu extends GObject.Object {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
interval(config.interval, () => this.update());
|
let source = interval(config.interval.get(), () => this.update());
|
||||||
|
config.interval.subscribe(i => {
|
||||||
|
source.cancel();
|
||||||
|
source = interval(i, () => this.update());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,12 @@ export default class Gpu extends GObject.Object {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.available) interval(config.interval, () => this.update());
|
if (this.available) {
|
||||||
|
let source = interval(config.interval.get(), () => this.update());
|
||||||
|
config.interval.subscribe(i => {
|
||||||
|
source.cancel();
|
||||||
|
source = interval(i, () => this.update());
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@ export default class Math extends GObject.Object {
|
||||||
return this.instance;
|
return this.instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly #maxHistory = config.maxHistory;
|
|
||||||
readonly #path = `${STATE}/math-history.json`;
|
readonly #path = `${STATE}/math-history.json`;
|
||||||
readonly #history: HistoryItem[] = [];
|
readonly #history: HistoryItem[] = [];
|
||||||
|
|
||||||
|
|
@ -42,7 +41,7 @@ export default class Math extends GObject.Object {
|
||||||
// Try select first to prevent duplicates, if it fails, add it
|
// Try select first to prevent duplicates, if it fails, add it
|
||||||
if (!this.select(this.#lastExpression)) {
|
if (!this.select(this.#lastExpression)) {
|
||||||
this.#history.unshift(this.#lastExpression);
|
this.#history.unshift(this.#lastExpression);
|
||||||
if (this.#history.length > this.#maxHistory) this.#history.pop();
|
while (this.#history.length > config.maxHistory.get()) this.#history.pop();
|
||||||
this.notify("history");
|
this.notify("history");
|
||||||
this.#save();
|
this.#save();
|
||||||
}
|
}
|
||||||
|
|
@ -148,5 +147,9 @@ export default class Math extends GObject.Object {
|
||||||
console.error("Math - Unable to load history", e);
|
console.error("Math - Unable to load history", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.maxHistory.subscribe(n => {
|
||||||
|
while (this.#history.length > n) this.#history.pop();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,10 @@ export default class Memory extends GObject.Object {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
interval(config.interval, () => this.update().catch(console.error));
|
let source = interval(config.interval.get(), () => this.update().catch(console.error));
|
||||||
|
config.interval.subscribe(i => {
|
||||||
|
source.cancel();
|
||||||
|
source = interval(i, () => this.update().catch(console.error));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,10 @@ export default class Storage extends GObject.Object {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
interval(config.interval, () => this.update());
|
let source = interval(config.interval.get(), () => this.update());
|
||||||
|
config.interval.subscribe(i => {
|
||||||
|
source.cancel();
|
||||||
|
source = interval(i, () => this.update());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -151,7 +151,7 @@ export default class Updates extends GObject.Object {
|
||||||
this.notify("loading");
|
this.notify("loading");
|
||||||
|
|
||||||
this.#timeout?.destroy();
|
this.#timeout?.destroy();
|
||||||
this.#timeout = setTimeout(() => this.getUpdates(), config.interval);
|
this.#timeout = setTimeout(() => this.getUpdates(), config.interval.get());
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
}
|
}
|
||||||
|
|
@ -164,5 +164,10 @@ export default class Updates extends GObject.Object {
|
||||||
writeFileAsync(this.#cachePath, JSON.stringify(this.#data)).catch(console.error)
|
writeFileAsync(this.#cachePath, JSON.stringify(this.#data)).catch(console.error)
|
||||||
);
|
);
|
||||||
this.getUpdates();
|
this.getUpdates();
|
||||||
|
|
||||||
|
config.interval.subscribe(i => {
|
||||||
|
this.#timeout?.destroy();
|
||||||
|
this.#timeout = setTimeout(() => this.getUpdates(), i);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,16 @@
|
||||||
import { weatherIcons } from "@/utils/icons";
|
import { weatherIcons } from "@/utils/icons";
|
||||||
import { notify } from "@/utils/system";
|
import { notify } from "@/utils/system";
|
||||||
import { execAsync, GLib, GObject, interval, property, readFileAsync, register, writeFileAsync } from "astal";
|
import {
|
||||||
|
execAsync,
|
||||||
|
GLib,
|
||||||
|
GObject,
|
||||||
|
interval,
|
||||||
|
property,
|
||||||
|
readFileAsync,
|
||||||
|
register,
|
||||||
|
writeFileAsync,
|
||||||
|
type Time,
|
||||||
|
} from "astal";
|
||||||
import { weather as config } from "config";
|
import { weather as config } from "config";
|
||||||
|
|
||||||
export interface WeatherCondition {
|
export interface WeatherCondition {
|
||||||
|
|
@ -211,6 +221,8 @@ export default class Weather extends GObject.Object {
|
||||||
#key: string = "";
|
#key: string = "";
|
||||||
#data: WeatherData = DEFAULT;
|
#data: WeatherData = DEFAULT;
|
||||||
|
|
||||||
|
#interval: Time | null = null;
|
||||||
|
|
||||||
@property(Object)
|
@property(Object)
|
||||||
get raw() {
|
get raw() {
|
||||||
return this.#data;
|
return this.#data;
|
||||||
|
|
@ -243,8 +255,8 @@ export default class Weather extends GObject.Object {
|
||||||
|
|
||||||
@property(String)
|
@property(String)
|
||||||
get wind() {
|
get wind() {
|
||||||
return `${Math.round(this.#data.current[`wind_${config.imperial ? "m" : "k"}ph`])} ${
|
return `${Math.round(this.#data.current[`wind_${config.imperial.get() ? "m" : "k"}ph`])} ${
|
||||||
config.imperial ? "m" : "k"
|
config.imperial.get() ? "m" : "k"
|
||||||
}ph`;
|
}ph`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -275,7 +287,7 @@ export default class Weather extends GObject.Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
getTemp(data: _WeatherState) {
|
getTemp(data: _WeatherState) {
|
||||||
return `${Math.round(data[`temp_${config.imperial ? "f" : "c"}`])}°${config.imperial ? "F" : "C"}`;
|
return `${Math.round(data[`temp_${config.imperial.get() ? "f" : "c"}`])}°${config.imperial.get() ? "F" : "C"}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
getTempIcon(temp: number) {
|
getTempIcon(temp: number) {
|
||||||
|
|
@ -322,7 +334,7 @@ export default class Weather extends GObject.Object {
|
||||||
if (GLib.file_test(this.#cache, GLib.FileTest.EXISTS)) {
|
if (GLib.file_test(this.#cache, GLib.FileTest.EXISTS)) {
|
||||||
const cache = await readFileAsync(this.#cache);
|
const cache = await readFileAsync(this.#cache);
|
||||||
const cache_data: WeatherData = JSON.parse(cache);
|
const cache_data: WeatherData = JSON.parse(cache);
|
||||||
if (cache_data.location.localtime_epoch * 1000 + config.interval > Date.now()) {
|
if (cache_data.location.localtime_epoch * 1000 + config.interval.get() > Date.now()) {
|
||||||
if (JSON.stringify(this.#data) !== cache) {
|
if (JSON.stringify(this.#data) !== cache) {
|
||||||
this.#data = cache_data;
|
this.#data = cache_data;
|
||||||
this.#notify();
|
this.#notify();
|
||||||
|
|
@ -342,18 +354,16 @@ export default class Weather extends GObject.Object {
|
||||||
this.#notify();
|
this.#notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
#init(first: boolean) {
|
||||||
super();
|
if (GLib.file_test(config.key.get(), GLib.FileTest.EXISTS))
|
||||||
|
readFileAsync(config.key.get())
|
||||||
if (GLib.file_test(config.key, GLib.FileTest.EXISTS))
|
|
||||||
readFileAsync(config.key)
|
|
||||||
.then(k => {
|
.then(k => {
|
||||||
this.#key = k.trim();
|
this.#key = k.trim();
|
||||||
this.updateWeather().catch(console.error);
|
this.updateWeather().catch(console.error);
|
||||||
interval(config.interval, () => this.updateWeather().catch(console.error));
|
this.#interval = interval(config.interval.get(), () => this.updateWeather().catch(console.error));
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
else
|
else if (first)
|
||||||
notify({
|
notify({
|
||||||
summary: "Weather API key required",
|
summary: "Weather API key required",
|
||||||
body: `A weather API key is required to get weather data. Get one from https://www.weatherapi.com and put it in ${config.key}.`,
|
body: `A weather API key is required to get weather data. Get one from https://www.weatherapi.com and put it in ${config.key}.`,
|
||||||
|
|
@ -364,4 +374,21 @@ export default class Weather extends GObject.Object {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.#init(true);
|
||||||
|
config.key.subscribe(() => this.#init(false));
|
||||||
|
|
||||||
|
config.interval.subscribe(i => {
|
||||||
|
this.#interval?.cancel();
|
||||||
|
this.#interval = interval(i, () => this.updateWeather().catch(console.error));
|
||||||
|
});
|
||||||
|
|
||||||
|
config.imperial.subscribe(() => {
|
||||||
|
this.notify("temperature");
|
||||||
|
this.notify("wind");
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue