delimiter character setup + extended api data
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -16,15 +17,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Port string `json:"port"`
|
Port string `json:"port"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
BaudRate int `json:"baud"`
|
BaudRate int `json:"baud"`
|
||||||
|
Delimiter string `json:"delimiter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultConfig = Config{
|
var defaultConfig = Config{
|
||||||
Port: "/dev/ttyACM0",
|
Port: "/dev/ttyACM0",
|
||||||
URL: "https://scanner.sekidesu.xyz/scan",
|
URL: "https://scanner.sekidesu.xyz/scan",
|
||||||
BaudRate: 115200,
|
BaudRate: 115200,
|
||||||
|
Delimiter: "\n",
|
||||||
}
|
}
|
||||||
|
|
||||||
const configPath = "config.json"
|
const configPath = "config.json"
|
||||||
@@ -35,12 +38,14 @@ func main() {
|
|||||||
portName := flag.String("port", cfg.Port, "Serial port name")
|
portName := flag.String("port", cfg.Port, "Serial port name")
|
||||||
endpoint := flag.String("url", cfg.URL, "Target URL endpoint")
|
endpoint := flag.String("url", cfg.URL, "Target URL endpoint")
|
||||||
baudRate := flag.Int("baud", cfg.BaudRate, "Baud rate")
|
baudRate := flag.Int("baud", cfg.BaudRate, "Baud rate")
|
||||||
|
delim := flag.String("delim", cfg.Delimiter, "Line delimiter")
|
||||||
save := flag.Bool("save", false, "Save current parameters to config.json")
|
save := flag.Bool("save", false, "Save current parameters to config.json")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
cfg.Port = *portName
|
cfg.Port = *portName
|
||||||
cfg.URL = *endpoint
|
cfg.URL = *endpoint
|
||||||
cfg.BaudRate = *baudRate
|
cfg.BaudRate = *baudRate
|
||||||
|
cfg.Delimiter = *delim
|
||||||
|
|
||||||
if *save {
|
if *save {
|
||||||
saveConfig(cfg)
|
saveConfig(cfg)
|
||||||
@@ -60,10 +65,24 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer port.Close()
|
defer port.Close()
|
||||||
|
|
||||||
fmt.Printf("Listening on %s (Baud: %d)...\n", cfg.Port, cfg.BaudRate)
|
fmt.Printf("Listening on %s (Baud: %d, Delim: %q)...\n", cfg.Port, cfg.BaudRate, cfg.Delimiter)
|
||||||
fmt.Printf("Sending data to: %s\n", cfg.URL)
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(port)
|
scanner := bufio.NewScanner(port)
|
||||||
|
|
||||||
|
// Custom split function to handle the configurable delimiter
|
||||||
|
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||||
|
if atEOF && len(data) == 0 {
|
||||||
|
return 0, nil, nil
|
||||||
|
}
|
||||||
|
if i := bytes.Index(data, []byte(cfg.Delimiter)); i >= 0 {
|
||||||
|
return i + len(cfg.Delimiter), data[0:i], nil
|
||||||
|
}
|
||||||
|
if atEOF {
|
||||||
|
return len(data), data, nil
|
||||||
|
}
|
||||||
|
return 0, nil, nil
|
||||||
|
})
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
content := strings.TrimSpace(scanner.Text())
|
content := strings.TrimSpace(scanner.Text())
|
||||||
if content != "" {
|
if content != "" {
|
||||||
@@ -93,6 +112,10 @@ func loadConfig() Config {
|
|||||||
if err := decoder.Decode(&cfg); err != nil {
|
if err := decoder.Decode(&cfg); err != nil {
|
||||||
return defaultConfig
|
return defaultConfig
|
||||||
}
|
}
|
||||||
|
// Handle case where JSON exists but field is missing
|
||||||
|
if cfg.Delimiter == "" {
|
||||||
|
cfg.Delimiter = "\n"
|
||||||
|
}
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user