1
0
Fork 0

GetName: Better error reporting

This commit is contained in:
Michał Rudowicz 2024-03-04 22:09:48 +01:00
parent 999413ca99
commit c93d9db530
1 changed files with 9 additions and 5 deletions

View File

@ -1,7 +1,9 @@
package satel package satel
import ( import (
"encoding/hex"
"errors" "errors"
"fmt"
"strings" "strings"
"golang.org/x/text/encoding/charmap" "golang.org/x/text/encoding/charmap"
@ -33,7 +35,7 @@ type NameEvent struct {
func makeNameEvent(bytes []byte) (*NameEvent, error) { func makeNameEvent(bytes []byte) (*NameEvent, error) {
if len(bytes) < 19 { if len(bytes) < 19 {
return nil, errors.New("Received data too short") return nil, errors.New(fmt.Sprint("Received data too short: ", hex.Dump(bytes)))
} }
cp1250dec := charmap.Windows1250.NewDecoder() cp1250dec := charmap.Windows1250.NewDecoder()
name, err := cp1250dec.String(string(bytes[3:19])) name, err := cp1250dec.String(string(bytes[3:19]))
@ -60,21 +62,23 @@ func (s *Satel) GetName(devType DeviceType, devIndex byte) (*NameEvent, error) {
err := s.sendCmd(0xEE, byte(devType), devIndex) err := s.sendCmd(0xEE, byte(devType), devIndex)
if err != nil { if err != nil {
resultChan <- getNameResult{nil, errors.New("Could not send Satel read device name")} resultChan <- getNameResult{nil, errors.New("Could not send Satel read device name")}
return
} }
var bytes = <-s.rawEvents var bytes = <-s.rawEvents
if len(bytes) < (1 + 19 + 2) { if len(bytes) < (1 + 2) {
resultChan <- getNameResult{nil, errors.New("Received data too short")} resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received data too short to get name: ", hex.Dump(bytes)))}
return
} }
cmd := bytes[0] cmd := bytes[0]
bytes = bytes[1 : len(bytes)-2] bytes = bytes[1 : len(bytes)-2]
if cmd == 0xEF { if cmd == 0xEF {
resultChan <- getNameResult{nil, errors.New("Did not receive a name")} resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received an error instead of a name: ", hex.Dump(bytes)))}
} else if cmd == 0xEE { } else if cmd == 0xEE {
name, err := makeNameEvent(bytes) name, err := makeNameEvent(bytes)
resultChan <- getNameResult{name, err} resultChan <- getNameResult{name, err}
} else { } else {
resultChan <- getNameResult{nil, errors.New("Received unexpected result instead of a name")} resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received unexpected reply: ", hex.Dump(bytes)))}
} }
} }
nameResult := <-resultChan nameResult := <-resultChan