Help us shape the future of Go! Take the 2026 Go Developer Survey

connectedbytcp

package module
v0.0.0-...-7eab8cf Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2016 License: MIT Imports: 12 Imported by: 1

README

connectedbytcp

A golang library for discovering and controlling ConnectedByTCP smart bulbs

##Documentation See godoc

##Installation

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

##Package

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

##Using the library To control a bulb, you first have to call Scan() to get a list of all the ConnectedByTCP hubs. The address of the hub will then be in ScanResponse.Location. The next important step is that before you can talk to the bulbs, you have to press the physical "sync" button on your hub hardware, then call the GetToken() function. This will then return a security token that must be used in all API calls. If you don't press the physical "sync" button on the hub before calling this function you will get an error.

Once you have the address and the token, you can then call RoomGetCarousel, this returns a list of rooms and devices (the bulbs). To control a bulb, you then need the hub address, the token and then the ID of the bulb you want to control, which was in the Device.DID field.

NOTE: it is assumed you configured the bulbs using the app provided by the hardware maker, once you have configured the bulbs you can then use this library to control them.

##Version History ###0.1.0 Initial release, support for scanning for devices and TurnOn/TurnOff/SetValue

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnauthorized = errors.New("unauthorized")

ErrUnauthorized represents an error when the user tried to call an API but were not authorized to do so. This can occur if you try to call GetToken without pressing the "sync" button on your physical hub or if you try to make API calls without a valid token

Functions

func GetToken

func GetToken(ctx context.Context, address string) (string, error)

GetToken returns the security token required to make any API calls to the ConnectedByTCP hub. In order for this function to succeed, you must press the physical "sync" button on the hub before calling this function. Calling this function without first pressing the sync button will cause a ErrUnauthorized error to be returned. The address field should be in the format "https://192.168.0.23" for example.

The context.Context instance is passed to the network code, so you can use it to set timeouts for the request. If you want to set a timeout, just call context.WithTimeout() and pass in the result. IF you want a blank context, just use context.TOOD()

IMPORTANT: It seems that every time you get a token the previous token becomes invalid, so you will need to update wherever you are using it after calling this function. Also the official app also generates tokens if you scan so things may stop working if you use the official app after getting these tokens, if so then you will need to generate a new one

func SetLevel

func SetLevel(ctx context.Context, hubAddress, zoneAddress, token string, level int32) error

SetLevel sets the bulb to the specified level

func TurnOff

func TurnOff(ctx context.Context, hubAddress, zoneAddress, token string) error

TurnOff turns the bulb off

func TurnOn

func TurnOn(ctx context.Context, hubAddress, zoneAddress, token string) error

TurnOn turns the bulb on

func VerifyConnection

func VerifyConnection(ctx context.Context, address, token string) error

VerifyConnection connects to the hub and tries to perform an API call to verify that the supplied parameters are correct. If no error is returned then the call was successful and the address and token are valid values

Types

type Device

type Device struct {
	DID   string  `xml:"did"`
	Known float64 `xml:"known"`
	// 0 == off, 1 == on
	State float64 `xml:"state"`
	// Intensity level of the bulb
	Level      float64 `xml:"level"`
	Offline    float64 `xml:"offline"`
	Node       float64 `xml:"node"`
	Port       float64 `xml:"port"`
	NodeType   float64 `xml:"nidetype"`
	Name       string  `xml:"name"`
	ColorID    float64 `xml:"colorid"`
	Type       string  `xml:"type"`
	RangeMin   float64 `xml:"rangemin"`
	Power      float64 `xml:"power"`
	PowerAvg   float64 `xml:"poweravg"`
	Energy     float64 `xml:"energy"`
	Score      float64 `xml:"score"`
	ProductID  float64 `xml:"productid"`
	ProdBrand  string  `xml:"prodbrand"`
	ProdModel  string  `xml:"prodmodel"`
	ProdType   string  `xml:"prodtype"`
	ProdTypeID float64 `xml:"prodtypeid"`
	ClassID    float64 `xml:"classid"`
}

Device represents the device information returned by the ConnectedByTCP hub in the response to the RoomGetCarousel call

type GIP

type GIP struct {
	Rooms []Room `xml:"gwrcmd>gdata>gip>room"`
}

GIP represents the gip element returned by the ConnectedByTCP hub in response to the RoomGetCarousel call

func RoomGetCarousel

func RoomGetCarousel(ctx context.Context, address, token string) (*GIP, error)

RoomGetCarousel return the room and device information from the ConnectedByTCP hub. Call this to get room and bulb information, such as device IDs, names etc. Address should be in the form "https://192.168.0.23" for example and the token value should be the token you retried from the GetToken function call

type Room

type Room struct {
	Name        string   `xml:"name"`
	Description string   `xml:"desc"`
	Known       float64  `xml:"known"`
	Type        float64  `xml:"type"`
	Color       string   `xml:"color"`
	ColorID     float64  `xml:"colorid"`
	Image       string   `xml:"img"`
	Power       float64  `xml:"power"`
	PowerAVG    float64  `xml:"poweravg"`
	Energy      float64  `xml:"energy"`
	Devices     []Device `xml:"device"`
}

Room represents the room information returned by the ConnectedByTCP hub in response to the RoomGetCarousel call

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

func Scan

func Scan(waitTimeSeconds int) ([]ScanResponse, error)

Scan returns ScanResponse instances for each device found on the network

Jump to

Keyboard shortcuts

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