|
|
@@ -31,8 +31,8 @@ func calculateLRC(data []byte) byte {
|
|
|
|
func (lock *SaltoLockServer) BuildCommand(req DoorCardRequest, checkIn, checkOut time.Time) error {
|
|
|
|
func (lock *SaltoLockServer) BuildCommand(req DoorCardRequest, checkIn, checkOut time.Time) error {
|
|
|
|
// helper: hh[mm]DDMMYY
|
|
|
|
// helper: hh[mm]DDMMYY
|
|
|
|
fmtStamp := func(t time.Time) string {
|
|
|
|
fmtStamp := func(t time.Time) string {
|
|
|
|
return fmt.Sprintf("%02d%02d%02d%02d%02d",
|
|
|
|
return fmt.Sprintf("%02d%02d%02d%02d",
|
|
|
|
t.Hour(), t.Minute(), t.Day(), int(t.Month()), t.Year()%100)
|
|
|
|
t.Hour(), t.Day(), int(t.Month()), t.Year()%100)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start := fmtStamp(checkIn)
|
|
|
|
start := fmtStamp(checkIn)
|
|
|
|
expiry := fmtStamp(checkOut)
|
|
|
|
expiry := fmtStamp(checkOut)
|
|
|
@@ -140,7 +140,7 @@ func (lock *SaltoLockServer) waitForAck(conn net.Conn, reader *bufio.Reader, tim
|
|
|
|
|
|
|
|
|
|
|
|
// LockSequence performs the full ENQ/ACK handshake and command exchange
|
|
|
|
// LockSequence performs the full ENQ/ACK handshake and command exchange
|
|
|
|
func (lock *SaltoLockServer) LockSequence() error {
|
|
|
|
func (lock *SaltoLockServer) LockSequence() error {
|
|
|
|
const timeout = 10 * time.Second
|
|
|
|
const timeout = 20 * time.Second
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
resp []byte
|
|
|
|
resp []byte
|
|
|
|
drained = 0 // count of stale frames consumed across waits
|
|
|
|
drained = 0 // count of stale frames consumed across waits
|
|
|
@@ -151,8 +151,8 @@ func (lock *SaltoLockServer) LockSequence() error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
|
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
reader := bufio.NewReader(conn)
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Send ENQ
|
|
|
|
// 1. Send ENQ
|
|
|
|
log.Infof("Sending ENQ")
|
|
|
|
log.Infof("Sending ENQ")
|
|
|
@@ -178,7 +178,7 @@ func (lock *SaltoLockServer) LockSequence() error {
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Now read the *next* STX frame which should be the response to our command.
|
|
|
|
// 5. Now read the *next* STX frame which should be the response to our command.
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
conn.SetReadDeadline(time.Now().Add(20 * time.Second))
|
|
|
|
conn.SetReadDeadline(time.Now().Add(40 * time.Second))
|
|
|
|
b, e := reader.ReadByte()
|
|
|
|
b, e := reader.ReadByte()
|
|
|
|
if e != nil {
|
|
|
|
if e != nil {
|
|
|
|
return fmt.Errorf("error reading response start: %w", e)
|
|
|
|
return fmt.Errorf("error reading response start: %w", e)
|
|
|
@@ -210,11 +210,11 @@ func (lock *SaltoLockServer) LockSequence() error {
|
|
|
|
case b1 == 'C' && b2 == 'C':
|
|
|
|
case b1 == 'C' && b2 == 'C':
|
|
|
|
log.Infof("LockSequence: command response is CC (follow-up)")
|
|
|
|
log.Infof("LockSequence: command response is CC (follow-up)")
|
|
|
|
case b1 == 'T' && b2 == 'D':
|
|
|
|
case b1 == 'T' && b2 == 'D':
|
|
|
|
log.Warnf("LockSequence: command response is TD (room does not exist)")
|
|
|
|
log.Warnf("LockSequence: command response is TD (room does not exist): %q", string(resp))
|
|
|
|
return fmt.Errorf("lock response indicates room does not exist")
|
|
|
|
return fmt.Errorf("lock response indicates room does not exist: %q", string(resp))
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
log.Warnf("LockSequence: unexpected command response %q", string(resp))
|
|
|
|
log.Warnf("LockSequence: unexpected command response %q", string(resp))
|
|
|
|
return fmt.Errorf("error encoding keycard, unexpected response")
|
|
|
|
return fmt.Errorf("error encoding keycard, unexpected response: %q", string(resp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
log.Warnf("LockSequence: response too short: %q", string(resp))
|
|
|
|
log.Warnf("LockSequence: response too short: %q", string(resp))
|
|
|
|