settings file for scanner

This commit is contained in:
2026-03-01 02:21:17 -03:00
parent 676f299796
commit fcb75cb5a4
2 changed files with 102 additions and 14 deletions

31
ScannerGO/build.sh Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/bash
# Define binary names
LINUX_BIN="ScannerGO-linux"
WINDOWS_BIN="ScannerGO-windows.exe"
echo "Starting build process..."
# Build for Linux (64-bit)
echo "Building for Linux..."
GOOS=linux GOARCH=amd64 go build -o "$LINUX_BIN" main.go
if [ $? -eq 0 ]; then
echo "Successfully built: $LINUX_BIN"
else
echo "Failed to build Linux binary"
exit 1
fi
# Build for Windows (64-bit)
echo "Building for Windows..."
GOOS=windows GOARCH=amd64 go build -o "$WINDOWS_BIN" main.go
if [ $? -eq 0 ]; then
echo "Successfully built: $WINDOWS_BIN"
else
echo "Failed to build Windows binary"
exit 1
fi
echo "Build complete."

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@@ -13,27 +14,53 @@ import (
"github.com/tarm/serial" "github.com/tarm/serial"
) )
type Config struct {
Port string `json:"port"`
URL string `json:"url"`
BaudRate int `json:"baud"`
}
var defaultConfig = Config{
Port: "/dev/ttyACM0",
URL: "https://scanner.sekidesu.xyz/scan",
BaudRate: 115200,
}
const configPath = "config.json"
func main() { func main() {
portName := flag.String("port", "/dev/ttyACM0", "Serial port name") cfg := loadConfig()
endpoint := flag.String("url", "https://scanner.sekidesu.xyz/scan", "Target URL endpoint")
baudRate := flag.Int("baud", 115200, "Baud rate") portName := flag.String("port", cfg.Port, "Serial port name")
endpoint := flag.String("url", cfg.URL, "Target URL endpoint")
baudRate := flag.Int("baud", cfg.BaudRate, "Baud rate")
save := flag.Bool("save", false, "Save current parameters to config.json")
flag.Parse() flag.Parse()
config := &serial.Config{ cfg.Port = *portName
Name: *portName, cfg.URL = *endpoint
Baud: *baudRate, cfg.BaudRate = *baudRate
if *save {
saveConfig(cfg)
fmt.Println("Settings saved to", configPath)
}
serialConfig := &serial.Config{
Name: cfg.Port,
Baud: cfg.BaudRate,
ReadTimeout: time.Second * 2, ReadTimeout: time.Second * 2,
} }
port, err := serial.OpenPort(config) port, err := serial.OpenPort(serialConfig)
if err != nil { if err != nil {
fmt.Printf("Error opening port %s: %v\n", *portName, err) fmt.Printf("Error opening port %s: %v\n", cfg.Port, err)
os.Exit(1) os.Exit(1)
} }
defer port.Close() defer port.Close()
fmt.Printf("Listening on %s (Baud: %d)...\n", *portName, *baudRate) fmt.Printf("Listening on %s (Baud: %d)...\n", cfg.Port, cfg.BaudRate)
fmt.Printf("Sending data to: %s\n", *endpoint) fmt.Printf("Sending data to: %s\n", cfg.URL)
buf := make([]byte, 128) buf := make([]byte, 128)
for { for {
@@ -48,25 +75,55 @@ func main() {
if n > 0 { if n > 0 {
content := strings.TrimSpace(string(buf[:n])) content := strings.TrimSpace(string(buf[:n]))
if content != "" { if content != "" {
sendToEndpoint(*endpoint, content) sendToEndpoint(cfg.URL, content)
} }
} }
} }
} }
func loadConfig() Config {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
saveConfig(defaultConfig)
return defaultConfig
}
file, err := os.Open(configPath)
if err != nil {
return defaultConfig
}
defer file.Close()
var cfg Config
decoder := json.NewDecoder(file)
if err := decoder.Decode(&cfg); err != nil {
return defaultConfig
}
return cfg
}
func saveConfig(cfg Config) {
file, err := os.Create(configPath)
if err != nil {
fmt.Printf("Failed to create/save config: %v\n", err)
return
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
encoder.Encode(cfg)
}
func sendToEndpoint(baseURL, content string) { func sendToEndpoint(baseURL, content string) {
client := &http.Client{ client := &http.Client{
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
} }
fullURL := fmt.Sprintf("%s?content=%s", baseURL, url.QueryEscape(content)) fullURL := fmt.Sprintf("%s?content=%s", baseURL, url.QueryEscape(content))
resp, err := client.Get(fullURL) resp, err := client.Get(fullURL)
if err != nil { if err != nil {
fmt.Printf("Network Error: %v\n", err) fmt.Printf("Network Error: %v\n", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
fmt.Printf("Data: [%s] | Status: %s\n", content, resp.Status) fmt.Printf("Data: [%s] | Status: %s\n", content, resp.Status)
} }