cli mode
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
"image/color"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
@@ -14,10 +17,11 @@ import (
|
|||||||
"github.com/google/gousb"
|
"github.com/google/gousb"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
type Config struct {
|
||||||
VendorID = 0xFFFF
|
TargetURL string `json:"target_url"`
|
||||||
ProductID = 0x0035
|
VendorID uint16 `json:"vendor_id"`
|
||||||
)
|
ProductID uint16 `json:"product_id"`
|
||||||
|
}
|
||||||
|
|
||||||
var hidMap = map[byte]string{
|
var hidMap = map[byte]string{
|
||||||
4: "a", 5: "b", 6: "c", 7: "d", 8: "e", 9: "f", 10: "g", 11: "h", 12: "i",
|
4: "a", 5: "b", 6: "c", 7: "d", 8: "e", 9: "f", 10: "g", 11: "h", 12: "i",
|
||||||
@@ -33,27 +37,56 @@ type BridgeApp struct {
|
|||||||
logList *widget.List
|
logList *widget.List
|
||||||
logs []string
|
logs []string
|
||||||
window fyne.Window
|
window fyne.Window
|
||||||
|
config Config
|
||||||
|
isCLI bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadConfig() Config {
|
||||||
|
conf := Config{
|
||||||
|
TargetURL: "https://scanner.sekidesu.xyz/scan",
|
||||||
|
VendorID: 0xFFFF,
|
||||||
|
ProductID: 0x0035,
|
||||||
|
}
|
||||||
|
file, err := os.ReadFile("config.json")
|
||||||
|
if err == nil {
|
||||||
|
json.Unmarshal(file, &conf)
|
||||||
|
} else {
|
||||||
|
// Create default config if missing
|
||||||
|
data, _ := json.MarshalIndent(conf, "", " ")
|
||||||
|
os.WriteFile("config.json", data, 0644)
|
||||||
|
}
|
||||||
|
return conf
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
a := app.New()
|
cliMode := flag.Bool("cli", false, "Run in CLI mode without GUI")
|
||||||
w := a.NewWindow("POS Hardware Bridge (Go)")
|
flag.Parse()
|
||||||
|
|
||||||
|
conf := loadConfig()
|
||||||
bridge := &BridgeApp{
|
bridge := &BridgeApp{
|
||||||
urlEntry: widget.NewEntry(),
|
config: conf,
|
||||||
status: canvas.NewText("Status: Booting...", color.Black),
|
isCLI: *cliMode,
|
||||||
window: w,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bridge.status.TextSize = 14
|
if *cliMode {
|
||||||
bridge.urlEntry.SetText("https://scanner.sekidesu.xyz/scan")
|
fmt.Println("Running in CLI mode...")
|
||||||
|
bridge.usbListenLoop()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
a := app.New()
|
||||||
|
w := a.NewWindow("POS Hardware Bridge (Go)")
|
||||||
|
bridge.window = w
|
||||||
|
bridge.urlEntry = widget.NewEntry()
|
||||||
|
bridge.urlEntry.SetText(conf.TargetURL)
|
||||||
|
bridge.status = canvas.NewText("Status: Booting...", color.Black)
|
||||||
|
bridge.status.TextSize = 14
|
||||||
|
|
||||||
// UI Layout
|
|
||||||
bridge.logList = widget.NewList(
|
bridge.logList = widget.NewList(
|
||||||
func() int { return len(bridge.logs) },
|
func() int { return len(bridge.logs) },
|
||||||
func() fyne.CanvasObject { return widget.NewLabel("template") },
|
func() fyne.CanvasObject { return widget.NewLabel("template") },
|
||||||
func(i widget.ListItemID, o fyne.CanvasObject) {
|
func(i widget.ListItemID, o fyne.CanvasObject) {
|
||||||
o.(*widget.Label).SetText(bridge.logs[len(bridge.logs)-1-i])
|
o.(*widget.Label).SetText(bridge.logs[i])
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -72,14 +105,20 @@ func main() {
|
|||||||
w.Resize(fyne.NewSize(500, 400))
|
w.Resize(fyne.NewSize(500, 400))
|
||||||
|
|
||||||
go bridge.usbListenLoop()
|
go bridge.usbListenLoop()
|
||||||
|
|
||||||
w.ShowAndRun()
|
w.ShowAndRun()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BridgeApp) addLog(msg string) {
|
func (b *BridgeApp) addLog(msg string) {
|
||||||
fyne.DoAndWait(func() {
|
|
||||||
ts := time.Now().Format("15:04:05")
|
ts := time.Now().Format("15:04:05")
|
||||||
b.logs = append([]string{fmt.Sprintf("[%s] %s", ts, msg)}, b.logs...)
|
formatted := fmt.Sprintf("[%s] %s", ts, msg)
|
||||||
|
|
||||||
|
if b.isCLI {
|
||||||
|
fmt.Println(formatted)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fyne.DoAndWait(func() {
|
||||||
|
b.logs = append([]string{formatted}, b.logs...)
|
||||||
if len(b.logs) > 15 {
|
if len(b.logs) > 15 {
|
||||||
b.logs = b.logs[:15]
|
b.logs = b.logs[:15]
|
||||||
}
|
}
|
||||||
@@ -87,10 +126,27 @@ func (b *BridgeApp) addLog(msg string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BridgeApp) updateStatus(msg string, col color.Color) {
|
||||||
|
if b.isCLI {
|
||||||
|
fmt.Printf("STATUS: %s\n", msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fyne.DoAndWait(func() {
|
||||||
|
b.status.Text = msg
|
||||||
|
b.status.Color = col
|
||||||
|
b.status.Refresh()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BridgeApp) sendToPos(barcode string) {
|
func (b *BridgeApp) sendToPos(barcode string) {
|
||||||
b.addLog(fmt.Sprintf("Captured: %s. Sending...", barcode))
|
url := b.config.TargetURL
|
||||||
|
if !b.isCLI {
|
||||||
|
url = b.urlEntry.Text
|
||||||
|
}
|
||||||
|
|
||||||
|
b.addLog(fmt.Sprintf("Captured: %s. Sending to %s", barcode, url))
|
||||||
client := http.Client{Timeout: 3 * time.Second}
|
client := http.Client{Timeout: 3 * time.Second}
|
||||||
resp, err := client.Get(b.urlEntry.Text + "?content=" + barcode)
|
resp, err := client.Get(url + "?content=" + barcode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.addLog("HTTP Error: Backend unreachable")
|
b.addLog("HTTP Error: Backend unreachable")
|
||||||
return
|
return
|
||||||
@@ -104,23 +160,15 @@ func (b *BridgeApp) usbListenLoop() {
|
|||||||
defer ctx.Close()
|
defer ctx.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
dev, err := ctx.OpenDeviceWithVIDPID(gousb.ID(VendorID), gousb.ID(ProductID))
|
dev, err := ctx.OpenDeviceWithVIDPID(gousb.ID(b.config.VendorID), gousb.ID(b.config.ProductID))
|
||||||
|
|
||||||
if err != nil || dev == nil {
|
if err != nil || dev == nil {
|
||||||
fyne.DoAndWait(func() {
|
b.updateStatus("Scanner unplugged. Waiting...", color.NRGBA{200, 0, 0, 255})
|
||||||
b.status.Text = "Status: Scanner unplugged. Waiting..."
|
|
||||||
b.status.Color = color.NRGBA{R: 200, G: 0, B: 0, A: 255}
|
|
||||||
b.status.Refresh()
|
|
||||||
})
|
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fyne.DoAndWait(func() {
|
b.updateStatus("Scanner Locked & Ready", color.NRGBA{0, 180, 0, 255})
|
||||||
b.status.Text = "Status: Scanner Locked & Ready"
|
|
||||||
b.status.Color = color.NRGBA{R: 0, G: 180, B: 0, A: 255}
|
|
||||||
b.status.Refresh()
|
|
||||||
})
|
|
||||||
|
|
||||||
intf, done, err := dev.DefaultInterface()
|
intf, done, err := dev.DefaultInterface()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -151,10 +199,9 @@ func (b *BridgeApp) usbListenLoop() {
|
|||||||
for {
|
for {
|
||||||
n, err := inEp.Read(buf)
|
n, err := inEp.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// This usually happens when the device is unplugged
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if n < 3 {
|
if n < 3 || buf[2] == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,15 +209,9 @@ func (b *BridgeApp) usbListenLoop() {
|
|||||||
keycode := buf[2]
|
keycode := buf[2]
|
||||||
isShift := (modifier == 2 || modifier == 32)
|
isShift := (modifier == 2 || modifier == 32)
|
||||||
|
|
||||||
if keycode == 0 {
|
if keycode == 40 {
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if keycode == 40 { // Enter
|
|
||||||
if currentBarcode != "" {
|
if currentBarcode != "" {
|
||||||
// Capture the barcode to avoid race conditions
|
go b.sendToPos(currentBarcode)
|
||||||
code := currentBarcode
|
|
||||||
go b.sendToPos(code)
|
|
||||||
currentBarcode = ""
|
currentBarcode = ""
|
||||||
}
|
}
|
||||||
} else if val, ok := hidMap[keycode]; ok {
|
} else if val, ok := hidMap[keycode]; ok {
|
||||||
|
|||||||
Reference in New Issue
Block a user