release version 1.1.0

This commit is contained in:
2026-01-29 17:23:00 +00:00
parent 9f0a9c939f
commit 4a255c06ed
5 changed files with 221 additions and 120 deletions
+185 -84
View File
@@ -3,6 +3,8 @@ package dispenser
import (
// "encoding/hex"
"fmt"
"strings"
// "log"
"time"
@@ -69,44 +71,78 @@ var (
}
)
func checkStatus(statusResp []byte) (string, error) {
if len(statusResp) > 3 {
statusBytes := statusResp[7:11] // Extract the relevant bytes from the response
// For each position, get the ASCII character, hex value, and mapped meaning.
posStatus := []struct {
pos int
value byte
mapper map[byte]string
}{
{pos: 1, value: statusBytes[0], mapper: statusPos0},
{pos: 2, value: statusBytes[1], mapper: statusPos1},
{pos: 3, value: statusBytes[2], mapper: statusPos2},
{pos: 4, value: statusBytes[3], mapper: statusPos3},
}
func logStatus(statusBytes []byte) {
// For each position, get the ASCII character, hex value, and mapped meaning.
posStatus := []struct {
pos int
value byte
mapper map[byte]string
}{
{pos: 1, value: statusBytes[0], mapper: statusPos0},
{pos: 2, value: statusBytes[1], mapper: statusPos1},
{pos: 3, value: statusBytes[2], mapper: statusPos2},
{pos: 4, value: statusBytes[3], mapper: statusPos3},
}
result := ""
for _, p := range posStatus {
statusMsg, exists := p.mapper[p.value]
if !exists {
statusMsg = "Unknown status"
}
if p.value != 0x30 {
result += fmt.Sprintf("Status: %s; ", statusMsg)
}
if p.pos == 4 && p.value == 0x38 {
return result, fmt.Errorf("Card well empty")
}
var result strings.Builder
for _, p := range posStatus {
statusMsg, exists := p.mapper[p.value]
if !exists {
statusMsg = fmt.Sprintf("Unknown status 0x%X;", p.value)
}
return result, nil
if p.value != 0x30 {
result.WriteString(statusMsg + "; ")
}
}
log.Infof("Dispenser status: %s", result.String())
}
func isAtEncoderPosition(statusBytes []byte) bool {
if statusBytes == nil {
return false
}
switch statusBytes[3] {
case 0x33: // Card at encoder position
return true
default:
return false // Not at encoder position
}
}
func stockTake(statusBytes []byte) string {
status := ""
if statusBytes == nil {
return status
}
if statusBytes[2] != 0x30 {
status = statusPos2[statusBytes[2]]
}
if statusBytes[3] == 0x38 { // Card well empty
status = statusPos3[statusBytes[3]]
}
return status
}
func isCardWellEmpty(statusBytes []byte) bool {
if statusBytes == nil {
return false
}
switch statusBytes[3] {
case 0x38: // Card well empty
return true
default:
return false
}
}
func checkACK(statusResp []byte) error {
if len(statusResp) == 3 && statusResp[0] == ACK && statusResp[1] == Address[0] && statusResp[2] == Address[1] {
return nil
} else if len(statusResp) > 0 && statusResp[0] == NAK {
return fmt.Errorf("negative response from dispenser")
} else {
if len(statusResp) == 3 && statusResp[0] == ACK && statusResp[1] == Address[0] && statusResp[2] == Address[1] {
return "active;", nil
} else if len(statusResp) > 0 && statusResp[0] == NAK {
return "", fmt.Errorf("negative response from dispenser")
} else {
return "", fmt.Errorf("unexpected response status: % X", statusResp)
}
return fmt.Errorf("unexpected response status: % X", statusResp)
}
}
@@ -171,47 +207,130 @@ func InitializeDispenser() (*serial.Port, error) {
return port, nil
}
func DispenserSequence(port *serial.Port) (string, error) {
func DispenserPrepare(port *serial.Port) (string, error) {
const funcName = "dispenserSequence"
var result string
stockStatus := ""
// Check dispenser status
status, err := CheckDispenserStatus(port)
if err != nil {
return status, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
return stockStatus, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
}
logStatus(status)
stockStatus = stockTake(status)
if isCardWellEmpty(status) {
return stockStatus, nil
}
result += status
if isAtEncoderPosition(status) {
return stockStatus, nil
}
// Send card to encoder position
status, err = CardToEncoderPosition(port)
err = CardToEncoderPosition(port)
if err != nil {
return status, fmt.Errorf("[%s] error sending card to encoder position: %v", funcName, err)
return stockStatus, fmt.Errorf("[%s] error sending card to encoder position: %v", funcName, err)
}
result += "; " + status
return result, nil
time.Sleep(delay)
// Check dispenser status
status, err = CheckDispenserStatus(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
}
logStatus(status)
stockStatus = stockTake(status)
return stockStatus, nil
}
func DispenserStart(port *serial.Port) (string, error) {
const funcName = "dispenserSequence"
stockStatus := ""
// Check dispenser status
status, err := CheckDispenserStatus(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
}
logStatus(status)
stockStatus = stockTake(status)
if isCardWellEmpty(status) {
return stockStatus, fmt.Errorf(stockStatus)
}
if isAtEncoderPosition(status) {
return stockStatus, nil
}
// Send card to encoder position
err = CardToEncoderPosition(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error sending card to encoder position: %v", funcName, err)
}
time.Sleep(delay)
// Check dispenser status
status, err = CheckDispenserStatus(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
}
logStatus(status)
stockStatus = stockTake(status)
return stockStatus, nil
}
func DispenserFinal(port *serial.Port) (string, error) {
const funcName = "dispenserSequence"
stockStatus := ""
err := CardOutOfMouth(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error sending card to out mouth position: %v", funcName, err)
}
time.Sleep(delay)
// Check dispenser status
status, err := CheckDispenserStatus(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
}
logStatus(status)
stockStatus = stockTake(status)
time.Sleep(delay)
// Send card to encoder position
err = CardToEncoderPosition(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error sending card to encoder position: %v", funcName, err)
}
time.Sleep(delay)
// Check dispenser status
status, err = CheckDispenserStatus(port)
if err != nil {
return stockStatus, fmt.Errorf("[%s] error checking dispenser status: %v", funcName, err)
}
logStatus(status)
stockStatus = stockTake(status)
return stockStatus, nil
}
// if dispenser is not responding, I should repeat the command
func CheckDispenserStatus(port *serial.Port) (string, error) {
func CheckDispenserStatus(port *serial.Port) ([]byte, error) {
const funcName = "checkDispenserStatus"
var result string
checkCmd := buildCheckAP(Address)
enq := append([]byte{ENQ}, Address...)
// Send check command (AP)
statusResp, err := sendAndReceive(port, checkCmd, delay)
if err != nil {
return "", fmt.Errorf("error sending check command: %v", err)
return nil, fmt.Errorf("error sending check command: %v", err)
}
if len(statusResp) == 0 {
return "", fmt.Errorf("no response from dispenser")
return nil, fmt.Errorf("no response from dispenser")
}
status, err := checkStatus(statusResp)
err = checkACK(statusResp)
if err != nil {
return status, err
return nil, err
}
result += "; " + status
// Send ENQ+ADDR to prompt device to execute the command.
statusResp, err = sendAndReceive(port, enq, delay)
@@ -219,17 +338,15 @@ func CheckDispenserStatus(port *serial.Port) (string, error) {
log.Errorf("error sending ENQ: %v", err)
}
if len(statusResp) == 0 {
return "", fmt.Errorf("no response from dispenser")
return nil, fmt.Errorf("no response from dispenser")
}
status, err = checkStatus(statusResp)
if err != nil {
return status, err
if len(statusResp) < 13 {
return nil, fmt.Errorf("incomplete status response from dispenser: % X", statusResp)
}
result += status
return result, nil
return statusResp[7:11], nil // Return status bytes
}
func CardToEncoderPosition(port *serial.Port) (string, error) {
func CardToEncoderPosition(port *serial.Port) error {
const funcName = "cartToEncoderPosition"
enq := append([]byte{ENQ}, Address...)
@@ -238,30 +355,22 @@ func CardToEncoderPosition(port *serial.Port) (string, error) {
log.Println("Send card to encoder position")
statusResp, err := sendAndReceive(port, dispenseCmd, delay)
if err != nil {
return "", fmt.Errorf("error sending card to encoder position: %v", err)
return fmt.Errorf("error sending card to encoder position: %v", err)
}
_, err = checkStatus(statusResp)
err = checkACK(statusResp)
if err != nil {
return "", err
return err
}
//Send ENQ to prompt device ---
_, err = port.Write(enq)
if err != nil {
return "", fmt.Errorf("error sending ENQ to prompt device: %v", err)
return fmt.Errorf("error sending ENQ to prompt device: %v", err)
}
time.Sleep(delay)
//Check card position status
status, err := CheckDispenserStatus(port)
if err != nil {
return "", err
}
return status, nil
return nil
}
func CardOutOfMouth(port *serial.Port) (string, error) {
func CardOutOfMouth(port *serial.Port) error {
const funcName = "CardOutOfMouth"
enq := append([]byte{ENQ}, Address...)
@@ -270,25 +379,17 @@ func CardOutOfMouth(port *serial.Port) (string, error) {
log.Println("Send card to out mouth position")
statusResp, err := sendAndReceive(port, dispenseCmd, delay)
if err != nil {
return "", fmt.Errorf("error sending out of mouth command: %v", err)
return fmt.Errorf("error sending out of mouth command: %v", err)
}
_, err = checkStatus(statusResp)
err = checkACK(statusResp)
if err != nil {
return "", err
return err
}
//Send ENQ to prompt device ---
_, err = port.Write(enq)
if err != nil {
return "", fmt.Errorf("error sending ENQ to prompt device: %v", err)
return fmt.Errorf("error sending ENQ to prompt device: %v", err)
}
time.Sleep(delay)
//Check card position status
status, err := CheckDispenserStatus(port)
if err != nil {
return "", err
}
return status, nil
return nil
}