Fix printing multiple files at once

This commit is contained in:
Wouter Groeneveld 2023-03-18 09:16:38 +01:00
parent 8730d2c29f
commit 377af1bf7a
1 changed files with 21 additions and 23 deletions

44
main.go
View File

@ -50,6 +50,10 @@ func main() {
} }
must("Enable Bluetooth stack", adapter.Enable()) must("Enable Bluetooth stack", adapter.Enable())
pyfilterloc := dumpFilter(*dir)
defer func() {
os.RemoveAll(pyfilterloc)
}()
log.Println("Scanning for Bluetooth devices...") log.Println("Scanning for Bluetooth devices...")
ch := make(chan bluetooth.ScanResult, 1) ch := make(chan bluetooth.ScanResult, 1)
@ -67,7 +71,13 @@ func main() {
select { select {
case device := <-ch: case device := <-ch:
log.Println("Found device: ", device.Address.String(), device.RSSI, device.LocalName()) log.Println("Found device: ", device.Address.String(), device.RSSI, device.LocalName())
tryToPrint(device, *dir, files) for _, file := range files {
filePath := filepath.Join(*dir, file.Name())
if isPossibleToPrint(filePath) {
log.Printf("Trying to print %s\n", filePath)
tryToPrint(device, pyfilterloc, filePath)
}
}
case <-time.After(10 * time.Second): case <-time.After(10 * time.Second):
log.Fatal("Timeout trying to locate Phomemo M02, is it on? Quitting.") log.Fatal("Timeout trying to locate Phomemo M02, is it on? Quitting.")
os.Exit(1) os.Exit(1)
@ -88,14 +98,14 @@ func copyFileToFolder(file string, folder string) error {
return nil return nil
} }
func tryToPrint(phomemoAddress bluetooth.ScanResult, dir string, files []os.DirEntry) { func tryToPrint(phomemoAddress bluetooth.ScanResult, pyfilterloc string, filePath string) {
var phomemo *bluetooth.Device var phomemo *bluetooth.Device
phomemo, err := adapter.Connect(phomemoAddress.Address, bluetooth.ConnectionParams{}) phomemo, err := adapter.Connect(phomemoAddress.Address, bluetooth.ConnectionParams{})
must("failed to connect to adapter", err) must("Failed to connect to adapter", err)
log.Println("connected to ", phomemoAddress.Address.String()) log.Println("Connected to ", phomemoAddress.Address.String())
defer func() { defer func() {
if err := phomemo.Disconnect(); err != nil { if err := phomemo.Disconnect(); err != nil {
log.Println(err) log.Fatalf("Cannot disconnect: %w\n", err)
} else { } else {
log.Println("disconnected") log.Println("disconnected")
} }
@ -109,25 +119,11 @@ func tryToPrint(phomemoAddress bluetooth.ScanResult, dir string, files []os.DirE
log.Println("- service", srvc.UUID().String()) log.Println("- service", srvc.UUID().String())
chars, err := srvc.DiscoverCharacteristics(nil) chars, err := srvc.DiscoverCharacteristics(nil)
must("failed to discover characteristics of service", err) must("Failed to discover characteristics of service", err)
pyfilterloc := filepath.Join(dir, "phomemo-filter.py")
dumpFilter(pyfilterloc)
defer func() {
os.RemoveAll(pyfilterloc)
}()
// 3 characteristics to discover 0000ff01, 0000ff02, and 0000ff03 // 3 characteristics to discover 0000ff01, 0000ff02, and 0000ff03
for _, file := range files { writeData(pyfilterloc, filePath, chars[1]) // 0000ff02-...
filePath := dir + "/" + file.Name() readResult(chars[0]) // 0000ff01-...
if isPossibleToPrint(filePath) {
writeData(pyfilterloc, filePath, chars[1]) // 0000ff02-...
readResult(chars[0]) // 0000ff01-...
os.RemoveAll(filePath)
}
}
log.Println("-- DONE")
} }
func isPossibleToPrint(filename string) bool { func isPossibleToPrint(filename string) bool {
@ -137,9 +133,11 @@ func isPossibleToPrint(filename string) bool {
strings.HasSuffix(filename, ".PNG") strings.HasSuffix(filename, ".PNG")
} }
func dumpFilter(filename string) { func dumpFilter(dir string) string {
filename := filepath.Join(dir, "phomemo-filter.py")
err := os.WriteFile(filename, phomemofilter.Pyfilter, 0666) err := os.WriteFile(filename, phomemofilter.Pyfilter, 0666)
must("unable to dump python filter for use", err) must("unable to dump python filter for use", err)
return filename
} }
func writeData(pyfilterloc string, path string, char bluetooth.DeviceCharacteristic) { func writeData(pyfilterloc string, path string, char bluetooth.DeviceCharacteristic) {