From bc1a0ba75311ea2d53f2cbd3e305b31a561fdafc Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 27 Mar 2023 20:18:05 +0200 Subject: Rework code documentation Comments inside functions are not that helpful. While the functino docstring should explain everything to a user, the implementation inside should speak for itself. --- hs100.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'hs100.go') diff --git a/hs100.go b/hs100.go index ff25576..f08c7de 100644 --- a/hs100.go +++ b/hs100.go @@ -26,18 +26,15 @@ type Hs100Conf struct { // encrypt() encrypts data for a TP-Link WiFi plug. func encrypt(data []byte) ([]byte, error) { - // assert maximum payload size to cast data length safely if len(data) > MAX_PAYLOAD { return []byte{}, fmt.Errorf("Too many bytes to encrypt (%d > %d)!\n", len(data), MAX_PAYLOAD) } length := uint32(len(data)) - // encode payload length as header - out := make([]byte, 4) // header buffer + out := make([]byte, 4) binary.BigEndian.PutUint32(out, length) - // encryption algorithm key := byte(171) for _, value := range data { key = key ^ value @@ -52,10 +49,8 @@ func decrypt(data []byte) []byte { // TODO check if length given in header is correct - // cut-off header data = data[4:] - // decryption algorithm key := byte(171) for index, value := range data { data[index] = key ^ value @@ -68,19 +63,16 @@ func decrypt(data []byte) []byte { // send() sends data via TCP to an address (like "192.168.1.42:9999"). func send(address string, data []byte) error { - // create a Dialer with context var d net.Dialer ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - // establish connection conn, err := d.DialContext(ctx, "tcp", address) if err != nil { return fmt.Errorf("Failed to dial: %v", err) } defer conn.Close() - // writing data _, err = conn.Write(data) if err != nil { return fmt.Errorf("Could not write data: %v", err) @@ -95,7 +87,6 @@ func set(host string, state string) error { cmd := "" - // modify command according to state if state == "on" { cmd = `{"system":{"set_relay_state":{"state":1}}}` } else if state == "off" { @@ -104,7 +95,6 @@ func set(host string, state string) error { return fmt.Errorf("set() just accepts values 'on' and 'off'!") } - // format address, encrypt data and send it address := fmt.Sprintf("%s:9999", host) data, err := encrypt([]byte(cmd)) if err != nil { -- cgit v1.2.3-70-g09d2