thing2

package module
v0.0.0-...-c08b5a7 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: BSD-3-Clause Imports: 36 Imported by: 0

README

thing2

Go Reference License Go Report Card Issues

Documentation

Index

Constants

View Source
const (
	FlagProgenitive   flags = 1 << iota // May have children
	FlagWantsHttpPort                   // HTTP port not optional

)

Variables

View Source
var Models = ModelMap{}

Functions

func GenerateUf2s

func GenerateUf2s(dir string) error

func Getenv

func Getenv(name string, defaultValue string) string

Getenv returns the environment variable's value, or if empty, the defaultValue

func Run

func Run()

Run the device

Environment variables:

PORT SITE DEMO WIFI_SSIDS WIFI_PASSPHRASES DEVICES DEVICES_FILE DEBUG_KEEP_BUILDS USER PASSWD

func Setenv

func Setenv(name, value string)

Setenv sets the environment variable

Types

type Config

type Config struct {
	// Model is the device model name
	Model string
	// Flags see FlagXxxx
	Flags flags
	// The device state
	State any
	// The device's embedded static file system
	FS *embed.FS
	// Targets support by device
	Targets []string
	// PollPeriod is the device polling period.  The default is 1 second.
	// The range is [1..forever) seconds.
	PollPeriod time.Duration
	// BgColor is the device background color
	BgColor string
	// FgColor is the device forground (text, border) color
	FgColor string
}

Config is the device model configuration

type Devicer

type Devicer interface {
	// GetConfig returns the device configuration
	GetConfig() Config
	// GetHandlers returns the device message Handlers
	GetHandlers() Handlers
	// Setup prepares the device for operation.  Device hardware and other
	// initializations are done here.  Returning an error fails the device load.
	Setup() error
	// Poll services the device.  Poll is called every Config.PollPeriod
	// seconds.  The Packet can be used to send a message.
	Poll(*Packet)
	// DemoSetup is DEMO mode Setup
	DemoSetup() error
	// DemoPoll is DEMO mode Poll
	DemoPoll(*Packet)
}

Devicer is the device model interface. A device is a concrete Devicer.

type Handler

type Handler[T any] struct {
	// Callback is called with packet containing type T message
	Callback func(pkt *Packet)
}

Handler for message type T

type Handlers

type Handlers map[string]handler

Handlers is a map of Handlers, keyed by path.

type Maker

type Maker func() Devicer

type Model

type Model struct {
	Package string
	Source  string
	Maker
}

type ModelMap

type ModelMap map[string]Model // key: model name

type NoMsg

type NoMsg struct{}

NoMsg is used as an empty message type when creating a Handle.

type Packet

type Packet struct {
	// Dst is the device id of the destination device
	Dst string
	// Path identifies the message content.  Path format is same as
	// url.URL.Path, with the leading slash.  e.g. /takeone.
	Path string
	// Msg is the packet payload.  Use NoMsg for no message.
	Msg json.RawMessage
}

Packet is the basic container for messages sent between devices.

func (*Packet) Marshal

func (p *Packet) Marshal(v any) *Packet

Marshal the packet message payload as JSON from v

func (*Packet) RouteDown

func (p *Packet) RouteDown()

RouteDown routes the packet down to a downlink. Which downlink is determined by a lookup in the routing table for the "next-hop" downlink, the downlink which is towards the destination.

func (*Packet) RouteUp

func (p *Packet) RouteUp()

RouteUp routes the packet up to:

  1. Each listening session, where a session is an http(s) client (browser, etc) that has also opened, and is listening on, a websocket at /wsx.

    The packet is transformed into an html snippet before being sent on the websocket to the client (see htmx, websockets). The packet path and the current session's view name the html template used for the transformation. The template name is in the format:

    {path}-{view}.tmpl

    For example, consider routing the packet with the message:

    var msg = MsgClicked{Relay: 2, State: true} pkt.SetPath("/clicked").Marshal(&msg).RouteUp()

    And say the current view is "overview". The template name is:

    clicked-overview.tmpl

    The template is executed and the resulting html snippet is sent on the websocket. Per htmx, the html snippet is swap by DOM id, so using a unique id in the template like:

    <div id="{{uniq `relay`}}"> ... </div>

  2. Each active uplink the device is dialed into. Each uplink is a websocket connected on /ws. The packet is JSON-encoded before sending on the websocket, and JSON-decoded by the receiving uplink device.

func (*Packet) SetDst

func (p *Packet) SetDst(dst string) *Packet

SetDst sets the packet destination, a device id

func (*Packet) SetPath

func (p *Packet) SetPath(path string) *Packet

SetPath sets the packet path

func (*Packet) String

func (p *Packet) String() string

String returns packet as string in format "[dst id/path] msg"

func (*Packet) Unmarshal

func (p *Packet) Unmarshal(v any) *Packet

Unmarshal the packet message payload as JSON into v

Directories

Path Synopsis
cmd
gen-models command
gen-uf2s command
hub command
examples
gps
io
gps
uf2
cmd/regen command
cmd/replace command

Jump to

Keyboard shortcuts

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