77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
|
// Package plugctl provides an API for Kasa smart home switches.
|
||
|
package plugctl
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"net"
|
||
|
)
|
||
|
|
||
|
// Plug provides an API for a single Kasa smart home switch.
|
||
|
type Plug struct {
|
||
|
Addr net.TCPAddr
|
||
|
}
|
||
|
|
||
|
type msg struct {
|
||
|
System *systemMsg `json:"system,omitempty"`
|
||
|
EMeter *eMeterMsg `json:"emeter,omitempty"`
|
||
|
}
|
||
|
|
||
|
type eMeterMsg struct {
|
||
|
GetRealtime *getRealtimeMsg `json:"get_realtime"`
|
||
|
}
|
||
|
|
||
|
type getRealtimeMsg struct{}
|
||
|
|
||
|
type systemMsg struct {
|
||
|
GetSysInfo *getSysInfoMsg `json:"get_sysinfo"`
|
||
|
SetRelayState *setRelayStateMsg `json:"set_relay_state,omitempty"`
|
||
|
}
|
||
|
|
||
|
type getSysInfoMsg struct{}
|
||
|
|
||
|
type setRelayStateMsg struct {
|
||
|
State int `json:"state"`
|
||
|
}
|
||
|
|
||
|
type decryptReader struct {
|
||
|
r io.Reader
|
||
|
c byte
|
||
|
}
|
||
|
|
||
|
func newDecrypter(r io.Reader) *decryptReader {
|
||
|
return &decryptReader{
|
||
|
r: r,
|
||
|
c: 0xab,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (d *decryptReader) Read(b []byte) (int, error) {
|
||
|
n, err := d.r.Read(b)
|
||
|
for i, x := range b {
|
||
|
b[i] = d.c ^ x
|
||
|
d.c = x
|
||
|
}
|
||
|
return n, err
|
||
|
}
|
||
|
|
||
|
type encryptWriter struct {
|
||
|
w io.Writer
|
||
|
c byte
|
||
|
}
|
||
|
|
||
|
func newEncrypter(w io.Writer) *encryptWriter {
|
||
|
return &encryptWriter{
|
||
|
w: w,
|
||
|
c: 0xab,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (e *encryptWriter) Write(b []byte) (int, error) {
|
||
|
eb := make([]byte, len(b))
|
||
|
for i, x := range b {
|
||
|
e.c ^= x
|
||
|
eb[i] = e.c
|
||
|
}
|
||
|
return e.w.Write(eb)
|
||
|
}
|