wemo

package module
v0.0.0-...-1e6e989 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2017 License: Apache-2.0 Imports: 17 Imported by: 3

README

go.wemo

GoDoc Build Status

Simple package to interface with Belkin wemo devices. That has forked from the original package.

This package supports, Belkin Wemo:

  • Bridge & Bulbs
  • Sockets
  • Insight

This package also includes a subscription to device events and adds basic device discovery.

Example - Device discovery
package main

import (
	"fmt"
	"github.com/danward79/go.wemo"
	"time"
)

func main() {
  api, _ := wemo.NewByInterface("en0")
  devices, _ := api.DiscoverAll(3*time.Second)
  for _, device := range devices {
    fmt.Printf("Found %+v\n", device)
  }
}
Example - Control a device
package main

import (
  "fmt"
  "github.com/danward79/go.wemo"
)

func main() {
  // you can either create a device directly OR use the
  // #Discover/#DiscoverAll methods to find devices
  device        := &wemo.Device{Host:"10.0.1.32:49153"}

  // retrieve device info
  deviceInfo, _ := device.FetchDeviceInfo()
  fmt.Printf("Found => %+v\n", deviceInfo)

  // device controls
  device.On()
  device.Off()
  device.Toggle()
  device.GetBinaryState() // returns 0 or 1
}
Example - Control a named light

As a convenience method, you can control lights through a more generic interface.

package main

import (
  "github.com/danward79/go.wemo"
  "time"
)

func main() {
  api, _ := wemo.NewByInterface("en0")
  api.On("Left Light", 3*time.Second)
  api.Off("Left Light", 3*time.Second)
  api.Toggle("Left Light", 3*time.Second)
}

###Example - Managing Subscriptions

This is an example of discovering devices, subscribing to there events and being notified of changed to there state. Resubscriptions are managed automatically at the timeout specified. Subscriber details and state are maintained in a map.

package main

import (
	"github.com/danward79/go.wemo"
	"time"
  "log"
)

func main() {

  listenerAddress := "192.168.0.6:6767"
  timeout := 300

  api, _ := wemo.NewByInterface("en0")

  devices, _ := api.DiscoverAll(3*time.Second)

  subscriptions := make(map[string]*wemo.SubscriptionInfo)

  for _, device := range devices {
    _, err := device.ManageSubscription(listenerAddress, timeout, subscriptions)
    if err != 200 {
      log.Println("Initial Error Subscribing: ", err)   
    }
  }

  cs := make(chan wemo.SubscriptionEvent)

  go wemo.Listener(listenerAddress, cs)

  for m := range cs{
    if _, ok := subscriptions[m.Sid]; ok {
      subscriptions[m.Sid].State = m.State
      log.Println("---Subscriber Event: ", subscriptions[m.Sid])
    } else {
      log.Println("Does'nt exist, ", m.Sid)
    }
  }

}

Device info can be found at: http://192.168.1.25:49153/setup.xml

Documentation

Overview

Package wemo ... Copyright 2014 Matt Ho

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package wemo ... Copyright 2014 Matt Ho

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package wemo ...

Copyright 2014 Matt Ho

// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.

Package wemo ... Copyright 2014 Matt Ho

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package wemo ... Copyright 2014 Matt Ho

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package wemo ...

Copyright 2014 Matt Ho

// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.

Index

Constants

View Source
const (
	Basic      = "urn:Belkin:service:basicevent:1"
	Bridge     = "urn:Belkin:device:bridge:1"
	Controllee = "urn:Belkin:device:controllee:1"
	Light      = "urn:Belkin:device:light:1"
	Sensor     = "urn:Belkin:device:sensor:1"
	NetCam     = "urn:Belkin:device:netcam:1"
	Insight    = "urn:Belkin:device:insight:1"
)

Constants for URNS

View Source
const (
	SSDPBROADCAST = "239.255.255.250:1900"
	MSEARCH       = "" /* 146-byte string literal not displayed */
	LOCATION      = "LOCATION: "
)

Constants associated with Scanning

Variables

This section is empty.

Functions

func Listener

func Listener(listenerAddress string, cs chan SubscriptionEvent)

Listener Listen for incomming subscribed state changes.

Types

type BulbStatusList

type BulbStatusList struct {
	DeviceStatus []DeviceStatus `xml:"Body>GetDeviceStatusResponse>DeviceStatusList>DeviceStatusList>DeviceStatus"`
}

BulbStatusList ...

type Device

type Device struct {
	Host   string
	Logger func(string, ...interface{}) (int, error)
}

Device struct

func (*Device) Bulb

func (d *Device) Bulb(id, cmd, value string, group bool) error

Bulb ...

func (*Device) FetchDeviceInfo

func (d *Device) FetchDeviceInfo(ctx context.Context) (*DeviceInfo, error)

FetchDeviceInfo from device

func (*Device) GetBinaryState

func (d *Device) GetBinaryState() int

GetBinaryState ...

func (*Device) GetBridgeEndDevices

func (d *Device) GetBridgeEndDevices(uuid string) *EndDevices

GetBridgeEndDevices ...

func (*Device) GetBulbStatus

func (d *Device) GetBulbStatus(ids string) (map[string]string, error)

GetBulbStatus return map of [DeviceID]status values, function returns a map of deviceid to status as it is possible to have several DeviceID results returned.

func (*Device) GetInsightParams

func (d *Device) GetInsightParams() *InsightParams

GetInsightParams ...

func (*Device) ManageSubscription

func (d *Device) ManageSubscription(listenerAddress string, timeout int, subscriptions map[string]*SubscriptionInfo) (string, int)

ManageSubscription Manage firstly the subscription and then the resubscription of this device.

func (*Device) Off

func (d *Device) Off()

Off toggle state to Off

func (*Device) On

func (d *Device) On()

On toggle state to On

func (*Device) ReSubscribe

func (d *Device) ReSubscribe(sid, address string, timeout int) (string, int)

ReSubscribe The subscription to the device must be renewed before the timeout. Return the Subscription ID (sid) and StatusCode

func (*Device) SetState

func (d *Device) SetState(newState bool) error

SetState is a wrapper for changeState, which allows errors to be exposed to caller.

func (*Device) Subscribe

func (d *Device) Subscribe(listenerAddress, address, path string, timeout int) (string, int)

Subscribe to the device event emitter, return the Subscription ID (sid) and StatusCode

func (*Device) Toggle

func (d *Device) Toggle()

Toggle state

func (*Device) UnSubscribe

func (d *Device) UnSubscribe(sid, address string) int

UnSubscribe According to the spec all subscribers must unsubscribe when the publisher is no longer required to provide state updates. Return the StatusCode

type DeviceInfo

type DeviceInfo struct {
	Device          *Device `json:"-"`
	DeviceType      string  `xml:"deviceType" json:"device-type"`
	FriendlyName    string  `xml:"friendlyName" json:"friendly-name"`
	MacAddress      string  `xml:"macAddress" json:"mac-address"`
	FirmwareVersion string  `xml:"firmwareVersion" json:"firmware-version"`
	SerialNumber    string  `xml:"serialNumber" json:"serial-number"`
	UDN             string  `xml:"UDN" json:"UDN"`
	EndDevices      EndDevices
}

DeviceInfo struct

type DeviceInfos

type DeviceInfos []*DeviceInfo

DeviceInfos slice

func (DeviceInfos) Len

func (d DeviceInfos) Len() int

func (DeviceInfos) Less

func (d DeviceInfos) Less(i, j int) bool

func (DeviceInfos) Swap

func (d DeviceInfos) Swap(i, j int)

type DeviceStatus

type DeviceStatus struct {
	DeviceID        string `xml:"DeviceID"`
	CapabilityValue string `xml:"CapabilityValue"`
}

DeviceStatus ...

type Deviceevent

type Deviceevent struct {
	XMLName     xml.Name   `xml:"propertyset"`
	BinaryState string     `xml:"property>BinaryState"`
	StateEvent  StateEvent `xml:"property>StatusChange>StateEvent"`
}

Deviceevent Structure for XML to Parse to

type EndDeviceInfo

type EndDeviceInfo struct {
	DeviceIndex     string `xml:"DeviceIndex"`
	DeviceID        string `xml:"DeviceID"`
	FriendlyName    string `xml:"FriendlyName"`
	FirmwareVersion string `xml:"FirmwareVersion"`
	CapabilityIDs   string `xml:"CapabilityIDs"`
	CurrentState    string `xml:"CurrentState"`
	Manufacturer    string `xml:"Manufacturer"`
	ModelCode       string `xml:"ModelCode"`
	ProductName     string `xml:"productName"`
	WeMoCertified   string `xml:"WeMoCertified"`
}

EndDeviceInfo ...

type EndDevices

type EndDevices struct {
	DeviceListType string          `xml:"Body>GetEndDevicesResponse>DeviceLists>DeviceLists>DeviceList>DeviceListType"`
	EndDeviceInfo  []EndDeviceInfo `xml:"Body>GetEndDevicesResponse>DeviceLists>DeviceLists>DeviceList>DeviceInfos>DeviceInfo"`
}

EndDevices ...

type InsightParams

type InsightParams struct {
	Power int // mW
}

InsightParams ...

type StateEvent

type StateEvent struct {
	DeviceID     string `xml:"DeviceID"`
	CapabilityID string `xml:"CapabilityId"`
	Value        string `xml:"Value"`
}

StateEvent ...

type SubscriptionEvent

type SubscriptionEvent struct {
	Sid         string
	Deviceevent Deviceevent
}

SubscriptionEvent Structure for sending subscribed event data with

type SubscriptionInfo

type SubscriptionInfo struct {
	DeviceInfo
	Timeout     int
	Sid         string
	Host        string
	Deviceevent Deviceevent
}

SubscriptionInfo struct

type Wemo

type Wemo struct {
	Debug bool
	// contains filtered or unexported fields
}

Wemo ...

func NewByIP

func NewByIP(ipAddr string) *Wemo

NewByIP ...

func NewByInterface

func NewByInterface(name string) (*Wemo, error)

NewByInterface find the ip address associated with the specified interface

func (*Wemo) Discover

func (w *Wemo) Discover(urn string, timeout time.Duration) ([]*Device, error)

Discover ...

func (*Wemo) DiscoverAll

func (w *Wemo) DiscoverAll(timeout time.Duration) ([]*Device, error)

DiscoverAll ...

func (*Wemo) Off

func (w *Wemo) Off(friendlyName string, timeout time.Duration)

Off ...

func (*Wemo) On

func (w *Wemo) On(friendlyName string, timeout time.Duration)

On ...

func (*Wemo) Toggle

func (w *Wemo) Toggle(friendlyName string, timeout time.Duration)

Toggle ...

Directories

Path Synopsis
package main ...
package main ...

Jump to

Keyboard shortcuts

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