-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver_windows.go
More file actions
180 lines (160 loc) · 5.53 KB
/
Copy pathdriver_windows.go
File metadata and controls
180 lines (160 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//go:build windows
package main
import (
"fmt"
"log"
"os/exec"
"strings"
)
// detectTSCDrivers checks for TSC drivers, USB devices, and registered printers on Windows.
func detectTSCDrivers() DriverStatus {
status := DriverStatus{
DownloadURL: tscDriverURLWindows,
}
// 1. Check installed printer drivers
status.DriverNames = detectTSCWindowsDrivers()
status.DriversInstalled = len(status.DriverNames) > 0
// 2. Detect TSC USB devices via PnP
status.USBDevices = detectTSCWindowsUSB()
// 3. Check registered printers
status.RegisteredPrinters = detectTSCWindowsPrinters()
// Determine setup needs
hasUSB := len(status.USBDevices) > 0
hasRegistered := len(status.RegisteredPrinters) > 0
hasDrivers := status.DriversInstalled
if hasUSB && !hasRegistered && hasDrivers {
status.NeedsSetup = true
status.CanAutoInstall = true
status.Instructions = "Driver TSC instalado y dispositivo detectado, pero no hay impresora registrada. Se puede registrar automaticamente."
} else if hasUSB && !hasDrivers {
status.NeedsSetup = true
status.CanAutoInstall = false
status.Instructions = "Dispositivo TSC detectado pero no hay driver instalado. Descargue el driver desde el sitio oficial de TSC."
} else if !hasUSB && !hasRegistered {
status.Instructions = "No se detectaron dispositivos TSC. Conecte la impresora e intente de nuevo."
}
return status
}
// detectTSCWindowsDrivers lists installed TSC printer drivers via PowerShell.
func detectTSCWindowsDrivers() []string {
var drivers []string
cmd := exec.Command("powershell", "-NoProfile", "-Command",
"Get-PrinterDriver | Where-Object {$_.Name -like '*TSC*'} | Select-Object -ExpandProperty Name")
hideWindow(cmd)
out, err := cmd.Output()
if err != nil {
return drivers
}
for _, line := range strings.Split(string(out), "\n") {
name := strings.TrimSpace(line)
if name != "" {
drivers = append(drivers, name)
}
}
return drivers
}
// detectTSCWindowsUSB detects TSC USB devices via PnP.
func detectTSCWindowsUSB() []USBDevice {
var devices []USBDevice
cmd := exec.Command("powershell", "-NoProfile", "-Command",
"Get-PnpDevice -Class Printer -Status OK -ErrorAction SilentlyContinue | Where-Object {$_.FriendlyName -like '*TSC*'} | Select-Object -ExpandProperty FriendlyName")
hideWindow(cmd)
out, err := cmd.Output()
if err != nil {
// Fallback: check all USB devices for vendor 1203
cmd2 := exec.Command("powershell", "-NoProfile", "-Command",
"Get-PnpDevice -Status OK -ErrorAction SilentlyContinue | Where-Object {$_.InstanceId -like '*VID_1203*'} | Select-Object -ExpandProperty FriendlyName")
hideWindow(cmd2)
out, err = cmd2.Output()
if err != nil {
return devices
}
}
for _, line := range strings.Split(string(out), "\n") {
name := strings.TrimSpace(line)
if name != "" {
devices = append(devices, USBDevice{
VendorID: 0x1203,
Name: name,
})
}
}
return devices
}
// detectTSCWindowsPrinters lists registered printers that appear to be TSC.
func detectTSCWindowsPrinters() []string {
var printers []string
cmd := exec.Command("powershell", "-NoProfile", "-Command",
"Get-Printer | Where-Object {$_.Name -like '*TSC*' -or $_.DriverName -like '*TSC*'} | Select-Object -ExpandProperty Name")
hideWindow(cmd)
out, err := cmd.Output()
if err != nil {
return printers
}
for _, line := range strings.Split(string(out), "\n") {
name := strings.TrimSpace(line)
if name != "" {
printers = append(printers, name)
}
}
return printers
}
// runDriverSetup executes a driver setup action on Windows.
func runDriverSetup(action string) (map[string]any, error) {
switch action {
case "register":
return registerTSCPrinterWindows()
case "download":
return map[string]any{
"status": "redirect",
"download_url": tscDriverURLWindows,
"message": "Descargue los drivers desde el sitio oficial de TSC",
}, nil
case "full-setup":
return registerTSCPrinterWindows()
default:
return nil, fmt.Errorf("unknown action: %s", action)
}
}
// registerTSCPrinterWindows registers a TSC printer in the Windows spooler.
func registerTSCPrinterWindows() (map[string]any, error) {
// Find an available TSC driver
drivers := detectTSCWindowsDrivers()
if len(drivers) == 0 {
return nil, fmt.Errorf("no TSC driver installed. Descargue el driver desde: %s", tscDriverURLWindows)
}
driverName := drivers[0]
printerName := "TSC-TDP-244-Plus"
// Find available USB port
portName := findTSCUSBPort()
if portName == "" {
portName = "USB001" // fallback
}
log.Printf("[driver] Registering TSC printer: name=%s driver=%s port=%s", printerName, driverName, portName)
cmd := exec.Command("powershell", "-NoProfile", "-Command",
fmt.Sprintf(`Add-Printer -Name "%s" -DriverName "%s" -PortName "%s" -ErrorAction Stop`, printerName, driverName, portName))
hideWindow(cmd)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("Add-Printer failed: %v — %s", err, string(output))
}
log.Printf("[driver] TSC printer registered successfully: %s", printerName)
return map[string]any{
"status": "registered",
"printer": printerName,
"driver": driverName,
"port": portName,
"message": "Impresora TSC registrada correctamente.",
}, nil
}
// findTSCUSBPort finds the USB port where a TSC printer is connected.
func findTSCUSBPort() string {
cmd := exec.Command("powershell", "-NoProfile", "-Command",
"Get-PrinterPort | Where-Object {$_.Name -like 'USB*'} | Select-Object -First 1 -ExpandProperty Name")
hideWindow(cmd)
out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}