common

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbeewayBatV

func AbeewayBatV(raw byte) float64

AbeewayBatV converts an Abeeway raw battery byte to volts. Formula: (raw * 10 + 2000) / 1000

func DecodePayloadBytes

func DecodePayloadBytes(encoded string) ([]byte, error)

DecodePayloadBytes decodes a base64-encoded payload string to bytes.

func ExtractBytes

func ExtractBytes(payload *RawPayload) []byte

ExtractBytes returns raw payload bytes from a RawPayload. It first tries the LNS handler (when LNSType is set), then falls back to standard base64 decoding of the Data field.

func GenerateEntityID

func GenerateEntityID(domain, orgSlug, manufacturer, model, devEUI, entityType string) string

GenerateEntityID creates a descriptive entity ID.

func GenerateUniqueID

func GenerateUniqueID(model, devEUI, entityType string) string

GenerateUniqueID creates a simple registry key for an entity.

func GetEntityDomain

func GetEntityDomain(entityType string) string

GetEntityDomain returns the HA domain for a given entity type key.

func I16BE

func I16BE(b []byte, off int) int16

func I16LE

func I16LE(b []byte, off int) int16

func I32BE

func I32BE(b []byte, off int) int32

func I32LE

func I32LE(b []byte, off int) int32

func LinearBatPct

func LinearBatPct(v, minV, maxV float64) float64

LinearBatPct returns a battery percentage assuming a linear curve from minV (0 %) to maxV (100 %), clamped to [0, 100].

func LocationSource

func LocationSource(deviceGPS *Location) string

LocationSource returns "gps" when the device itself provided a valid fix, or "gateway" when the location was computed from gateway trilateration as a fallback.

func ReadInt24BE

func ReadInt24BE(b []byte, off int) int32

ReadInt24BE reads a signed 24-bit big-endian integer with sign extension. Used by Cayenne LPP GPS channels (tbeam).

func U16BE

func U16BE(b []byte, off int) uint16

func U16LE

func U16LE(b []byte, off int) uint16

func U24BE

func U24BE(b []byte, off int) uint32

func U32BE

func U32BE(b []byte, off int) uint32

func U32LE

func U32LE(b []byte, off int) uint32

func ValidateCoordinates

func ValidateCoordinates(lat, lon float64) error

ValidateCoordinates checks that lat/lon are within valid ranges and not null-island.

func YabbyBatV

func YabbyBatV(raw uint32) float64

YabbyBatV converts a Yabby Edge raw uint32 reading to volts. Formula: 2.0 + 0.007 * raw, rounded to 3 decimal places.

Types

type DeviceInfo

type DeviceInfo struct {
	Identifiers  []string   `json:"identifiers"`
	Connections  [][]string `json:"connections,omitempty"`
	Name         string     `json:"name"`
	Manufacturer string     `json:"manufacturer"`
	Model        string     `json:"model"`
	ModelID      string     `json:"model_id"`
	SWVersion    string     `json:"sw_version,omitempty"`
	HWVersion    string     `json:"hw_version,omitempty"`
	ViaDevice    string     `json:"via_device,omitempty"`
}

DeviceInfo carries device metadata for Home-Assistant-style discovery.

func CreateDeviceInfo

func CreateDeviceInfo(devEUI, name, manufacturer, model, modelID string) DeviceInfo

CreateDeviceInfo builds a DeviceInfo struct.

type DeviceType

type DeviceType string

DeviceType represents the model/type of a device.

const DeviceTypeUnknown DeviceType = "UNKNOWN"

DeviceTypeUnknown is the default when the type cannot be determined.

type Entity

type Entity struct {
	Key         string                 `json:"key,omitempty"`
	UniqueID    string                 `json:"unique_id"`
	EntityID    string                 `json:"entity_id"`
	EntityType  string                 `json:"entity_type"`
	DeviceClass string                 `json:"device_class,omitempty"`
	Name        string                 `json:"name"`
	State       interface{}            `json:"state"`
	Attributes  map[string]interface{} `json:"attributes"`
	DisplayType []string               `json:"display_type,omitempty"`
	UnitOfMeas  string                 `json:"unit_of_measurement,omitempty"`
	Icon        string                 `json:"icon,omitempty"`
	Enabled     bool                   `json:"enabled"`
	Timestamp   time.Time              `json:"timestamp"`
}

Entity represents a single device capability or sensor reading.

func BuildEntitiesFromState added in v0.0.5

func BuildEntitiesFromState(orgSlug, model, manufacturer, modelKey, devEUI string, defs []EntityDef, sensorData map[string]interface{}, ts time.Time) []Entity

func BuildEntityFromDef added in v0.0.5

func BuildEntityFromDef(orgSlug, model, manufacturer, modelKey, devEUI string, def EntityDef, state interface{}, attributes map[string]interface{}, ts time.Time) Entity

func BuildEntityTemplates added in v0.0.5

func BuildEntityTemplates(orgSlug, model, manufacturer, modelKey, devEUI string, defs []EntityDef) []Entity

func BuildLocationTemplate added in v0.0.5

func BuildLocationTemplate(orgSlug, model, manufacturer, modelKey, devEUI string, gpsCapable bool, requiresCalculation bool) Entity

type EntityDef added in v0.0.5

type EntityDef struct {
	Key         string
	DomainKey   string
	Name        string
	EntityType  string
	DeviceClass string
	UnitOfMeas  string
	Icon        string
	DisplayType []string
	Attributes  map[string]interface{}
	Transform   EntityTransformFunc
	Skip        EntitySkipFunc
}

type EntitySkipFunc added in v0.0.5

type EntitySkipFunc func(value any) bool

type EntityTemplateDef added in v0.0.5

type EntityTemplateDef = EntityDef

type EntityTransformFunc added in v0.0.5

type EntityTransformFunc func(value any) (state any, attributes map[string]any, ok bool)

type GatewayInfo

type GatewayInfo struct {
	GatewayID string   `json:"gateway_id"`
	RSSI      int      `json:"rssi"`
	SNR       float64  `json:"snr"`
	Location  Location `json:"location,omitempty"`
}

GatewayInfo contains information about a gateway that received the message.

type Location

type Location struct {
	Latitude  float64 `json:"latitude"`
	Longitude float64 `json:"longitude"`
	Altitude  float64 `json:"altitude,omitempty"`
	Bearing   float64 `json:"bearing,omitempty"`
}

Location represents geographical coordinates.

func ExtractGPS

func ExtractGPS(src map[string]interface{}) *Location

ExtractGPS looks for latitude/longitude in a decoded field map using common key names. Returns nil if no valid coordinates are found.

func ResolveLocationBearing added in v0.0.5

func ResolveLocationBearing(parsedLocation, deviceLocation *Location, sensorData map[string]interface{}) *Location

ResolveLocationBearing returns the preferred bearing for a location, using a device-reported heading when present and otherwise falling back to the computed device location bearing.

type ParseResult

type ParseResult struct {
	DeviceEUI  string     `json:"device_eui"`
	DeviceID   string     `json:"device_id,omitempty"`
	SpaceSlug  string     `json:"space_slug,omitempty"`
	DeviceInfo DeviceInfo `json:"device_info"`
	Entities   []Entity   `json:"entities"`
	Timestamp  time.Time  `json:"timestamp"`
}

ParseResult is the final result containing all generated entities.

type ParsedData

type ParsedData struct {
	DeviceEUI    string                 `json:"device_eui"`
	DeviceType   DeviceType             `json:"device_type"`
	Timestamp    time.Time              `json:"timestamp"`
	Location     *Location              `json:"location,omitempty"`
	SensorData   map[string]interface{} `json:"sensor_data"`
	BatteryLevel *float64               `json:"battery_level,omitempty"`
	RawData      string                 `json:"raw_data,omitempty"`
}

ParsedData is the intermediate result of parsing a single device uplink.

type Parser

type Parser interface {
	ParsePayload(payload *RawPayload) (*ParsedData, error)
	ParseToEntities(orgSlug, model string, payload *RawPayload, deviceLocation *Location) ([]Entity, error)
	SupportsGPS() bool
	GetSupportedPorts() []int
	GetSupportedEntityTypes() []string
}

Parser is the interface every device model must implement.

type RawPayload

type RawPayload struct {
	DeviceEUI string                 `json:"device_eui"`
	FPort     int                    `json:"fport"`
	Data      string                 `json:"data"` // Base64-encoded payload
	Timestamp time.Time              `json:"timestamp"`
	RxInfo    []GatewayInfo          `json:"rx_info"`
	Metadata  map[string]interface{} `json:"metadata"`
	LNSType   lns.LNSType            `json:"lns_type"`
}

RawPayload represents the incoming device data before parsing.

type TemplateProvider added in v0.0.5

type TemplateProvider interface {
	GetEntityTemplates(model, devEUI string) []Entity
}

TemplateProvider is implemented by parsers that can expose bootstrap entity templates.

Jump to

Keyboard shortcuts

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