restructure hardlink layout
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// Package lockserver provides functionality for interacting with Assa Abloy lock servers.
|
||||
package lockserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// BuildCommand builds a key encoding request command for the Assa Abloy lock server.
|
||||
func (lock *AssaLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
||||
ci := checkIn.Format("200601021504")
|
||||
co := checkOut.Format("200601021504")
|
||||
|
||||
lock.command = fmt.Sprintf("CCA;EA%s;GR%s;CO%s;CI%s;AM1;\r\n", lock.encoderAddr, doorReq.RoomField, co, ci)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Checks heart beat of the Assa Abloy lock server and perform key encoding
|
||||
func (lock *AssaLockServer) LockSequence() error {
|
||||
const funcName = "AssaLockServer.LockSequence"
|
||||
|
||||
conn, err := InitializeServerConnection(LockServerURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
resp, err := sendHeartbeatToServer(conn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[%s] heartbeat failed: %v", funcName, err)
|
||||
}
|
||||
log.Infof("Heartbeat response: %s", resp)
|
||||
|
||||
resp, err = requestEncoding(conn, lock.command)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[%s] request encoding failed: %v", funcName, err)
|
||||
}
|
||||
log.Infof("Encoding response: %s", resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendHeartbeatToServer(conn net.Conn) (string, error) {
|
||||
const heartbeatRegister = "CCC;EAHEARTBEAT;AM1;\r\n"
|
||||
|
||||
raw, err := sendAndReceive(conn, []byte(heartbeatRegister))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to send Heartbeat command: %v", err)
|
||||
}
|
||||
|
||||
return parseAssaResponse(raw)
|
||||
}
|
||||
|
||||
func requestEncoding(conn net.Conn, command string) (string, error) {
|
||||
raw, err := sendAndReceive(conn, []byte(command))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to send Encoding request: %v", err)
|
||||
}
|
||||
|
||||
return parseAssaResponse(raw)
|
||||
}
|
||||
|
||||
func parseAssaResponse(raw string) (string, error) {
|
||||
clean := strings.ReplaceAll(raw, "\r\n", "")
|
||||
idx := strings.Index(clean, "RC")
|
||||
code := clean[idx+2 : idx+3] // Extract the response code
|
||||
if code != "0" {
|
||||
return "", fmt.Errorf("negative response code: %s", clean)
|
||||
}
|
||||
return "Success: " + clean, nil
|
||||
}
|
||||
Reference in New Issue
Block a user