Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ce9fdcf0b | ||
|
|
b303721a92 |
@@ -10,11 +10,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Build key encoding request command for the Assa Abloy lock server.
|
// Build key encoding request command for the Assa Abloy lock server.
|
||||||
func (lock *AssaLockServer) BuildCommand(lockId string, checkIn, checkOut time.Time) error {
|
func (lock *AssaLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
||||||
ci := checkIn.Format("200601021504")
|
ci := checkIn.Format("200601021504")
|
||||||
co := checkOut.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)
|
lock.command = fmt.Sprintf("CCA;EA%s;GR%s;CO%s;CI%s;AM1;\r\n", lock.encoderAddr, doorReq.RoomField, co, ci)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,14 +12,23 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DoorCardRequest is the JSON payload for /issue-door-card.
|
||||||
|
type DoorCardRequest struct {
|
||||||
|
RoomField string `json:"roomField"`
|
||||||
|
CheckinTime string `json:"checkinTime"`
|
||||||
|
CheckoutTime string `json:"checkoutTime"`
|
||||||
|
FollowStr string `json:"followStr"`
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AssaAbloy = "assaabloy"
|
AssaAbloy = "assaabloy"
|
||||||
Omnitec = "omnitec"
|
Omnitec = "omnitec"
|
||||||
|
Salto = "salto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
LockServer interface {
|
LockServer interface {
|
||||||
BuildCommand(lockId string, checkIn, checkOut time.Time) error
|
BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error
|
||||||
LockSequence(conn net.Conn) error
|
LockSequence(conn net.Conn) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,6 +41,11 @@ type (
|
|||||||
encoderAddr string // Encoder unit address
|
encoderAddr string // Encoder unit address
|
||||||
command []byte // Command to be sent to the lock server
|
command []byte // Command to be sent to the lock server
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SaltoLockServer struct {
|
||||||
|
encoderAddr string
|
||||||
|
command []byte
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewLockServer(lockType, encoderAddr string, fatalError func(error)) LockServer {
|
func NewLockServer(lockType, encoderAddr string, fatalError func(error)) LockServer {
|
||||||
@@ -40,6 +54,8 @@ func NewLockServer(lockType, encoderAddr string, fatalError func(error)) LockSer
|
|||||||
return &AssaLockServer{encoderAddr: encoderAddr}
|
return &AssaLockServer{encoderAddr: encoderAddr}
|
||||||
case Omnitec:
|
case Omnitec:
|
||||||
return &OmniLockServer{encoderAddr: encoderAddr}
|
return &OmniLockServer{encoderAddr: encoderAddr}
|
||||||
|
case Salto:
|
||||||
|
return &SaltoLockServer{encoderAddr: encoderAddr}
|
||||||
default:
|
default:
|
||||||
fatalError(fmt.Errorf("unsupported LockType: %s; must be 'assaabloy' or 'omnitec'", lockType))
|
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
|
return nil // This line will never be reached, but is needed to satisfy the compiler
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Build key encoding request command for the Omnitec lock server.
|
// Build key encoding request command for the Omnitec lock server.
|
||||||
func (lock *OmniLockServer) BuildCommand(lockId string, checkIn, checkOut time.Time) error {
|
func (lock *OmniLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
||||||
const funcName = "OmniLockServer.BuildCommand"
|
const funcName = "OmniLockServer.BuildCommand"
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -21,7 +21,7 @@ func (lock *OmniLockServer) BuildCommand(lockId string, checkIn, checkOut time.T
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Format lockId as 4-digit zero-padded string
|
// Format lockId as 4-digit zero-padded string
|
||||||
idInt, err := strconv.Atoi(lockId)
|
idInt, err := strconv.Atoi(doorReq.RoomField)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("[%s] failed to convert lockId to integer: %v", funcName, err)
|
return fmt.Errorf("[%s] failed to convert lockId to integer: %v", funcName, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
package lockserver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
// "strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
STX = 0x02 // Start of Text
|
||||||
|
ETX = 0x03 // End of Text
|
||||||
|
ENQ = 0x05 // Enquiry from host
|
||||||
|
ACK = 0x06 // Positive response
|
||||||
|
NAK = 0x15 // Negative response
|
||||||
|
)
|
||||||
|
|
||||||
|
func calculateLRC(data []byte) byte {
|
||||||
|
lrc := byte(0x00)
|
||||||
|
for _, b := range data {
|
||||||
|
lrc ^= b
|
||||||
|
}
|
||||||
|
return lrc
|
||||||
|
}
|
||||||
|
|
||||||
|
// simple join that avoids importing strings just for this.
|
||||||
|
func stringJoin(parts []string, sep string) string {
|
||||||
|
if len(parts) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
out := parts[0]
|
||||||
|
for _, p := range parts[1:] {
|
||||||
|
out += sep + p
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildCommand builds a Salto card‑issuance command in the form:
|
||||||
|
// STX|CN|<encoder addres>|E|<room>| | | | | |<start>|<expiry>|ETX|<LRC>
|
||||||
|
// where <start> and <expiry> are hhmmDDMMYY, and LRC is the XOR of all bytes
|
||||||
|
func (lock *SaltoLockServer) BuildCommand(doorReq DoorCardRequest, checkIn, checkOut time.Time) error {
|
||||||
|
// format helper: hhmmDDMMYY
|
||||||
|
var command string
|
||||||
|
fmtStamp := func(t time.Time) string {
|
||||||
|
return fmt.Sprintf("%02d%02d%02d%02d%02d",
|
||||||
|
t.Hour(), t.Minute(), t.Day(), int(t.Month()), t.Year()%100)
|
||||||
|
}
|
||||||
|
|
||||||
|
start := fmtStamp(checkIn)
|
||||||
|
expiry := fmtStamp(checkOut)
|
||||||
|
|
||||||
|
switch doorReq.FollowStr {
|
||||||
|
case "0":
|
||||||
|
command = "CN" // encode keycard
|
||||||
|
case "1":
|
||||||
|
command = "CC" // encode keycard copy
|
||||||
|
default:
|
||||||
|
command = "CN"
|
||||||
|
}
|
||||||
|
|
||||||
|
// the 12 fields between STX and ETX
|
||||||
|
fields := []string{
|
||||||
|
command, lock.encoderAddr, "E", doorReq.RoomField, "", "", "", "", "", "", start, expiry,
|
||||||
|
}
|
||||||
|
|
||||||
|
body := "|" + stringJoin(fields, "|") + "|" // leading/trailing pipes, so the ETX ends the last field
|
||||||
|
|
||||||
|
// wrap with STX/ETX
|
||||||
|
msg := append([]byte{STX}, []byte(body)...)
|
||||||
|
msg = append(msg, ETX)
|
||||||
|
|
||||||
|
// compute LRC over everything *after* STX up to and including ETX
|
||||||
|
lrc := calculateLRC(msg[1:]) // skip STX
|
||||||
|
msg = append(msg, lrc)
|
||||||
|
|
||||||
|
lock.command = msg
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks heart beat of the Assa Abloy lock server and perform key encoding
|
||||||
|
func (lock *SaltoLockServer) LockSequence(conn net.Conn) error {
|
||||||
|
const funcName = "SaltoLockServer.LockSequence"
|
||||||
|
|
||||||
|
// Step 1: ENQ to check availability
|
||||||
|
respStr, err := sendAndReceive(conn, []byte{ENQ})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed sending ENQ: %w", funcName, err)
|
||||||
|
}
|
||||||
|
resp := []byte(respStr)
|
||||||
|
|
||||||
|
if len(resp) != 1 || resp[0] != ACK {
|
||||||
|
return fmt.Errorf("[%s] expected ACK (0x06) after ENQ, got: %q (hex: % X)", funcName, respStr, resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Send actual command
|
||||||
|
respStr, err = sendAndReceive(conn, lock.command)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("[%s] failed sending command: %w", funcName, err)
|
||||||
|
}
|
||||||
|
resp = []byte(respStr)
|
||||||
|
|
||||||
|
if len(resp) == 1 && resp[0] == NAK {
|
||||||
|
return fmt.Errorf("[%s] command rejected by lock server (NAK)", funcName)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Encoding response: %q", respStr)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
buildVersion = "1.0.0"
|
buildVersion = "1.0.5"
|
||||||
serviceName = "hardlink"
|
serviceName = "hardlink"
|
||||||
customLayout = "2006-01-02 15:04:05 -0700"
|
customLayout = "2006-01-02 15:04:05 -0700"
|
||||||
transactionUrl = "http://127.0.0.1:18181/start-transaction/"
|
transactionUrl = "http://127.0.0.1:18181/start-transaction/"
|
||||||
@@ -46,19 +46,9 @@ type configRec struct {
|
|||||||
DispenserAdrr string `yaml:"dispensAddr"`
|
DispenserAdrr string `yaml:"dispensAddr"`
|
||||||
PrinterName string `yaml:"printerName"`
|
PrinterName string `yaml:"printerName"`
|
||||||
LogDir string `yaml:"logdir"`
|
LogDir string `yaml:"logdir"`
|
||||||
dbport int `yaml:"dbport"` // Port for the database connection
|
isPayment bool `yaml:"isPayment"`
|
||||||
dbname string `yaml:"dbname"` // Database name for the connection
|
|
||||||
dbuser string `yaml:"dbuser"` // User for the database connection
|
|
||||||
dbpassword string `yaml:"dbpassword"` // Password for the database connection
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoorCardRequest is the JSON payload for /issue-door-card.
|
|
||||||
type DoorCardRequest struct {
|
|
||||||
RoomField string `json:"roomField"`
|
|
||||||
CheckinTime string `json:"checkinTime"`
|
|
||||||
CheckoutTime string `json:"checkoutTime"`
|
|
||||||
FollowStr string `json:"followStr"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// App holds shared resources.
|
// App holds shared resources.
|
||||||
type App struct {
|
type App struct {
|
||||||
@@ -124,62 +114,65 @@ func main() {
|
|||||||
// }
|
// }
|
||||||
// defer db.Close()
|
// defer db.Close()
|
||||||
|
|
||||||
startClient := func() (*exec.Cmd, error) {
|
if config.isPayment {
|
||||||
cmd := exec.Command("./ChipDNAClient/ChipDnaClient.exe")
|
startClient := func() (*exec.Cmd, error) {
|
||||||
err := cmd.Start()
|
cmd := exec.Command("./ChipDNAClient/ChipDnaClient.exe")
|
||||||
if err != nil {
|
err := cmd.Start()
|
||||||
return nil, fmt.Errorf("Failed to start ChipDnaClient: %v", err)
|
|
||||||
}
|
|
||||||
log.Infof("ChipDnaClient started with PID %d", cmd.Process.Pid)
|
|
||||||
return cmd, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd, err := startClient()
|
|
||||||
if err != nil {
|
|
||||||
fatalError(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Restart loop
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
err := cmd.Wait()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("ChipDnaClient exited unexpectedly: %v", err)
|
return nil, fmt.Errorf("failed to start ChipDnaClient: %v", err)
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
cmd, err = startClient()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Restart failed: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Info("ChipDnaClient restarted successfully")
|
|
||||||
}
|
}
|
||||||
|
log.Infof("ChipDnaClient started with PID %d", cmd.Process.Pid)
|
||||||
|
return cmd, nil
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
// Handle shutdown signals
|
cmd, err := startClient()
|
||||||
sigs := make(chan os.Signal, 1)
|
if err != nil {
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
fatalError(err)
|
||||||
go func() {
|
|
||||||
<-sigs
|
|
||||||
log.Info("Shutting down...")
|
|
||||||
if cmd.Process != nil {
|
|
||||||
log.Info("Sending SIGTERM to ChipDnaClient...")
|
|
||||||
_ = cmd.Process.Signal(syscall.SIGTERM)
|
|
||||||
// wait up to 5s for graceful shutdown
|
|
||||||
done := make(chan error, 1)
|
|
||||||
go func() { done <- cmd.Wait() }()
|
|
||||||
select {
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
log.Warn("ChipDnaClient did not exit in time, killing...")
|
|
||||||
_ = cmd.Process.Kill()
|
|
||||||
case err := <-done:
|
|
||||||
log.Infof("ChipDnaClient exited cleanly: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
|
||||||
}()
|
// Restart loop
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
err := cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("ChipDnaClient exited unexpectedly: %v", err)
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
cmd, err = startClient()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Restart failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Info("ChipDnaClient restarted successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Handle shutdown signals
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-sigs
|
||||||
|
log.Info("Shutting down...")
|
||||||
|
if cmd.Process != nil {
|
||||||
|
log.Info("Sending SIGTERM to ChipDnaClient...")
|
||||||
|
_ = cmd.Process.Signal(syscall.SIGTERM)
|
||||||
|
// wait up to 5s for graceful shutdown
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- cmd.Wait() }()
|
||||||
|
select {
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
log.Warn("ChipDnaClient did not exit in time, killing...")
|
||||||
|
_ = cmd.Process.Kill()
|
||||||
|
case err := <-done:
|
||||||
|
log.Infof("ChipDnaClient exited cleanly: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// Create App and wire routes
|
// Create App and wire routes
|
||||||
|
// dispHandle := &serial.Port{} // Placeholder, replace with actual dispenser handle
|
||||||
app := newApp(dispHandle, lockConn, config)
|
app := newApp(dispHandle, lockConn, config)
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
@@ -324,7 +317,7 @@ func (app *App) startTransaction(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
||||||
const op = logging.Op("issueDoorCard")
|
const op = logging.Op("issueDoorCard")
|
||||||
var (
|
var (
|
||||||
doorReq DoorCardRequest
|
doorReq lockserver.DoorCardRequest
|
||||||
theResponse cmstypes.StatusRec
|
theResponse cmstypes.StatusRec
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -385,7 +378,7 @@ func (app *App) issueDoorCard(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// build lock server command
|
// build lock server command
|
||||||
app.lockserver.BuildCommand(doorReq.RoomField, checkIn, checkOut)
|
app.lockserver.BuildCommand(doorReq, checkIn, checkOut)
|
||||||
|
|
||||||
// lock server sequence
|
// lock server sequence
|
||||||
err = app.lockserver.LockSequence(app.lockConn)
|
err = app.lockserver.LockSequence(app.lockConn)
|
||||||
|
|||||||
+7
-1
@@ -2,7 +2,13 @@
|
|||||||
|
|
||||||
builtVersion is a const in main.go
|
builtVersion is a const in main.go
|
||||||
|
|
||||||
#### 1.0.0 - 30 Jun 2024
|
#### 1.0.5 - 24 July 2024
|
||||||
|
added encoding keycard copy for Salto locks
|
||||||
|
|
||||||
|
#### 1.0.4 - 22 July 2024
|
||||||
|
added salto lock server and implemented workflow for Salto
|
||||||
|
|
||||||
|
#### 1.0.0 - 30 June 2024
|
||||||
added creditcall payment method
|
added creditcall payment method
|
||||||
`/starttransaction` - API payment endpoint to start a transaction
|
`/starttransaction` - API payment endpoint to start a transaction
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user