setall.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package protocol
  2. import "fmt"
  3. type SetAll struct {
  4. Fan [4]uint8
  5. PTC [4]uint8
  6. DoorZ uint8
  7. DoorX uint8
  8. DoorY uint8
  9. }
  10. func (s SetAll) Payload() ([]byte, error) {
  11. for i, v := range s.Fan {
  12. if v > DutyMax {
  13. return nil, fmt.Errorf("%w: fan[%d]=%d", ErrDutyRange, i, v)
  14. }
  15. }
  16. for i, v := range s.PTC {
  17. if v > DutyMax {
  18. return nil, fmt.Errorf("%w: ptc[%d]=%d", ErrDutyRange, i, v)
  19. }
  20. }
  21. for name, v := range map[string]uint8{"Z": s.DoorZ, "X": s.DoorX, "Y": s.DoorY} {
  22. if v > DoorPercentMax {
  23. return nil, fmt.Errorf("%w: door %s=%d", ErrDoorRange, name, v)
  24. }
  25. }
  26. return []byte{
  27. s.Fan[0], s.Fan[1], s.Fan[2], s.Fan[3],
  28. s.PTC[0], s.PTC[1], s.PTC[2], s.PTC[3],
  29. s.DoorZ, s.DoorX, s.DoorY,
  30. }, nil
  31. }
  32. func (s SetAll) Encode() ([]byte, error) {
  33. p, err := s.Payload()
  34. if err != nil {
  35. return nil, err
  36. }
  37. return Encode(TypeSetAll, p)
  38. }
  39. func DecodeSetAll(f Frame) (SetAll, error) {
  40. if f.Type != TypeSetAll {
  41. return SetAll{}, fmt.Errorf("%w: not SET_ALL", ErrType)
  42. }
  43. if len(f.Payload) != SetAllPayloadLen {
  44. return SetAll{}, ErrPayload
  45. }
  46. s := SetAll{
  47. Fan: [4]uint8{f.Payload[0], f.Payload[1], f.Payload[2], f.Payload[3]},
  48. PTC: [4]uint8{f.Payload[4], f.Payload[5], f.Payload[6], f.Payload[7]},
  49. DoorZ: f.Payload[8],
  50. DoorX: f.Payload[9],
  51. DoorY: f.Payload[10],
  52. }
  53. if _, err := s.Payload(); err != nil {
  54. return SetAll{}, err
  55. }
  56. return s, nil
  57. }
  58. func GetTemperatureRequest() []byte {
  59. b, err := Encode(TypeGetTemperature, nil)
  60. if err != nil {
  61. panic(err)
  62. }
  63. return b
  64. }
  65. func GetMotorStatusRequest() []byte {
  66. b, err := Encode(TypeGetMotorStatus, nil)
  67. if err != nil {
  68. panic(err)
  69. }
  70. return b
  71. }
  72. func EqualSetAll(a, b SetAll) bool {
  73. return a == b
  74. }
  75. func WithDoorsClearedTo(s SetAll, z, x, y uint8) SetAll {
  76. s.DoorZ, s.DoorX, s.DoorY = z, x, y
  77. return s
  78. }