util

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package util provides shared helper functions used across drivers, transports, and the rule engine.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyTransform

func ApplyTransform(value any, scale, offset float64) any

ApplyTransform applies scale and offset to a numeric value. If scale and offset are both 0, the value is returned unchanged.

func GetBoolSetting

func GetBoolSetting(settings map[string]any, key string, defaultVal bool) bool

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

func GetIntSetting(settings map[string]any, key string, defaultVal int) int

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

func IsConnectionError(err error) bool

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 ToFloat32

func ToFloat32(v any) float32

ToFloat32 converts any numeric value to float32. Non-numeric returns 0.

func ToFloat64

func ToFloat64(v any) float64

ToFloat64 converts any numeric value (including bool) to float64. Non-numeric values return 0. bool is treated as 1 (true) / 0 (false).

func ToUint16

func ToUint16(v any) uint16

ToUint16 converts a numeric any value to uint16.

func ToUint32

func ToUint32(v any) uint32

ToUint32 converts any numeric value to uint32. Non-numeric returns 0.

func ToUint64

func ToUint64(v any) uint64

ToUint64 converts any numeric value to uint64. Non-numeric returns 0.

Types

This section is empty.

Jump to

Keyboard shortcuts

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