Skip to content

Commit

Permalink
Replaced from/to Date pointer fields in GetTimeProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Jun 4, 2024
1 parent 61d4334 commit 4993b61
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
- [x] Add timezone to NewDevice
- [x] `put-card`: error on bad card numbers (0,0xffffffff,0x00ffffff)
- [ ] Rework any remaining Date pointers to rather use IsZero/IsValid
- [ ] GetTimeProfile
- [x] GetTimeProfile
- [ ] types.TimeProfile
- [ ] // FIXME: replace From in types.TimeProfile
- [ ] lib::encoding/tsv
- [ ] simulator get-device ReleaseDate
- [ ] uhppoted-app-wild-apricot types.Date
Expand Down
4 changes: 2 additions & 2 deletions messages/get_time_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type GetTimeProfileResponse struct {
MsgType types.MsgType `uhppote:"value:0x98"`
SerialNumber types.SerialNumber `uhppote:"offset:4"`
ProfileID uint8 `uhppote:"offset:8"`
From *types.Date `uhppote:"offset:9"`
To *types.Date `uhppote:"offset:13"`
From types.Date `uhppote:"offset:9"`
To types.Date `uhppote:"offset:13"`
Monday bool `uhppote:"offset:17"`
Tuesday bool `uhppote:"offset:18"`
Wednesday bool `uhppote:"offset:19"`
Expand Down
57 changes: 46 additions & 11 deletions messages/get_time_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,53 @@ func TestUnmarshalGetTimeProfileResponse(t *testing.T) {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}

from := types.MustParseDate("2021-04-01")
to := types.MustParseDate("2021-12-29")
expected := GetTimeProfileResponse{
MsgType: 0x98,
SerialNumber: 423187757,
ProfileID: 4,
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Monday: true,
Tuesday: true,
Wednesday: false,
Thursday: true,
Friday: false,
Saturday: true,
Sunday: true,
Segment1Start: hhmm("08:30"),
Segment1End: hhmm("09:45"),
Segment2Start: hhmm("11:35"),
Segment2End: hhmm("13:15"),
Segment3Start: hhmm("14:01"),
Segment3End: hhmm("17:59"),
LinkedProfileID: 19,
}

reply := GetTimeProfileResponse{}

if err := codec.Unmarshal(message, &reply); err != nil {
t.Fatalf("Unexpected error: %v\n", err)
}

if !reflect.DeepEqual(reply, expected) {
t.Errorf("Incorrect reply\n expected:%+v\n got: %+v", expected, reply)
}
}

func TestUnmarshalGetTimeProfileResponseWithZeroFromAndToDate(t *testing.T) {
message := []byte{
0x17, 0x98, 0x00, 0x00, 0x2d, 0x55, 0x39, 0x19, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x08, 0x30, 0x09, 0x45, 0x11, 0x35, 0x13, 0x15,
0x14, 0x01, 0x17, 0x59, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}

expected := GetTimeProfileResponse{
MsgType: 0x98,
SerialNumber: 423187757,
ProfileID: 4,
From: &from,
To: &to,
From: types.Date{},
To: types.Date{},
Monday: true,
Tuesday: true,
Wednesday: false,
Expand Down Expand Up @@ -111,15 +149,12 @@ func TestFactoryUnmarshalGetTimeProfileResponse(t *testing.T) {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}

from := types.MustParseDate("2021-04-01")
to := types.MustParseDate("2021-12-29")

expected := GetTimeProfileResponse{
MsgType: 0x98,
SerialNumber: 423187757,
ProfileID: 4,
From: &from,
To: &to,
From: types.MustParseDate("2021-04-01"),
To: types.MustParseDate("2021-12-29"),
Monday: true,
Tuesday: true,
Wednesday: false,
Expand Down Expand Up @@ -168,8 +203,8 @@ func TestUnmarshalGetTimeProfileDeactivatedResponse(t *testing.T) {
MsgType: 0x98,
SerialNumber: 423187757,
ProfileID: 0,
From: nil,
To: nil,
From: types.Date{},
To: types.Date{},
Monday: false,
Tuesday: false,
Wednesday: false,
Expand Down
18 changes: 10 additions & 8 deletions uhppote/get_time_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ func (u *uhppote) GetTimeProfile(deviceID uint32, profileID uint8) (*types.TimeP
return nil, nil
}

if reply.From == nil {
return nil, fmt.Errorf("invalid 'from' date in response")
}
// NTS: nil pointer replaced with zero value and zero value is (possibly) valid
// if reply.From == nil {
// return nil, fmt.Errorf("invalid 'from' date in response")
// }

if reply.To == nil {
return nil, fmt.Errorf("invalid 'to' date in response")
}
// NTS: nil pointer replaced with zero value and zero value is (possibly) valid
// if reply.To == nil {
// return nil, fmt.Errorf("invalid 'to' date in response")
// }

segments := []types.Segment{types.Segment{}, types.Segment{}, types.Segment{}}

Expand Down Expand Up @@ -67,8 +69,8 @@ func (u *uhppote) GetTimeProfile(deviceID uint32, profileID uint8) (*types.TimeP
profile := types.TimeProfile{
ID: reply.ProfileID,
LinkedProfileID: reply.LinkedProfileID,
From: reply.From,
To: reply.To,
From: &reply.From,
To: &reply.To,

Weekdays: types.Weekdays{
time.Monday: reply.Monday,
Expand Down

0 comments on commit 4993b61

Please sign in to comment.