server

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORSMiddleware

func CORSMiddleware(next http.Handler) http.Handler

CORSMiddleware adds CORS headers for cross-origin requests.

func ContentTypeMiddleware

func ContentTypeMiddleware(contentType string) func(http.Handler) http.Handler

ContentTypeMiddleware ensures responses have Content-Type header.

func GetDevice

func GetDevice(handlers *Handlers) http.HandlerFunc

GetDevice is a wrapper handler that serves as a redirect from the base path. This handles requests like GET /api/v1/devices/ without an identifier.

func LoggingMiddleware

func LoggingMiddleware(logger *slog.Logger) func(http.Handler) http.Handler

LoggingMiddleware logs HTTP requests.

func RecoverMiddleware

func RecoverMiddleware(logger *slog.Logger) func(http.Handler) http.Handler

RecoverMiddleware recovers from panics in handlers.

func SetupRoutes

func SetupRoutes(mux *http.ServeMux, manager *state.Manager, adapter *adapter.Adapter, logger *slog.Logger)

SetupRoutes configures and returns the HTTP router with all handlers registered.

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, data interface{})

WriteJSON writes a JSON response with the given status code.

Types

type ControlResponse

type ControlResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
}

ControlResponse represents a device control response.

type DeviceDetailsResponse

type DeviceDetailsResponse struct {
	IEEEAddr     string              `json:"ieee_addr"`
	NwkAddr      string              `json:"nwk_addr"`
	Slug         string              `json:"slug"`
	Name         string              `json:"name,omitempty"`
	Manufacturer string              `json:"manufacturer,omitempty"`
	Model        string              `json:"model,omitempty"`
	Capabilities string              `json:"capabilities,omitempty"`
	Endpoints    []uint8             `json:"endpoints"`
	LastSeen     time.Time           `json:"last_seen"`
	JoinedAt     time.Time           `json:"joined_at"`
	State        DeviceStateResponse `json:"state"`
}

DeviceDetailsResponse represents the device details response.

type DeviceListResponse

type DeviceListResponse struct {
	Devices []DeviceSummary `json:"devices"`
	Count   int             `json:"count"`
}

DeviceListResponse represents the list devices response.

type DeviceNameResponse

type DeviceNameResponse struct {
	Success bool   `json:"success"`
	Slug    string `json:"slug,omitempty"`
	Error   string `json:"error,omitempty"`
}

DeviceNameResponse represents the device name response.

type DeviceStateResponse

type DeviceStateResponse struct {
	OnOff       *bool     `json:"on_off,omitempty"`
	Brightness  *uint8    `json:"brightness,omitempty"`
	ColorTemp   *uint16   `json:"color_temp,omitempty"`
	ColorHue    *uint8    `json:"color_hue,omitempty"`
	ColorSat    *uint8    `json:"color_saturation,omitempty"`
	Temperature *float32  `json:"temperature,omitempty"`
	Humidity    *float32  `json:"humidity,omitempty"`
	LastUpdated time.Time `json:"last_updated"`
}

DeviceStateResponse represents the cached device state.

type DeviceSummary

type DeviceSummary struct {
	IEEEAddr     string    `json:"ieee_addr"`
	NwkAddr      string    `json:"nwk_addr"`
	Slug         string    `json:"slug"`
	Name         string    `json:"name,omitempty"`
	Model        string    `json:"model,omitempty"`
	Manufacturer string    `json:"manufacturer,omitempty"`
	LastSeen     time.Time `json:"last_seen"`
	JoinedAt     time.Time `json:"joined_at"`
	IsRouter     bool      `json:"is_router"`
	IsBattery    bool      `json:"is_battery_powered"`
}

DeviceSummary represents a summary of a device for list views.

type ErrorResponse

type ErrorResponse struct {
	Error   string `json:"error"`
	Message string `json:"message,omitempty"`
}

ErrorResponse represents an API error response.

type Handlers

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

Handlers contains all HTTP handlers for the daemon API.

func NewHandlers

func NewHandlers(manager *state.Manager, adapter *adapter.Adapter, logger *slog.Logger) *Handlers

NewHandlers creates a new handlers instance.

func (*Handlers) GetDevice

func (h *Handlers) GetDevice(w http.ResponseWriter, r *http.Request)

GetDevice returns details for a specific device. GET /api/v1/devices/:identifier (can be slug or IEEE address)

func (*Handlers) GetDeviceState

func (h *Handlers) GetDeviceState(w http.ResponseWriter, r *http.Request)

GetDeviceState returns the cached state for a specific device. GET /api/v1/devices/:identifier/state

func (*Handlers) GetSensorData

func (h *Handlers) GetSensorData(w http.ResponseWriter, r *http.Request)

GetSensorData returns the latest sensor readings for a device. GET /api/v1/devices/:identifier/sensor

func (*Handlers) HealthCheck

func (h *Handlers) HealthCheck(w http.ResponseWriter, r *http.Request)

HealthCheck returns the health status of the daemon. GET /health

func (*Handlers) Identify

func (h *Handlers) Identify(w http.ResponseWriter, r *http.Request)

Identify makes a device identify itself for a specific duration. POST /api/v1/devices/:identifier/identify Request body: {"time": 5} (duration in seconds, default: 5)

func (*Handlers) ListDevices

func (h *Handlers) ListDevices(w http.ResponseWriter, _ *http.Request)

ListDevices returns all devices. GET /api/v1/devices

func (*Handlers) SetBrightness

func (h *Handlers) SetBrightness(w http.ResponseWriter, r *http.Request)

SetBrightness sets the brightness level of a device. POST /api/v1/devices/:identifier/brightness Request body: {"level": 128} (0-254)

func (*Handlers) SetColorTemperature

func (h *Handlers) SetColorTemperature(w http.ResponseWriter, r *http.Request)

SetColorTemperature sets the color temperature. POST /api/v1/devices/:identifier/color-temp Request body: {"kelvin": 2700} (2700-6500)

func (*Handlers) SetDeviceName

func (h *Handlers) SetDeviceName(w http.ResponseWriter, r *http.Request)

SetDeviceName sets a friendly name for a device. POST /api/v1/devices/:identifier/name Request body: {"name": "Device Name"}

func (*Handlers) Toggle

func (h *Handlers) Toggle(w http.ResponseWriter, r *http.Request)

Toggle toggles a device. POST /api/v1/devices/:identifier/toggle

func (*Handlers) TurnOff

func (h *Handlers) TurnOff(w http.ResponseWriter, r *http.Request)

TurnOff turns a device off. POST /api/v1/devices/:identifier/off

func (*Handlers) TurnOn

func (h *Handlers) TurnOn(w http.ResponseWriter, r *http.Request)

TurnOn turns a device on. POST /api/v1/devices/:identifier/on

type HealthResponse

type HealthResponse struct {
	Status    string    `json:"status"`
	Timestamp time.Time `json:"timestamp"`
}

HealthResponse represents the health check response.

type SensorDataResponse

type SensorDataResponse struct {
	IEEEAddr    string        `json:"ieee_addr"`
	Slug        string        `json:"slug,omitempty"`
	LastUpdated time.Time     `json:"last_updated"`
	Reading     SensorReading `json:"reading"`
}

SensorDataResponse represents the sensor data response.

type SensorReading

type SensorReading struct {
	Temperature *state.TimestampedValue[float32] `json:"temperature,omitempty"`
	Humidity    *state.TimestampedValue[float32] `json:"humidity,omitempty"`
	Pressure    *state.TimestampedValue[float64] `json:"pressure,omitempty"`
	Voltage     *state.TimestampedValue[float64] `json:"voltage,omitempty"`
	Current     *state.TimestampedValue[float64] `json:"current,omitempty"`
	Power       *state.TimestampedValue[float64] `json:"power,omitempty"`
	Energy      *state.TimestampedValue[float64] `json:"energy,omitempty"`
	Battery     *state.TimestampedValue[uint8]   `json:"battery,omitempty"`
	Illuminance *state.TimestampedValue[float32] `json:"illuminance,omitempty"`
	Occupied    *state.TimestampedValue[bool]    `json:"occupied,omitempty"`
}

SensorReading represents sensor readings.

type TimestampedValue

type TimestampedValue[T any] struct {
	Value      T         `json:"value"`
	Timestamp  time.Time `json:"timestamp"`
	ReceivedAt time.Time `json:"received_at"`
}

TimestampedValue represents a value with timestamps.

Jump to

Keyboard shortcuts

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