Skip to content

Commit

Permalink
emulator: genericized driver 'get'
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Dec 17, 2024
1 parent ff9d7f3 commit ff3ba72
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 100 deletions.
4 changes: 2 additions & 2 deletions Rev.0/driver/rpc/ssmp/ssmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func (s SSMP) Get(oid string) (any, error) {

// ... controller error
if oid == ".1.3.6.1.4.1.65536.2.9" {
return 0x12, nil
return uint8(0x12), nil
}

// ... controller special info
if oid == ".1.3.6.1.4.1.65536.2.10" {
return 0x23, nil
return uint8(0x23), nil
}

// ... door 1 unlocked
Expand Down
12 changes: 0 additions & 12 deletions Rev.0/emulator/go/UT0311/UT0311.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package UT0311

import (
"fmt"
"reflect"
"sync"
"time"

"github.com/uhppoted/uhppote-core/messages"

"emulator/MIB"
"emulator/config"
"emulator/driver"
"emulator/log"
Expand Down Expand Up @@ -139,16 +137,6 @@ func isnil(v any) bool {
return false
}

func (ut0311 *UT0311) getID() (uint32, error) {
if v, err := ut0311.driver.Get(MIB.OID_CONTROLLER_ID); err != nil {
return 0, err
} else if id, ok := v.(uint32); !ok {
return 0, fmt.Errorf("invalid controller ID (%v)", v)
} else {
return id, nil
}
}

func debugf(format string, args ...any) {
log.Debugf("UT0311", format, args...)
}
Expand Down
16 changes: 5 additions & 11 deletions Rev.0/emulator/go/UT0311/get-controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package UT0311

import (
"fmt"
"net"
"net/netip"

Expand All @@ -10,10 +9,11 @@ import (

"emulator/MIB"
"emulator/config"
"emulator/driver"
)

func (ut0311 *UT0311) getController(rq *messages.GetDeviceRequest) (any, error) {
if id, err := ut0311.getID(); err != nil {
if id, err := driver.Get[uint32](ut0311.driver, MIB.OID_CONTROLLER_ID); err != nil {
return nil, err
} else if id == 0 || (rq.SerialNumber != 0 && uint32(rq.SerialNumber) != id) {
return nil, nil
Expand Down Expand Up @@ -46,22 +46,16 @@ func (ut0311 *UT0311) getController(rq *messages.GetDeviceRequest) (any, error)
response.MacAddress = []byte(MAC)
}

if v, err := ut0311.driver.Get(MIB.OID_CONTROLLER_VERSION); err != nil {
if version, err := driver.Get[uint16](ut0311.driver, MIB.OID_CONTROLLER_VERSION); err != nil {
return nil, err
} else if version, ok := v.(uint16); !ok {
return nil, fmt.Errorf("invalid controller version (%v)", version)
} else {
response.Version = types.Version(version)
}

if v, err := ut0311.driver.Get(MIB.OID_CONTROLLER_RELEASED); err != nil {
if released, err := driver.Get[types.Date](ut0311.driver, MIB.OID_CONTROLLER_RELEASED); err != nil {
return nil, err
} else if released, ok := v.(string); !ok {
return nil, fmt.Errorf("invalid controller release date (%v)", released)
} else if date, err := types.ParseDate(released); err != nil {
return nil, fmt.Errorf("invalid controller release date (%v)", released)
} else {
response.Date = date
response.Date = released
}

return response, nil
Expand Down
3 changes: 2 additions & 1 deletion Rev.0/emulator/go/UT0311/get-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (

"emulator/MIB"
"emulator/config"
"emulator/driver"
)

func (ut0311 *UT0311) getListener(rq *messages.GetListenerRequest) (any, error) {
if id, err := ut0311.getID(); err != nil {
if id, err := driver.Get[uint32](ut0311.driver, MIB.OID_CONTROLLER_ID); err != nil {
return nil, err
} else if id == 0 || (rq.SerialNumber != 0 && uint32(rq.SerialNumber) != id) {
return nil, nil
Expand Down
71 changes: 19 additions & 52 deletions Rev.0/emulator/go/UT0311/get-status.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package UT0311

import (
"fmt"

"github.com/uhppoted/uhppote-core/messages"
"github.com/uhppoted/uhppote-core/types"

"emulator/MIB"
"emulator/driver"
)

func (ut0311 *UT0311) getStatus(rq *messages.GetStatusRequest) (any, error) {
if id, err := ut0311.getID(); err != nil {
if id, err := driver.Get[uint32](ut0311.driver, MIB.OID_CONTROLLER_ID); err != nil {
return nil, err
} else if id == 0 || (rq.SerialNumber != 0 && uint32(rq.SerialNumber) != id) {
return nil, nil
Expand All @@ -20,129 +19,97 @@ func (ut0311 *UT0311) getStatus(rq *messages.GetStatusRequest) (any, error) {
RelayState: 0x00,
}

if v, err := ut0311.driver.Get(MIB.OID_CONTROLLER_DATETIME); err != nil {
return nil, err
} else if s, ok := v.(string); !ok {
return nil, fmt.Errorf("invalid controller date/time (%v)", v)
} else if datetime, err := types.ParseDateTime(s); err != nil {
if datetime, err := driver.Get[types.DateTime](ut0311.driver, MIB.OID_CONTROLLER_DATETIME); err != nil {
return nil, err
} else {
response.SystemDate = types.SystemDate(datetime)
response.SystemTime = types.SystemTime(datetime)
}

if v, err := ut0311.driver.Get(MIB.OID_CONTROLLER_ERROR); err != nil {
if v, err := driver.Get[uint8](ut0311.driver, MIB.OID_CONTROLLER_ERROR); err != nil {
return nil, err
} else if e, ok := v.(int); !ok {
return nil, fmt.Errorf("invalid controller error (%T)", v)
} else {
response.SystemError = uint8(e)
response.SystemError = v
}

if v, err := ut0311.driver.Get(MIB.OID_CONTROLLER_SPECIAL_INFO); err != nil {
if v, err := driver.Get[uint8](ut0311.driver, MIB.OID_CONTROLLER_SPECIAL_INFO); err != nil {
return nil, err
} else if e, ok := v.(int); !ok {
return nil, fmt.Errorf("invalid controller special info (%T)", v)
} else {
response.SpecialInfo = uint8(e)
response.SpecialInfo = v
}

// ... door 1
if v, err := ut0311.driver.Get(MIB.OID_DOORS_1_UNLOCKED); err != nil {
if unlocked, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_1_UNLOCKED); err != nil {
return nil, err
} else if unlocked, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 1 unlocked (%T)", v)
} else if unlocked {
response.RelayState |= 0x01
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_1_OPEN); err != nil {
if open, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_1_OPEN); err != nil {
return nil, err
} else if open, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 1 open (%T)", v)
} else {
response.Door1State = open
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_1_BUTTON); err != nil {
if pressed, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_1_BUTTON); err != nil {
return nil, err
} else if pressed, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 1 button (%T)", v)
} else {
response.Door1Button = pressed
}

// ... door 2
if v, err := ut0311.driver.Get(MIB.OID_DOORS_2_UNLOCKED); err != nil {
if unlocked, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_2_UNLOCKED); err != nil {
return nil, err
} else if unlocked, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 2 unlocked (%T)", v)
} else if unlocked {
response.RelayState |= 0x02
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_2_OPEN); err != nil {
if open, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_2_OPEN); err != nil {
return nil, err
} else if open, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 2 open (%T)", v)
} else {
response.Door2State = open
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_2_BUTTON); err != nil {
if pressed, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_2_BUTTON); err != nil {
return nil, err
} else if pressed, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 2 button (%T)", v)
} else {
response.Door2Button = pressed
}

// ... door 3
if v, err := ut0311.driver.Get(MIB.OID_DOORS_3_UNLOCKED); err != nil {
if unlocked, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_3_UNLOCKED); err != nil {
return nil, err
} else if unlocked, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 3 unlocked (%T)", v)
} else if unlocked {
response.RelayState |= 0x04
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_3_OPEN); err != nil {
if open, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_3_OPEN); err != nil {
return nil, err
} else if open, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 3 open (%T)", v)
} else {
response.Door3State = open
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_3_BUTTON); err != nil {
if pressed, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_3_BUTTON); err != nil {
return nil, err
} else if pressed, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 3 button (%T)", v)
} else {
response.Door3Button = pressed
}

// ... door 4
if v, err := ut0311.driver.Get(MIB.OID_DOORS_4_UNLOCKED); err != nil {
if unlocked, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_4_UNLOCKED); err != nil {
return nil, err
} else if unlocked, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 4 unlocked (%T)", v)
} else if unlocked {
response.RelayState |= 0x08
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_4_OPEN); err != nil {
if open, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_4_OPEN); err != nil {
return nil, err
} else if open, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 4 open (%T)", v)
} else {
response.Door4State = open
}

if v, err := ut0311.driver.Get(MIB.OID_DOORS_4_BUTTON); err != nil {
if pressed, err := driver.Get[bool](ut0311.driver, MIB.OID_DOORS_4_BUTTON); err != nil {
return nil, err
} else if pressed, ok := v.(bool); !ok {
return nil, fmt.Errorf("invalid controller door 4 button (%T)", v)
} else {
response.Door4Button = pressed
}
Expand Down
26 changes: 9 additions & 17 deletions Rev.0/emulator/go/UT0311/get-time.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
package UT0311

import (
"fmt"

"github.com/uhppoted/uhppote-core/messages"
"github.com/uhppoted/uhppote-core/types"

"emulator/MIB"
"emulator/driver"
)

func (ut0311 *UT0311) getTime(rq *messages.GetTimeRequest) (any, error) {
if id, err := ut0311.getID(); err != nil {
if id, err := driver.Get[uint32](ut0311.driver, MIB.OID_CONTROLLER_ID); err != nil {
return nil, err
} else if id == 0 || (rq.SerialNumber != 0 && uint32(rq.SerialNumber) != id) {
return nil, nil
} else if datetime, err := driver.Get[types.DateTime](ut0311.driver, MIB.OID_CONTROLLER_DATETIME); err != nil {
return nil, err
} else {

if v, err := ut0311.driver.Get(MIB.OID_CONTROLLER_DATETIME); err != nil {
return nil, err
} else if s, ok := v.(string); !ok {
return nil, fmt.Errorf("invalid controller date/time (%v)", v)
} else if datetime, err := types.ParseDateTime(s); err != nil {
return nil, err
} else {
response := messages.GetTimeResponse{
SerialNumber: types.SerialNumber(id),
DateTime: datetime,
}

return response, nil
response := messages.GetTimeResponse{
SerialNumber: types.SerialNumber(id),
DateTime: datetime,
}

return response, nil
}
}
3 changes: 2 additions & 1 deletion Rev.0/emulator/go/UT0311/set-listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (

"emulator/MIB"
"emulator/config"
"emulator/driver"
)

func (ut0311 *UT0311) setListener(rq *messages.SetListenerRequest) (any, error) {
if id, err := ut0311.getID(); err != nil {
if id, err := driver.Get[uint32](ut0311.driver, MIB.OID_CONTROLLER_ID); err != nil {
return nil, err
} else if id == 0 || (rq.SerialNumber != 0 && uint32(rq.SerialNumber) != id) {
return nil, nil
Expand Down
3 changes: 2 additions & 1 deletion Rev.0/emulator/go/UT0311/set-time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/uhppoted/uhppote-core/types"

"emulator/MIB"
"emulator/driver"
)

func (ut0311 *UT0311) setTime(rq *messages.SetTimeRequest) (any, error) {
if id, err := ut0311.getID(); err != nil {
if id, err := driver.Get[uint32](ut0311.driver, MIB.OID_CONTROLLER_ID); err != nil {
return nil, err
} else if id == 0 || (rq.SerialNumber != 0 && uint32(rq.SerialNumber) != id) {
return nil, nil
Expand Down
Loading

0 comments on commit ff3ba72

Please sign in to comment.