Refactored code and added support for kasa smart plugs
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-09-24 00:42:42 +02:00
parent 76a8c5e620
commit dace0eba29
10 changed files with 553 additions and 210 deletions

88
kasa/kasa.go Normal file
View File

@@ -0,0 +1,88 @@
package kasa
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"net"
)
func encrypt(data []byte) []byte {
var key byte = 171
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(len(data)))
for _, c := range []byte(data) {
a := key ^ c
key = a
buf.WriteByte(a)
}
return buf.Bytes()
}
func decrypt(data []byte) string {
var key byte = 171
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, uint32(len(data)))
for _, c := range data {
a := key ^ c
key = c
buf.WriteByte(a)
}
return string(buf.Bytes())
}
type Kasa struct {
ip string
}
func New(ip string) Kasa {
return Kasa{ip}
}
func (kasa *Kasa) sendCmd(cmd cmd) {
con, err := net.Dial("tcp", fmt.Sprintf("%s:9999", kasa.ip))
if err != nil {
panic(err)
}
defer con.Close()
b, err := json.Marshal(cmd)
if err != nil {
panic(err)
}
_, err = con.Write(encrypt(b))
if err != nil {
panic(err)
}
resp := make([]byte, 2048)
_, err = con.Read(resp)
if err != nil {
panic(err)
}
var reply reply
json.Unmarshal(resp, &reply)
if reply.System.SetRelayState.ErrCode != 0 {
fmt.Println(reply)
fmt.Println(resp)
}
}
func (kasa *Kasa) SetState(on bool) {
var cmd cmd
if on {
cmd.System.SetRelayState.State = 1
}
kasa.sendCmd(cmd)
}

19
kasa/reply.go Normal file
View File

@@ -0,0 +1,19 @@
package kasa
type errCode struct {
ErrCode int
}
type reply struct {
System struct {
SetRelayState errCode `json:"set_relay_state"`
} `json:"system"`
}
type cmd struct {
System struct {
SetRelayState struct {
State int `json:"state"`
} `json:"set_relay_state"`
} `json:"system"`
}