presence

package module
v0.0.1 Latest Latest
Warning

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

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

README

GoDocGo

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	Status(mac string, found bool)
}

Action Status function is called with the processed MAC address and info whether the device was found or not

type ActionFunc

type ActionFunc func(mac string, found bool)

ActionFunc is called with the processed MAC address and info whether the device was found or not

func (ActionFunc) Status

func (a ActionFunc) Status(mac string, found bool)

Status is called with the status of the given MAC address

type BluetoothScanner

type BluetoothScanner interface {
	IsPresent(context context.Context, mac string) (bool, error)
}

BluetoothScanner provides required methods to scan for bluetooth devices

type HCITool

type HCITool struct {
	// contains filtered or unexported fields
}

HCITool is the default BluetoothScanner implementation

func NewHCITool

func NewHCITool() (*HCITool, error)

NewHCITool returns a new instance of HCITool

func (*HCITool) IsPresent

func (h *HCITool) IsPresent(ctx context.Context, mac string) (bool, error)

IsPresent checks if bluetooth device with given MAC address can be found

type Presence

type Presence struct {
	// contains filtered or unexported fields
}

Presence allows scanning for nearby bluetooth devices based on their MAC address

func New

func New(btScanner BluetoothScanner) *Presence

New returns new Presence instance

func (*Presence) RegisterAction

func (p *Presence) RegisterAction(mac string, actions ...Action)

RegisterAction registers one ore more actions for a certain MAC address.

Note: Given actions are added to already existing registered actions for given MAC address.

func (*Presence) Scan

func (p *Presence) Scan(ctx context.Context)

Scan executes a single presence check for all registered MAC addresses and calls action functions with the scan result.

Example
mockBTScanner := &MockBTScanner{
	config: map[string]struct {
		found bool
		err   error
	}{
		"mymac":  {true, nil},
		"mymac2": {false, nil},
	},
}
p := New(mockBTScanner)

p.RegisterAction("mymac", ActionFunc(func(mac string, present bool) {
	fmt.Printf("%s found: %t\n", mac, present)
}))

p.RegisterAction("mymac", ActionFunc(func(mac string, present bool) {
	fmt.Printf("%s found: %t\n", mac, present)
}))

p.RegisterAction("mymac2", ActionFunc(func(mac string, present bool) {
	fmt.Printf("%s found: %t\n", mac, present)
}))

p.Scan(context.Background())
Output:

mymac2 found: false
mymac found: true
mymac found: true

func (*Presence) ScanPeriodically

func (p *Presence) ScanPeriodically(ctx context.Context, interval time.Duration)

ScanPeriodically will call Scan periodically with a given interval after a run cycle has finished

func (*Presence) UnregisterActions

func (p *Presence) UnregisterActions(mac string)

UnregisterActions removes all registered actions for a given MAC address

If no actions are registered, this method is a no-op.

Jump to

Keyboard shortcuts

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