From: Bernhard Tittelbach Date: Sat, 5 Oct 2013 23:57:02 +0000 (+0000) Subject: uc_sensor_node_zmq forwarder X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=30c704726752c02121b3f453916a0a76dd5dcab8;p=svn42.git uc_sensor_node_zmq forwarder --- diff --git a/go/door_client_zmq/make_deploy.zsh b/go/door_client_zmq/make_deploy.zsh index 746215b..f43a274 100644 --- a/go/door_client_zmq/make_deploy.zsh +++ b/go/door_client_zmq/make_deploy.zsh @@ -4,4 +4,4 @@ export CGO_ENABLED=1 go-linux-386 clean #go-linux-386 build #strip ${PWD:t} -go-linux-386 build -ldflags "-s" && rsync -v ${PWD:t} wuzzler.realraum.at:/flash/tuer/ +go-linux-386 build -ldflags "-s" && rsync -v --progress ${PWD:t} wuzzler.realraum.at:/flash/tuer/ diff --git a/go/door_daemon_zmq/make_deploy.zsh b/go/door_daemon_zmq/make_deploy.zsh index 746215b..f43a274 100644 --- a/go/door_daemon_zmq/make_deploy.zsh +++ b/go/door_daemon_zmq/make_deploy.zsh @@ -4,4 +4,4 @@ export CGO_ENABLED=1 go-linux-386 clean #go-linux-386 build #strip ${PWD:t} -go-linux-386 build -ldflags "-s" && rsync -v ${PWD:t} wuzzler.realraum.at:/flash/tuer/ +go-linux-386 build -ldflags "-s" && rsync -v --progress ${PWD:t} wuzzler.realraum.at:/flash/tuer/ diff --git a/go/door_daemon_zmq/serial_tty.go b/go/door_daemon_zmq/serial_tty.go index 48afd33..a5528d5 100644 --- a/go/door_daemon_zmq/serial_tty.go +++ b/go/door_daemon_zmq/serial_tty.go @@ -30,6 +30,7 @@ func serialWriter(in <- chan string, serial * os.File) { serial.WriteString(totty) serial.Sync() } + close(serial) } func serialReader(out chan <- [][]byte, serial * os.File) { diff --git a/go/door_nick_lookup_zmq/make_deploy.zsh b/go/door_nick_lookup_zmq/make_deploy.zsh index 746215b..f43a274 100644 --- a/go/door_nick_lookup_zmq/make_deploy.zsh +++ b/go/door_nick_lookup_zmq/make_deploy.zsh @@ -4,4 +4,4 @@ export CGO_ENABLED=1 go-linux-386 clean #go-linux-386 build #strip ${PWD:t} -go-linux-386 build -ldflags "-s" && rsync -v ${PWD:t} wuzzler.realraum.at:/flash/tuer/ +go-linux-386 build -ldflags "-s" && rsync -v --progress ${PWD:t} wuzzler.realraum.at:/flash/tuer/ diff --git a/go/uc_sensor_node_zmq/logging.go b/go/uc_sensor_node_zmq/logging.go new file mode 100644 index 0000000..b3473ed --- /dev/null +++ b/go/uc_sensor_node_zmq/logging.go @@ -0,0 +1,31 @@ +// (c) Bernhard Tittelbach, 2013 + +package main + +import "os" +import "log" +import "log/syslog" + +type NullWriter struct {} +func (n *NullWriter) Write(p []byte) (int, error) {return len(p),nil} + +var ( + Syslog_ *log.Logger + Debug_ *log.Logger +) + +func init() { + Syslog_ = log.New(&NullWriter{}, "", 0) + Debug_ = log.New(&NullWriter{}, "", 0) +} + +func LogEnableSyslog() { + var logerr error + Syslog_, logerr = syslog.NewLogger(syslog.LOG_INFO | (18<<3), 0) + if logerr != nil { panic(logerr) } +} + +func LogEnableDebuglog() { + Syslog_ = log.New(os.Stdout, "", log.LstdFlags) + Debug_ = log.New(os.Stderr, "DEBUG ", log.LstdFlags) +} \ No newline at end of file diff --git a/go/uc_sensor_node_zmq/main.go b/go/uc_sensor_node_zmq/main.go new file mode 100644 index 0000000..3e99253 --- /dev/null +++ b/go/uc_sensor_node_zmq/main.go @@ -0,0 +1,44 @@ +// (c) Bernhard Tittelbach, 2013 + +package main + +import ( + "flag" +) + + +// ---------- Main Code ------------- + +var ( + tty_dev_ string + pub_addr string + use_syslog_ bool +) + +func init() { + flag.StringVar(&pub_addr, "brokeraddr", "tcp://torwaechter.realraum.at:4244", "zmq address to send stuff to") + flag.StringVar(&tty_dev_, "ttydev", "", "path do tty uc device") + flag.BoolVar(&use_syslog_, "syslog", false, "log to syslog local1 facility") + //~ flag.Usage = flag.PrintDefaults + flag.Parse() +} + +func main() { + zmqctx, pub_sock := ZmqsInit(pub_addr) + defer zmqctx.Close() + defer pub_sock.Close() + + if use_syslog_ { + LogEnableSyslog() + Syslog_.Print("started") + } + + serial_wr, serial_rd, err := OpenAndHandleSerial(tty_dev_) + if err != nil { panic(err) } + defer close(serial_wr) + + for incoming_ser_line := range(serial_rd) { + Syslog_.Printf("%s",incoming_ser_line) + if err := pub_sock.Send(incoming_ser_line); err != nil { panic(err) } + } +} diff --git a/go/uc_sensor_node_zmq/make_deploy.zsh b/go/uc_sensor_node_zmq/make_deploy.zsh new file mode 100644 index 0000000..70b8b99 --- /dev/null +++ b/go/uc_sensor_node_zmq/make_deploy.zsh @@ -0,0 +1,5 @@ +#!/bin/zsh +export GO386=387 +export CGO_ENABLED=1 +go-linux-386 clean +go-linux-386 build -ldflags "-s" && rsync -v --progress ${PWD:t} gw.realraum.at:/flash/home/realraum/ diff --git a/go/uc_sensor_node_zmq/serial_tty.go b/go/uc_sensor_node_zmq/serial_tty.go new file mode 100644 index 0000000..de06bdd --- /dev/null +++ b/go/uc_sensor_node_zmq/serial_tty.go @@ -0,0 +1,59 @@ +// (c) Bernhard Tittelbach, 2013 + +package main + +import ( + "bufio" + "bytes" + "os" + "svn.spreadspace.org/realraum/go.svn/termios" +) + + +// ---------- Serial TTY Code ------------- + +func openTTY(name string) (*os.File, error) { + file, err := os.OpenFile(name,os.O_RDWR, 0600) // For read access. + if err != nil { + Syslog_.Print(err.Error()) + return nil, err + } + termios.Ttyfd(file.Fd()) + termios.SetRaw() + return file, nil +} + +func serialWriter(in <- chan string, serial * os.File) { + for totty := range(in) { + serial.WriteString(totty) + serial.Sync() + } + serial.Close() +} + +func serialReader(out chan <- [][]byte, serial * os.File) { + linescanner := bufio.NewScanner(serial) + linescanner.Split(bufio.ScanLines) + for linescanner.Scan() { + if err := linescanner.Err(); err != nil { + panic(err.Error()) + } + text := bytes.Fields([]byte(linescanner.Text())) + if len(text) == 0 { + continue + } + out <- text + } +} + +func OpenAndHandleSerial(filename string) (chan string, chan [][]byte, error) { + serial, err :=openTTY(filename) + if err != nil { + return nil, nil, err + } + wr := make(chan string, 1) + rd := make(chan [][]byte, 20) + go serialWriter(wr, serial) + go serialReader(rd, serial) + return wr, rd, nil +} diff --git a/go/uc_sensor_node_zmq/zeromq.go b/go/uc_sensor_node_zmq/zeromq.go new file mode 100644 index 0000000..2cbd495 --- /dev/null +++ b/go/uc_sensor_node_zmq/zeromq.go @@ -0,0 +1,42 @@ +// (c) Bernhard Tittelbach, 2013 + +package main + +import ( + zmq "github.com/vaughan0/go-zmq" + ) + +// ---------- ZeroMQ Code ------------- + +func ZmqsInit(pub_addr string) (ctx *zmq.Context, pub_sock *zmq.Socket) { + var err error + ctx, err = zmq.NewContext() + if err != nil { + panic(err) + } + //close only on later panic, otherwise leave open: + defer func(){ if r:= recover(); r != nil { ctx.Close(); panic(r) } }() + + if len(pub_addr) > 0 { + pub_sock, err = ctx.Socket(zmq.Pub) + if err != nil { + panic(err) + } + defer func() { if r:= recover(); r != nil { pub_sock.Close(); panic(r) } }() + + if err = pub_sock.Connect(pub_addr); err != nil { + panic(err) + } + } else { + pub_sock = nil + } + + return +} + +func zmqsHandleError(chans *zmq.Channels) { + for error := range(chans.Errors()) { + chans.Close() + panic(error) + } +}