Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b002c80b01 | ||
|
|
d9871e6a1c | ||
|
|
140c9a44cc | ||
|
|
b200eb3b91 | ||
|
|
7a9aa5ae7a | ||
|
|
fdf6985282 |
@@ -171,6 +171,26 @@ func InitializeDispenser() (*serial.Port, error) {
|
|||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DispenserSequence(port *serial.Port) (string, error) {
|
||||||
|
const funcName = "dispenserSequence"
|
||||||
|
var result string
|
||||||
|
|
||||||
|
// Check dispenser status
|
||||||
|
status, err := CheckDispenserStatus(port)
|
||||||
|
if err != nil {
|
||||||
|
return status, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
|
||||||
|
}
|
||||||
|
result += status
|
||||||
|
|
||||||
|
// Send card to encoder position
|
||||||
|
status, err = CardToEncoderPosition(port)
|
||||||
|
if err != nil {
|
||||||
|
return status, fmt.Errorf("[%s] error sending card to encoder position: %v", funcName, err)
|
||||||
|
}
|
||||||
|
result += "; " + status
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// if dispenser is not responding, I should repeat the command
|
// if dispenser is not responding, I should repeat the command
|
||||||
func CheckDispenserStatus(port *serial.Port) (string, error) {
|
func CheckDispenserStatus(port *serial.Port) (string, error) {
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package lockserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Build key encoding request command for the Assa Abloy lock server.
|
||||||
|
func (lock *AssaLockServer) BuildCommand(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", lock.encoderAddr, lockId, co, ci)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks heart beat of the Assa Abloy lock server and perform key encoding
|
||||||
|
func (lock *AssaLockServer) LockSequence(conn net.Conn) error {
|
||||||
|
const funcName = "AssaLockServer.LockSequence"
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
package lockserver
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/url"
|
|
||||||
|
|
||||||
// "os"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
LockserverUrl string
|
|
||||||
)
|
|
||||||
|
|
||||||
func InitializeServerConnection() (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
|
|
||||||
}
|
|
||||||
|
|
||||||
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))
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
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))
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package lockserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
// "io"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
AssaAbloy = "assaabloy"
|
||||||
|
Omnitec = "omnitec"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
LockServer interface {
|
||||||
|
BuildCommand(lockId string, checkIn, checkOut time.Time) error
|
||||||
|
LockSequence(conn net.Conn) error
|
||||||
|
}
|
||||||
|
|
||||||
|
AssaLockServer struct {
|
||||||
|
encoderAddr string
|
||||||
|
command string
|
||||||
|
}
|
||||||
|
|
||||||
|
OmniLockServer struct {
|
||||||
|
encoderAddr string // Encoder address for the lock server
|
||||||
|
command []byte // Command to be sent to the lock server
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewLockServer(lockType, encoderAddr string, fatalError func(error)) LockServer {
|
||||||
|
switch lockType {
|
||||||
|
case AssaAbloy:
|
||||||
|
return &AssaLockServer{encoderAddr: encoderAddr}
|
||||||
|
case Omnitec:
|
||||||
|
return &OmniLockServer{encoderAddr: encoderAddr}
|
||||||
|
default:
|
||||||
|
fatalError(fmt.Errorf("unsupported LockType: %s; must be 'assaabloy' or 'omnitec'", lockType))
|
||||||
|
return nil // This line will never be reached, but is needed to satisfy the compiler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 lock 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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,132 @@
|
|||||||
|
package lockserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Build key encoding request command for the Omnitec lock server.
|
||||||
|
func (lock *OmniLockServer) BuildCommand(lockId string, checkIn, checkOut time.Time) error {
|
||||||
|
const funcName = "OmniLockServer.BuildCommand"
|
||||||
|
hostname, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed to get hostname: %v", funcName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format lockId as 4-digit zero-padded string
|
||||||
|
idInt, err := strconv.Atoi(lockId)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed to convert lockId to integer: %v", funcName, err)
|
||||||
|
}
|
||||||
|
formattedLockId := fmt.Sprintf("%04d", idInt)
|
||||||
|
|
||||||
|
// Format date/time parts
|
||||||
|
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
|
||||||
|
|
||||||
|
// 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|",
|
||||||
|
lock.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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Starts link to the Omnitec lock server and perform key encoding
|
||||||
|
func (lock *OmniLockServer) LockSequence(conn net.Conn) error {
|
||||||
|
const funcName = "OmniLockServer.LockSequence"
|
||||||
|
// Start the link with the lock server
|
||||||
|
regs, err := lock.linkStart(conn)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] linkStart failed: %v", funcName, err)
|
||||||
|
}
|
||||||
|
for _, reg := range regs {
|
||||||
|
log.Printf("Received: %q", reg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request encoding from the lock server
|
||||||
|
raw, err := lock.requestEncoding(conn)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] request encoding failed: %v", funcName, err)
|
||||||
|
}
|
||||||
|
log.Infof("Encoding response: %s", raw)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lock *OmniLockServer) linkStart(conn net.Conn) ([]string, error) {
|
||||||
|
payload := fmt.Sprintf("LS|DA%s|TI%s|",
|
||||||
|
time.Now().Format("060102"),
|
||||||
|
time.Now().Format("150405"),
|
||||||
|
)
|
||||||
|
command := append([]byte{0x02}, append([]byte(payload), 0x03)...)
|
||||||
|
|
||||||
|
log.Printf("Sending command: %q", command)
|
||||||
|
_, err := conn.Write(command)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to send command: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
raw, err := sendAndReceive(conn, lock.command)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to send Encoding request: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseOmniResponse(raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -24,8 +24,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
buildVersion = "0.9.0"
|
buildVersion = "0.9.2"
|
||||||
serviceName = "accesspoint"
|
serviceName = "hardlink"
|
||||||
customLayout = "2006-01-02 15:04:05 -0700"
|
customLayout = "2006-01-02 15:04:05 -0700"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,6 +33,7 @@ const (
|
|||||||
type configRec struct {
|
type configRec struct {
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
LockserverUrl string `yaml:"lockservUrl"`
|
LockserverUrl string `yaml:"lockservUrl"`
|
||||||
|
LockType string `yaml:"lockType"`
|
||||||
EncoderAddress string `yaml:"encoderAddr"`
|
EncoderAddress string `yaml:"encoderAddr"`
|
||||||
DispenserPort string `yaml:"dispensPort"`
|
DispenserPort string `yaml:"dispensPort"`
|
||||||
DispenserAdrr string `yaml:"dispensAddr"`
|
DispenserAdrr string `yaml:"dispensAddr"`
|
||||||
@@ -50,9 +51,17 @@ type DoorCardRequest struct {
|
|||||||
|
|
||||||
// App holds shared resources.
|
// App holds shared resources.
|
||||||
type App struct {
|
type App struct {
|
||||||
dispPort *serial.Port
|
dispPort *serial.Port
|
||||||
lockConn net.Conn
|
lockConn net.Conn
|
||||||
config configRec
|
lockserver lockserver.LockServer
|
||||||
|
}
|
||||||
|
|
||||||
|
func newApp(dispPort *serial.Port, lockConn net.Conn, config configRec) *App {
|
||||||
|
return &App{
|
||||||
|
dispPort: dispPort,
|
||||||
|
lockConn: lockConn,
|
||||||
|
lockserver: lockserver.NewLockServer(config.LockType, config.EncoderAddress, fatalError),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -91,21 +100,15 @@ func main() {
|
|||||||
log.Infof("Dispenser initialized on port %s, %s", config.DispenserPort, status)
|
log.Infof("Dispenser initialized on port %s, %s", config.DispenserPort, status)
|
||||||
|
|
||||||
// Initialize lock-server connection once
|
// Initialize lock-server connection once
|
||||||
lockserver.LockserverUrl = config.LockserverUrl
|
lockConn, err := lockserver.InitializeServerConnection(config.LockserverUrl)
|
||||||
lockConn, err := lockserver.InitializeServerConnection()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatalError(err)
|
fatalError(err)
|
||||||
}
|
}
|
||||||
defer lockConn.Close()
|
defer lockConn.Close()
|
||||||
|
|
||||||
log.Infof("Connected to lock server at %s", config.LockserverUrl)
|
log.Infof("Connected to lock server at %s", config.LockserverUrl)
|
||||||
|
|
||||||
// Create App and wire routes
|
// Create App and wire routes
|
||||||
app := &App{
|
app := newApp(dispHandle, lockConn, config)
|
||||||
dispPort: dispHandle,
|
|
||||||
lockConn: lockConn,
|
|
||||||
config: config,
|
|
||||||
}
|
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
setUpRoutes(app, mux)
|
setUpRoutes(app, mux)
|
||||||
@@ -171,7 +174,6 @@ func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
|||||||
theResponse cmstypes.StatusRec
|
theResponse cmstypes.StatusRec
|
||||||
)
|
)
|
||||||
|
|
||||||
log.Println("issueDoorCard called")
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
@@ -182,6 +184,7 @@ func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("issueDoorCard called")
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
writeError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
writeError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
||||||
return
|
return
|
||||||
@@ -213,57 +216,31 @@ func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// build command
|
|
||||||
ci := checkIn.Format("200601021504")
|
|
||||||
co := checkOut.Format("200601021504")
|
|
||||||
cmd := fmt.Sprintf("CCA;EA%s;GR%s;CO%s;CI%s;AM1;\r\n",
|
|
||||||
app.config.EncoderAddress, doorReq.RoomField, co, ci,
|
|
||||||
)
|
|
||||||
|
|
||||||
// dispenser sequence
|
// dispenser sequence
|
||||||
if status, err := dispenser.CheckDispenserStatus(app.dispPort); err != nil {
|
if status, err := dispenser.DispenserSequence(app.dispPort); err != nil {
|
||||||
if status != "" {
|
if status != "" {
|
||||||
logging.Error(serviceName, status, "Dispenser error", string(op), "", "", 0)
|
logging.Error(serviceName, status, "Dispense error", string(op), "", "", 0)
|
||||||
writeError(w, http.StatusServiceUnavailable, "Dispenser error: "+err.Error())
|
writeError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error())
|
||||||
} else {
|
} else {
|
||||||
logging.Error(serviceName, err.Error(), "Dispenser error", string(op), "", "", 0)
|
logging.Error(serviceName, err.Error(), "Dispense error", string(op), "", "", 0)
|
||||||
writeError(w, http.StatusServiceUnavailable, err.Error()+"; check card stock")
|
writeError(w, http.StatusServiceUnavailable, "Dispense error: "+err.Error()+"; check card stock")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
log.Info(status)
|
log.Info(status)
|
||||||
}
|
}
|
||||||
|
|
||||||
if status, err := dispenser.CardToEncoderPosition(app.dispPort); err != nil {
|
// build lock server command
|
||||||
if status != "" {
|
app.lockserver.BuildCommand(doorReq.RoomField, checkIn, checkOut)
|
||||||
logging.Error(serviceName, status, "Dispenser error", string(op), "", "", 0)
|
|
||||||
writeError(w, http.StatusServiceUnavailable, "Dispenser move error: "+err.Error())
|
|
||||||
} else {
|
|
||||||
logging.Error(serviceName, err.Error(), "Dispenser move error", string(op), "", "", 0)
|
|
||||||
writeError(w, http.StatusServiceUnavailable, "Dispenser move error: "+err.Error()+"; check card stock")
|
|
||||||
}
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
log.Info(status)
|
|
||||||
}
|
|
||||||
|
|
||||||
// lock server sequence
|
// lock server sequence
|
||||||
resp, err := lockserver.SendHeartbeatToServer(app.lockConn)
|
err = app.lockserver.LockSequence(app.lockConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logging.Error(serviceName, err.Error(), "Lock server heartbeat error", string(op), "", "", 0)
|
logging.Error(serviceName, err.Error(), "Key encoding", string(op), "", "", 0)
|
||||||
writeError(w, http.StatusBadGateway, "Lock server heartbeat failed: "+err.Error())
|
writeError(w, http.StatusBadGateway, err.Error())
|
||||||
dispenser.CardOutOfMouth(app.dispPort)
|
dispenser.CardOutOfMouth(app.dispPort)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Infof("Heartbeat response: %s", resp)
|
|
||||||
resp, err = lockserver.RequestEncoding(app.lockConn, cmd)
|
|
||||||
if err != nil {
|
|
||||||
logging.Error(serviceName, err.Error(), "Lock server encoding error", string(op), "", "", 0)
|
|
||||||
writeError(w, http.StatusBadGateway, "Lock server encoding failed: "+err.Error())
|
|
||||||
dispenser.CardOutOfMouth(app.dispPort)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Infof("Lock server response: %s", resp)
|
|
||||||
|
|
||||||
// final dispenser steps
|
// final dispenser steps
|
||||||
if status, err := dispenser.CardOutOfMouth(app.dispPort); err != nil {
|
if status, err := dispenser.CardOutOfMouth(app.dispPort); err != nil {
|
||||||
@@ -284,7 +261,6 @@ func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (app *App) printRoomTicket(w http.ResponseWriter, r *http.Request) {
|
func (app *App) printRoomTicket(w http.ResponseWriter, r *http.Request) {
|
||||||
const op = logging.Op("printRoomTicket")
|
const op = logging.Op("printRoomTicket")
|
||||||
log.Println("printRoomTicket called")
|
|
||||||
var roomDetails printer.RoomDetailsRec
|
var roomDetails printer.RoomDetailsRec
|
||||||
// Allow CORS preflight if needed
|
// Allow CORS preflight if needed
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
@@ -295,6 +271,7 @@ func (app *App) printRoomTicket(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Println("printRoomTicket called")
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
writeError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
writeError(w, http.StatusMethodNotAllowed, "Method not allowed; use POST")
|
||||||
return
|
return
|
||||||
@@ -351,6 +328,13 @@ func readConfig() configRec {
|
|||||||
if cfg.Port == 0 {
|
if cfg.Port == 0 {
|
||||||
cfg.Port = defaultPort
|
cfg.Port = defaultPort
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.LockType == "" {
|
||||||
|
err = fmt.Errorf("LockType is required in %s", configName)
|
||||||
|
fatalError(err)
|
||||||
|
}
|
||||||
|
cfg.LockType = strings.ToLower(cfg.LockType)
|
||||||
|
|
||||||
if cfg.LogDir == "" {
|
if cfg.LogDir == "" {
|
||||||
cfg.LogDir = "./logs" + sep
|
cfg.LogDir = "./logs" + sep
|
||||||
} else if !strings.HasSuffix(cfg.LogDir, sep) {
|
} else if !strings.HasSuffix(cfg.LogDir, sep) {
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package payment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StartTransaction sends a start transaction XML to ChipDNA Server
|
||||||
|
func (pc *PaymentClient) StartTransaction(amountMinorUnits int, reference string) (string, error) {
|
||||||
|
conn, err := net.Dial("tcp", pc.Addr)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to connect: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
// Set timeout
|
||||||
|
conn.SetDeadline(time.Now().Add(15 * time.Second))
|
||||||
|
|
||||||
|
// Format XML request
|
||||||
|
request := fmt.Sprintf(`
|
||||||
|
<StartTransaction>
|
||||||
|
<Amount>%d</Amount>
|
||||||
|
<AmountType>Actual</AmountType>
|
||||||
|
<Reference>%s</Reference>
|
||||||
|
<TransactionType>Sale</TransactionType>
|
||||||
|
</StartTransaction>`, amountMinorUnits, reference)
|
||||||
|
|
||||||
|
_, err = conn.Write([]byte(request))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to write to server: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response
|
||||||
|
buff := make([]byte, 4096)
|
||||||
|
n, err := conn.Read(buff)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to read from server: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(buff[:n]), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package payment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PaymentClient holds connection data
|
||||||
|
type PaymentClient struct {
|
||||||
|
Addr string // e.g., "127.0.0.1:1869"
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitializeConnection(paymentrUrl string) (net.Conn, error) {
|
||||||
|
const funcName = "InitializeServerConnection"
|
||||||
|
// Parse the URL to extract host and port
|
||||||
|
parsedUrl, err := url.Parse(paymentrUrl)
|
||||||
|
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 payment server: %v", err)
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
|
## Release notes
|
||||||
|
|
||||||
|
builtVersion is a const in main.go
|
||||||
|
|
||||||
|
#### 0.9.1 - 22 May 2024
|
||||||
|
added lockserver interface and implemented workflow for Omnitec
|
||||||
|
|
||||||
#### 0.9.0 - 22 May 2024
|
#### 0.9.0 - 22 May 2024
|
||||||
The new API has two new endpoints:
|
The new API has two new endpoints:
|
||||||
- `/issuedoorcard` - encoding the door card for the room.
|
- `/issuedoorcard` - encoding the door card for the room.
|
||||||
|
|||||||
Reference in New Issue
Block a user