api

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package api provides the HTTP API server for configuration and image management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadOrCreateToken

func LoadOrCreateToken() (string, error)

LoadOrCreateToken reads the capability token from disk, creating a new one if none exists. The file is written with mode 0600 so only the owning user can read it.

Types

type BrightnessRequest

type BrightnessRequest struct {
	// openapi:description Brightness level (0-100)
	// openapi:example 75
	Brightness int `json:"brightness"`
}

BrightnessRequest represents a request to set device brightness. openapi:schema BrightnessRequest

type DetailFrameProvider

type DetailFrameProvider interface {
	GetDetailFrame() *image.RGBA
}

DetailFrameProvider returns the most recently rendered detail overlay frame.

type DeviceController

type DeviceController interface {
	SetBrightness(brightness int) error
	GetFirmwareVersion() (string, error)
	GetDeviceInfo() device.DeviceInfo
	IsConnected() bool
}

DeviceController provides an interface for controlling device features.

type DraftManager

type DraftManager struct {
	// contains filtered or unexported fields
}

DraftManager holds an in-memory draft layout that Flutter can mutate and preview live before committing to the DB. A single draft is active at a time; it is auto-discarded after draftIdleTimeout of inactivity or when all WS clients disconnect.

func NewDraftManager

func NewDraftManager(store LayoutStore, reloader LayoutReloader, broadcast func(WSMessage)) *DraftManager

NewDraftManager creates a DraftManager wired to the given store, live-preview reloader, and WS broadcast function.

func (*DraftManager) Commit

func (dm *DraftManager) Commit(commitFn func(*zone.Config) error) error

Commit writes the current draft to the DB via commitFn and clears the draft.

func (*DraftManager) Discard

func (dm *DraftManager) Discard()

Discard abandons the draft and reverts the live display to the committed state read fresh from the DB.

func (*DraftManager) GetDraft

func (dm *DraftManager) GetDraft() *zone.Config

GetDraft returns a copy of the current draft, or nil if no draft is open.

func (*DraftManager) HasDraft

func (dm *DraftManager) HasDraft() bool

HasDraft reports whether a draft is currently open.

func (*DraftManager) OpenDraft

func (dm *DraftManager) OpenDraft() (*zone.Config, error)

OpenDraft returns the active draft. If none is open it reads the full layout from the DB (including empty pages) and initialises a fresh draft from it. Normalizes plugin IDs on the way out so the Flutter UI always sees clean IDs. Returns a copy — mutating the returned config does not affect the stored draft.

func (*DraftManager) UpdateDraft

func (dm *DraftManager) UpdateDraft(cfg *zone.Config) error

UpdateDraft replaces the stored draft with cfg, previews a render-safe version live (empty pages stripped), broadcasts draft_state, and resets the idle timer.

type ErrorResponse

type ErrorResponse struct {
	// openapi:description The error type/category
	// openapi:example Bad Request
	Error string `json:"error"`
	// openapi:description Detailed error message
	// openapi:example Invalid brightness value provided
	Message string `json:"message,omitempty"`
}

ErrorResponse represents an API error response. openapi:schema ErrorResponse

type FrameProvider

type FrameProvider interface {
	GetLastFrame() *image.RGBA
}

FrameProvider returns the most recently composed full-display frame.

type LayoutReloader

type LayoutReloader interface {
	ReloadFromConfig(config *zone.Config) error
	GetConfig() *zone.Config
	NumPages() int
}

LayoutReloader applies a new layout config to the live zone manager without restarting.

type LayoutStore

type LayoutStore interface {
	GetPages() ([]store.StoredPage, error)
	GetZonesForPage(pageID int64) ([]store.StoredZone, error)
	GetFullLayout() ([]store.StoredPage, map[int64][]store.StoredZone, error)
	CreatePage(name string, ord int) (int64, error)
	UpdatePage(id int64, name string, ord int) error
	DeletePage(id int64) error
	ReorderPages(order []int64) error
	GetZonePageID(zoneID string) (int64, error)
	CreateZone(z store.StoredZone) error
	UpdateZone(z store.StoredZone) error
	DeleteZone(id string) error
	ReorderZones(pageID int64, order []string) error
	HasLayout() (bool, error)
	ImportLayout(pages []store.StoredPage, zonesByPage map[int64][]store.StoredZone) error
}

LayoutEditor is the subset of interfaces needed to edit the live layout. The store and zone manager are wired separately so neither depends on the other.

type Navigator interface {
	GetCurrentPage() int
	NumPages() int
	GetPageInfos() []zone.PageInfo
	SwitchPage(pageIndex int) error
}

Navigator is the subset of zone.Manager needed for page navigation from the UI.

type PluginCatalogProvider

type PluginCatalogProvider interface {
	GetCatalog() []zone.CatalogEntry
}

PluginCatalogProvider returns the list of available plugins with their schemas.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server manages the HTTP API server.

func NewServer

func NewServer(addr string, cfg *settings.Manager, device DeviceController, logger *slog.Logger) *Server

NewServer creates a new API server instance.

func (*Server) BroadcastDetailState

func (s *Server) BroadcastDetailState(active bool, closeX, closeY int)

BroadcastDetailState sends the detail overlay active/inactive flag to all WS clients. closeX/Y are the hardware pixel center of the close button; meaningful only when active=true.

func (*Server) BroadcastPageState

func (s *Server) BroadcastPageState()

BroadcastPageState sends current page index and page list to all WS clients. Called by the app's render loop whenever the page changes.

func (*Server) Hub

func (s *Server) Hub() *hub

Hub returns the WebSocket hub so callers can broadcast messages (e.g. frames).

func (*Server) SetDetailFrameProvider

func (s *Server) SetDetailFrameProvider(p DetailFrameProvider)

SetDetailFrameProvider wires in the zone manager for the debug render-detail endpoint.

func (*Server) SetFrameProvider

func (s *Server) SetFrameProvider(p FrameProvider)

SetFrameProvider wires in the zone manager for the debug frame endpoint.

func (*Server) SetLastConnectError

func (s *Server) SetLastConnectError(err error)

SetLastConnectError stores the most recent device connect error so it can be surfaced in API responses (e.g. GET /api/device/info).

func (*Server) SetLayoutReloader

func (s *Server) SetLayoutReloader(lr LayoutReloader)

SetLayoutReloader wires in the zone manager for live layout reloads.

func (*Server) SetLayoutStore

func (s *Server) SetLayoutStore(ls LayoutStore)

SetLayoutStore wires in the store for layout CRUD.

func (*Server) SetNavigator

func (s *Server) SetNavigator(n Navigator)

SetNavigator wires in the zone manager for page navigation.

func (*Server) SetPluginCatalog

func (s *Server) SetPluginCatalog(p PluginCatalogProvider)

SetPluginCatalog wires in the sampler for GET /api/plugins.

func (*Server) SetSwipeSimulator

func (s *Server) SetSwipeSimulator(sim SwipeSimulator)

SetSwipeSimulator wires in the zone manager for the debug swipe endpoint.

func (*Server) SetZoneConfigManager

func (s *Server) SetZoneConfigManager(zoneCfg ZoneCfgManager)

SetZoneConfigManager sets the zone config manager.

func (*Server) SetZoneConfigNotifier

func (s *Server) SetZoneConfigNotifier(notifier ZoneConfigNotifier)

SetZoneConfigNotifier sets the zone config notifier.

func (*Server) SetZoneStatusProvider

func (s *Server) SetZoneStatusProvider(p ZoneStatusProvider)

SetZoneStatusProvider wires in the sampler so zone status can be queried.

func (*Server) SetZoneTapper

func (s *Server) SetZoneTapper(t ZoneTapper)

SetZoneTapper wires in the zone manager for the debug tap endpoint.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully stops the API server.

func (*Server) Start

func (s *Server) Start() error

Start begins listening for HTTP requests.

func (*Server) Token

func (s *Server) Token() string

Token returns the capability token required in X-Nexus-Token on all requests except GET /api/health. Used by tests to authorise their requests.

func (*Server) WindowClosedChannel

func (s *Server) WindowClosedChannel() <-chan struct{}

WindowClosedChannel returns the channel that fires when Flutter reports it is closing.

type SuccessResponse

type SuccessResponse struct {
	// openapi:description Status of the operation
	// openapi:example success
	Status string `json:"status"`
	// openapi:description Human-readable success message
	// openapi:example Configuration updated successfully
	Message string `json:"message,omitempty"`
	// openapi:description Additional response data
	Data interface{} `json:"data,omitempty"`
}

SuccessResponse represents a successful API response. openapi:schema SuccessResponse

type SwipeSimRequest

type SwipeSimRequest struct {
	// Direction: "left" (forward) or "right" (backward). Default: "left".
	Direction string `json:"direction"`
	// DurationMs: total time for the drag phase in milliseconds. Default: 200.
	DurationMs int `json:"duration_ms"`
	// FinalizeMs: time for the snap-to-end animation after release. Default: 120.
	FinalizeMs int `json:"finalize_ms"`
	// Steps: number of incremental UpdateLiveSwipe calls during the drag. Default: 20.
	// More steps = smoother simulation of finger drag; fewer = choppier.
	Steps int `json:"steps"`
	// Velocity: finger velocity at release in pixels/second. Typical real swipes
	// range from ~120 px/s (slow drag) to ~500 px/s (fast flick). Default: 150.
	Velocity float32 `json:"velocity"`
	// ReleaseAt: progress (0–1) at which the finger is "lifted", triggering finalize.
	// Real swipes typically release at 0.5–0.8. Default: 0.7.
	ReleaseAt float32 `json:"release_at"`
}

SwipeSimRequest describes a synthetic swipe to simulate.

type SwipeSimulator

type SwipeSimulator interface {
	UpdateLiveSwipe(progress float32, isLeft bool) error
	FinalizeLiveSwipe(progress float32, velocity float32, isLeft bool) error
	CancelLiveSwipe() error
}

SwipeSimulator is the subset of zone.Manager needed to drive synthetic swipes.

type WSMessage

type WSMessage struct {
	Type string `json:"type"` // "frame" | "window_state" | "config"
	Data any    `json:"data"`
}

WSMessage is a typed envelope for all WebSocket messages sent to clients.

type ZoneCfgManager

type ZoneCfgManager interface {
	GetZoneOverride(zoneID string) map[string]any
	SetZoneOverride(zoneID string, cfg map[string]any) error
	DeleteZoneOverride(zoneID string) error
	BroadcastZoneConfigChange(zoneID string, cfg map[string]any) error
}

ZoneCfgManager reads and writes per-zone plugin configuration.

type ZoneConfigNotifier

type ZoneConfigNotifier interface {
	BroadcastZoneConfigChange(zoneID string, config map[string]any) error
}

ZoneConfigNotifier can notify zones about config changes.

type ZoneStatusProvider

type ZoneStatusProvider interface {
	GetZoneStatus(zoneID string) zone.ZoneStatus
	AllZoneStatuses() map[string]zone.ZoneStatus
}

ZoneStatusProvider returns per-zone health status from the sampler.

type ZoneTapper

type ZoneTapper interface {
	HandleZoneTapAtX(x int) error
	HandleZoneTap(zoneID string) error
	IsShowingDetail() bool
	ClearDetail()
}

ZoneTapper is the subset of zone.Manager needed to simulate a hardware tap.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL