discover

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 11 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 discover finds Shelly devices on a network in three ways, each confirmed with the device's GET /shelly:

  • DNS-SD: Gen2+ devices announce _shelly._tcp (with gen, app and ver in TXT) and _http._tcp; Gen1 devices announce _http._tcp with an instance name starting with "shelly". MDNS browses both through the application's mDNS browser (a Browser), so this package needs no mDNS code of its own; github.com/cB-Sigma/mdns is one browser (see the README for the five lines that connect it).
  • CoIoT: Gen1 devices multicast their status every few seconds; FromCoIoT turns a message received with shelly.ListenCoIoT into a candidate.
  • A scan: Scan asks every address of a prefix for GET /shelly, for networks where multicast does not reach.

It is a separate package so the shelly package stays small; it uses only the standard library and the shelly package.

Index

Constants

View Source
const (
	ServiceShelly = "_shelly._tcp" // Gen2 and later
	ServiceHTTP   = "_http._tcp"   // every generation
)

Service types Shelly devices announce.

Variables

This section is empty.

Functions

func ListenCoIoT

func ListenCoIoT(ctx context.Context, l *shelly.CoIoTListener, o Options, found func(Device)) error

ListenCoIoT reads CoIoT messages from l until ctx ends and calls found once per device (by id), confirmed with GET /shelly unless o.NoProbe.

Types

type Browser

type Browser func(ctx context.Context, service string) ([]Service, error)

Browser browses one DNS-SD service type ("_shelly._tcp") on the local link and returns what answered.

type Device

type Device struct {
	// Addr is host:port of its HTTP API.
	Addr string
	// ID is the Gen2+ id or, for Gen1, shelly<type>-<MAC> as announced
	// (or the MAC alone when only CoIoT saw it).
	ID string
	// Gen is the generation (1, 2, 3, 4); 0 while unknown.
	Gen int
	// Model is the model or Gen1 type when known; App is the Gen2+
	// application name; Ver the firmware.
	Model, App, Ver string
	// Via says how it was found: mdns, coiot or scan.
	Via string
	// Info is GET /shelly's answer when it was read.
	Info *shelly.Info
}

Device is a Shelly device found on the network.

func FromCoIoT

func FromCoIoT(m shelly.CoIoTMessage, from *net.UDPAddr) (Device, bool)

FromCoIoT turns a CoIoT message from a Gen1 device into a candidate at the sender's address, port 80.

func FromService

func FromService(s Service) (Device, bool)

FromService turns a DNS-SD answer into a candidate; ok is false when it is not a Shelly device.

func MDNS

func MDNS(ctx context.Context, b Browser, o Options) ([]Device, error)

MDNS browses _shelly._tcp and _http._tcp with b and returns the Shelly devices, one per address, confirmed with GET /shelly unless o.NoProbe. Devices that answer neither are left out.

func Probe

func Probe(ctx context.Context, hc *http.Client, addr string) (Device, error)

Probe reads GET /shelly at addr (host or host:port) and fills d from it. It fails when the answer is not a Shelly device's.

func Scan

func Scan(ctx context.Context, prefix netip.Prefix, o Options) ([]Device, error)

Scan probes every address of prefix (IPv4 only, at most a /16) on o.Port and returns the Shelly devices that answered, sorted by address.

type Options

type Options struct {
	// HTTP is used for GET /shelly; a 3 s timeout client when nil.
	HTTP *http.Client
	// NoProbe returns DNS-SD candidates without reading GET /shelly.
	NoProbe bool
	// Workers bounds concurrent probes (16 when 0).
	Workers int
	// Port is the port Scan probes (80 when 0).
	Port int
}

Options configure MDNS and Scan.

type Service

type Service struct {
	Instance string
	Host     string
	Port     int
	IPs      []net.IP
	TXT      map[string]string
}

Service is one DNS-SD service instance as an mDNS browser found it.

Jump to

Keyboard shortcuts

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