diff options
author | xengineering <me@xengineering.eu> | 2023-03-27 20:18:05 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-03-27 20:20:52 +0200 |
commit | bc1a0ba75311ea2d53f2cbd3e305b31a561fdafc (patch) | |
tree | f90691f1e98f14d1f84d89fb5ef04c254b2b15a5 | |
parent | 5c2d27cd4adbf517a15b7b4644098fa42d69df19 (diff) | |
download | webiot-bc1a0ba75311ea2d53f2cbd3e305b31a561fdafc.tar webiot-bc1a0ba75311ea2d53f2cbd3e305b31a561fdafc.tar.zst webiot-bc1a0ba75311ea2d53f2cbd3e305b31a561fdafc.zip |
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.
-rw-r--r-- | hs100.go | 12 | ||||
-rw-r--r-- | main.go | 9 |
2 files changed, 2 insertions, 19 deletions
@@ -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 { @@ -28,7 +28,6 @@ type WebConfig struct { Listen netip.AddrPort } -// main() contains the control flow of this program. func main() { configPath := parseFlags() c := parseConfig(configPath) @@ -42,7 +41,7 @@ func main() { // parseFlags() handles command line interface (CLI) flags. func parseFlags() string { - var r string // return value + var r string flag.StringVar(&r, "c", "/etc/webiot/config.json", "path to configuration file") @@ -57,13 +56,11 @@ func parseFlags() string { // returns it as Go datastructure. func parseConfig(path string) RuntimeConfig { - // read config file and ensure proper JSON formatting data := mustRead(path) if !json.Valid(data) { log.Fatalf("%s contains invalid JSON!", path) } - // read to RuntimeConfig struct and handle errors config := RuntimeConfig{} err := json.Unmarshal(data, &config) if err != nil { @@ -76,7 +73,6 @@ func parseConfig(path string) RuntimeConfig { // index() returns a HTTP handler for the index page. func index(devices DevicesConfig, appdata string) func(http.ResponseWriter, *http.Request) { - // prepare HTML path := filepath.Join(appdata, "index.html.tmpl") html := mustRender(path, devices) @@ -87,7 +83,6 @@ func index(devices DevicesConfig, appdata string) func(http.ResponseWriter, *htt func css(appdata string) func(http.ResponseWriter, *http.Request) { - // read CSS file path := filepath.Join(appdata, "simple.css") css := string(mustRead(path)) @@ -128,7 +123,6 @@ func api() func(http.ResponseWriter, *http.Request) { // TODO assert correct HTTP method - // read parameters and handle errors errHost, host := assertSingleParam(r, "host") errState, state := assertSingleParam(r, "state") if (errHost != nil) || (errState != nil) { @@ -137,7 +131,6 @@ func api() func(http.ResponseWriter, *http.Request) { return } - // set WiFi plug err := set(host, state) if err != nil { http.Error(w, "Could not set WiFi plug.", 500) |