Browse Source

Add the serial read functionality and run into troubles with vim

master
Aaron Fischer 5 years ago
parent
commit
145ecf4c4c
  1. 48
      agent.go

48
agent.go

@ -1,25 +1,46 @@
package main
// Create a new virtual serial device:
// socat PTY,link=/dev/ttyS10 PTY,link=/dev/ttyS11
import (
"log"
"os"
"path"
"github.com/fsnotify/fsnotify"
"github.com/tarm/serial"
"github.com/mkideal/cli"
)
// TODO: Make the error code optional (as a parameter)
func stop() {
log.Println("Have a nice day.")
// TODO: Make the error code optional (as a parameter)
os.Exit(1)
os.Exit(0)
}
func waitAndExecuteCommandsFromDevice() {
// TODO: Establish connection to the device
// TODO: Wait for action loop
// TODO: Execute action
// TODO: Wait for action loop ...
func waitAndExecuteCommandsFromDevice(device string) {
config := &serial.Config{Name: device, Baud: 115200}
serial, err := serial.OpenPort(config)
if err != nil {
log.Fatal(err)
}
log.Printf("Open the connection to the n3rdpad over %s", device)
// Read from serial
buffer := make([]byte, 128)
for {
num, err := serial.Read(buffer)
if err != nil {
log.Fatal(err)
}
// TODO: Do something with the incomind commands
log.Printf("%q", buffer[:num])
}
}
func watchForConfigChanges(configFile string) {
@ -47,6 +68,10 @@ func watchForConfigChanges(configFile string) {
// TODO: Close the connection?
}
// TODO: vim has a very strange behaviour to save a file. There is
// no WRITE event triggered. Instead: RENAME -> CHMOD -> REMOVE is
// called on that file and after that, this mechanism lost track of
// the file. Why is that tha case? Cant find a valid answer to that.
if event.Op&fsnotify.Rename == fsnotify.Rename ||
event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("The configuration file is renamed or removed. We cant do any further changes without the config file. So create the file or rename it back and restart the agent.")
@ -70,6 +95,7 @@ func watchForConfigChanges(configFile string) {
type argT struct {
cli.Helper
ConfigFile string `cli:"c,config" usage:"specify a keymap config file"`
Device string `cli:"d,device" usage:"specify the serial port, the n3rdpad is hooked to"`
}
func main() {
@ -81,12 +107,16 @@ func main() {
if args.ConfigFile != "" {
configFile = args.ConfigFile
}
if args.Device == "" {
log.Fatal("You need to specify the serial device with the --device parameter.")
stop();
}
if !args.Help {
// Start up the application
log.Println("Using the configuration file", configFile)
watchForConfigChanges(configFile)
waitAndExecuteCommandsFromDevice()
go watchForConfigChanges(configFile)
waitAndExecuteCommandsFromDevice(args.Device)
stop()
}