Skip to content

Commit

Permalink
Replaced pointers in Event and StatusEvent types with zero values (cf.
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Oct 23, 2023
1 parent 1162b79 commit 7921b18
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 27 deletions.
2 changes: 1 addition & 1 deletion messages/get_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GetStatusResponse struct {
Door byte `uhppote:"offset:14"`
Direction uint8 `uhppote:"offset:15"`
CardNumber uint32 `uhppote:"offset:16"`
Timestamp *types.DateTime `uhppote:"offset:20"`
Timestamp types.DateTime `uhppote:"offset:20"` // using zero value for 'no event'
Reason uint8 `uhppote:"offset:27"`
Door1State bool `uhppote:"offset:28"`
Door2State bool `uhppote:"offset:29"`
Expand Down
8 changes: 4 additions & 4 deletions messages/get_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestUnmarshalGetStatusResponse(t *testing.T) {
}

swiped, _ := time.ParseInLocation("2006-01-02 15:04:05", "2019-04-19 17:00:09", time.Local)
if reply.Timestamp == nil || *reply.Timestamp != types.DateTime(swiped) {
if reply.Timestamp.IsZero() || reply.Timestamp != types.DateTime(swiped) {
t.Errorf("Incorrect 'event timestamp' - expected:%v, got:%v", swiped.Format("2006-01-02 15:04:05"), reply.Timestamp)
}

Expand Down Expand Up @@ -263,7 +263,7 @@ func TestFactoryUnmarshalGetStatusResponse(t *testing.T) {
}

swiped, _ := time.ParseInLocation("2006-01-02 15:04:05", "2019-04-19 17:00:09", time.Local)
if reply.Timestamp == nil || *reply.Timestamp != types.DateTime(swiped) {
if reply.Timestamp.IsZero() || reply.Timestamp != types.DateTime(swiped) {
t.Errorf("Incorrect 'event timestamp' - expected:%v, got:%v", swiped.Format("2006-01-02 15:04:05"), reply.Timestamp)
}

Expand Down Expand Up @@ -404,8 +404,8 @@ func TestUnmarshalGetStatusResponseWithNoEvent(t *testing.T) {
t.Errorf("Incorrect 'card number' - expected:%v, got:%v", 0, reply.CardNumber)
}

if reply.Timestamp != nil {
t.Errorf("Incorrect 'event timestamp' - expected:%v, got:%v", nil, reply.Timestamp)
if !reply.Timestamp.IsZero() {
t.Errorf("Incorrect 'event timestamp' - expected:%v, got:%v", types.DateTime{}, reply.Timestamp)
}

if reply.Reason != 0 {
Expand Down
20 changes: 14 additions & 6 deletions types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ type Event struct {
Reason uint8
}

func (s *EventIndex) String() string {
return fmt.Sprintf("%s %d", s.SerialNumber, s.Index)
func (e *EventIndex) String() string {
return fmt.Sprintf("%v %v", e.SerialNumber, e.Index)
}

func (s *EventIndexResult) String() string {
return fmt.Sprintf("%s %-8d %v", s.SerialNumber, s.Index, s.Changed)
func (e *EventIndexResult) String() string {
return fmt.Sprintf("%v %-8v %v", e.SerialNumber, e.Index, e.Changed)
}

func (s *Event) String() string {
return fmt.Sprintf("%s %-6d %s %-12d %1d %-5v %d", s.SerialNumber, s.Index, s.Timestamp.String(), s.CardNumber, s.Door, s.Granted, s.Reason)
func (e Event) IsZero() bool {
return e.Index == 0
}

func (e Event) String() string {
if e.IsZero() {
return ""
} else {
return fmt.Sprintf("%v %-6v %v %-12v %1v %-5v %v", e.SerialNumber, e.Index, e.Timestamp, e.CardNumber, e.Door, e.Granted, e.Reason)
}
}
115 changes: 115 additions & 0 deletions types/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package types

import (
"fmt"
"testing"
"time"
)

func TestEventIsZeroWithZeroEvent(t *testing.T) {
datetime := func(y int, m time.Month, d int, hh int, mm int, ss int) DateTime {
return DateTime(time.Date(y, m, d, hh, mm, ss, 0, time.Local))
}

event := Event{
SerialNumber: 405419896,
Index: 0,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 10058399,
Timestamp: datetime(2023, time.January, 25, 11, 20, 28),
Reason: 5,
}

if isZero := event.IsZero(); !isZero {
t.Errorf("'zero value' event returned %v for IsZero()", isZero)
}
}

func TestEventIsZeroWithNonZeroEvent(t *testing.T) {
datetime := func(y int, m time.Month, d int, hh int, mm int, ss int) DateTime {
return DateTime(time.Date(y, m, d, hh, mm, ss, 0, time.Local))
}

event := Event{
SerialNumber: 405419896,
Index: 12345,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 10058399,
Timestamp: datetime(2023, time.January, 25, 11, 20, 28),
Reason: 5,
}

if isZero := event.IsZero(); isZero {
t.Errorf("'zero value' event returned %v for IsZero()", isZero)
}
}

func TestEventToString(t *testing.T) {
datetime := func(y int, m time.Month, d int, hh int, mm int, ss int) DateTime {
return DateTime(time.Date(y, m, d, hh, mm, ss, 0, time.Local))
}

tests := []struct {
event Event
expected string
}{
{
event: Event{
SerialNumber: 405419896,
Index: 207405,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 10058399,
Timestamp: datetime(2023, time.January, 25, 11, 20, 28),
Reason: 5,
},
expected: "405419896 207405 2023-01-25 11:20:28 10058399 3 false 5",
},

{
event: Event{
SerialNumber: 405419896,
Index: 257,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 8165538,
Timestamp: datetime(2023, time.January, 25, 12, 30, 24),
Reason: 5,
},
expected: "405419896 257 2023-01-25 12:30:24 8165538 3 false 5",
},

{
event: Event{
SerialNumber: 405419896,
Index: 0,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 8165538,
Timestamp: datetime(2023, time.January, 25, 12, 30, 24),
Reason: 5,
},
expected: "",
},
}

for _, test := range tests {
s := fmt.Sprintf("%v", test.event)

if s != test.expected {
t.Errorf("Incorrectly formatted event\n expected:%v\n got: %v", test.expected, s)
}
}
}
14 changes: 9 additions & 5 deletions types/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Status struct {
SpecialInfo uint8
RelayState uint8
InputState uint8
Event *StatusEvent
Event StatusEvent // using zero value for 'no event'
}

type StatusEvent struct {
Expand All @@ -25,10 +25,14 @@ type StatusEvent struct {
Door byte
Direction uint8
CardNumber uint32
Timestamp *DateTime
Timestamp DateTime
Reason uint8
}

func (e StatusEvent) IsZero() bool {
return e.Index == 0
}

func (s Status) String() string {
sysdatetime := func() string {
if s.SystemDateTime.IsZero() {
Expand All @@ -38,8 +42,8 @@ func (s Status) String() string {
}
}

timestamp := func(t *DateTime) string {
if t == nil || t.IsZero() {
timestamp := func(t DateTime) string {
if t.IsZero() {
return "---"
} else {
return fmt.Sprintf("%v", t)
Expand All @@ -58,7 +62,7 @@ func (s Status) String() string {
b.WriteString(fmt.Sprintf(" %02X", s.RelayState))
b.WriteString(fmt.Sprintf(" %02X", s.InputState))

if s.Event != nil {
if s.Event.Index > 0 {
b.WriteString(fmt.Sprintf(" | %-7d", s.Event.Index))
b.WriteString(fmt.Sprintf(" %-3d", s.Event.Type))
b.WriteString(fmt.Sprintf(" %-5v", s.Event.Granted))
Expand Down
78 changes: 69 additions & 9 deletions types/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,51 @@ import (
"time"
)

func TestStatusToString(t *testing.T) {
func TestStatusEventIsZeroWithZeroEvent(t *testing.T) {
datetime := func(y int, m time.Month, d int, hh int, mm int, ss int) DateTime {
return DateTime(time.Date(y, m, d, hh, mm, ss, 0, time.Local))
}

event := StatusEvent{
Index: 0,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 10058399,
Timestamp: datetime(2023, time.January, 25, 11, 20, 28),
Reason: 5,
}

if isZero := event.IsZero(); !isZero {
t.Errorf("'zero value' status event returned %v for IsZero()", isZero)
}
}

func TestStatusEventIsZeroWithNonZeroEvent(t *testing.T) {
datetime := func(y int, m time.Month, d int, hh int, mm int, ss int) DateTime {
return DateTime(time.Date(y, m, d, hh, mm, ss, 0, time.Local))
}

datetimePtr := func(y int, m time.Month, d int, hh int, mm int, ss int) *DateTime {
dt := DateTime(time.Date(y, m, d, hh, mm, ss, 0, time.Local))
event := StatusEvent{
Index: 12345,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 10058399,
Timestamp: datetime(2023, time.January, 25, 11, 20, 28),
Reason: 5,
}

return &dt
if isZero := event.IsZero(); isZero {
t.Errorf("Valid status event returned %v for IsZero()", isZero)
}
}

func TestStatusToString(t *testing.T) {
datetime := func(y int, m time.Month, d int, hh int, mm int, ss int) DateTime {
return DateTime(time.Date(y, m, d, hh, mm, ss, 0, time.Local))
}

tests := []struct {
Expand All @@ -32,14 +68,14 @@ func TestStatusToString(t *testing.T) {
SpecialInfo: 0,
RelayState: 0,
InputState: 0,
Event: &StatusEvent{
Event: StatusEvent{
Index: 207405,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 10058399,
Timestamp: datetimePtr(2023, time.January, 25, 11, 20, 28),
Timestamp: datetime(2023, time.January, 25, 11, 20, 28),
Reason: 5,
},
},
Expand All @@ -57,19 +93,44 @@ func TestStatusToString(t *testing.T) {
SpecialInfo: 0,
RelayState: 0,
InputState: 0,
Event: &StatusEvent{
Event: StatusEvent{
Index: 257,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 8165538,
Timestamp: datetimePtr(2023, time.January, 25, 12, 30, 24),
Timestamp: datetime(2023, time.January, 25, 12, 30, 24),
Reason: 5,
},
},
expected: "405419896 false false false false false false false false 0 2023-01-25 12:30:24 0 0 00 00 | 257 1 false 3 1 8165538 2023-01-25 12:30:24 5",
},

{
status: Status{
SerialNumber: 405419896,
DoorState: map[uint8]bool{1: false, 2: false, 3: false, 4: false},
DoorButton: map[uint8]bool{1: false, 2: false, 3: false, 4: false},
SystemError: 0,
SystemDateTime: datetime(2023, time.January, 25, 12, 30, 24),
SequenceId: 0,
SpecialInfo: 0,
RelayState: 0,
InputState: 0,
Event: StatusEvent{
Index: 0,
Type: 1,
Granted: false,
Door: 3,
Direction: 1,
CardNumber: 8165538,
Timestamp: datetime(2023, time.January, 25, 12, 30, 24),
Reason: 5,
},
},
expected: "405419896 false false false false false false false false 0 2023-01-25 12:30:24 0 0 00 00",
},
}

for _, test := range tests {
Expand All @@ -79,5 +140,4 @@ func TestStatusToString(t *testing.T) {
t.Errorf("Incorrectly formatted status\n expected:%v\n got: %v", test.expected, s)
}
}

}
2 changes: 1 addition & 1 deletion uhppote/get_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (u *uhppote) GetStatus(serialNumber uint32) (*types.Status, error) {
}

if reply.EventIndex != 0 {
status.Event = &types.StatusEvent{
status.Event = types.StatusEvent{
Index: reply.EventIndex,
Type: reply.EventType,
Granted: reply.Granted,
Expand Down
2 changes: 1 addition & 1 deletion uhppote/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (u *uhppote) Listen(listener Listener, q chan os.Signal) error {
}

if e.EventIndex != 0 {
status.Event = &types.StatusEvent{
status.Event = types.StatusEvent{
Index: e.EventIndex,
Type: e.EventType,
Granted: e.Granted,
Expand Down

0 comments on commit 7921b18

Please sign in to comment.