223 lines
5.9 KiB
Go
223 lines
5.9 KiB
Go
package lockserver
|
|
|
|
import (
|
|
"bufio"
|
|
// "io"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
AssaAbloy = "assaabloy"
|
|
Omnitec = "omnitec"
|
|
)
|
|
|
|
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)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("[%s] failed to parse LockserverUrl: %v", funcName, err)
|
|
}
|
|
|
|
// Remove any leading/trailing slashes just in case
|
|
address := strings.Trim(parsedUrl.Host, "/")
|
|
|
|
// Establish a TCP connection to the Visionline server
|
|
conn, err := net.Dial("tcp", address)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to Visionline server: %v", err)
|
|
}
|
|
return conn, nil
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
return parseAssaResponse(raw)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|