78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package lockserver
|
|
|
|
import (
|
|
"bufio"
|
|
// "io"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"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) {
|
|
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
|
|
}
|
|
|
|
|
|
|
|
|
|
|