device

package module
v0.0.0-...-dd8e555 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: BSD-2-Clause Imports: 6 Imported by: 0

README

otto-devices

Devices written to work with OttO

Documentation

Overview

Package device provides a framework for managing hardware devices with support for messaging, periodic operations, and state management. Devices can be controlled via MQTT messages and can publish their state and data periodically.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsMock

func IsMock() bool

IsMock returns the current mock state

func Mock

func Mock(mocking bool)

Mock enables or disables mock device behavior

Types

type Device

type Device struct {
	Name   string        // Human readable device name
	State  DeviceState   // Current device state
	Period time.Duration // Period for timed operations
	Val    any           // Mock value storage

	Opener // Device opening interface
	// contains filtered or unexported fields
}

Device represents a physical or virtual device with messaging capabilities

func NewDevice

func NewDevice(name string, t string) *Device

NewDevice creates a new device with the given name

func (*Device) Error

func (d *Device) Error() error

ErrorVal returns the last error encountered by the device

func (*Device) JSON

func (d *Device) JSON() ([]byte, error)

JSON returns a JSON representation of the device

func (*Device) SetError

func (d *Device) SetError(err error)

SetError sets the device error and updates the state to StateError

func (*Device) String

func (d *Device) String() string

String returns the device name

func (*Device) TimerLoop

func (d *Device) TimerLoop(ctx context.Context, period time.Duration, readpub func() error) error

TimerLoop runs periodic operations with context support

type DeviceManager

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

DeviceManager handles the registration and retrieval of devices. It ensures thread-safe access to the device collection.

func GetDeviceManager

func GetDeviceManager() *DeviceManager

GetDeviceManager returns the singleton instance of DeviceManager. It ensures thread-safe initialization and access to the device manager.

func (*DeviceManager) Add

func (dm *DeviceManager) Add(d Name) error

Add registers a new device with the manager. If a device with the same name exists, it will be replaced.

func (*DeviceManager) Clear

func (dm *DeviceManager) Clear()

Clear removes all devices from the manager.

func (*DeviceManager) Get

func (dm *DeviceManager) Get(name string) (Name, bool)

Get retrieves a device by name. Returns the device and true if found, nil and false otherwise.

func (*DeviceManager) List

func (dm *DeviceManager) List() []string

List returns a slice of all registered device names.

func (*DeviceManager) Remove

func (dm *DeviceManager) Remove(name string) bool

Remove removes a device from the manager. Returns true if the device was removed, false if it didn't exist.

type DeviceState

type DeviceState string

DeviceState represents the current operational state of a device

const (
	StateUnknown      DeviceState = "unknown"
	StateInitializing DeviceState = "initializing"
	StateRunning      DeviceState = "running"
	StateError        DeviceState = "error"
	StateStopped      DeviceState = "stopped"
)

type Name

type Name interface {
	Name() string
}

Name represents a device that has a human-readable name.

type OnOff

type OnOff interface {
	On() error
	Off() error
}

OnOff represents a device that can be turned on and off.

type Opener

type Opener interface {
	Open() error
	Close() error
}

Opener represents a device that can be opened and closed for communication.

Directories

Path Synopsis
Package bme280 provides a driver for the BME280 temperature, humidity, and pressure sensor using I2C communication.
Package bme280 provides a driver for the BME280 temperature, humidity, and pressure sensor using I2C communication.
The drivers include GPIO (digital), analog (via ads1115), serial, I2C at this point.
The drivers include GPIO (digital), analog (via ads1115), serial, I2C at this point.
examples
ads1115 command
blink command
bme280 command
gtu7 command
json command
oled command
relay command
switch command
vh400 command

Jump to

Keyboard shortcuts

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