| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package protocol
- import "fmt"
- type SetAll struct {
- Fan [4]uint8
- PTC [4]uint8
- DoorZ uint8
- DoorX uint8
- DoorY uint8
- }
- func (s SetAll) Payload() ([]byte, error) {
- for i, v := range s.Fan {
- if v > DutyMax {
- return nil, fmt.Errorf("%w: fan[%d]=%d", ErrDutyRange, i, v)
- }
- }
- for i, v := range s.PTC {
- if v > DutyMax {
- return nil, fmt.Errorf("%w: ptc[%d]=%d", ErrDutyRange, i, v)
- }
- }
- for name, v := range map[string]uint8{"Z": s.DoorZ, "X": s.DoorX, "Y": s.DoorY} {
- if v > DoorPercentMax {
- return nil, fmt.Errorf("%w: door %s=%d", ErrDoorRange, name, v)
- }
- }
- return []byte{
- s.Fan[0], s.Fan[1], s.Fan[2], s.Fan[3],
- s.PTC[0], s.PTC[1], s.PTC[2], s.PTC[3],
- s.DoorZ, s.DoorX, s.DoorY,
- }, nil
- }
- func (s SetAll) Encode() ([]byte, error) {
- p, err := s.Payload()
- if err != nil {
- return nil, err
- }
- return Encode(TypeSetAll, p)
- }
- func DecodeSetAll(f Frame) (SetAll, error) {
- if f.Type != TypeSetAll {
- return SetAll{}, fmt.Errorf("%w: not SET_ALL", ErrType)
- }
- if len(f.Payload) != SetAllPayloadLen {
- return SetAll{}, ErrPayload
- }
- s := SetAll{
- Fan: [4]uint8{f.Payload[0], f.Payload[1], f.Payload[2], f.Payload[3]},
- PTC: [4]uint8{f.Payload[4], f.Payload[5], f.Payload[6], f.Payload[7]},
- DoorZ: f.Payload[8],
- DoorX: f.Payload[9],
- DoorY: f.Payload[10],
- }
- if _, err := s.Payload(); err != nil {
- return SetAll{}, err
- }
- return s, nil
- }
- func GetTemperatureRequest() []byte {
- b, err := Encode(TypeGetTemperature, nil)
- if err != nil {
- panic(err)
- }
- return b
- }
- func GetMotorStatusRequest() []byte {
- b, err := Encode(TypeGetMotorStatus, nil)
- if err != nil {
- panic(err)
- }
- return b
- }
- func EqualSetAll(a, b SetAll) bool {
- return a == b
- }
- func WithDoorsClearedTo(s SetAll, z, x, y uint8) SetAll {
- s.DoorZ, s.DoorX, s.DoorY = z, x, y
- return s
- }
|