added check if the room exists

This commit is contained in:
yurii 2025-07-25 17:36:08 +01:00
parent fc91f2c0f2
commit 25eab0dc75
3 changed files with 37 additions and 13 deletions

View File

@ -77,6 +77,8 @@ func (lock *SaltoLockServer) BuildCommand(req DoorCardRequest, checkIn, checkOut
// LockSequence performs the full ENQ/ACK handshake and command exchange // LockSequence performs the full ENQ/ACK handshake and command exchange
func (lock *SaltoLockServer) LockSequence(conn net.Conn) error { func (lock *SaltoLockServer) LockSequence(conn net.Conn) error {
log.Infof("Sending command: %q", string(lock.command))
const timeout = 10 * time.Second const timeout = 10 * time.Second
reader := bufio.NewReader(conn) reader := bufio.NewReader(conn)
@ -96,7 +98,7 @@ func (lock *SaltoLockServer) LockSequence(conn net.Conn) error {
return fmt.Errorf("expected ACK after ENQ, got 0x%X", b) return fmt.Errorf("expected ACK after ENQ, got 0x%X", b)
} }
// 3. Send the command frame // 3. Send command frame
if _, err := conn.Write(lock.command); err != nil { if _, err := conn.Write(lock.command); err != nil {
return fmt.Errorf("failed to send command frame: %w", err) return fmt.Errorf("failed to send command frame: %w", err)
} }
@ -113,18 +115,37 @@ func (lock *SaltoLockServer) LockSequence(conn net.Conn) error {
return fmt.Errorf("expected ACK to command, got 0x%X", b) return fmt.Errorf("expected ACK to command, got 0x%X", b)
} }
// 5. Read response: expect STX // 5. Expect STX
conn.SetReadDeadline(time.Now().Add(timeout)) conn.SetReadDeadline(time.Now().Add(timeout))
b, err = reader.ReadByte() stx, err := reader.ReadByte()
if err != nil { if err != nil {
return fmt.Errorf("error reading response STX: %w", err) return fmt.Errorf("error reading STX: %w", err)
} }
if b != STX { if stx != STX {
return fmt.Errorf("expected STX at response start, got 0x%X", b) return fmt.Errorf("expected STX, got 0x%X", stx)
}
conn.SetReadDeadline(time.Now().Add(timeout))
_ , err = reader.ReadByte()
if err != nil {
return fmt.Errorf("error reading separator after STX: %w", err)
} }
// 6. Read until ETX // 6. Read next two bytes and check for TD error
var resp []byte conn.SetReadDeadline(time.Now().Add(timeout))
b1, err := reader.ReadByte()
if err != nil {
return fmt.Errorf("error reading first response byte: %w", err)
}
b2, err := reader.ReadByte()
if err != nil {
return fmt.Errorf("error reading second response byte: %w", err)
}
if b1 == 'T' && b2 == 'D' {
return fmt.Errorf("lock response indicates room does not exist (TD)")
}
// 7. Continue reading until ETX
resp := []byte{b1, b2}
for { for {
conn.SetReadDeadline(time.Now().Add(timeout)) conn.SetReadDeadline(time.Now().Add(timeout))
c, err := reader.ReadByte() c, err := reader.ReadByte()
@ -137,7 +158,7 @@ func (lock *SaltoLockServer) LockSequence(conn net.Conn) error {
} }
} }
// 7. (Optional) Read LRC or final CR // 8. Optional: read LRC or trailing byte
conn.SetReadDeadline(time.Now().Add(timeout)) conn.SetReadDeadline(time.Now().Add(timeout))
if lrc, err := reader.ReadByte(); err == nil { if lrc, err := reader.ReadByte(); err == nil {
resp = append(resp, lrc) resp = append(resp, lrc)
@ -145,6 +166,6 @@ func (lock *SaltoLockServer) LockSequence(conn net.Conn) error {
log.Warnf("LockSequence: failed to read trailing LRC/CR: %v", err) log.Warnf("LockSequence: failed to read trailing LRC/CR: %v", err)
} }
log.Infof("LockSequence: received response: % X", resp) log.Infof("LockSequence: received response: %q", string(resp))
return nil return nil
} }

View File

@ -30,7 +30,7 @@ import (
) )
const ( const (
buildVersion = "1.0.6" buildVersion = "1.0.7"
serviceName = "hardlink" serviceName = "hardlink"
customLayout = "2006-01-02 15:04:05 -0700" customLayout = "2006-01-02 15:04:05 -0700"
transactionUrl = "http://127.0.0.1:18181/start-transaction/" transactionUrl = "http://127.0.0.1:18181/start-transaction/"
@ -46,7 +46,7 @@ type configRec struct {
DispenserAdrr string `yaml:"dispensAddr"` DispenserAdrr string `yaml:"dispensAddr"`
PrinterName string `yaml:"printerName"` PrinterName string `yaml:"printerName"`
LogDir string `yaml:"logdir"` LogDir string `yaml:"logdir"`
isPayment bool `yaml:"isPayment"` IsPayment bool `yaml:"isPayment"`
} }
// App holds shared resources. // App holds shared resources.
@ -113,7 +113,7 @@ func main() {
// } // }
// defer db.Close() // defer db.Close()
if config.isPayment { if config.IsPayment {
startClient := func() (*exec.Cmd, error) { startClient := func() (*exec.Cmd, error) {
cmd := exec.Command("./ChipDNAClient/ChipDnaClient.exe") cmd := exec.Command("./ChipDNAClient/ChipDnaClient.exe")
err := cmd.Start() err := cmd.Start()

View File

@ -2,6 +2,9 @@
builtVersion is a const in main.go builtVersion is a const in main.go
#### 1.0.7 - 25 July 2024
added check if the room exists
#### 1.0.6 - 25 July 2024 #### 1.0.6 - 25 July 2024
updated workflow for Salto locks updated workflow for Salto locks