From 6241c45c87bb88777f5b7de387a567217b7ecc8f Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sat, 18 Mar 2023 08:47:45 +0100 Subject: [PATCH] Added -dir and -file options to support printing a single file or custom folder --- README.md | 7 ++++++ main.go | 65 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a45f1ab..bcea1e5 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,13 @@ The script checks a folder for files (intended to be set up as a cron job), if t See `main.go`, you'll probably want to edit some variables. I hacked everything together in a day for our specific case. +To inspect the possible command-line options, use `phomemoprinter --help`: + +- `-dir [dir]` customize the dir to scan (by default `~/Downloads/phomemo`) +- `-file [file]` print only that image + +If `-file` is used, `-dir` will be ignored. + ## Launchd service To install as a `launchd` service (mac's "cron"), copy the binary to `/usr/local/bin` and the file `com.brainbaking.PhomemoPrinter.plist` to `~/Library/LaunchAgents`. Then, in a terminal, type `launchctl load com.brainbaking.PhomemoPrinter.plist`. diff --git a/main.go b/main.go index 9a3324d..5cf6cf9 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,11 @@ package main import ( phomemofilter "brainbaking.com/phomemoprinter/filter" + "flag" "log" "os" "os/exec" + "path/filepath" "strconv" "strings" "time" @@ -12,16 +14,33 @@ import ( ) var adapter = bluetooth.DefaultAdapter -var phomemoFolder = "/Downloads/phomemo" func main() { - home, _ := os.UserHomeDir() - inDir := home + phomemoFolder + var home, _ = os.UserHomeDir() + var defaultDir = filepath.Join(home, "Downloads", "phomemo") - files, err := os.ReadDir(inDir) - must(phomemoFolder + " does not exist", err) + dir := flag.String("dir", defaultDir, "Directory to pick up any images from") + file := flag.String("file", "", "Print only a specific image. If provided, -dir will be ignored.") + flag.Parse() + + if *file != "" { + log.Printf("Attempting to print file %s...\n", *file) + + *dir = filepath.Join(os.TempDir(), "phomemo") + os.Mkdir(*dir, os.ModePerm) // ignore error in case still existing + err := copyFileToFolder(*file, *dir) + if err != nil { + log.Fatalf("Cannot copy %s to %s: %w. Quitting. \n", *file, *dir, err) + os.Exit(1) + } + } else { + log.Printf("Attempting to scan dir %s for files to print...\n", *dir) + } + + files, err := os.ReadDir(*dir) + must("Open directory target", err) if len(files) == 0 { - log.Printf("No files to print found in %s, nothing to do. Quitting.\n", inDir) + log.Fatalf("No files to print found in %s, nothing to do. Quitting.\n", *dir) os.Exit(0) } @@ -29,11 +48,6 @@ func main() { log.Println("Scanning for Bluetooth devices...") ch := make(chan bluetooth.ScanResult, 1) - timeout := make(chan bool, 1) - go func() { - time.Sleep(10 * time.Second) - timeout <- true - }() go func() { err = adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) { @@ -46,13 +60,27 @@ func main() { }() select { - case device := <- ch: - log.Println("Found device: ", device.Address.String(), device.RSSI, device.LocalName()) - tryToPrint(device, inDir, files) - case <- timeout: + case device := <-ch: + log.Println("Found device: ", device.Address.String(), device.RSSI, device.LocalName()) + tryToPrint(device, *dir, files) + case <-time.After(10 * time.Second): log.Fatal("Timeout trying to locate Phomemo M02, is it on? Quitting.") + os.Exit(1) + } +} + +func copyFileToFolder(file string, folder string) error { + contents, err := os.ReadFile(file) + if err != nil { + return err } + err = os.WriteFile(filepath.Join(folder, "toprint.jpg"), contents, os.ModePerm) + if err != nil { + return err + } + + return nil } func tryToPrint(phomemoAddress bluetooth.ScanResult, dir string, files []os.DirEntry) { @@ -78,7 +106,7 @@ func tryToPrint(phomemoAddress bluetooth.ScanResult, dir string, files []os.DirE chars, err := srvc.DiscoverCharacteristics(nil) must("failed to discover characteristics of service", err) - pyfilterloc := dir + "/" + "phomemo-filter.py" + pyfilterloc := filepath.Join(dir, "phomemo-filter.py") dumpFilter(pyfilterloc) defer func() { os.RemoveAll(pyfilterloc) @@ -89,7 +117,7 @@ func tryToPrint(phomemoAddress bluetooth.ScanResult, dir string, files []os.DirE filePath := dir + "/" + file.Name() if isPossibleToPrint(filePath) { writeData(pyfilterloc, filePath, chars[1]) // 0000ff02-... - readResult(chars[0]) // 0000ff01-... + readResult(chars[0]) // 0000ff01-... os.RemoveAll(filePath) } } @@ -144,6 +172,7 @@ func readResult(char bluetooth.DeviceCharacteristic) { func must(action string, err error) { if err != nil { - panic("failed to " + action + ": " + err.Error()) + log.Fatalf("Fatal error: Failed to "+action+": %w \n", err) + os.Exit(1) } }