57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package device
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// SwitchInfo contains information about the ZKL 3000 RC switch
|
|
type SwitchInfo struct {
|
|
SwitchStatus uint32
|
|
Released bool
|
|
Token uint32
|
|
}
|
|
|
|
// NewSwitchInfo generates the switch information based on the given switch reply/message
|
|
func NewSwitchInfo(switchMsg []string) (*SwitchInfo, error) {
|
|
if len(switchMsg) == 0 {
|
|
return nil, fmt.Errorf("too few parameters")
|
|
}
|
|
var switchInfo SwitchInfo
|
|
|
|
params := strings.Split(switchMsg[0], ",")
|
|
switchStatus, err := strconv.ParseUint(params[0], 16, 32)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("switch status is not numeric: %v", err)
|
|
}
|
|
switchInfo.SwitchStatus = uint32(switchStatus)
|
|
if len(params) > 1 {
|
|
token, tokenErr := strconv.ParseUint(params[1], 10, 32)
|
|
if tokenErr != nil {
|
|
return nil, fmt.Errorf("token is not numeric: %v", err)
|
|
}
|
|
switchInfo.Released = true
|
|
switchInfo.Token = uint32(token)
|
|
}
|
|
return &switchInfo, nil
|
|
}
|
|
|
|
// Valid returns true when the switch info is valid
|
|
func (s *SwitchInfo) Valid() bool {
|
|
return s.SwitchStatus&0x01000000 == 0x01000000
|
|
}
|
|
|
|
// Malfunction returns true when the switch info is malfunction
|
|
func (s *SwitchInfo) Malfunction() bool {
|
|
return s.SwitchStatus&0x80000000 == 0x80000000
|
|
}
|
|
|
|
// Changed returns true when the switch was released, but is now not released or visa versa
|
|
func (s *SwitchInfo) Changed(p *SwitchInfo) bool {
|
|
if p == nil {
|
|
return true
|
|
}
|
|
return s.Released != p.Released
|
|
}
|