64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package store
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"yovision/sense/internal/device"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
DriverSQLite = "sqlite"
|
||
|
|
DriverPostgres = "postgres"
|
||
|
|
)
|
||
|
|
|
||
|
|
var (
|
||
|
|
ErrQuotaProjectionUnavailable = errors.New("quota projection unavailable")
|
||
|
|
ErrQuotaProjectionInvalid = errors.New("quota projection invalid")
|
||
|
|
)
|
||
|
|
|
||
|
|
// Repository is the storage boundary used by the Sense process. SQLite stays
|
||
|
|
// available for M1 development; PostgreSQL implements the M2 production path.
|
||
|
|
type Repository interface {
|
||
|
|
Close() error
|
||
|
|
CreateDevice(context.Context, device.Device) error
|
||
|
|
SetDesiredState(context.Context, string, device.DesiredState) error
|
||
|
|
GetDevice(context.Context, string) (device.Device, error)
|
||
|
|
ListDueReconcile(context.Context, time.Time, int) ([]ReconcileCandidate, error)
|
||
|
|
ListEnabledVideoDevices(context.Context, int) ([]device.Device, error)
|
||
|
|
MarkReconciled(context.Context, string, int64, time.Time) error
|
||
|
|
MarkReconcileFailure(context.Context, string, int, time.Time, string, time.Time) error
|
||
|
|
UpdateActualState(context.Context, string, device.ActualState, time.Time) error
|
||
|
|
RequestReconcile(context.Context, string, time.Time) error
|
||
|
|
ConvergenceSnapshot(context.Context) (ConvergenceSnapshot, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
func OpenRepository(ctx context.Context, driver, dsn string) (Repository, error) {
|
||
|
|
switch strings.ToLower(strings.TrimSpace(driver)) {
|
||
|
|
case "", DriverSQLite:
|
||
|
|
return OpenSQLite(ctx, dsn)
|
||
|
|
case DriverPostgres:
|
||
|
|
return OpenPostgres(ctx, dsn)
|
||
|
|
default:
|
||
|
|
return nil, fmt.Errorf("unsupported database driver %q", driver)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type quotaProjectionError struct {
|
||
|
|
kind error
|
||
|
|
}
|
||
|
|
|
||
|
|
func (e *quotaProjectionError) Error() string { return e.kind.Error() }
|
||
|
|
func (e *quotaProjectionError) Unwrap() error { return e.kind }
|
||
|
|
|
||
|
|
func projectionUnavailable() error {
|
||
|
|
return "aProjectionError{kind: ErrQuotaProjectionUnavailable}
|
||
|
|
}
|
||
|
|
|
||
|
|
func projectionInvalid() error {
|
||
|
|
return "aProjectionError{kind: ErrQuotaProjectionInvalid}
|
||
|
|
}
|