belkin

package module
v0.0.0-...-5256eb3 Latest Latest
Warning

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

Go to latest
Published: May 25, 2020 License: MIT Imports: 12 Imported by: 0

README

This is a fork of the version at github.com/go-home-iot/belkin and should be imported as import belkin "github.com/magicmonkey/gobelkinwemo". This fork adds a callback which gets called as soon as a device is discovered on the network, instead of waiting for the full timeout duration before returning any devices. To use this, call ScanWithCallback instead of Scan

belkin

A golang library to scan and control Belkin devices, such as the WeMo Maker, WeMo Insight

##Documentation See godoc

##Support This library has only been tested against:

  • WeMo Maker
  • WeMo Insight Switch

It may work for the other devices, but has not been tested against them.

##Installation

go get github.com/go-home-iot/belkin

##Package

import "github.com/go-home-iot/belkin"

##Testing Run the unit tests to talk to actual devices. The tests assume that you have real devices connected to the local network that can be used during testing

go test

or for more detailed responses from the devices

go test -v

##Version History ###0.2.0 Added support for GetBinaryState and GetAttributes calls ###0.1.0 Initial release, support for scanning for belkin devices and TurnOn/TurnOff

Documentation

Overview

Package belkin provides support for Belkin devices, such as the WeMo Switch

Index

Constants

View Source
const (
	// DTBridge - belkin bridge
	DTBridge DeviceType = "urn:Belkin:device:bridge:1"

	// DTSwitch - belkin switch
	DTSwitch = "urn:Belkin:device:controllee:1"

	// DTMotion - belkin motion sensor
	DTMotion = "urn:Belkin:device:sensor:1"

	// DTMaker - belkin maker
	DTMaker = "urn:Belkin:device:Maker:1"

	// DTInsight - belkin insight
	DTInsight = "urn:Belkin:device:insight:1"

	// DTLightSwitch - belkin light switch
	DTLightSwitch = "urn:Belkin:device:lightswitch:1"
)

Variables

View Source
var ErrUnsupportedAction = errors.New("unsupported action")

ErrUnsupportedAction is returned when you try to perform an action on a piece of hardware that doesn't support it, e.g. calling FetchAttributes on a non Maker device

Functions

func ScanWithCallback

func ScanWithCallback(dt DeviceType, waitTimeSeconds int, callback func(Device)) error

Types

type BinaryState

type BinaryState struct {
	OnOff   int
	OnSince int64
}

BinaryState represents the BinaryState information returned from a device

func ParseBinaryState

func ParseBinaryState(body string) *BinaryState

ParseBinaryState parses the BinaryState element that is received from some WeMo devices. It is expected that the body string looks like: <BinaryState>1|1477978435|0|0|0|1168438|0|100|0|0</BinaryState>

type Device

type Device struct {
	Scan             ScanResponse
	DeviceType       string    `xml:"deviceType"`
	FriendlyName     string    `xml:"friendlyName"`
	Manufacturer     string    `xml:"manufacturer"`
	ManufacturerURL  string    `xml:"manufacturerURL"`
	ModelDescription string    `xml:"modelDescription"`
	ModelName        string    `xml:"modelName"`
	ModelNumber      string    `xml:"modelNumber"`
	ModelURL         string    `xml:"modelURL"`
	SerialNumber     string    `xml:"serialNumber"`
	UDN              string    `xml:"UDN"`
	UPC              string    `xml:"UPC"`
	MACAddress       string    `xml:"macAddress"`
	FirmwareVersion  string    `xml:"firmwareVersion"`
	IconVersion      string    `xml:"iconVersion"`
	BinaryState      int       `xml:"binaryState"`
	ServiceList      []Service `xml:"serviceList>service"`
	Timeout          time.Duration
}

Device contains information about a device that has been found on the network

func NewDeviceFromURL

func NewDeviceFromURL(location string, timeout time.Duration) (*Device, error)

NewDeviceFromURL allows a device with a known IP to be loaded with a URL like http://1.2.3.4:49153/setup.xml

func Scan

func Scan(dt DeviceType, waitTimeSeconds int) ([]*Device, error)

Scan detects Belkin devices on the network. The devices that are returned have limited information in the Scan field, to get more detailed information you will have to call Load() on the device

func (*Device) FetchAttributes

func (d *Device) FetchAttributes(timeout time.Duration) (*DeviceAttributes, error)

FetchAttributes fetches the attributes of the device, such as switch state, sensor state etc

func (*Device) FetchBinaryState

func (d *Device) FetchBinaryState(timeout time.Duration) (int, error)

FetchBinaryState fetches the latest binary state value from the device

func (*Device) Load

func (d *Device) Load(timeout time.Duration) error

Load fetches all of the device specific information and updates the calling struct. The timeout parameter specifies how long to wait to connect and get a response before giving up

func (*Device) TurnOff

func (d *Device) TurnOff(timeout time.Duration) error

TurnOff turns off the device.

func (*Device) TurnOn

func (d *Device) TurnOn(timeout time.Duration) error

TurnOn turns on the device.

type DeviceAttributes

type DeviceAttributes struct {
	// The current state of the switch, 1 -> on, 0 -> off
	Switch *int

	// The sensor value, 1 -> open, 0 -> closed
	Sensor *int

	// The mode of the switch, 0 -> toggle, 1 -> momentary
	SwitchMode *int

	// If the sensor is active, 1 -> yes, 0 -> no
	SensorPresent *int
}

DeviceAttributes contains values returned from the FetchAttributes call. Values are pointers, a nil value indicated we didn't get any value for this particular attribute

func ParseAttributeList

func ParseAttributeList(body string) *DeviceAttributes

ParseAttributeList parses the xml attributeList response from the device e.g. <attributeList><attribute><name>...</name><value>...</value></attribute>...</attributeList> the body must be the open and close attributeList element. If a valid input is not found the function will return nil

type DeviceType

type DeviceType string

DeviceType represents an identifier for the type of Belkin device you want to scan the network for

type ScanResponse

type ScanResponse struct {
	MaxAge     int
	SearchType string
	DeviceID   string
	USN        string
	Location   string
	Server     string
	URN        string
}

ScanResponse contains information from a device that responded to a scan response

type Service

type Service struct {
	ServiceType string `xml:"serviceType"`
	ServiceID   string `xml:"serviceId"`
	ControlURL  string `xml:"controlURL"`
	EventSubURL string `xml:"eventSubURL"`
	SCPDURL     string `xml:"SCPDURL"`
}

Service contains information about a service exposed by the Belkin device

Jump to

Keyboard shortcuts

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