Skip to content

Commit

Permalink
Added 'protocol' field to Device struct (cf. #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Apr 23, 2024
1 parent a56f887 commit 9685fd0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
12 changes: 11 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

- [ ] TCP/IP protocol (cf. https://github.com/uhppoted/uhppote-core/issues/17)
- [x] Verify TCP with real controller
- [ ] Rework address as *netip.AddrPort
- [x] uhppote-core
- [ ] Update uhppoted-lib::conf
- [ ] Update throughout
- [x] uhppote-cli
- [ ] uhppoted-lib
= [ ] Replace pointer with zero value
- [ ] Add protocol field to UHPPOTE controller struct
- [ ] Rework address as *netip.AddrPort
- [ ] uhppote-core
- [ ] Update uhppoted-lib::conf
- [ ] Update throughout
- [ ] uhppote-cli
- [ ] uhppoted-lib
- [ ] TCP 'driver'
- [ ] CHANGELOG
- [ ] README
Expand Down
3 changes: 3 additions & 0 deletions uhppote/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Device struct {
Address *netip.AddrPort // controller IPv4 address
Doors []string // controller door names (required for ACL functions)
TimeZone *time.Location // controller timezone (required when controller is located in a different time zone to the application)
Protocol string // controller network protocol ("udp", "tcp", "any")
}

// Convenience function to instantiate a configured controller from the information in a configuration file.
Expand All @@ -23,6 +24,7 @@ func NewDevice(name string, serialNumber uint32, address *netip.AddrPort, doors
Address: address,
Doors: doors,
TimeZone: time.Local,
Protocol: "udp",
}
}

Expand All @@ -34,6 +36,7 @@ func (d Device) Clone() Device {
Address: nil,
Doors: make([]string, len(d.Doors)),
TimeZone: d.TimeZone,
Protocol: d.Protocol,
}

if d.Address != nil {
Expand Down
79 changes: 79 additions & 0 deletions uhppote/device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package uhppote

import (
"net/netip"
"reflect"
"testing"
"time"
)

func TestNewDevice(t *testing.T) {
address := netip.MustParseAddrPort("192.168.1.100:60000")

expected := Device{
Name: "Alpha",
DeviceID: 405419896,
Address: &address,
Doors: []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
},
TimeZone: time.Local,
Protocol: "udp",
}

device := NewDevice("Alpha", 405419896, &address, []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
})

if device == nil {
t.Fatalf("error creating Device (%v)", device)
}

if !reflect.DeepEqual(*device, expected) {
t.Errorf("incorrectly created Device\nexpected: %+v\ngot: %+v", expected, *device)
}
}

func TestDeviceClone(t *testing.T) {
address := netip.MustParseAddrPort("192.168.1.100:60000")

expected := Device{
Name: "Alpha",
DeviceID: 405419896,
Address: &address,
Doors: []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
},
TimeZone: time.FixedZone("UTC-8", -8*60*60),
Protocol: "udp",
}

device := Device{
Name: "Alpha",
DeviceID: 405419896,
Address: &address,
Doors: []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
},
TimeZone: time.FixedZone("UTC-8", -8*60*60),
Protocol: "udp",
}

cloned := device.Clone()

if !reflect.DeepEqual(cloned, expected) {
t.Errorf("incorrectly cloned Device\nexpected: %+v\ngot: %+v", expected, cloned)
}
}

0 comments on commit 9685fd0

Please sign in to comment.