From bf109fd05b5806d8d4a615882f769b82bc850e0a Mon Sep 17 00:00:00 2001 From: twystd Date: Thu, 30 May 2024 10:26:35 -0700 Subject: [PATCH] Updated config to include timezone in controller initialisation --- CHANGELOG.md | 1 + Makefile | 3 +- commands/comand_test.go | 210 ++++++++++++++++++++++++++++++++++++++++ commands/command.go | 10 +- go.mod | 4 +- go.sum | 8 +- 6 files changed, 227 insertions(+), 9 deletions(-) create mode 100644 commands/comand_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 674f473..a442081 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### 1. Added github nightly build with executable artifacts. +2. Changed default controller timezone from UTC to Local. ## [0.8.8](https://github.com/uhppoted/uhppote-cli/releases/tag/v0.8.8) - 2024-03-27 diff --git a/Makefile b/Makefile index ef332c2..5a43881 100644 --- a/Makefile +++ b/Makefile @@ -85,8 +85,7 @@ publish: release gh release create "$(VERSION)" "./dist/uhppote-cli_$(VERSION).tar.gz" "./dist/uhppote-cli_$(VERSION).zip" --draft --prerelease --title "$(VERSION)-beta" --notes-file release-notes.md debug: build - ./bin/uhppote-cli --debug --bind 192.168.1.100:12345 get-device 405419896 - ./bin/uhppote-cli --debug --bind 192.168.1.100:12345 get-device 303986753 + ./bin/uhppote-cli --debug set-address 423187757 192.168.1.125 255.255.255.255 192.168.1.1 irl: build diff --git a/commands/comand_test.go b/commands/comand_test.go new file mode 100644 index 0000000..9759b15 --- /dev/null +++ b/commands/comand_test.go @@ -0,0 +1,210 @@ +package commands + +import ( + "fmt" + "net" + "net/netip" + "os" + "reflect" + "testing" + "time" + + "github.com/uhppoted/uhppote-core/types" + "github.com/uhppoted/uhppote-core/uhppote" + "github.com/uhppoted/uhppoted-lib/config" +) + +func TestCommandNewContext(t *testing.T) { + LA, _ := time.LoadLocation("America/Los_Angeles") + + u := stub{} + c := config.Config{ + Devices: config.DeviceMap{ + 405419896: &config.Device{ + Name: "Alpha", + Address: types.MustParseControllerAddr("192.168.1.100:60000"), + Doors: []string{"Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"}, + TimeZone: "America/Los_Angeles", + Protocol: "tcp", + }, + 303986753: &config.Device{ + Name: "Beta", + Address: types.MustParseControllerAddr("192.168.1.100:60000"), + Doors: []string{"Great Hall", "Kitchen", "Dungeon", "Hogsmeade"}, + }, + }, + } + + expected := Context{ + uhppote: &u, + config: &c, + devices: []uhppote.Device{ + uhppote.Device{ + Name: "Beta", + DeviceID: 303986753, + Doors: []string{"Great Hall", "Kitchen", "Dungeon", "Hogsmeade"}, + Address: types.MustParseControllerAddr("192.168.1.100:60000"), + TimeZone: time.Local, + Protocol: "udp", + }, + uhppote.Device{ + Name: "Alpha", + DeviceID: 405419896, + Doors: []string{"Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"}, + Address: types.MustParseControllerAddr("192.168.1.100:60000"), + TimeZone: LA, + Protocol: "tcp", + }, + }, + debug: false, + } + + ctx := NewContext(&u, &c, false) + + if !reflect.DeepEqual(ctx, expected) { + t.Errorf("incorrect context\n expected:%v\n got: %v", expected, ctx) + } +} + +type stub struct { +} + +func (s *stub) GetDevices() ([]types.Device, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) GetDevice(controller uint32) (*types.Device, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) SetAddress(controller uint32, address, mask, gateway net.IP) (*types.Result, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) GetTime(controller uint32) (*types.Time, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) SetTime(controller uint32, datetime time.Time) (*types.Time, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) GetDoorControlState(controller uint32, door byte) (*types.DoorControlState, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) SetDoorControlState(controller uint32, door uint8, state types.ControlState, delay uint8) (*types.DoorControlState, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) SetDoorPasscodes(controller uint32, door uint8, passcodes ...uint32) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) SetInterlock(controller uint32, interlock types.Interlock) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) ActivateKeypads(controller uint32, keypads map[uint8]bool) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) GetListener(controller uint32) (netip.AddrPort, error) { + return netip.AddrPort{}, fmt.Errorf("Not implemented") +} + +func (s *stub) SetListener(controller uint32, address netip.AddrPort) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) GetStatus(controller uint32) (*types.Status, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) GetCards(controller uint32) (uint32, error) { + return 0, fmt.Errorf("Not implemented") +} + +func (s *stub) GetCardByIndex(controller, index uint32) (*types.Card, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) GetCardByID(controller, cardID uint32) (*types.Card, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) PutCard(controller uint32, card types.Card, formats ...types.CardFormat) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) DeleteCard(controller uint32, cardNumber uint32) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) DeleteCards(controller uint32) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) GetTimeProfile(controller uint32, profileID uint8) (*types.TimeProfile, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) SetTimeProfile(controller uint32, profile types.TimeProfile) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) ClearTimeProfiles(controller uint32) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) ClearTaskList(controller uint32) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) AddTask(controller uint32, task types.Task) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) RefreshTaskList(controller uint32) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) RecordSpecialEvents(controller uint32, enable bool) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) GetEvent(controller, index uint32) (*types.Event, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) GetEventIndex(controller uint32) (*types.EventIndex, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) SetEventIndex(controller, index uint32) (*types.EventIndexResult, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) Listen(listener uhppote.Listener, q chan os.Signal) error { + return fmt.Errorf("Not implemented") +} + +func (s *stub) OpenDoor(controller uint32, door uint8) (*types.Result, error) { + return nil, fmt.Errorf("Not implemented") +} + +func (s *stub) SetPCControl(controller uint32, enable bool) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) RestoreDefaultParameters(controller uint32) (bool, error) { + return false, fmt.Errorf("Not implemented") +} + +func (s *stub) DeviceList() map[uint32]uhppote.Device { + return nil +} + +func (s *stub) ListenAddrList() []netip.AddrPort { + return nil +} diff --git a/commands/command.go b/commands/command.go index 2da9671..aae9330 100644 --- a/commands/command.go +++ b/commands/command.go @@ -7,6 +7,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/uhppoted/uhppote-core/types" "github.com/uhppoted/uhppote-core/uhppote" @@ -36,12 +37,19 @@ func NewContext(u uhppote.IUHPPOTE, c *config.Config, debug bool) Context { d := c.Devices[id] address := types.ControllerAddr{} protocol := d.Protocol + timezone := time.Local if d.Address.IsValid() { address = types.ControllerAddrFrom(d.Address.Addr(), d.Address.Port()) } - if device := uhppote.NewDevice(d.Name, id, address, protocol, d.Doors); device.IsValid() { + if d.TimeZone != "" { + if tz, err := time.LoadLocation(d.TimeZone); err == nil && tz != nil { + timezone = tz + } + } + + if device := uhppote.NewDevice(d.Name, id, address, protocol, d.Doors, timezone); device.IsValid() { devices = append(devices, device) } } diff --git a/go.mod b/go.mod index d2446d9..ea65ba8 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/uhppoted/uhppote-cli go 1.22 require ( - github.com/uhppoted/uhppote-core v0.8.9-0.20240528150603-37c5df8bb47f - github.com/uhppoted/uhppoted-lib v0.8.9-0.20240528151010-a3cffa5b9381 + github.com/uhppoted/uhppote-core v0.8.9-0.20240530164535-c743e72dd723 + github.com/uhppoted/uhppoted-lib v0.8.9-0.20240530171621-d3485dd0b34d ) require golang.org/x/sys v0.20.0 // indirect diff --git a/go.sum b/go.sum index 22812e6..d1a0aa3 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ -github.com/uhppoted/uhppote-core v0.8.9-0.20240528150603-37c5df8bb47f h1:cee7NdQFQ8P+C6u1J++FFAaK+yr3aV0hHXlvIK4sXiQ= -github.com/uhppoted/uhppote-core v0.8.9-0.20240528150603-37c5df8bb47f/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM= -github.com/uhppoted/uhppoted-lib v0.8.9-0.20240528151010-a3cffa5b9381 h1:QuAU374rlmn9vBraZ6XbywgRLpIQw3m0yYMwj8bR8Cc= -github.com/uhppoted/uhppoted-lib v0.8.9-0.20240528151010-a3cffa5b9381/go.mod h1:Yf3cROHpYsNUX7IJWsZncCQVwyG4eppUWQEs1ST6tv0= +github.com/uhppoted/uhppote-core v0.8.9-0.20240530164535-c743e72dd723 h1:itDcd2d0Iz23IWG+Vl6kj8MqPM/mv4C097O5hUCkLYg= +github.com/uhppoted/uhppote-core v0.8.9-0.20240530164535-c743e72dd723/go.mod h1:Q+DHtT8s74efLs2b0eF20DRBUL9yBkySwaTQ+0lsEVM= +github.com/uhppoted/uhppoted-lib v0.8.9-0.20240530171621-d3485dd0b34d h1:tF9pdfTgbUfdP2tQ9/H/olYEHkXVh+aDiuq0DatcdFQ= +github.com/uhppoted/uhppoted-lib v0.8.9-0.20240530171621-d3485dd0b34d/go.mod h1:luEG0DJWxSzCTDqYkqvE+T0HP59LKDUJ4iZHGn84C6w= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=