Get-Service TeamViewer | Select-Object Status, Name, StartType
This commit is contained in:
+159
-96
@@ -1,8 +1,11 @@
|
||||
package dispenser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -27,6 +30,13 @@ const (
|
||||
CardWellEmptyMessage = "Card well is empty"
|
||||
)
|
||||
|
||||
const (
|
||||
positionPreDispense = 0x01
|
||||
positionEncoder = 0x02
|
||||
positionMouth = 0x04
|
||||
positionEmpty = 0x08
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCardWellEmpty = errors.New(CardWellEmptyMessage)
|
||||
|
||||
@@ -59,20 +69,21 @@ var (
|
||||
0x31: "Card pre-empty",
|
||||
0x30: "Normal",
|
||||
}
|
||||
statusPos3 = map[byte]string{
|
||||
0x38: "Card empty",
|
||||
0x34: "Card ready position",
|
||||
0x33: "Card at encoder position",
|
||||
0x32: "Card at hold card position",
|
||||
0x31: "Card out of card mouth position",
|
||||
0x30: "Normal",
|
||||
}
|
||||
)
|
||||
|
||||
// --------------------
|
||||
// Status helpers
|
||||
// --------------------
|
||||
|
||||
// decodePositionStatus decodes the manual's 0x30 + combined sensor/empty flags.
|
||||
// Sensor 2 (0x02) is the read position; sensor 1 and sensor 2 together give 0x33.
|
||||
func decodePositionStatus(value byte) (flags byte, valid bool) {
|
||||
if value&0xF0 != 0x30 {
|
||||
return 0, false
|
||||
}
|
||||
return value & 0x0F, true
|
||||
}
|
||||
|
||||
func statusDescription(statusBytes []byte) string {
|
||||
if len(statusBytes) < 4 {
|
||||
return fmt.Sprintf("<invalid len=%d>", len(statusBytes))
|
||||
@@ -86,7 +97,6 @@ func statusDescription(statusBytes []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},
|
||||
}
|
||||
|
||||
var result strings.Builder
|
||||
@@ -99,6 +109,24 @@ func statusDescription(statusBytes []byte) string {
|
||||
result.WriteString(statusMsg + "; ")
|
||||
}
|
||||
}
|
||||
flags, valid := decodePositionStatus(statusBytes[3])
|
||||
if !valid {
|
||||
fmt.Fprintf(&result, "Unknown status 0x%X at position 4; ", statusBytes[3])
|
||||
} else {
|
||||
for _, position := range []struct {
|
||||
mask byte
|
||||
text string
|
||||
}{
|
||||
{positionPreDispense, "Card at pre-dispense position"},
|
||||
{positionEncoder, "Card at encoder position"},
|
||||
{positionMouth, "Card at mouth position"},
|
||||
{positionEmpty, "Card empty"},
|
||||
} {
|
||||
if flags&position.mask != 0 {
|
||||
result.WriteString(position.text + "; ")
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
|
||||
@@ -107,7 +135,11 @@ func logStatus(statusBytes []byte) {
|
||||
}
|
||||
|
||||
func isAtEncoderPosition(statusBytes []byte) bool {
|
||||
return len(statusBytes) >= 4 && statusBytes[3] == 0x33
|
||||
if len(statusBytes) < 4 {
|
||||
return false
|
||||
}
|
||||
flags, valid := decodePositionStatus(statusBytes[3])
|
||||
return valid && flags&positionEncoder != 0
|
||||
}
|
||||
|
||||
func validateDispenserStatusData(statusBytes []byte) error {
|
||||
@@ -115,12 +147,15 @@ func validateDispenserStatusData(statusBytes []byte) error {
|
||||
return fmt.Errorf("malformed dispenser status: got %d bytes, want 4", len(statusBytes))
|
||||
}
|
||||
|
||||
statusMaps := []map[byte]string{statusPos0, statusPos1, statusPos2, statusPos3}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -162,14 +197,18 @@ func stockTake(statusBytes []byte) string {
|
||||
if statusBytes[2] != 0x30 {
|
||||
status = statusPos2[statusBytes[2]]
|
||||
}
|
||||
if statusBytes[3] == 0x38 {
|
||||
status = statusPos3[statusBytes[3]]
|
||||
if isCardWellEmpty(statusBytes) {
|
||||
status = "Card empty"
|
||||
}
|
||||
return status
|
||||
}
|
||||
|
||||
func isCardWellEmpty(statusBytes []byte) bool {
|
||||
return len(statusBytes) >= 4 && statusBytes[3] == 0x38
|
||||
if len(statusBytes) < 4 {
|
||||
return false
|
||||
}
|
||||
flags, valid := decodePositionStatus(statusBytes[3])
|
||||
return valid && flags&positionEmpty != 0
|
||||
}
|
||||
|
||||
func checkACK(statusResp []byte) error {
|
||||
@@ -208,20 +247,99 @@ func createPacket(address []byte, command []byte) []byte {
|
||||
|
||||
func buildCheckAP(address []byte) []byte { return createPacket(address, []byte{STX, 0x41, 0x50}) }
|
||||
|
||||
func sendAndReceive(port *serial.Port, packet []byte, delay time.Duration) ([]byte, error) {
|
||||
_, err := port.Write(packet)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error writing to port: %w", err)
|
||||
}
|
||||
// serialTransport is used only by the serial-port owner.
|
||||
type serialTransport interface {
|
||||
io.Reader
|
||||
io.Writer
|
||||
}
|
||||
|
||||
time.Sleep(delay)
|
||||
|
||||
buf := make([]byte, 128)
|
||||
n, err := port.Read(buf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading from port: %w", err)
|
||||
func writePacket(ctx context.Context, port serialTransport, packet []byte) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return buf[:n], nil
|
||||
n, err := port.Write(packet)
|
||||
if err != nil {
|
||||
return fmt.Errorf("write dispenser packet: %w", err)
|
||||
}
|
||||
if n != len(packet) {
|
||||
return fmt.Errorf("write dispenser packet (%d/%d bytes): %w", n, len(packet), io.ErrShortWrite)
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func readExact(ctx context.Context, port serialTransport, data []byte) error {
|
||||
for len(data) > 0 {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := port.Read(data)
|
||||
data = data[n:]
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return ctxErr
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("read dispenser response: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
return fmt.Errorf("read dispenser response: %w", io.ErrNoProgress)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendAndReadACK(ctx context.Context, port serialTransport, packet []byte, processingDelay time.Duration) error {
|
||||
if err := writePacket(ctx, port, packet); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := waitForSequence(ctx, processingDelay); err != nil {
|
||||
return err
|
||||
}
|
||||
response := make([]byte, 3)
|
||||
if err := readExact(ctx, port, response); err != nil {
|
||||
return fmt.Errorf("read ACK: %w", err)
|
||||
}
|
||||
return checkACK(response)
|
||||
}
|
||||
|
||||
// queryStatus accepts only the fixed RF/AP payload sizes, before reading a body.
|
||||
func queryStatus(ctx context.Context, port serialTransport, command []byte, statusCount int, processingDelay time.Duration) ([]byte, error) {
|
||||
if err := sendAndReadACK(ctx, port, createPacket(Address, command), processingDelay); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := writePacket(ctx, port, append([]byte{ENQ}, Address...)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := waitForSequence(ctx, processingDelay); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
header := make([]byte, 5)
|
||||
if err := readExact(ctx, port, header); err != nil {
|
||||
return nil, fmt.Errorf("read status header: %w", err)
|
||||
}
|
||||
if header[0] != STX {
|
||||
return nil, fmt.Errorf("invalid status STX: % X", header)
|
||||
}
|
||||
if len(Address) != 2 || header[1] != Address[0] || header[2] != Address[1] {
|
||||
return nil, fmt.Errorf("unexpected status address: % X", header[1:3])
|
||||
}
|
||||
length := int(binary.BigEndian.Uint16(header[3:5]))
|
||||
if length != statusCount+2 {
|
||||
return nil, fmt.Errorf("invalid status payload length: got %d, want %d", length, statusCount+2)
|
||||
}
|
||||
frame := append(header, make([]byte, length+2)...)
|
||||
if err := readExact(ctx, port, frame[5:]); err != nil {
|
||||
return nil, fmt.Errorf("read status body: %w", err)
|
||||
}
|
||||
if frame[len(frame)-2] != ETX {
|
||||
return nil, fmt.Errorf("invalid status ETX: % X", frame)
|
||||
}
|
||||
if calculateBCC(frame[:len(frame)-1]) != frame[len(frame)-1] {
|
||||
return nil, fmt.Errorf("invalid status BCC: % X", frame)
|
||||
}
|
||||
if frame[5] != 'S' || frame[6] != 'F' {
|
||||
return nil, fmt.Errorf("unexpected status response type: % X", frame[5:7])
|
||||
}
|
||||
return frame[7 : 7+statusCount], nil
|
||||
}
|
||||
|
||||
// --------------------
|
||||
@@ -271,83 +389,28 @@ func InitializeDispenser() (*serial.Port, error) {
|
||||
// --------------------
|
||||
|
||||
// checkDispenserStatus talks to the device and returns the 4 status bytes [pos0..pos3].
|
||||
func checkDispenserStatus(port *serial.Port) ([]byte, error) {
|
||||
checkCmd := buildCheckAP(Address)
|
||||
enq := append([]byte{ENQ}, Address...)
|
||||
|
||||
statusResp, err := sendAndReceive(port, checkCmd, delay)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error sending check command: %w", err)
|
||||
}
|
||||
if len(statusResp) == 0 {
|
||||
return nil, fmt.Errorf("no response from dispenser")
|
||||
}
|
||||
if err := checkACK(statusResp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
statusResp, err = sendAndReceive(port, enq, delay)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error sending ENQ: %w", err)
|
||||
}
|
||||
if len(statusResp) < 13 {
|
||||
return nil, fmt.Errorf("incomplete status response from dispenser: % X", statusResp)
|
||||
}
|
||||
return statusResp[7:11], nil
|
||||
func checkDispenserStatus(ctx context.Context, port serialTransport) ([]byte, error) {
|
||||
return queryStatus(ctx, port, []byte{0x02, 'A', 'P'}, 4, delay)
|
||||
}
|
||||
|
||||
func cardToEncoderPosition(port *serial.Port) error {
|
||||
enq := append([]byte{ENQ}, Address...)
|
||||
// dispatchCommand confirms ACK and sends ENQ; it does not wait for movement.
|
||||
func dispatchCommand(ctx context.Context, port serialTransport, command []byte, processingDelay time.Duration) error {
|
||||
if err := sendAndReadACK(ctx, port, createPacket(Address, command), processingDelay); err != nil {
|
||||
return err
|
||||
}
|
||||
return writePacket(ctx, port, append([]byte{ENQ}, Address...))
|
||||
}
|
||||
|
||||
dispenseCmd := createPacket(Address, commandFC7)
|
||||
func cardToEncoderPosition(ctx context.Context, port serialTransport) 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: %w", err)
|
||||
}
|
||||
if err := checkACK(statusResp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = port.Write(enq)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending ENQ to prompt device: %w", err)
|
||||
}
|
||||
return nil
|
||||
return dispatchCommand(ctx, port, commandFC7, delay)
|
||||
}
|
||||
|
||||
func resetDispenser(port *serial.Port) error {
|
||||
response, err := sendAndReceive(port, createPacket(Address, commandRS), delay)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending reset command: %w", err)
|
||||
}
|
||||
if err := checkACK(response); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := port.Write(append([]byte{ENQ}, Address...)); err != nil {
|
||||
return fmt.Errorf("error sending ENQ to reset device: %w", err)
|
||||
}
|
||||
return nil
|
||||
func resetDispenser(ctx context.Context, port serialTransport) error {
|
||||
return dispatchCommand(ctx, port, commandRS, delay)
|
||||
}
|
||||
|
||||
func cardOutOfMouth(port *serial.Port) error {
|
||||
enq := append([]byte{ENQ}, Address...)
|
||||
|
||||
dispenseCmd := createPacket(Address, commandFC0)
|
||||
func cardOutOfMouth(ctx context.Context, port serialTransport) 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: %w", err)
|
||||
}
|
||||
if err := checkACK(statusResp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = port.Write(enq)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error sending ENQ to prompt device: %w", err)
|
||||
}
|
||||
return nil
|
||||
return dispatchCommand(ctx, port, commandFC0, delay)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user