Skip to content

Yet another tp-link HS100 library for golang

License

Notifications You must be signed in to change notification settings

jaedle/golang-tplink-hs100

Repository files navigation

Library for tp-link HS100/HS110

Yet another tp-link HS100 library for golang

Badges

Codacy Badge Tests Coverage Status Code Smells Maintainability Rating Technical Debt

Supported Go versions:

  • 1.16
  • 1.17
  • 1.18

Usage

Please make sure to have go-modules enabled.

Run within your go project folder: go get -u github.com/jaedle/golang-tplink-hs100

Usage example

use the following code as main and replace YOUR_HS100_DEVICE with the address of your HS100-device.

package main

import (
	"github.com/jaedle/golang-tplink-hs100/pkg/configuration"
	"github.com/jaedle/golang-tplink-hs100/pkg/hs100"
	"os"
)

func main() {
	h := hs100.NewHs100("YOUR_HS100_DEVICE", configuration.Default())

	info, err := h.GetInfo()
	if err != nil {
		println("Error on accessing device")
		os.Exit(1)
	}

	println("Name of device: " + info.Name)
}

Device discovery

It is possible to discover devices automatically. Because this library uses tcp communication this requires to specify a subnet by CIDR notation. For this example 192.168.2.0/24 all ips from 192.168.2.1 to 192.168.2.255 will be tried to reached. By using withTimeout(time.Duration) a custom timeout can be specified instead of the default timeout.

The discovery process will take at least the time of the default timeout.

package main

import (
	"github.com/jaedle/golang-tplink-hs100/pkg/configuration"
	"github.com/jaedle/golang-tplink-hs100/pkg/hs100"
	"log"
	"time"
)

func main() {
	devices, err := hs100.Discover("192.168.2.0/24",
		configuration.Default().WithTimeout(time.Second),
	)

	if err != nil {
		panic(err)
	}

	log.Printf("Found devices: %d", len(devices))
	for _, d := range devices {
		info, _ := d.GetInfo()
		log.Printf("Found device (name, id): %s, %s", info.Name, info.DeviceId)
	}
}

Acknowledgements

Development

Prerequisites

  1. go-task
  2. docker

Project structure

This project tries to stick as close as possible to the golang standard project layout

The public parts for this library are located in /pkg.

All files in /cmd are for demo purposes only.

License

MIT