fix(dispenser): simplify card preparation and recovery
This commit is contained in:
@@ -40,6 +40,9 @@ const (
|
||||
var (
|
||||
ErrCardWellEmpty = errors.New(CardWellEmptyMessage)
|
||||
|
||||
// ErrPreparationExhausted permits a new, independent UI preparation attempt.
|
||||
ErrPreparationExhausted = errors.New("card preparation exhausted")
|
||||
|
||||
SerialPort string
|
||||
Address []byte
|
||||
|
||||
@@ -142,38 +145,35 @@ func isAtEncoderPosition(statusBytes []byte) bool {
|
||||
return valid && flags&positionEncoder != 0
|
||||
}
|
||||
|
||||
func validateDispenserStatusData(statusBytes []byte) error {
|
||||
if len(statusBytes) != 4 {
|
||||
return fmt.Errorf("malformed dispenser status: got %d bytes, want 4", len(statusBytes))
|
||||
}
|
||||
type preparationClass string
|
||||
|
||||
statusMaps := []map[byte]string{statusPos0, statusPos1, statusPos2}
|
||||
for position, mapper := range statusMaps {
|
||||
if _, ok := mapper[statusBytes[position]]; !ok {
|
||||
return fmt.Errorf("unknown dispenser status 0x%X at position %d", statusBytes[position], position+1)
|
||||
}
|
||||
}
|
||||
if _, valid := decodePositionStatus(statusBytes[3]); !valid {
|
||||
return fmt.Errorf("unknown dispenser status 0x%X at position 4", statusBytes[3])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
const (
|
||||
encoderConfirmed preparationClass = "encoder confirmed"
|
||||
positionUncertain preparationClass = "valid but uncertain"
|
||||
positionWellEmpty preparationClass = "empty"
|
||||
positionNoCard preparationClass = "no card on sensors"
|
||||
)
|
||||
|
||||
func dispenserStatusError(statusBytes []byte) error {
|
||||
switch statusBytes[0] {
|
||||
case 0x34, 0x32, 0x36:
|
||||
return fmt.Errorf("dispenser error: %s", statusPos0[statusBytes[0]])
|
||||
// classifyPreparationStatus is the only readiness gate after AP wire validation.
|
||||
// Diagnostics describe the device; they do not veto a physical position class.
|
||||
func classifyPreparationStatus(status []byte) (preparationClass, error) {
|
||||
if len(status) != 4 {
|
||||
return "", fmt.Errorf("malformed dispenser status: got %d bytes, want 4", len(status))
|
||||
}
|
||||
switch statusBytes[1] {
|
||||
case 0x32, 0x31:
|
||||
return fmt.Errorf("dispenser error: %s", statusPos1[statusBytes[1]])
|
||||
flags, valid := decodePositionStatus(status[3])
|
||||
if !valid {
|
||||
return "", fmt.Errorf("malformed dispenser position encoding: 0x%X", status[3])
|
||||
}
|
||||
switch statusBytes[2] {
|
||||
case 0x34, 0x32:
|
||||
return fmt.Errorf("dispenser error: %s", statusPos2[statusBytes[2]])
|
||||
switch {
|
||||
case flags&positionEncoder != 0:
|
||||
return encoderConfirmed, nil
|
||||
case status[3] == 0x38:
|
||||
return positionWellEmpty, nil
|
||||
case status[3] == 0x30:
|
||||
return positionNoCard, nil
|
||||
default:
|
||||
return positionUncertain, nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isPreparationMoving(statusBytes []byte) bool {
|
||||
@@ -181,11 +181,6 @@ func isPreparationMoving(statusBytes []byte) bool {
|
||||
(statusBytes[0] == 0x31 || statusBytes[1] == 0x38)
|
||||
}
|
||||
|
||||
func hasPreparationDiagnostics(statusBytes []byte) bool {
|
||||
return len(statusBytes) == 4 &&
|
||||
(statusBytes[0] != 0x30 || statusBytes[1] != 0x30 || statusBytes[2] != 0x30)
|
||||
}
|
||||
|
||||
func stockTake(statusBytes []byte) string {
|
||||
if len(statusBytes) < 4 {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user