update reading from link start
This commit is contained in:
parent
b200eb3b91
commit
140c9a44cc
@ -154,7 +154,7 @@ func (lock *OmniLockServer) BuildCommand(encoderAddr, lockId string, checkIn, ch
|
||||
formattedLockId := fmt.Sprintf("%04d", idInt)
|
||||
|
||||
// Format date/time parts
|
||||
dt := checkOut.Format("15:04") // DT = HH:mm
|
||||
dt := checkOut.Format("15:04") // DT = HH:mm
|
||||
ga := checkIn.Format("060102") // GA = ddMMyy
|
||||
gd := checkOut.Format("060102") // GD = ddMMyy
|
||||
ti := checkIn.Format("150405") // TI = HHmmss
|
||||
@ -181,14 +181,16 @@ func (lock *OmniLockServer) BuildCommand(encoderAddr, lockId string, checkIn, ch
|
||||
func (lock *OmniLockServer) LockSequence(conn net.Conn) error {
|
||||
const funcName = "OmniLockServer.LockSequence"
|
||||
// Start the link with the lock server
|
||||
raw, err := lock.linkStart(conn)
|
||||
regs, err := lock.linkStart(conn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[%s] linkStart failed: %v", funcName, err)
|
||||
}
|
||||
log.Infof("Link start response: %s", raw)
|
||||
for _, reg := range regs {
|
||||
log.Printf("Received: %q", reg)
|
||||
}
|
||||
|
||||
// Request encoding from the lock server
|
||||
raw, err = lock.requestEncoding(conn)
|
||||
raw, err := lock.requestEncoding(conn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[%s] requestEncoding failed: %v", funcName, err)
|
||||
}
|
||||
@ -197,17 +199,43 @@ func (lock *OmniLockServer) LockSequence(conn net.Conn) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lock *OmniLockServer) linkStart(conn net.Conn) (string, error) {
|
||||
func (lock *OmniLockServer) linkStart(conn net.Conn) ([]string, error) {
|
||||
const funcName = "OmniLockServer.linkStart"
|
||||
// Send the link start command
|
||||
payload := fmt.Sprintf("LS|DA%s|TI%s|", time.Now().Format("060102"), time.Now().Format("150405"))
|
||||
payload := fmt.Sprintf("LS|DA%s|TI%s|",
|
||||
time.Now().Format("060102"),
|
||||
time.Now().Format("150405"),
|
||||
)
|
||||
command := append([]byte{0x02}, append([]byte(payload), 0x03)...)
|
||||
|
||||
raw, err := sendAndReceive(conn, command)
|
||||
log.Printf("Sending command: %q", command)
|
||||
_, err := conn.Write(command)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to send Link Start command: %v", err)
|
||||
return nil, fmt.Errorf("failed to send command: %v", err)
|
||||
}
|
||||
return raw, nil
|
||||
|
||||
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
|
||||
reader := bufio.NewReader(conn)
|
||||
|
||||
var registers []string
|
||||
for {
|
||||
reg, err := reader.ReadString(0x03)
|
||||
if err != nil {
|
||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||
break // timeout -> assume no more registers
|
||||
}
|
||||
return nil, fmt.Errorf("error reading register: %v", err)
|
||||
}
|
||||
|
||||
registers = append(registers, reg)
|
||||
|
||||
// stop when you see the final register type, e.g. "LA|…|<ETX>"
|
||||
if strings.HasPrefix(reg, "\x02LA|") {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return registers, nil
|
||||
}
|
||||
|
||||
func (lock *OmniLockServer) requestEncoding(conn net.Conn) (string, error) {
|
||||
@ -218,5 +246,5 @@ func (lock *OmniLockServer) requestEncoding(conn net.Conn) (string, error) {
|
||||
return "", fmt.Errorf("failed to send Encoding request: %v", err)
|
||||
}
|
||||
|
||||
return parseOmniResponse(raw)
|
||||
return parseOmniResponse(raw)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user