added lockserver interface and implemented workflow for Omnitec

This commit is contained in:
2025-06-09 13:14:35 +01:00
parent 1f383eafa1
commit fdf6985282
3 changed files with 217 additions and 82 deletions
+178 -54
View File
@@ -2,22 +2,39 @@ package lockserver
import (
"bufio"
// "io"
"fmt"
"net"
"net/url"
// "os"
"os"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
var (
LockserverUrl string
const (
AssaAbloy = "assaabloy"
Omnitec = "omnitec"
)
func InitializeServerConnection() (net.Conn, error) {
type (
LockServer interface {
LockSequence(conn net.Conn) error
BuildCommand(encoderAddr, lockId string, checkIn, checkOut time.Time) error
}
AssaLockServer struct {
command string
}
OmniLockServer struct {
command []byte // Command to be sent to the lock server
}
)
func InitializeServerConnection(LockserverUrl string) (net.Conn, error) {
const funcName = "InitializeServerConnection"
// Parse the URL to extract host and port
parsedUrl, err := url.Parse(LockserverUrl)
@@ -36,63 +53,170 @@ func InitializeServerConnection() (net.Conn, error) {
return conn, nil
}
func SendHeartbeatToServer(conn net.Conn) (string, error) {
const funcName = "SendHeartbeatToServer"
// Write the check command to the server
heartbeat := "CCC;EAHEARTBEAT;AM1;\r\n"
log.Printf("Sending headrbeat: %q", heartbeat)
_, err := conn.Write([]byte(heartbeat))
// sendAndReceive sends a command to the lock server and waits for a response.
func sendAndReceive(conn net.Conn, command []byte) (string, error) {
const funcName = "SendAndReceive"
// Write the command to the connection
log.Printf("Sending command: %q", command)
_, err := conn.Write(command)
if err != nil {
return "", fmt.Errorf("failed to send command: %v", err)
}
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
buf := make([]byte, 128)
reader := bufio.NewReader(conn)
n, err := reader.Read(buf)
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
response := buf[:n]
return string(response), nil
}
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
}
func parseOmniResponse(raw string) (string, error) {
clean := strings.Trim(raw, "\x02\x03")
idx := strings.Index(clean, "AS")
code := clean[idx+2 : idx+4] // Extract the response code
code = strings.ToLower(code) // Convert to lowercase for consistency
if code != "ok" {
return "", fmt.Errorf("negative response code: %s", clean)
}
return "Success: " + clean, 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)
}
// set a read timeout to avoid indefinite blocking
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
// Read the response from the server. Visionline returns the response as ASCII text terminated by CRLF.
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
substr := "RC"
response = strings.ReplaceAll(response, "\r\n", "") // Remove CRLF from the response
num := strings.Index(response, substr) // Find the index of the response code
responseCode := response[num+2 : len(response)-1] // Extract the result code from the response
if responseCode != "0" {
return "", fmt.Errorf("negative Heartbeat response code: %s", responseCode)
}
return "Success: " + response, nil
return parseAssaResponse(raw)
}
func RequestEncoding(conn net.Conn, command string) (string, error) {
const funcName = "RequestEncoding"
// Write the command to the connection
log.Printf("Sending Encoding request: %q", command)
_, err := conn.Write([]byte(command))
func requestEncoding(conn net.Conn, command string) (string, error) {
// 1) Send and read raw response
raw, err := sendAndReceive(conn, []byte(command))
if err != nil {
return "", fmt.Errorf("failed to send Encoding request: %v", err)
}
// Optional: set a read timeout to avoid indefinite blocking
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
// Read the response from the server. Visionline returns the response as ASCII text terminated by CRLF.
reader := bufio.NewReader(conn)
response, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("error reading response: %v", err)
}
substr := "RC"
response = strings.ReplaceAll(response, "\r\n", "") // Remove CRLF from the response
num := strings.Index(response, substr) // Find the index of the response code
responseCode := response[num+2 : len(response)-1] // Extract the result code from the response
if responseCode != "0" {
return "", fmt.Errorf("negative lock server response code: %s", responseCode)
}
return "Success: " + response, nil
return parseAssaResponse(raw)
}
func (lock *AssaLockServer) LockSequence(conn net.Conn) error {
resp, err := sendHeartbeatToServer(conn)
if err != nil {
return fmt.Errorf("lock server heartbeat failed: %v", err)
}
log.Infof("Heartbeat response: %s", resp)
resp, err = requestEncoding(conn, lock.command)
if err != nil {
return fmt.Errorf("lock server request encoding failed: %v", err)
}
log.Infof("Encoding response: %s", resp)
return nil
}
func (lock *AssaLockServer) BuildCommand(encoderAddr, lockId string, 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", encoderAddr, lockId, co, ci)
return nil
}
func (lock *OmniLockServer) BuildCommand(encoderAddr, lockId string, checkIn, checkOut time.Time) error {
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("could not get hostname: %v", err)
}
// Format lockId as 4-digit zero-padded string
idInt, err := strconv.Atoi(lockId)
if err != nil {
return fmt.Errorf("invalid lockId %q: %v", lockId, err)
}
formattedLockId := fmt.Sprintf("%04d", idInt)
// Format date/time parts
ga := checkIn.Format("020106") // GA = ddMMyy
gd := checkOut.Format("020106") // GD = ddMMyy
dt := checkIn.Format("15:04") // DT = HH:mm
ti := checkIn.Format("150405") // TI = HHmmss
// Construct payload
payload := fmt.Sprintf(
"KR|KC%s|KTD|RN%s|%s|DT%s|G#75|GA%s|GD%s|KO0000|DA%s|TI%s|",
encoderAddr,
formattedLockId,
hostname,
dt,
ga,
gd,
ga,
ti,
)
// Assign to command field with STX and ETX
lock.command = append([]byte{0x02}, append([]byte(payload), 0x03)...)
return nil
}
func (lock *OmniLockServer) LockSequence(conn net.Conn) error {
const funcName = "OmniLockServer.LockSequence"
// Start the link with the lock server
raw, err := lock.linkStart(conn)
if err != nil {
return fmt.Errorf("[%s] linkStart failed: %v", funcName, err)
}
log.Infof("Link start response: %s", raw)
// Request encoding from the lock server
raw, err = lock.requestEncoding(conn)
if err != nil {
return fmt.Errorf("[%s] requestEncoding failed: %v", funcName, err)
}
log.Infof("Encoding response: %s", raw)
return nil
}
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("150405"), time.Now().Format("150405"))
command := append([]byte{0x02}, append([]byte(payload), 0x03)...)
raw, err := sendAndReceive(conn, command)
if err != nil {
return "", fmt.Errorf("failed to send Link Start command: %v", err)
}
return raw, nil
}
func (lock *OmniLockServer) requestEncoding(conn net.Conn) (string, error) {
const funcName = "OmniLockServer.requestEncoding"
// Send the encoding request command
raw, err := sendAndReceive(conn, lock.command)
if err != nil {
return "", fmt.Errorf("failed to send Encoding request: %v", err)
}
return parseOmniResponse(raw)
}