Documentation
¶
Overview ¶
Package util provides shared helper functions used across drivers, transports, and the rule engine.
Index ¶
- func ApplyTransform(value any, scale, offset float64) any
- func GetBoolSetting(settings map[string]any, key string, defaultVal bool) bool
- func GetDurationSetting(settings map[string]any, key string, defaultVal time.Duration) time.Duration
- func GetIntSetting(settings map[string]any, key string, defaultVal int) int
- func IsConnectionError(err error) bool
- func ReconnectLoop(ctx context.Context, name string, connect func() error, ...)
- func ReconnectLoopWithBreaker(ctx context.Context, name string, connect func() error, ...)
- func ToFloat32(v any) float32
- func ToFloat64(v any) float64
- func ToUint16(v any) uint16
- func ToUint32(v any) uint32
- func ToUint64(v any) uint64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyTransform ¶
ApplyTransform applies scale and offset to a numeric value. If scale and offset are both 0, the value is returned unchanged.
func GetBoolSetting ¶
GetBoolSetting extracts a bool from a settings map, falling back to defaultVal if the key is missing or the value is not a bool.
func GetDurationSetting ¶
func GetDurationSetting(settings map[string]any, key string, defaultVal time.Duration) time.Duration
GetDurationSetting extracts a duration string (e.g. "5s") from a settings map, falling back to defaultVal if missing or unparseable.
func GetIntSetting ¶
GetIntSetting extracts an int from a settings map, falling back to defaultVal if the key is missing or the value is not an int/float64. YAML unmarshalling produces float64 for unquoted numbers, so both int and float64 are accepted.
func IsConnectionError ¶
IsConnectionError reports whether err looks like a connection-lost error by checking for common keywords in its message.
func ReconnectLoop ¶
func ReconnectLoop(ctx context.Context, name string, connect func() error, initialBackoff, maxBackoff time.Duration)
ReconnectLoop repeatedly calls connect with exponential backoff until it succeeds or ctx is cancelled. The backoff starts at initialBackoff and doubles on each failure, capped at maxBackoff. <=0 values fall back to 2s initial and 30s max. This is the backward-compatible variant without a circuit breaker.
func ReconnectLoopWithBreaker ¶ added in v0.0.2
func ReconnectLoopWithBreaker(ctx context.Context, name string, connect func() error, initialBackoff, maxBackoff time.Duration, maxFailures int)
ReconnectLoopWithBreaker is like ReconnectLoop but adds a circuit breaker: after maxFailures consecutive failures (maxFailures > 0), the backoff is increased to circuitBreakerBackoff (5 minutes) to avoid hammering a permanently offline device. The breaker resets on the next successful connection. maxFailures <= 0 disables the breaker (pure exponential backoff, backward-compatible behaviour).
func ToFloat64 ¶
ToFloat64 converts any numeric value (including bool) to float64. Non-numeric values return 0. bool is treated as 1 (true) / 0 (false).
Types ¶
This section is empty.