forked from Blendman974/faceclaw
Connection reliability and thread safety fixes
This commit is contained in:
1 parent
0d0b55c2da
commit
aa0b1d6874
8 files changed
+567
-277
No files matched your search
+36
-21
@@ -149,6 +149,7 @@ const PLUGIN_MAP = new Map<DashboardPluginId, DashboardPlugin>(
|
||||
const nightscoutLargeFont = loadEmbeddedTerminus24();
|
||||
const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
|
||||
const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
|
||||
const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
|
||||
|
||||
export function listDashboardPlugins(): DashboardPlugin[] {
|
||||
return [...DASHBOARD_PLUGINS];
|
||||
@@ -294,8 +295,12 @@ function drawNightscoutGraph(
|
||||
return;
|
||||
}
|
||||
|
||||
const windowStartMs = nowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const visibleHistory = nightscout.history.filter((point) => point.timestampMs >= windowStartMs && point.timestampMs <= nowMs);
|
||||
const graphNowMs = minuteBucketTimestampMs(nowMs);
|
||||
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const visibleHistory = nightscout.history.filter((point) => {
|
||||
const pointTimestampMs = minuteBucketTimestampMs(point.timestampMs);
|
||||
return pointTimestampMs >= windowStartMs && pointTimestampMs <= graphNowMs;
|
||||
});
|
||||
if (visibleHistory.length === 0) {
|
||||
return;
|
||||
}
|
||||
@@ -308,18 +313,14 @@ function drawNightscoutGraph(
|
||||
const paddedMax = max + range * 0.1;
|
||||
|
||||
image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 56);
|
||||
drawNightscoutBasalOverlay(image, bounds, nightscout.basal, nowMs);
|
||||
drawNightscoutBasalOverlay(image, bounds, nightscout.basal, graphNowMs);
|
||||
drawNightscoutReferenceLine(image, bounds, paddedMin, paddedMax, 60, 40);
|
||||
drawNightscoutReferenceLine(image, bounds, paddedMin, paddedMax, 100, 40);
|
||||
drawNightscoutReferenceLine(image, bounds, paddedMin, paddedMax, 180, 40);
|
||||
|
||||
const plotted = visibleHistory.map((point) => ({
|
||||
point,
|
||||
x:
|
||||
bounds.x +
|
||||
Math.round(
|
||||
((point.timestampMs - windowStartMs) / Math.max(1, NIGHTSCOUT_GRAPH_WINDOW_MS)) * Math.max(1, bounds.width - 1),
|
||||
),
|
||||
x: timeToGraphX(bounds, windowStartMs, point.timestampMs),
|
||||
y:
|
||||
bounds.y +
|
||||
bounds.height -
|
||||
@@ -343,8 +344,8 @@ function drawNightscoutGraph(
|
||||
image.fillRect(current.x - 1, current.y - 1, 2, 2, 220);
|
||||
}
|
||||
}
|
||||
drawNightscoutCarbMarkers(image, bounds, font, nightscout.carbs, nowMs);
|
||||
drawNightscoutBolusMarkers(image, bounds, font, nightscout.boluses, nowMs);
|
||||
drawNightscoutCarbMarkers(image, bounds, font, nightscout.carbs, graphNowMs);
|
||||
drawNightscoutBolusMarkers(image, bounds, font, nightscout.boluses, graphNowMs);
|
||||
}
|
||||
|
||||
function drawNightscoutBasalOverlay(
|
||||
@@ -353,9 +354,12 @@ function drawNightscoutBasalOverlay(
|
||||
basalEvents: NightscoutState["basal"],
|
||||
nowMs: number,
|
||||
): void {
|
||||
const windowStartMs = nowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const graphNowMs = minuteBucketTimestampMs(nowMs);
|
||||
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const visibleEvents = basalEvents.filter(
|
||||
(event) => event.timestampMs + event.durationMinutes * 60_000 >= windowStartMs && event.timestampMs <= nowMs,
|
||||
(event) =>
|
||||
minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000) >= windowStartMs &&
|
||||
minuteBucketTimestampMs(event.timestampMs) <= graphNowMs,
|
||||
);
|
||||
if (visibleEvents.length === 0) {
|
||||
return;
|
||||
@@ -367,8 +371,8 @@ function drawNightscoutBasalOverlay(
|
||||
const overlayHeight = Math.max(8, Math.min(18, Math.floor(bounds.height * 0.22)));
|
||||
const bottomY = bounds.y + bounds.height - 2;
|
||||
for (const event of visibleEvents) {
|
||||
const startMs = Math.max(windowStartMs, event.timestampMs);
|
||||
const endMs = Math.min(nowMs, event.timestampMs + event.durationMinutes * 60_000);
|
||||
const startMs = Math.max(windowStartMs, minuteBucketTimestampMs(event.timestampMs));
|
||||
const endMs = Math.min(graphNowMs, minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000));
|
||||
if (endMs <= startMs) {
|
||||
continue;
|
||||
}
|
||||
@@ -386,13 +390,15 @@ function drawNightscoutCarbMarkers(
|
||||
carbEvents: NightscoutState["carbs"],
|
||||
nowMs: number,
|
||||
): void {
|
||||
const windowStartMs = nowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const graphNowMs = minuteBucketTimestampMs(nowMs);
|
||||
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const baselineY = bounds.y + bounds.height - 2;
|
||||
for (const event of carbEvents) {
|
||||
if (event.timestampMs < windowStartMs || event.timestampMs > nowMs) {
|
||||
const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs);
|
||||
if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) {
|
||||
continue;
|
||||
}
|
||||
const x = timeToGraphX(bounds, windowStartMs, event.timestampMs);
|
||||
const x = timeToGraphX(bounds, windowStartMs, eventTimestampMs);
|
||||
image.drawLine(x, baselineY, x, baselineY - 6, 150);
|
||||
drawTextCentered(image, font, x, Math.max(bounds.y + 2, baselineY - 18), `${Math.round(event.carbs)}`, 150, bounds);
|
||||
}
|
||||
@@ -405,13 +411,15 @@ function drawNightscoutBolusMarkers(
|
||||
bolusEvents: NightscoutState["boluses"],
|
||||
nowMs: number,
|
||||
): void {
|
||||
const windowStartMs = nowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const graphNowMs = minuteBucketTimestampMs(nowMs);
|
||||
const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS;
|
||||
const topY = bounds.y + 1;
|
||||
for (const event of bolusEvents) {
|
||||
if (event.timestampMs < windowStartMs || event.timestampMs > nowMs) {
|
||||
const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs);
|
||||
if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) {
|
||||
continue;
|
||||
}
|
||||
const x = timeToGraphX(bounds, windowStartMs, event.timestampMs);
|
||||
const x = timeToGraphX(bounds, windowStartMs, eventTimestampMs);
|
||||
image.drawLine(x, topY, x, topY + 6, 144);
|
||||
if (event.insulin >= 1) {
|
||||
drawTextCentered(
|
||||
@@ -446,12 +454,19 @@ function timeToGraphX(
|
||||
windowStartMs: number,
|
||||
timestampMs: number,
|
||||
): number {
|
||||
const graphTimestampMs = minuteBucketTimestampMs(timestampMs);
|
||||
return (
|
||||
bounds.x +
|
||||
Math.round(((timestampMs - windowStartMs) / Math.max(1, NIGHTSCOUT_GRAPH_WINDOW_MS)) * Math.max(1, bounds.width - 1))
|
||||
Math.round(
|
||||
((graphTimestampMs - windowStartMs) / Math.max(1, NIGHTSCOUT_GRAPH_WINDOW_MS)) * Math.max(1, bounds.width - 1),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function minuteBucketTimestampMs(timestampMs: number): number {
|
||||
return Math.floor(timestampMs / NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS) * NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS;
|
||||
}
|
||||
|
||||
function drawNightscoutReferenceLine(
|
||||
image: GrayImage,
|
||||
bounds: { x: number; y: number; width: number; height: number },
|
||||
|
||||
+77
-3
@@ -84,6 +84,7 @@ const dashboardActions: LayerActions = {
|
||||
startNightscoutApiTokenEdit: () => {},
|
||||
endTextSettingEdit: () => {},
|
||||
setVoiceControlEnabled: () => {},
|
||||
setStopwatchRenderActive: () => {},
|
||||
};
|
||||
const dashboardFont = loadEmbeddedTerminus12();
|
||||
const dashboardSystemFont = loadEmbeddedTerminus16();
|
||||
@@ -443,12 +444,12 @@ function createRootMenuLayer(): MenuLayer {
|
||||
return new RootMenuLayer(
|
||||
"Menu",
|
||||
[
|
||||
/*{
|
||||
{
|
||||
label: "Apps",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(createAppsMenuLayer());
|
||||
},
|
||||
},*/
|
||||
},
|
||||
{
|
||||
label: "Settings",
|
||||
onSelect: (ctx) => {
|
||||
@@ -476,7 +477,80 @@ function createRootMenuLayer(): MenuLayer {
|
||||
}
|
||||
|
||||
function createAppsMenuLayer(): MenuLayer {
|
||||
return new MenuLayer("Apps", [], TOP_LEFT_MENU_LAYOUT);
|
||||
return new MenuLayer(
|
||||
"Apps",
|
||||
[
|
||||
{
|
||||
label: "Stopwatch",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new StopwatchLayer());
|
||||
void ctx.actions.setStopwatchRenderActive(true);
|
||||
},
|
||||
},
|
||||
],
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
}
|
||||
|
||||
class StopwatchLayer implements Layer {
|
||||
private readonly startedAtMs = Date.now();
|
||||
private pausedAtMs: number | null = null;
|
||||
private pausedDurationMs = 0;
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||
const elapsedMs = this.elapsedMs();
|
||||
const timeLabel = formatStopwatchElapsed(elapsedMs);
|
||||
const stateLabel = this.pausedAtMs === null ? "Running" : "Paused";
|
||||
|
||||
image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52);
|
||||
image.drawText(ctx.font, 24, 24, "Stopwatch", 180);
|
||||
image.drawText(ctx.font, G2_LENS_WIDTH - 114, 24, stateLabel, this.pausedAtMs === null ? 200 : 120);
|
||||
|
||||
const timeX = Math.max(0, ((G2_LENS_WIDTH - dashboardSystemFont.measureText(timeLabel)) / 2) | 0);
|
||||
image.drawText(dashboardSystemFont, timeX, 106, timeLabel, 245);
|
||||
image.drawText(ctx.font, 126, 160, "Click: pause / resume", 150);
|
||||
image.drawText(ctx.font, 126, 178, "Double-click: back", 120);
|
||||
image.drawText(ctx.font, 126, 214, "Renders requested every 100ms", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "double-click") {
|
||||
void ctx.actions.setStopwatchRenderActive(false);
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
}
|
||||
if (event.type === "click") {
|
||||
if (this.pausedAtMs === null) {
|
||||
this.pausedAtMs = Date.now();
|
||||
void ctx.actions.setStopwatchRenderActive(false);
|
||||
} else {
|
||||
this.pausedDurationMs += Date.now() - this.pausedAtMs;
|
||||
this.pausedAtMs = null;
|
||||
void ctx.actions.setStopwatchRenderActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private elapsedMs(): number {
|
||||
const now = this.pausedAtMs ?? Date.now();
|
||||
return Math.max(0, now - this.startedAtMs - this.pausedDurationMs);
|
||||
}
|
||||
}
|
||||
|
||||
function formatStopwatchElapsed(elapsedMs: number): string {
|
||||
const totalTenths = Math.floor(elapsedMs / 100);
|
||||
const tenths = totalTenths % 10;
|
||||
const totalSeconds = Math.floor(totalTenths / 10);
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
if (hours > 0) {
|
||||
return `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
||||
}
|
||||
return `${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
||||
}
|
||||
|
||||
function createSystemMenuLayer(): MenuLayer {
|
||||
|
||||
@@ -16,6 +16,7 @@ export type LayerActions = {
|
||||
startNightscoutApiTokenEdit: () => Promise<void> | void;
|
||||
endTextSettingEdit: () => Promise<void> | void;
|
||||
setVoiceControlEnabled: (enabled: boolean) => Promise<void> | void;
|
||||
setStopwatchRenderActive: (active: boolean) => Promise<void> | void;
|
||||
};
|
||||
|
||||
export type PaintBelow = () => GrayImage;
|
||||
|
||||
Reference in new issue
Block a user