Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ jobs:
with:
submodules: recursive

- name: Setup TinyGo Bluetooth
shell: bash
run: |
chmod +x ./scripts/setup-tinygo-bluetooth.sh
./scripts/setup-tinygo-bluetooth.sh

- name: Build wails
uses: ./.github/actions
id: build
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ frontend/dist
/frontend/wailsjs/
.vscode/
package.json.md5

# Cloned from upstream by scripts/setup-tinygo-bluetooth.sh — not tracked in git
tinygo-bluetooth/
96 changes: 96 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"time"

"epos-proxy/bluetooth"
"epos-proxy/config"
"epos-proxy/logger"
"epos-proxy/printer"
Expand Down Expand Up @@ -55,6 +56,7 @@ func (a *App) startup(ctx context.Context) {

a.config = cfg
a.printerManager = printer.NewManager()
bluetooth.InitBluetoothManager(cfg)

port, err := cfg.ResolvePort()
if err != nil {
Expand All @@ -78,6 +80,8 @@ type Printer struct {
Id string `json:"id"`
IsLAN bool `json:"isLAN"`
LANIp string `json:"lanIp,omitempty"`
IsBT bool `json:"isBT"`
BTMac string `json:"btMac,omitempty"`
Online bool `json:"online"`
Type string `json:"type"`
}
Expand Down Expand Up @@ -154,6 +158,25 @@ func (a *App) Status() Status {
})
}

// Bluetooth printers from config
btPrinters := a.config.GetBluetoothPrinters()
for _, btCfg := range btPrinters {
id := printer.EncodeBluetoothPrinterID(btCfg.Address)
name := btCfg.Name
if name == "" {
name = "Bluetooth - " + btCfg.Address

}
printers = append(printers, Printer{
Id: id,
Name: name,
Ip: a.GetPrinterIp(id),
IsBT: true,
BTMac: btCfg.Address,
Type: string(printer.PrinterTypeReceipt),
})
}

return Status{
ServerRunning: a.webserver.Running(),
DefaultIp: fmt.Sprintf("127.0.0.1:%d", a.webserver.Port),
Expand Down Expand Up @@ -272,3 +295,76 @@ func (a *App) DisableAutostart() error {

return nil
}

// --- Bluetooth printer methods ---
func (a *App) ScanBluetoothPrinters() ([]bluetooth.BluetoothPrinterInfo, error) {
logger.Debug("Scanning for Bluetooth devices")
devices, err := bluetooth.ScanBluetoothPrinters()
if err != nil {
logger.Errorf("Bluetooth scan failed: %v", err)
return nil, err
}
return devices, nil
}

func (a *App) CheckBluetoothDependencies() []bluetooth.DependencyStatus {
return bluetooth.CheckDependencies()
}

func (a *App) AddBluetoothPrinter(address, name string) error {
logger.Debugf("Adding Bluetooth printer: %s (%s)", address, name)
address = bluetooth.NormalizeAddress(address)
if err := bluetooth.ValidateAddress(address); err != nil {
logger.Errorf("Invalid MAC address: %v", err)
return err
}

if err := bluetooth.BTManager.CheckBluetoothPrinter(address); err != nil {
logger.Errorf("Errow while check printer %v", err)
return err
}

if err := a.config.AddBluetoothPrinter(address, name); err != nil {
logger.Errorf("Failed to save Bluetooth printer: %v", err)
return fmt.Errorf("failed to save Bluetooth printer: %w", err)
}

logger.Debugf("Bluetooth printer added: %s (%s)", address, name)
return nil
}

func (a *App) ConfirmRemoveBluetoothPrinter(address string) (bool, error) {
logger.Debugf("Remove Bluetooth printer requested: %s", address)

result, err := wailsruntime.MessageDialog(a.ctx, wailsruntime.MessageDialogOptions{
Type: wailsruntime.QuestionDialog,
Title: "Remove Printer",
Message: fmt.Sprintf("Are you sure you want to remove the Bluetooth printer %s?", address),
Buttons: []string{"Cancel", "Confirm"},
DefaultButton: "Cancel",
CancelButton: "Cancel",
})
if err != nil {
logger.Errorf("failed to show confirmation dialog: %v", err)
return false, fmt.Errorf("failed to show confirmation dialog: %w", err)
}
if result == "Confirm" || result == "Yes" {
if err := a.config.RemoveBluetoothPrinter(address); err != nil {
logger.Errorf("Failed to remove Bluetooth printer: %v", err)
return false, fmt.Errorf("failed to remove Bluetooth printer: %v", err)
}
logger.Debugf("Bluetooth printer removed successfully")
return true, nil
}
logger.Debugf("Remove Bluetooth printer cancelled")
return false, nil
}

func (a *App) CheckBluetoothPrinterStatus(address string) bool {
logger.Debugf("Checking Bluetooth printer status: %s", address)
if err := bluetooth.BTManager.CheckBluetoothPrinter(address); err != nil {
logger.Errorf("Errow while check printer %v", err)
return false
}
return true
}
Loading