diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index d29d456d..8d6addf1 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -5,7 +5,7 @@ import { ensureBlePermissions, ensureVoicePermissions } from "./android-permissi import { FaceclawCommunicatorBridge, type FrameMetrics, type RawInputEvent } from "../native/faceclaw-communicator"; import { startForegroundNotification, stopForegroundNotification, updateForegroundNotification } from "../native/foreground-service"; import { mediaControllerBridge } from "../native/media-controller"; -import { nightscoutBridge } from "../native/nightscout"; +import { nightscoutBridge } from "../native/nightscout-bridge"; import { onAndroidNotificationPosted } from "../native/notification-icons"; import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict"; import { grayImageToPreviewSource, grayImageToPreviewSourceWithBitmapFactory } from "../native/gray-image-preview"; diff --git a/app/native/nightscout.ts b/app/native/nightscout-bridge.ts similarity index 100% rename from app/native/nightscout.ts rename to app/native/nightscout-bridge.ts diff --git a/app/ui/apps/debug-log.ts b/app/ui/apps/debug-log.ts new file mode 100644 index 00000000..7268fcde --- /dev/null +++ b/app/ui/apps/debug-log.ts @@ -0,0 +1,63 @@ +import takeRight from "lodash/takeRight"; +import { getDefaultSmallFont } from "~/graphics/bdffont"; +import { DashboardPlugin, DashboardPluginState } from "../dashboard-plugins"; +import { Layer, LayerContext, type DashboardInputEvent } from "../layers"; +import { GrayImage } from "~/graphics/image"; + +export const debugLogDashboardPlugin: DashboardPlugin = { + id: "input-debug-log", + label: "Input debug log", + renderCard: ({ image, bounds, state }) => { + const font = getDefaultSmallFont(); + image.drawText(font, bounds.x + 10, bounds.y + 14, "Input log", 180); + const lineHeight = 14; + const visibleLineCount = Math.max(1, Math.floor((bounds.height - 28) / lineHeight)); + const visibleLines = takeRight(state.logLines, visibleLineCount); + for (let i = 0; i < visibleLines.length; i++) { + image.drawText(font, bounds.x + 8, bounds.y + 26 + i * lineHeight, visibleLines[i]!, 190); + } + }, + createFullscreenLayer: (getState) => new DebugLogLayer(getState), +}; + +class DebugLogLayer implements Layer { + private scrollOffset = 0; + + constructor(private readonly getState: () => DashboardPluginState) {} + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const image = new GrayImage(576, 288, 0); + image.drawRect(12, 12, 552, 264, 52); + image.drawText(font, 22, 16, "Input debug log", 220); + + const logs = this.getState().logLines; + const lineHeight = 14; + const visibleCount = Math.max(1, Math.floor((288 - 64) / lineHeight)); + const maxOffset = Math.max(0, logs.length - visibleCount); + this.scrollOffset = Math.max(0, Math.min(this.scrollOffset, maxOffset)); + const start = Math.max(0, maxOffset - this.scrollOffset); + const visible = logs.slice(start, start + visibleCount); + for (let index = 0; index < visible.length; index++) { + image.drawText(font, 22, 42 + index * lineHeight, visible[index]!, 180); + } + image.drawText(font, 22, 252, "Scroll: browse Double-click: back", 110); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + switch (event.type) { + case "scroll-up": + this.scrollOffset += 1; + return; + case "scroll-down": + this.scrollOffset = Math.max(0, this.scrollOffset - 1); + return; + case "double-click": + ctx.stack.pop(); + return; + default: + return; + } + } +} \ No newline at end of file diff --git a/app/ui/apps/music-controller.ts b/app/ui/apps/music-controller.ts new file mode 100644 index 00000000..38320eba --- /dev/null +++ b/app/ui/apps/music-controller.ts @@ -0,0 +1,123 @@ +import { DashboardPlugin, DashboardPluginState } from "../dashboard-plugins"; +import { getDefaultSmallFont } from "../../graphics/bdffont"; +import { GrayImage } from "../../graphics/image"; +import { wrapText } from "../../graphics/textwrap"; +import { mediaControllerBridge, type MediaControllerState } from "../../native/media-controller"; +import { Layer, type DashboardInputEvent, type LayerContext } from "../layers"; + +export const musicControllerDashboardPlugin: DashboardPlugin = { + id: "music-controller", + label: "Music controller", + renderCard: ({ image, bounds, state }) => { + const font = getDefaultSmallFont(); + const media = state.media; + image.drawText(font, bounds.x + 10, bounds.y + 14, "Now playing", 180); + if (!media.accessEnabled) { + image.drawText(font, bounds.x + 10, bounds.y + 30, "Enable notification", 150); + image.drawText(font, bounds.x + 10, bounds.y + 44, "access for media", 150); + return; + } + if (!media.available) { + image.drawText(font, bounds.x + 10, bounds.y + 30, "No active session", 150); + return; + } + + const titleLines = wrapText(font, media.title || "Unknown title", bounds.width - 20).slice(0, 2); + const artistLine = media.artist || media.album || media.packageName || "Unknown source"; + image.drawText(font, bounds.x + 10, bounds.y + 30, titleLines[0] ?? "", 210); + if (titleLines[1]) { + image.drawText(font, bounds.x + 10, bounds.y + 44, titleLines[1], 210); + } + image.drawText(font, bounds.x + 10, bounds.y + 62, artistLine, 160); + image.drawText(font, bounds.x + 10, bounds.y + bounds.height - 18, playbackLabel(media), 130); + }, + createFullscreenLayer: (getState) => new MusicControllerLayer(getState), +}; + +function playbackLabel(media: MediaControllerState): string { + switch (media.playbackState) { + case "playing": + return "Playing"; + case "paused": + return "Paused"; + case "buffering": + return "Buffering"; + case "stopped": + return "Stopped"; + case "notification-access-required": + return "Access required"; + default: + return media.status || "Idle"; + } +} + +class MusicControllerLayer implements Layer { + constructor(private readonly getState: () => DashboardPluginState) {} + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const image = new GrayImage(576, 288, 0); + const media = this.getState().media; + image.drawRect(12, 12, 552, 264, 52); + image.drawText(font, 22, 16, "Music controller", 220); + + if (!media.accessEnabled) { + const lines = wrapText( + font, + "Notification access is required before Android will expose active media sessions to Faceclaw. Click to open settings.", + 520, + ); + for (let i = 0; i < lines.length; i++) { + image.drawText(font, 22, 44 + i * 14, lines[i]!, 180); + } + image.drawText(font, 22, 252, "Click: open settings Double-click: back", 110); + return image; + } + + if (!media.available) { + image.drawText(font, 22, 44, "No active media session.", 180); + image.drawText(font, 22, 58, "Start playback in another app,", 180); + image.drawText(font, 22, 72, "then reopen this card.", 180); + image.drawText(font, 22, 252, "Double-click: back", 110); + return image; + } + + image.drawText(font, 22, 44, media.title || "Unknown title", 220); + image.drawText(font, 22, 60, media.artist || media.album || "Unknown artist", 180); + image.drawText(font, 22, 76, media.packageName, 130); + image.drawText(font, 22, 104, `State: ${playbackLabel(media)}`, 180); + image.drawText(font, 22, 132, "Click: play/pause", 180); + image.drawText(font, 22, 146, "Scroll up: previous", 180); + image.drawText(font, 22, 160, "Scroll down: next", 180); + image.drawText(font, 22, 252, "Double-click: back", 110); + return image; + } + + async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + const media = this.getState().media; + switch (event.type) { + case "click": + if (!media.accessEnabled) { + mediaControllerBridge.openNotificationAccessSettings(); + } else if (media.canPlayPause) { + await mediaControllerBridge.playPause(); + } + return; + case "scroll-up": + if (media.canSkipPrevious) { + await mediaControllerBridge.skipPrevious(); + } + return; + case "scroll-down": + if (media.canSkipNext) { + await mediaControllerBridge.skipNext(); + } + return; + case "double-click": + ctx.stack.pop(); + return; + default: + return; + } + } +} \ No newline at end of file diff --git a/app/ui/apps/nightscout.ts b/app/ui/apps/nightscout.ts new file mode 100644 index 00000000..45d138b8 --- /dev/null +++ b/app/ui/apps/nightscout.ts @@ -0,0 +1,457 @@ +import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/bdffont"; +import { GrayImage, imageFromAsciiArt } from "../../graphics/image"; +import { DashboardPlugin, DashboardPluginState } from "../dashboard-plugins"; +import { DashboardInputEvent, Layer, LayerContext } from "../layers"; +import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge"; + +const nightscoutLargeFont = getDefaultLargeFont(); +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 const nightscoutDashboardPlugin: DashboardPlugin = { + id: "nightscout", + label: "Nightscout", + renderCard: ({ image, bounds, state }) => { + const font = getDefaultSmallFont(); + const nightscout = state.nightscout; + const nowMs = Date.now(); + image.drawText(font, bounds.x + 10, bounds.y + 10, "Nightscout", 180); + if (!state.nightscoutConfigured || nightscout.configurationMissing) { + image.drawText(font, bounds.x + 10, bounds.y + 30, "Needs setup", 170); + image.drawText(font, bounds.x + 10, bounds.y + 44, "Click to configure", 140); + return; + } + if (!nightscout.available || !nightscout.latest) { + image.drawText(font, bounds.x + 10, bounds.y + 30, "No glucose data", 150); + image.drawText(font, bounds.x + 10, bounds.y + 44, truncateLine(nightscout.status, 26), 120); + return; + } + + const latest = nightscout.latest; + const deltaLabel = formatDelta(nightscout.delta); + const glucoseText = `${latest.sgv}`; + const glucoseX = bounds.x + bounds.width - 92; + image.drawText(nightscoutLargeFont, glucoseX, bounds.y + 10, glucoseText, 230); + if (isNightscoutPointStale(latest, nowMs)) { + drawNightscoutValueStrikeThrough(image, glucoseX, bounds.y + 22, nightscoutLargeFont.measureText(glucoseText)); + } + drawNightscoutDelta(image, font, bounds.x + bounds.width - 92, bounds.y + 30, deltaLabel, nightscout.direction, 160); + image.drawText( + font, + bounds.x + 10, + bounds.y + 30, + `IOB ${nightscout.iob === null ? "--" : nightscout.iob.toFixed(1)} COB ${nightscout.cob === null ? "--" : formatWholeNumber(nightscout.cob)}`, + 160, + ); + image.drawText( + font, + bounds.x + 10, + bounds.y + 44, + `Loop ${nightscout.openapsStatusShort} CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)}`, + 138, + ); + drawNightscoutGraph( + image, + { + x: bounds.x + 10, + y: bounds.y + 60, + width: bounds.width - 20, + height: Math.max(16, bounds.height - 82), + }, + nightscout, + nowMs, + font, + ); + }, + createFullscreenLayer: (getState) => new NightscoutLayer(getState), +} + +function drawNightscoutDelta( + image: GrayImage, + font: BdfFont, + x: number, + y: number, + deltaLabel: string, + direction: string, + shade: number, +): void { + const text = direction ? `${deltaLabel} ` : deltaLabel; + image.drawText(font, x, y, text, shade); + drawDirectionIndicator(image, font, x + font.measureText(text), y, direction, shade); +} + +function drawDirectionIndicator( + image: GrayImage, + font: BdfFont, + x: number, + y: number, + direction: string, + shade: number, +): void { + const label = directionGlyphLabel(font, direction); + if (label) { + image.drawText(font, x, y, label, shade); + return; + } + + const art = directionAsciiArt(direction); + if (art) { + image.bitBlt(imageFromAsciiArt(art, shade), x, y + 1); + return; + } + + if (direction) { + image.drawText(font, x, y, truncateLine(direction, 10), shade); + } +} + +function directionGlyphLabel(font: BdfFont, direction: string): string { + switch (direction) { + case "DoubleUp": + return font.hasGlyph("↑".codePointAt(0)!) ? "↑↑" : ""; + case "SingleUp": + return font.hasGlyph("↑".codePointAt(0)!) ? "↑" : ""; + case "FortyFiveUp": + return font.hasGlyph("↗".codePointAt(0)!) ? "↗" : ""; + case "Flat": + return font.hasGlyph("→".codePointAt(0)!) ? "→" : ""; + case "FortyFiveDown": + return font.hasGlyph("↘".codePointAt(0)!) ? "↘" : ""; + case "SingleDown": + return font.hasGlyph("↓".codePointAt(0)!) ? "↓" : ""; + case "DoubleDown": + return font.hasGlyph("↓".codePointAt(0)!) ? "↓↓" : ""; + default: + return ""; + } +} + +function directionAsciiArt(direction: string): readonly string[] | undefined { + switch (direction) { + case "FortyFiveUp": + return [" ##", " ###", " # ##", "# ##", " ##"]; + case "FortyFiveDown": + return [" ##", "# ##", " # ##", " ###", " ##"]; + default: + return undefined; + } +} + +function truncateLine(text: string, maxChars: number): string { + if (text.length <= maxChars) return text; + return `${text.slice(0, Math.max(0, maxChars - 3))}...`; +} + +function formatDelta(delta: number | null): string { + if (delta === null) return "--"; + return `${delta >= 0 ? "+" : ""}${Math.round(delta)}`; +} + +function formatWholeNumber(value: number): string { + return `${Math.round(value)}`; +} + +function formatBolusLabel(value: number): string { + return Number.isInteger(value) ? `${value}` : value.toFixed(1); +} + +function formatAgeShortFromTimestamp(timestampMs: number | null, nowMs: number): string { + if (!timestampMs) { + return "--"; + } + const elapsedMinutes = Math.max(0, Math.round((nowMs - timestampMs) / 60_000)); + if (elapsedMinutes < 60) { + return `${elapsedMinutes}m`; + } + const elapsedHours = Math.round(elapsedMinutes / 60); + if (elapsedHours < 48) { + return `${elapsedHours}h`; + } + return `${Math.round(elapsedHours / 24)}d`; +} + +function drawNightscoutGraph( + image: GrayImage, + bounds: { x: number; y: number; width: number; height: number }, + nightscout: NightscoutState, + nowMs: number, + font: BdfFont, +): void { + if (bounds.width <= 2 || bounds.height <= 2) { + return; + } + + 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; + } + + const values = visibleHistory.map((point) => point.sgv); + const min = Math.min(...values, 60); + const max = Math.max(...values, 200); + const range = Math.max(20, max - min); + const paddedMin = min - range * 0.1; + const paddedMax = max + range * 0.1; + + image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 56); + 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: timeToGraphX(bounds, windowStartMs, point.timestampMs), + y: + bounds.y + + bounds.height - + 1 - + Math.round(((point.sgv - paddedMin) / Math.max(1, paddedMax - paddedMin)) * Math.max(1, bounds.height - 1)), + })); + + for (let i = 0; i < plotted.length; i++) { + const current = plotted[i]!; + const previous = i > 0 ? plotted[i - 1]! : null; + const next = i + 1 < plotted.length ? plotted[i + 1]! : null; + const connectedToPrevious = + previous !== null && current.point.timestampMs - previous.point.timestampMs <= NIGHTSCOUT_STALE_MS; + const connectedToNext = + next !== null && next.point.timestampMs - current.point.timestampMs <= NIGHTSCOUT_STALE_MS; + + if (connectedToPrevious && previous) { + image.drawLine(previous.x, previous.y, current.x, current.y, 220); + } + if (!connectedToPrevious && !connectedToNext) { + image.fillRect(current.x - 1, current.y - 1, 2, 2, 220); + } + } + drawNightscoutCarbMarkers(image, bounds, font, nightscout.carbs, graphNowMs); + drawNightscoutBolusMarkers(image, bounds, font, nightscout.boluses, graphNowMs); +} + +function drawNightscoutBasalOverlay( + image: GrayImage, + bounds: { x: number; y: number; width: number; height: number }, + basalEvents: NightscoutState["basal"], + nowMs: number, +): void { + const graphNowMs = minuteBucketTimestampMs(nowMs); + const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS; + const visibleEvents = basalEvents.filter( + (event) => + minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000) >= windowStartMs && + minuteBucketTimestampMs(event.timestampMs) <= graphNowMs, + ); + if (visibleEvents.length === 0) { + return; + } + const maxRate = Math.max(...visibleEvents.map((event) => event.rate), 0); + if (maxRate <= 0) { + return; + } + 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, minuteBucketTimestampMs(event.timestampMs)); + const endMs = Math.min(graphNowMs, minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000)); + if (endMs <= startMs) { + continue; + } + const leftX = timeToGraphX(bounds, windowStartMs, startMs); + const rightX = timeToGraphX(bounds, windowStartMs, endMs); + const height = Math.max(1, Math.round((event.rate / maxRate) * overlayHeight)); + image.fillRect(leftX, bottomY - height + 1, Math.max(1, rightX - leftX + 1), height, 15); + } +} + +function drawNightscoutCarbMarkers( + image: GrayImage, + bounds: { x: number; y: number; width: number; height: number }, + font: BdfFont, + carbEvents: NightscoutState["carbs"], + nowMs: number, +): void { + const graphNowMs = minuteBucketTimestampMs(nowMs); + const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS; + const baselineY = bounds.y + bounds.height - 2; + for (const event of carbEvents) { + const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs); + if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) { + continue; + } + 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); + } +} + +function drawNightscoutBolusMarkers( + image: GrayImage, + bounds: { x: number; y: number; width: number; height: number }, + font: BdfFont, + bolusEvents: NightscoutState["boluses"], + nowMs: number, +): void { + const graphNowMs = minuteBucketTimestampMs(nowMs); + const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS; + const topY = bounds.y + 1; + for (const event of bolusEvents) { + const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs); + if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) { + continue; + } + const x = timeToGraphX(bounds, windowStartMs, eventTimestampMs); + image.drawLine(x, topY, x, topY + 6, 144); + if (event.insulin >= 1) { + drawTextCentered( + image, + font, + x, + Math.min(bounds.y + bounds.height - font.lineHeight - 1, topY + 8), + formatBolusLabel(event.insulin), + 144, + bounds, + ); + } + } +} + +function drawTextCentered( + image: GrayImage, + font: BdfFont, + centerX: number, + y: number, + text: string, + shade: number, + bounds: { x: number; y: number; width: number; height: number }, +): void { + const textWidth = font.measureText(text); + const x = Math.max(bounds.x + 1, Math.min(bounds.x + bounds.width - textWidth - 1, centerX - Math.round(textWidth / 2))); + image.drawText(font, x, y, text, shade); +} + +function timeToGraphX( + bounds: { x: number; y: number; width: number; height: number }, + windowStartMs: number, + timestampMs: number, +): number { + const graphTimestampMs = minuteBucketTimestampMs(timestampMs); + return ( + bounds.x + + 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 }, + minValue: number, + maxValue: number, + value: number, + shade: number, +): void { + const y = + bounds.y + + bounds.height - + 1 - + Math.round(((value - minValue) / Math.max(1, maxValue - minValue)) * Math.max(1, bounds.height - 1)); + image.drawLine(bounds.x + 1, y, bounds.x + bounds.width - 2, y, shade); +} + +class NightscoutLayer implements Layer { + constructor(private readonly getState: () => DashboardPluginState) {} + + paint(ctx: LayerContext): GrayImage { + const font = getDefaultSmallFont(); + const image = new GrayImage(576, 288, 0); + const nightscout = this.getState().nightscout; + const nowMs = Date.now(); + image.drawRect(12, 12, 552, 264, 52); + image.drawText(font, 22, 16, "Nightscout", 220); + + if (!this.getState().nightscoutConfigured || nightscout.configurationMissing) { + image.drawText(font, 22, 44, "Nightscout needs configuration.", 180); + image.drawText(font, 22, 62, "Double-click and use Settings", 140); + image.drawText(font, 22, 78, "Integrations > Nightscout.", 140); + return image; + } + + if (!nightscout.available || !nightscout.latest) { + image.drawText(font, 22, 44, "No Nightscout data available.", 180); + image.drawText(font, 22, 58, truncateLine(nightscout.status, 60), 140); + image.drawText(font, 22, 252, "Click: refresh Double-click: back", 110); + return image; + } + + const latest = nightscout.latest; + const glucoseText = `${latest.sgv}`; + const glucoseX = 22; + const glucoseWidth = nightscoutLargeFont.measureText(glucoseText); + image.drawText(nightscoutLargeFont, glucoseX, 28, glucoseText, 230); + if (isNightscoutPointStale(latest, nowMs)) { + drawNightscoutValueStrikeThrough(image, glucoseX, 40, glucoseWidth); + } + image.drawText(font, glucoseX + glucoseWidth + 8, 36, nightscout.units, 140); + const trendText = `Delta ${formatDelta(nightscout.delta)} Trend `; + image.drawText(font, 22, 62, trendText, 180); + drawDirectionIndicator(image, font, 22 + font.measureText(trendText), 62, nightscout.direction, 180); + image.drawText( + font, + 22, + 78, + `IOB ${nightscout.iob === null ? "--" : nightscout.iob.toFixed(2)} COB ${nightscout.cob === null ? "--" : formatWholeNumber(nightscout.cob)} Updated ${formatTimestamp(latest.timestampMs)}`, + 160, + ); + image.drawText( + font, + 22, + 94, + `CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)} Loop ${nightscout.openapsStatusShort}`, + 160, + ); + image.drawText(font, 22, 110, `Pump ${truncateLine(nightscout.pumpStatus || "--", 56)}`, 150); + drawNightscoutGraph(image, { x: 22, y: 128, width: 532, height: 96 }, nightscout, nowMs, font); + image.drawText(font, 22, 234, "2-hour glucose history with basal / carbs / boluses", 130); + image.drawText(font, 22, 252, "Click: refresh Double-click: back", 110); + return image; + } + + async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { + switch (event.type) { + case "click": + await nightscoutBridge.refreshNow(); + return; + case "double-click": + ctx.stack.pop(); + return; + default: + return; + } + } +} + +function formatTimestamp(timestampMs: number): string { + const date = new Date(timestampMs); + return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`; +} + +function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean { + return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS; +} + +function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void { + image.drawLine(x, y, x + width, y, 180); +} \ No newline at end of file diff --git a/app/ui/dashboard-plugins.ts b/app/ui/dashboard-plugins.ts index 00b7174b..dde81963 100644 --- a/app/ui/dashboard-plugins.ts +++ b/app/ui/dashboard-plugins.ts @@ -1,12 +1,11 @@ -import takeRight from "lodash/takeRight"; - -import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../graphics/bdffont"; -import { GrayImage, imageFromAsciiArt } from "../graphics/image"; -import { wrapText } from "../graphics/textwrap"; -import { mediaControllerBridge, type MediaControllerState } from "../native/media-controller"; -import { nightscoutBridge, type NightscoutState } from "../native/nightscout"; +import { GrayImage } from "../graphics/image"; +import { type MediaControllerState } from "../native/media-controller"; import { type DashboardPluginId } from "./dashboard-settings"; -import { Layer, type DashboardInputEvent, type LayerContext } from "./layers"; +import { Layer } from "./layers"; +import { nightscoutDashboardPlugin } from "./apps/nightscout"; +import { NightscoutState } from "~/native/nightscout-bridge"; +import { musicControllerDashboardPlugin } from "./apps/music-controller"; +import { debugLogDashboardPlugin } from "./apps/debug-log"; export type DashboardPluginState = { logLines: string[]; @@ -43,116 +42,14 @@ const DASHBOARD_PLUGINS: DashboardPlugin[] = [ renderCard: () => {}, createFullscreenLayer: () => null, }, - { - id: "input-debug-log", - label: "Input debug log", - renderCard: ({ image, bounds, state }) => { - const font = getDefaultSmallFont(); - image.drawText(font, bounds.x + 10, bounds.y + 14, "Input log", 180); - const lineHeight = 14; - const visibleLineCount = Math.max(1, Math.floor((bounds.height - 28) / lineHeight)); - const visibleLines = takeRight(state.logLines, visibleLineCount); - for (let i = 0; i < visibleLines.length; i++) { - image.drawText(font, bounds.x + 8, bounds.y + 26 + i * lineHeight, visibleLines[i]!, 190); - } - }, - createFullscreenLayer: (getState) => new DebugLogLayer(getState), - }, - { - id: "music-controller", - label: "Music controller", - renderCard: ({ image, bounds, state }) => { - const font = getDefaultSmallFont(); - const media = state.media; - image.drawText(font, bounds.x + 10, bounds.y + 14, "Now playing", 180); - if (!media.accessEnabled) { - image.drawText(font, bounds.x + 10, bounds.y + 30, "Enable notification", 150); - image.drawText(font, bounds.x + 10, bounds.y + 44, "access for media", 150); - return; - } - if (!media.available) { - image.drawText(font, bounds.x + 10, bounds.y + 30, "No active session", 150); - return; - } - - const titleLines = wrapText(font, media.title || "Unknown title", bounds.width - 20).slice(0, 2); - const artistLine = media.artist || media.album || media.packageName || "Unknown source"; - image.drawText(font, bounds.x + 10, bounds.y + 30, titleLines[0] ?? "", 210); - if (titleLines[1]) { - image.drawText(font, bounds.x + 10, bounds.y + 44, titleLines[1], 210); - } - image.drawText(font, bounds.x + 10, bounds.y + 62, artistLine, 160); - image.drawText(font, bounds.x + 10, bounds.y + bounds.height - 18, playbackLabel(media), 130); - }, - createFullscreenLayer: (getState) => new MusicControllerLayer(getState), - }, - { - id: "nightscout", - label: "Nightscout", - renderCard: ({ image, bounds, state }) => { - const font = getDefaultSmallFont(); - const nightscout = state.nightscout; - const nowMs = Date.now(); - image.drawText(font, bounds.x + 10, bounds.y + 10, "Nightscout", 180); - if (!state.nightscoutConfigured || nightscout.configurationMissing) { - image.drawText(font, bounds.x + 10, bounds.y + 30, "Needs setup", 170); - image.drawText(font, bounds.x + 10, bounds.y + 44, "Click to configure", 140); - return; - } - if (!nightscout.available || !nightscout.latest) { - image.drawText(font, bounds.x + 10, bounds.y + 30, "No glucose data", 150); - image.drawText(font, bounds.x + 10, bounds.y + 44, truncateLine(nightscout.status, 26), 120); - return; - } - - const latest = nightscout.latest; - const deltaLabel = formatDelta(nightscout.delta); - const glucoseText = `${latest.sgv}`; - const glucoseX = bounds.x + bounds.width - 92; - image.drawText(nightscoutLargeFont, glucoseX, bounds.y + 10, glucoseText, 230); - if (isNightscoutPointStale(latest, nowMs)) { - drawNightscoutValueStrikeThrough(image, glucoseX, bounds.y + 22, nightscoutLargeFont.measureText(glucoseText)); - } - drawNightscoutDelta(image, font, bounds.x + bounds.width - 92, bounds.y + 30, deltaLabel, nightscout.direction, 160); - image.drawText( - font, - bounds.x + 10, - bounds.y + 30, - `IOB ${nightscout.iob === null ? "--" : nightscout.iob.toFixed(1)} COB ${nightscout.cob === null ? "--" : formatWholeNumber(nightscout.cob)}`, - 160, - ); - image.drawText( - font, - bounds.x + 10, - bounds.y + 44, - `Loop ${nightscout.openapsStatusShort} CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)}`, - 138, - ); - drawNightscoutGraph( - image, - { - x: bounds.x + 10, - y: bounds.y + 60, - width: bounds.width - 20, - height: Math.max(16, bounds.height - 82), - }, - nightscout, - nowMs, - font, - ); - }, - createFullscreenLayer: (getState) => new NightscoutLayer(getState), - }, + debugLogDashboardPlugin, + musicControllerDashboardPlugin, + nightscoutDashboardPlugin, ]; const PLUGIN_MAP = new Map( DASHBOARD_PLUGINS.map((plugin) => [plugin.id, plugin]), ); -const nightscoutLargeFont = getDefaultLargeFont(); -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]; } @@ -164,521 +61,3 @@ export function getDashboardPlugin(pluginId: DashboardPluginId): DashboardPlugin export function isBlankDashboardPlugin(pluginId: DashboardPluginId): boolean { return pluginId === "blank"; } - -function playbackLabel(media: MediaControllerState): string { - switch (media.playbackState) { - case "playing": - return "Playing"; - case "paused": - return "Paused"; - case "buffering": - return "Buffering"; - case "stopped": - return "Stopped"; - case "notification-access-required": - return "Access required"; - default: - return media.status || "Idle"; - } -} - -function formatDelta(delta: number | null): string { - if (delta === null) return "--"; - return `${delta >= 0 ? "+" : ""}${Math.round(delta)}`; -} - -function drawNightscoutDelta( - image: GrayImage, - font: BdfFont, - x: number, - y: number, - deltaLabel: string, - direction: string, - shade: number, -): void { - const text = direction ? `${deltaLabel} ` : deltaLabel; - image.drawText(font, x, y, text, shade); - drawDirectionIndicator(image, font, x + font.measureText(text), y, direction, shade); -} - -function drawDirectionIndicator( - image: GrayImage, - font: BdfFont, - x: number, - y: number, - direction: string, - shade: number, -): void { - const label = directionGlyphLabel(font, direction); - if (label) { - image.drawText(font, x, y, label, shade); - return; - } - - const art = directionAsciiArt(direction); - if (art) { - image.bitBlt(imageFromAsciiArt(art, shade), x, y + 1); - return; - } - - if (direction) { - image.drawText(font, x, y, truncateLine(direction, 10), shade); - } -} - -function directionGlyphLabel(font: BdfFont, direction: string): string { - switch (direction) { - case "DoubleUp": - return font.hasGlyph("↑".codePointAt(0)!) ? "↑↑" : ""; - case "SingleUp": - return font.hasGlyph("↑".codePointAt(0)!) ? "↑" : ""; - case "FortyFiveUp": - return font.hasGlyph("↗".codePointAt(0)!) ? "↗" : ""; - case "Flat": - return font.hasGlyph("→".codePointAt(0)!) ? "→" : ""; - case "FortyFiveDown": - return font.hasGlyph("↘".codePointAt(0)!) ? "↘" : ""; - case "SingleDown": - return font.hasGlyph("↓".codePointAt(0)!) ? "↓" : ""; - case "DoubleDown": - return font.hasGlyph("↓".codePointAt(0)!) ? "↓↓" : ""; - default: - return ""; - } -} - -function directionAsciiArt(direction: string): readonly string[] | undefined { - switch (direction) { - case "FortyFiveUp": - return [" ##", " ###", " # ##", "# ##", " ##"]; - case "FortyFiveDown": - return [" ##", "# ##", " # ##", " ###", " ##"]; - default: - return undefined; - } -} - -function truncateLine(text: string, maxChars: number): string { - if (text.length <= maxChars) return text; - return `${text.slice(0, Math.max(0, maxChars - 3))}...`; -} - -function formatWholeNumber(value: number): string { - return `${Math.round(value)}`; -} - -function formatBolusLabel(value: number): string { - return Number.isInteger(value) ? `${value}` : value.toFixed(1); -} - -function formatAgeShortFromTimestamp(timestampMs: number | null, nowMs: number): string { - if (!timestampMs) { - return "--"; - } - const elapsedMinutes = Math.max(0, Math.round((nowMs - timestampMs) / 60_000)); - if (elapsedMinutes < 60) { - return `${elapsedMinutes}m`; - } - const elapsedHours = Math.round(elapsedMinutes / 60); - if (elapsedHours < 48) { - return `${elapsedHours}h`; - } - return `${Math.round(elapsedHours / 24)}d`; -} - -function drawNightscoutGraph( - image: GrayImage, - bounds: { x: number; y: number; width: number; height: number }, - nightscout: NightscoutState, - nowMs: number, - font: BdfFont, -): void { - if (bounds.width <= 2 || bounds.height <= 2) { - return; - } - - 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; - } - - const values = visibleHistory.map((point) => point.sgv); - const min = Math.min(...values, 60); - const max = Math.max(...values, 200); - const range = Math.max(20, max - min); - const paddedMin = min - range * 0.1; - const paddedMax = max + range * 0.1; - - image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 56); - 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: timeToGraphX(bounds, windowStartMs, point.timestampMs), - y: - bounds.y + - bounds.height - - 1 - - Math.round(((point.sgv - paddedMin) / Math.max(1, paddedMax - paddedMin)) * Math.max(1, bounds.height - 1)), - })); - - for (let i = 0; i < plotted.length; i++) { - const current = plotted[i]!; - const previous = i > 0 ? plotted[i - 1]! : null; - const next = i + 1 < plotted.length ? plotted[i + 1]! : null; - const connectedToPrevious = - previous !== null && current.point.timestampMs - previous.point.timestampMs <= NIGHTSCOUT_STALE_MS; - const connectedToNext = - next !== null && next.point.timestampMs - current.point.timestampMs <= NIGHTSCOUT_STALE_MS; - - if (connectedToPrevious && previous) { - image.drawLine(previous.x, previous.y, current.x, current.y, 220); - } - if (!connectedToPrevious && !connectedToNext) { - image.fillRect(current.x - 1, current.y - 1, 2, 2, 220); - } - } - drawNightscoutCarbMarkers(image, bounds, font, nightscout.carbs, graphNowMs); - drawNightscoutBolusMarkers(image, bounds, font, nightscout.boluses, graphNowMs); -} - -function drawNightscoutBasalOverlay( - image: GrayImage, - bounds: { x: number; y: number; width: number; height: number }, - basalEvents: NightscoutState["basal"], - nowMs: number, -): void { - const graphNowMs = minuteBucketTimestampMs(nowMs); - const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS; - const visibleEvents = basalEvents.filter( - (event) => - minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000) >= windowStartMs && - minuteBucketTimestampMs(event.timestampMs) <= graphNowMs, - ); - if (visibleEvents.length === 0) { - return; - } - const maxRate = Math.max(...visibleEvents.map((event) => event.rate), 0); - if (maxRate <= 0) { - return; - } - 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, minuteBucketTimestampMs(event.timestampMs)); - const endMs = Math.min(graphNowMs, minuteBucketTimestampMs(event.timestampMs + event.durationMinutes * 60_000)); - if (endMs <= startMs) { - continue; - } - const leftX = timeToGraphX(bounds, windowStartMs, startMs); - const rightX = timeToGraphX(bounds, windowStartMs, endMs); - const height = Math.max(1, Math.round((event.rate / maxRate) * overlayHeight)); - image.fillRect(leftX, bottomY - height + 1, Math.max(1, rightX - leftX + 1), height, 15); - } -} - -function drawNightscoutCarbMarkers( - image: GrayImage, - bounds: { x: number; y: number; width: number; height: number }, - font: BdfFont, - carbEvents: NightscoutState["carbs"], - nowMs: number, -): void { - const graphNowMs = minuteBucketTimestampMs(nowMs); - const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS; - const baselineY = bounds.y + bounds.height - 2; - for (const event of carbEvents) { - const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs); - if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) { - continue; - } - 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); - } -} - -function drawNightscoutBolusMarkers( - image: GrayImage, - bounds: { x: number; y: number; width: number; height: number }, - font: BdfFont, - bolusEvents: NightscoutState["boluses"], - nowMs: number, -): void { - const graphNowMs = minuteBucketTimestampMs(nowMs); - const windowStartMs = graphNowMs - NIGHTSCOUT_GRAPH_WINDOW_MS; - const topY = bounds.y + 1; - for (const event of bolusEvents) { - const eventTimestampMs = minuteBucketTimestampMs(event.timestampMs); - if (eventTimestampMs < windowStartMs || eventTimestampMs > graphNowMs) { - continue; - } - const x = timeToGraphX(bounds, windowStartMs, eventTimestampMs); - image.drawLine(x, topY, x, topY + 6, 144); - if (event.insulin >= 1) { - drawTextCentered( - image, - font, - x, - Math.min(bounds.y + bounds.height - font.lineHeight - 1, topY + 8), - formatBolusLabel(event.insulin), - 144, - bounds, - ); - } - } -} - -function drawTextCentered( - image: GrayImage, - font: BdfFont, - centerX: number, - y: number, - text: string, - shade: number, - bounds: { x: number; y: number; width: number; height: number }, -): void { - const textWidth = font.measureText(text); - const x = Math.max(bounds.x + 1, Math.min(bounds.x + bounds.width - textWidth - 1, centerX - Math.round(textWidth / 2))); - image.drawText(font, x, y, text, shade); -} - -function timeToGraphX( - bounds: { x: number; y: number; width: number; height: number }, - windowStartMs: number, - timestampMs: number, -): number { - const graphTimestampMs = minuteBucketTimestampMs(timestampMs); - return ( - bounds.x + - 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 }, - minValue: number, - maxValue: number, - value: number, - shade: number, -): void { - const y = - bounds.y + - bounds.height - - 1 - - Math.round(((value - minValue) / Math.max(1, maxValue - minValue)) * Math.max(1, bounds.height - 1)); - image.drawLine(bounds.x + 1, y, bounds.x + bounds.width - 2, y, shade); -} - -class DebugLogLayer implements Layer { - private scrollOffset = 0; - - constructor(private readonly getState: () => DashboardPluginState) {} - - paint(ctx: LayerContext): GrayImage { - const font = getDefaultSmallFont(); - const image = new GrayImage(576, 288, 0); - image.drawRect(12, 12, 552, 264, 52); - image.drawText(font, 22, 16, "Input debug log", 220); - - const logs = this.getState().logLines; - const lineHeight = 14; - const visibleCount = Math.max(1, Math.floor((288 - 64) / lineHeight)); - const maxOffset = Math.max(0, logs.length - visibleCount); - this.scrollOffset = Math.max(0, Math.min(this.scrollOffset, maxOffset)); - const start = Math.max(0, maxOffset - this.scrollOffset); - const visible = logs.slice(start, start + visibleCount); - for (let index = 0; index < visible.length; index++) { - image.drawText(font, 22, 42 + index * lineHeight, visible[index]!, 180); - } - image.drawText(font, 22, 252, "Scroll: browse Double-click: back", 110); - return image; - } - - handleInput(event: DashboardInputEvent, ctx: LayerContext): void { - switch (event.type) { - case "scroll-up": - this.scrollOffset += 1; - return; - case "scroll-down": - this.scrollOffset = Math.max(0, this.scrollOffset - 1); - return; - case "double-click": - ctx.stack.pop(); - return; - default: - return; - } - } -} - -class MusicControllerLayer implements Layer { - constructor(private readonly getState: () => DashboardPluginState) {} - - paint(ctx: LayerContext): GrayImage { - const font = getDefaultSmallFont(); - const image = new GrayImage(576, 288, 0); - const media = this.getState().media; - image.drawRect(12, 12, 552, 264, 52); - image.drawText(font, 22, 16, "Music controller", 220); - - if (!media.accessEnabled) { - const lines = wrapText( - font, - "Notification access is required before Android will expose active media sessions to Faceclaw. Click to open settings.", - 520, - ); - for (let i = 0; i < lines.length; i++) { - image.drawText(font, 22, 44 + i * 14, lines[i]!, 180); - } - image.drawText(font, 22, 252, "Click: open settings Double-click: back", 110); - return image; - } - - if (!media.available) { - image.drawText(font, 22, 44, "No active media session.", 180); - image.drawText(font, 22, 58, "Start playback in another app,", 180); - image.drawText(font, 22, 72, "then reopen this card.", 180); - image.drawText(font, 22, 252, "Double-click: back", 110); - return image; - } - - image.drawText(font, 22, 44, media.title || "Unknown title", 220); - image.drawText(font, 22, 60, media.artist || media.album || "Unknown artist", 180); - image.drawText(font, 22, 76, media.packageName, 130); - image.drawText(font, 22, 104, `State: ${playbackLabel(media)}`, 180); - image.drawText(font, 22, 132, "Click: play/pause", 180); - image.drawText(font, 22, 146, "Scroll up: previous", 180); - image.drawText(font, 22, 160, "Scroll down: next", 180); - image.drawText(font, 22, 252, "Double-click: back", 110); - return image; - } - - async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { - const media = this.getState().media; - switch (event.type) { - case "click": - if (!media.accessEnabled) { - mediaControllerBridge.openNotificationAccessSettings(); - } else if (media.canPlayPause) { - await mediaControllerBridge.playPause(); - } - return; - case "scroll-up": - if (media.canSkipPrevious) { - await mediaControllerBridge.skipPrevious(); - } - return; - case "scroll-down": - if (media.canSkipNext) { - await mediaControllerBridge.skipNext(); - } - return; - case "double-click": - ctx.stack.pop(); - return; - default: - return; - } - } -} - -class NightscoutLayer implements Layer { - constructor(private readonly getState: () => DashboardPluginState) {} - - paint(ctx: LayerContext): GrayImage { - const font = getDefaultSmallFont(); - const image = new GrayImage(576, 288, 0); - const nightscout = this.getState().nightscout; - const nowMs = Date.now(); - image.drawRect(12, 12, 552, 264, 52); - image.drawText(font, 22, 16, "Nightscout", 220); - - if (!this.getState().nightscoutConfigured || nightscout.configurationMissing) { - image.drawText(font, 22, 44, "Nightscout needs configuration.", 180); - image.drawText(font, 22, 62, "Double-click and use Settings", 140); - image.drawText(font, 22, 78, "Integrations > Nightscout.", 140); - return image; - } - - if (!nightscout.available || !nightscout.latest) { - image.drawText(font, 22, 44, "No Nightscout data available.", 180); - image.drawText(font, 22, 58, truncateLine(nightscout.status, 60), 140); - image.drawText(font, 22, 252, "Click: refresh Double-click: back", 110); - return image; - } - - const latest = nightscout.latest; - const glucoseText = `${latest.sgv}`; - const glucoseX = 22; - const glucoseWidth = nightscoutLargeFont.measureText(glucoseText); - image.drawText(nightscoutLargeFont, glucoseX, 28, glucoseText, 230); - if (isNightscoutPointStale(latest, nowMs)) { - drawNightscoutValueStrikeThrough(image, glucoseX, 40, glucoseWidth); - } - image.drawText(font, glucoseX + glucoseWidth + 8, 36, nightscout.units, 140); - const trendText = `Delta ${formatDelta(nightscout.delta)} Trend `; - image.drawText(font, 22, 62, trendText, 180); - drawDirectionIndicator(image, font, 22 + font.measureText(trendText), 62, nightscout.direction, 180); - image.drawText( - font, - 22, - 78, - `IOB ${nightscout.iob === null ? "--" : nightscout.iob.toFixed(2)} COB ${nightscout.cob === null ? "--" : formatWholeNumber(nightscout.cob)} Updated ${formatTimestamp(latest.timestampMs)}`, - 160, - ); - image.drawText( - font, - 22, - 94, - `CAGE ${formatAgeShortFromTimestamp(nightscout.cageTimestampMs, nowMs)} Loop ${nightscout.openapsStatusShort}`, - 160, - ); - image.drawText(font, 22, 110, `Pump ${truncateLine(nightscout.pumpStatus || "--", 56)}`, 150); - drawNightscoutGraph(image, { x: 22, y: 128, width: 532, height: 96 }, nightscout, nowMs, font); - image.drawText(font, 22, 234, "2-hour glucose history with basal / carbs / boluses", 130); - image.drawText(font, 22, 252, "Click: refresh Double-click: back", 110); - return image; - } - - async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { - switch (event.type) { - case "click": - await nightscoutBridge.refreshNow(); - return; - case "double-click": - ctx.stack.pop(); - return; - default: - return; - } - } -} - -function formatTimestamp(timestampMs: number): string { - const date = new Date(timestampMs); - return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`; -} - -function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean { - return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS; -} - -function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void { - image.drawLine(x, y, x + width, y, 180); -} diff --git a/app/ui/dashboard.ts b/app/ui/dashboard.ts index f391f90e..a6e65319 100644 --- a/app/ui/dashboard.ts +++ b/app/ui/dashboard.ts @@ -4,7 +4,7 @@ import { BATTERY_ICON_WIDTH, drawBattery } from "../graphics/battery"; import { loadPngAsGrayImage } from "../graphics/imagefile"; import { type RawInputEvent } from "../native/faceclaw-communicator"; import { mediaControllerBridge } from "../native/media-controller"; -import { nightscoutBridge } from "../native/nightscout"; +import { nightscoutBridge } from "../native/nightscout-bridge"; import { readActiveNotificationIcons } from "../native/notification-icons"; import { readPhoneBatteryState } from "../native/phone-battery"; import { readSystemStatusIcons } from "../native/system-status-icons";