polling

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2025 License: LGPL-3.0 Imports: 9 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTagInPoll = errors.New("no tag detected in polling cycle")

ErrNoTagInPoll indicates no tag was detected during polling (not an error condition)

Functions

This section is empty.

Types

type CardDetectionState

type CardDetectionState int

CardDetectionState represents the finite state machine for card detection

const (
	StateIdle CardDetectionState = iota
	StateTagDetected
	StateReading
	StatePostReadGrace
)

type CardState

type CardState struct {
	LastSeenTime   time.Time
	ReadStartTime  time.Time
	RemovalTimer   *time.Timer
	LastUID        string
	LastType       string
	TestedUID      string
	DetectionState CardDetectionState
	Present        bool
}

CardState tracks the state of a card on a reader

func (*CardState) CanStartRemovalTimer

func (cs *CardState) CanStartRemovalTimer() bool

CanStartRemovalTimer returns true if the state allows removal timer to run

func (*CardState) TransitionToDetected

func (cs *CardState) TransitionToDetected(timeout time.Duration, callback func())

TransitionToDetected moves to tag detected state with normal removal timeout

func (*CardState) TransitionToIdle

func (cs *CardState) TransitionToIdle()

TransitionToIdle resets to idle state

func (*CardState) TransitionToPostReadGrace

func (cs *CardState) TransitionToPostReadGrace(timeout time.Duration, callback func())

TransitionToPostReadGrace moves to post-read grace period with short timeout

func (*CardState) TransitionToReading

func (cs *CardState) TransitionToReading()

TransitionToReading moves to reading state and suspends removal timer

type Config

type Config struct {
	PollInterval       time.Duration
	CardRemovalTimeout time.Duration
	// HardwareTimeoutRetries controls how long PN532 waits for card detection
	// 0x00 = immediate return, 0x01-0xFE = retry count (~150ms each), 0xFF = infinite
	// Higher values reduce LED blinking frequency but increase detection latency
	HardwareTimeoutRetries byte
}

Config holds polling configuration options

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns the default polling configuration

type DeviceActor added in v0.5.0

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

DeviceActor minimal implementation to make the test pass

func NewDeviceActor added in v0.5.0

func NewDeviceActor(device *pn532.Device, config *Config, callbacks DeviceCallbacks) *DeviceActor

NewDeviceActor creates a new device actor (minimal implementation to pass test)

func (*DeviceActor) GetCurrentPollInterval added in v0.5.0

func (da *DeviceActor) GetCurrentPollInterval() time.Duration

GetCurrentPollInterval returns the current adaptive polling interval

func (*DeviceActor) GetMetrics added in v0.5.0

func (da *DeviceActor) GetMetrics() DeviceMetrics

GetMetrics returns current operational metrics

func (*DeviceActor) Start added in v0.5.0

func (da *DeviceActor) Start(_ context.Context) error

Start minimal implementation to pass test

func (*DeviceActor) Stop added in v0.5.0

func (da *DeviceActor) Stop(_ context.Context) error

Stop stops the device actor and waits for the polling goroutine to exit

type DeviceCallbacks added in v0.5.0

type DeviceCallbacks struct {
	OnCardDetected func(tag *pn532.DetectedTag) error
	OnCardRemoved  func()
	OnCardChanged  func(tag *pn532.DetectedTag) error
}

DeviceCallbacks defines callback functions for device events

type DeviceMetrics added in v0.5.0

type DeviceMetrics struct {
	PollCycles      int64         // Total number of polling cycles
	PollErrors      int64         // Number of polling errors
	CardsDetected   int64         // Number of cards detected
	CallbackErrors  int64         // Number of callback errors
	LastPollLatency time.Duration // Duration of last polling operation
}

DeviceMetrics tracks operational metrics for DeviceActor

type Session added in v0.5.0

type Session struct {
	OnMultiTagRemoved func()

	OnCardDetected func(tag *pn532.DetectedTag) error
	OnCardRemoved  func()
	OnCardChanged  func(tag *pn532.DetectedTag) error

	OnMultiTagChanged func(tags []*pn532.DetectedTag) error

	OnMultiTagDetected func(tags []*pn532.DetectedTag) error
	// contains filtered or unexported fields
}

Session handles continuous card monitoring with state machine

func NewActorBasedSession added in v0.5.0

func NewActorBasedSession(device *pn532.Device, config *Config) *Session

NewActorBasedSession creates a session using DeviceActor underneath

func NewSession added in v0.5.0

func NewSession(device *pn532.Device, config *Config) *Session

NewSession creates a new card monitoring session

func (*Session) Close added in v0.5.0

func (s *Session) Close() error

Close cleans up the monitor resources

func (*Session) GetDevice added in v0.5.0

func (s *Session) GetDevice() *pn532.Device

GetDevice returns the underlying PN532 device

func (*Session) GetDeviceActor added in v0.5.0

func (s *Session) GetDeviceActor() *DeviceActor

GetDeviceActor returns the underlying DeviceActor for actor-based sessions

func (*Session) GetState added in v0.5.0

func (s *Session) GetState() CardState

GetState returns the current card state

func (*Session) Pause added in v0.5.0

func (s *Session) Pause()

Pause temporarily stops the polling loop This is used to coordinate with write operations

func (*Session) Resume added in v0.5.0

func (s *Session) Resume()

Resume restarts the polling loop after a pause

func (*Session) SetOnCardChanged added in v0.9.0

func (s *Session) SetOnCardChanged(callback func(*pn532.DetectedTag) error)

SetOnCardChanged sets the callback for when the card changes.

func (*Session) SetOnCardDetected added in v0.9.0

func (s *Session) SetOnCardDetected(callback func(*pn532.DetectedTag) error)

SetOnCardDetected sets the callback for when a card is detected.

func (*Session) SetOnCardRemoved added in v0.9.0

func (s *Session) SetOnCardRemoved(callback func())

SetOnCardRemoved sets the callback for when a card is removed.

func (*Session) SetOnMultiTagChanged added in v0.11.0

func (s *Session) SetOnMultiTagChanged(callback func([]*pn532.DetectedTag) error)

SetOnMultiTagChanged sets the callback for when the set of tags changes in multi-tag mode.

func (*Session) SetOnMultiTagDetected added in v0.11.0

func (s *Session) SetOnMultiTagDetected(callback func([]*pn532.DetectedTag) error)

SetOnMultiTagDetected sets the callback for when tags are first detected in multi-tag mode.

func (*Session) SetOnMultiTagRemoved added in v0.11.0

func (s *Session) SetOnMultiTagRemoved(callback func())

SetOnMultiTagRemoved sets the callback for when all tags are removed in multi-tag mode.

func (*Session) Start added in v0.5.0

func (s *Session) Start(ctx context.Context) error

Start begins continuous monitoring for cards

func (*Session) StartMultiTag added in v0.11.0

func (s *Session) StartMultiTag(ctx context.Context) error

StartMultiTag begins continuous monitoring for up to 2 cards simultaneously. Uses OnMultiTagDetected/OnMultiTagRemoved/OnMultiTagChanged callbacks instead of the single-tag callbacks.

func (*Session) WriteToNextTag added in v0.5.0

func (s *Session) WriteToNextTag(
	sessionCtx context.Context,
	writeCtx context.Context,
	timeout time.Duration,
	writeFn func(context.Context, pn532.Tag) error,
) error

WriteToNextTag waits for the next tag detection and performs a write operation This method blocks until a tag is detected or timeout occurs sessionCtx controls session lifetime, writeCtx controls write operation lifetime

func (*Session) WriteToNextTagWithRetry added in v0.9.1

func (s *Session) WriteToNextTagWithRetry(
	sessionCtx context.Context,
	writeCtx context.Context,
	timeout time.Duration,
	maxRetries int,
	writeFn func(context.Context, pn532.Tag) error,
) error

WriteToNextTagWithRetry waits for the next tag detection and performs a write operation with automatic retry on transient errors. This is useful for handling intermittent write failures due to card placement issues or timing problems. sessionCtx controls session lifetime, writeCtx controls write operation lifetime. maxRetries specifies how many times to retry the write operation (default 3 if <= 0).

func (*Session) WriteToTag added in v0.5.0

func (s *Session) WriteToTag(
	sessionCtx context.Context,
	writeCtx context.Context,
	detectedTag *pn532.DetectedTag,
	writeFn func(context.Context, pn532.Tag) error,
) error

WriteToTag performs a thread-safe write operation to a detected tag This method pauses polling during the write to prevent interference sessionCtx controls session lifetime, writeCtx controls write operation lifetime

func (*Session) WriteToTagWithRetry added in v0.9.1

func (s *Session) WriteToTagWithRetry(
	sessionCtx context.Context,
	writeCtx context.Context,
	detectedTag *pn532.DetectedTag,
	maxRetries int,
	writeFn func(context.Context, pn532.Tag) error,
) error

WriteToTagWithRetry performs a thread-safe write operation to a detected tag with automatic retry on transient errors. This is useful for handling intermittent write failures due to card placement issues or timing problems. sessionCtx controls session lifetime, writeCtx controls write operation lifetime. maxRetries specifies how many times to retry the write operation (default 3 if <= 0).

Jump to

Keyboard shortcuts

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