Skip to content

Commit

Permalink
Added timezone to NewDevice factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed May 30, 2024
1 parent 86f73e0 commit c743e72
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
19 changes: 0 additions & 19 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
# TODO

- [x] TCP/IP protocol (cf. https://github.com/uhppoted/uhppote-core/issues/17)
- [x] Add protocol field to UHPPOTE controller struct
- [x] Verify TCP with real controller
- [x] TCP 'driver'
- [x] See if controller sends events to TCP listener
- [x] Use UDP broadcast if addrport is nil **OR** zero value **OR** INADDR_ANY
- [x] Rework `sendto` as generic function
- [x] sendto:broadcast - don't wait for timeout if reply is valid
- [x] Rework UDP to use connected sockets
- [x] Unify send/sendto implementations
- [x] Rework bind address as netip.AddrPort
- [x] Rework broadcast address as netip.AddrPort
- [x] Rework listen address as netip.AddrPort
- [x] Set socket.SO_REUSEADDR
- [x] Rework Address as netip.AddrPort
- [x] ~~CLI get-devices EOF~~
- [x] Update _uhppoted.conf_ documentation
- [x] CHANGELOG
- [x] README

- [ ] Add timezone to NewDevice
- [ ] Replace (* UHPPOTE) in API functions with non-pointer version
- [ ] Rework any remaining Date/DateTime pointers to rather use IsZero/IsValid
Expand Down
15 changes: 9 additions & 6 deletions uhppote/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"github.com/uhppoted/uhppote-core/types"
)

// Configuration information for an access controller declared in the common
// uhppoted.conf configuration.
// Configuration information for an access controller declared in the common uhppoted.conf configuration.
type Device struct {
Name string // controller name
DeviceID uint32 // controller serial number
Expand All @@ -17,20 +16,24 @@ type Device struct {
Protocol string // controller network protocol ("udp", "tcp", "any")
}

// Factory function to initialise a configured controller from the information in
// a 'conf' file.
func NewDevice(name string, serialNumber uint32, address types.ControllerAddr, protocol string, doors []string) Device {
// Factory function to initialise a configured controller from the information in a 'conf' file.
func NewDevice(name string, serialNumber uint32, address types.ControllerAddr, protocol string, doors []string, timezone *time.Location) Device {
p := "udp"
if protocol == "tcp" {
p = "tcp"
}

tz := time.Local
if timezone != nil {
tz = timezone
}

return Device{
Name: name,
DeviceID: serialNumber,
Address: address,
Doors: doors,
TimeZone: time.Local,
TimeZone: tz,
Protocol: p,
}
}
Expand Down
49 changes: 43 additions & 6 deletions uhppote/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ import (
)

func TestNewDevice(t *testing.T) {
expected := Device{
Name: "Alpha",
DeviceID: 405419896,
Address: types.MustParseControllerAddr("192.168.1.100"),
Doors: []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
},
TimeZone: time.FixedZone("UTC+2", 2*60*60),
Protocol: "udp",
}

device := NewDevice(
"Alpha",
405419896,
types.MustParseControllerAddr("192.168.1.100"),
"udp", []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
},
time.FixedZone("UTC+2", 2*60*60))

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

func TestNewDeviceWithNilTimeZone(t *testing.T) {
expected := Device{
Name: "Alpha",
DeviceID: 405419896,
Expand All @@ -23,12 +55,17 @@ func TestNewDevice(t *testing.T) {
Protocol: "udp",
}

device := NewDevice("Alpha", 405419896, types.MustParseControllerAddr("192.168.1.100"), "udp", []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
})
device := NewDevice(
"Alpha",
405419896,
types.MustParseControllerAddr("192.168.1.100"),
"udp", []string{
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
},
nil)

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

0 comments on commit c743e72

Please sign in to comment.