2024-03-03 21:42:13 +00:00
|
|
|
package satel
|
|
|
|
|
|
|
|
import (
|
2024-03-04 21:09:48 +00:00
|
|
|
"encoding/hex"
|
2024-03-04 19:24:13 +00:00
|
|
|
"errors"
|
2024-03-04 21:09:48 +00:00
|
|
|
"fmt"
|
2024-03-03 21:42:13 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/text/encoding/charmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeviceType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
DeviceType_Partition DeviceType = 0
|
|
|
|
DeviceType_Zone DeviceType = 1
|
|
|
|
DeviceType_User DeviceType = 2
|
|
|
|
DeviceType_ExpanderOrLCD DeviceType = 3
|
|
|
|
DeviceType_Output DeviceType = 4
|
|
|
|
DeviceType_ZoneWithPartitionAssignment DeviceType = 5
|
|
|
|
DeviceType_Timer DeviceType = 6
|
|
|
|
DeviceType_Telephone DeviceType = 7
|
|
|
|
DeviceType_Object DeviceType = 15
|
|
|
|
DeviceType_PartitionWithObjectAssignment DeviceType = 16
|
|
|
|
DeviceType_OutputWithDurationTime DeviceType = 17
|
|
|
|
DeviceType_PartitionWithObjectAssignmentAndOptions DeviceType = 18
|
|
|
|
)
|
|
|
|
|
|
|
|
type NameEvent struct {
|
|
|
|
DevType DeviceType
|
|
|
|
DevNumber byte
|
|
|
|
DevTypeFunction byte
|
|
|
|
DevName string
|
|
|
|
}
|
|
|
|
|
2024-03-04 19:24:13 +00:00
|
|
|
func makeNameEvent(bytes []byte) (*NameEvent, error) {
|
|
|
|
if len(bytes) < 19 {
|
2024-03-04 21:09:48 +00:00
|
|
|
return nil, errors.New(fmt.Sprint("Received data too short: ", hex.Dump(bytes)))
|
2024-03-04 19:24:13 +00:00
|
|
|
}
|
2024-03-03 21:42:13 +00:00
|
|
|
cp1250dec := charmap.Windows1250.NewDecoder()
|
|
|
|
name, err := cp1250dec.String(string(bytes[3:19]))
|
|
|
|
if err != nil {
|
2024-03-04 19:24:13 +00:00
|
|
|
return nil, err
|
2024-03-03 21:42:13 +00:00
|
|
|
}
|
2024-03-04 19:24:13 +00:00
|
|
|
return &NameEvent{
|
2024-03-03 21:42:13 +00:00
|
|
|
DevType: DeviceType(bytes[0]),
|
|
|
|
DevNumber: bytes[1],
|
|
|
|
DevTypeFunction: bytes[2],
|
|
|
|
DevName: strings.TrimSpace(name),
|
2024-03-04 19:24:13 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type getNameResult struct {
|
2024-03-05 20:42:56 +00:00
|
|
|
ev *NameEvent
|
2024-03-04 19:24:13 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Satel) GetName(devType DeviceType, devIndex byte) (*NameEvent, error) {
|
|
|
|
resultChan := make(chan getNameResult)
|
|
|
|
|
|
|
|
s.commandQueue <- func() {
|
2024-03-06 18:22:45 +00:00
|
|
|
err := s.sendCmd(SatelCommand_ReadDeviceName, byte(devType), devIndex)
|
2024-03-04 19:24:13 +00:00
|
|
|
if err != nil {
|
|
|
|
resultChan <- getNameResult{nil, errors.New("Could not send Satel read device name")}
|
2024-03-04 21:09:48 +00:00
|
|
|
return
|
2024-03-04 19:24:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var bytes = <-s.rawEvents
|
2024-03-04 21:09:48 +00:00
|
|
|
if len(bytes) < (1 + 2) {
|
|
|
|
resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received data too short to get name: ", hex.Dump(bytes)))}
|
|
|
|
return
|
2024-03-04 19:24:13 +00:00
|
|
|
}
|
|
|
|
cmd := bytes[0]
|
|
|
|
bytes = bytes[1 : len(bytes)-2]
|
2024-03-06 18:22:45 +00:00
|
|
|
if cmd == Satel_Result {
|
2024-03-04 21:09:48 +00:00
|
|
|
resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received an error instead of a name: ", hex.Dump(bytes)))}
|
2024-03-06 18:22:45 +00:00
|
|
|
} else if cmd == SatelCommand_ReadDeviceName {
|
2024-03-04 19:24:13 +00:00
|
|
|
name, err := makeNameEvent(bytes)
|
|
|
|
resultChan <- getNameResult{name, err}
|
|
|
|
} else {
|
2024-03-04 21:09:48 +00:00
|
|
|
resultChan <- getNameResult{nil, errors.New(fmt.Sprint("Received unexpected reply: ", hex.Dump(bytes)))}
|
2024-03-04 19:24:13 +00:00
|
|
|
}
|
2024-03-03 21:42:13 +00:00
|
|
|
}
|
2024-03-04 19:24:13 +00:00
|
|
|
nameResult := <-resultChan
|
|
|
|
return nameResult.ev, nameResult.err
|
2024-03-03 21:42:13 +00:00
|
|
|
}
|