diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 230effa..4881256 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -16,85 +16,19 @@ - - + - - - - - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - - + + + + + + + + + + @@ -165,6 +108,14 @@ + + + + + + + + @@ -173,13 +124,23 @@ + + + + + + + + + + + - @@ -227,7 +188,14 @@ @@ -239,12 +207,17 @@ + true @@ -301,7 +275,7 @@ - + @@ -309,9 +283,9 @@ - + - + @@ -319,9 +293,9 @@ - + - + @@ -329,9 +303,9 @@ - + - + @@ -339,7 +313,7 @@ - + @@ -362,10 +336,18 @@ + + + + + + + + diff --git a/cmd/server/server.go b/cmd/server/server.go index 5e43977..dc22536 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -1,6 +1,7 @@ package main import ( +<<<<<<< Updated upstream "encoding/json" "fmt" "git.timovolkmann.de/gyrogpsc/dispatcher" @@ -17,6 +18,16 @@ import ( const ( TCP_PORT = ":3010" SERIAL_PORT = "/dev/tty.usbmodem14201" +======= + "git.timovolkmann.de/gyrogpsc/gnet" +) + +const ( + TCP_PORT = ":3010" + HTTP_PORT = ":3011" + SERIAL_PORT = "/dev/tty.usbmodem14201" + MAX_WS_DELAY = 20 +>>>>>>> Stashed changes ) var upgrader = websocket.Upgrader{} // use default options @@ -72,7 +83,7 @@ func home(w http.ResponseWriter, r *http.Request) { } func main() { - d := dispatcher.New() + d := gnet.NewDispatcher() collectRoutines(d) http.HandleFunc("/echo", echo(d)) http.HandleFunc("/", home) @@ -81,7 +92,9 @@ func main() { log.Fatal(http.ListenAndServe(":3011", nil)) } -func collectRoutines(d *dispatcher.Dispatcher) { +func collectRoutines(d *gnet.Dispatcher) { + // TODO: Hier die Sensordaten zwischenspeichern und per + // collectRoutines Serial UBX Sensor Data go serialUbxCollector(d) // collectRoutines TCP JSON Sensor Data diff --git a/cmd/server_only/server_only.go b/cmd/server_only/server_only.go new file mode 100644 index 0000000..bd77897 --- /dev/null +++ b/cmd/server_only/server_only.go @@ -0,0 +1,25 @@ +package main + +import ( + "git.timovolkmann.de/gyrogpsc/gnet" +) + +const ( + TCP_PORT = ":3010" + HTTP_PORT = ":3011" + SERIAL_PORT = "/dev/tty.usbmodem14201" + MAX_WS_DELAY = 20 +) + +func main() { + d := gnet.NewDispatcher() + collectRoutines(d) + gnet.NewHttpServer(d, HTTP_PORT) +} + +func collectRoutines(d *gnet.Dispatcher) { + // collectRoutines Serial UBX Sensor Data + // go gnet.SerialUbxCollector(d, SERIAL_PORT) + // collectRoutines TCP JSON Sensor Data + go gnet.TcpJsonCollector(d, TCP_PORT) +} diff --git a/dispatcher/dispatcher.go b/gnet/dispatcher.go similarity index 87% rename from dispatcher/dispatcher.go rename to gnet/dispatcher.go index 05bcce4..0dd7144 100644 --- a/dispatcher/dispatcher.go +++ b/gnet/dispatcher.go @@ -1,4 +1,4 @@ -package dispatcher +package gnet import ( "errors" @@ -10,7 +10,7 @@ type Dispatcher struct { counter int16 } -func New() *Dispatcher { +func NewDispatcher() *Dispatcher { fmt.Println("new dispatcher") return &Dispatcher{ listeners: make(map[int16]chan string), @@ -19,7 +19,7 @@ func New() *Dispatcher { } func (d *Dispatcher) Publish(message string) { - fmt.Println("publish to listeners", len(d.listeners)) + //fmt.Println("publish to listeners", len(d.listeners)) for _, ch := range d.listeners { ch <- message } diff --git a/gnet/net.go b/gnet/net.go new file mode 100644 index 0000000..c8981e9 --- /dev/null +++ b/gnet/net.go @@ -0,0 +1,131 @@ +package gnet + +import ( + "encoding/json" + "fmt" + "git.timovolkmann.de/gyrogpsc/serial_ubx" + "github.com/gorilla/websocket" + "github.com/tidwall/pretty" + "html/template" + "log" + "net" + "net/http" + "os" +) + +func echo(d *Dispatcher) func(w http.ResponseWriter, r *http.Request) { + var upgrader = websocket.Upgrader{} // use default options + return func(w http.ResponseWriter, r *http.Request) { + fmt.Println("upgrading to ws") + c, err := upgrader.Upgrade(w, r, nil) + if err != nil { + log.Print("upgrade:", err) + return + } + //defer c.Close() + go func() { + for { + if _, _, err := c.NextReader(); err != nil { + c.Close() + break + } + } + }() + + dispatcherId, channel := d.Subscribe() + defer d.Unsubscribe(dispatcherId) + for { + log.Println("") + //if err != nil { + // log.Println("read:", err) + // break + //} + cmsg := <-channel + err = c.WriteMessage(websocket.TextMessage, []byte(cmsg)) + if err != nil { + log.Println("write:", err) + + break + } + } + } +} + +func home(w http.ResponseWriter, r *http.Request) { + //var homeTemplate = template.Must(template.NewDispatcher("").ParseFiles("index.html")) + tpl, err := template.ParseFiles("index.html") + if err != nil { + log.Fatalln(err) + } + err = tpl.Execute(w, "ws://"+r.Host+"/echo") + if err != nil { + log.Fatalln(err) + } +} + +func NewHttpServer(d *Dispatcher, httpPort string) { + http.HandleFunc("/echo", echo(d)) + http.HandleFunc("/", home) + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(".")))) + + log.Fatal(http.ListenAndServe(httpPort, nil)) +} + +func SerialUbxCollector(d *Dispatcher, serialPort string) { + r, err := serial_ubx.Setup(serialPort) + if err != nil { + log.Fatalln(err) + } + + for { + meas, err := r.NextMeasurement() + if err != nil { + continue + } + measjson, err := json.Marshal(meas) + fmt.Println(string(pretty.Pretty(measjson))) + d.Publish(string(measjson)) + } +} + +func TcpJsonCollector(d *Dispatcher, tcpPort string) { + listener, err := net.Listen("tcp", tcpPort) + if err != nil { + fmt.Println("Error listening:", err.Error()) + os.Exit(1) + } + // Close the listener when the application closes. + defer listener.Close() + + for { + // Listen for an incoming connection. + conn, err := listener.Accept() + if err != nil { + fmt.Println("Error accepting: ", err.Error()) + os.Exit(1) + } + // Handle connections in a new goroutine. + go handleTcpJsonSensorData(conn, d) + } +} + +// Handles incoming requests. +func handleTcpJsonSensorData(conn net.Conn, d *Dispatcher) { + defer conn.Close() + // Make a buffer to hold incoming data. + for { + buf := make([]byte, 2048) + // Read the incoming connection into the buffer. + _, err := conn.Read(buf) + if err != nil { + fmt.Println("Error reading:", err.Error()) + break + } + json := pretty.Pretty(buf) + fmt.Println(string(json)) + d.Publish(string(buf)) + // Send a response back to person contacting us. + //conn.Write([]byte("success")) + // Close the connection when you're done with it. + } +} diff --git a/hyperimu.json b/hyperimu.json index 1567da8..c5b6bfa 100644 --- a/hyperimu.json +++ b/hyperimu.json @@ -63,4 +63,4 @@ -0.006618400104343891 ], "tmd3702_proximity proximity sensor": [5, 0, 0] -} +} \ No newline at end of file diff --git a/serial_ubx/serial.go b/serial_ubx/serial.go index 6201a0f..30ae70c 100644 --- a/serial_ubx/serial.go +++ b/serial_ubx/serial.go @@ -61,7 +61,7 @@ func (u *ubxReceiver) NextMeasurement() (*Measurement, error) { u.currentMeas.Position[0] = v.Lat_dege7 u.currentMeas.Position[1] = v.Lon_dege7 u.currentMeas.Position[2] = v.Height_mm - fmt.Printf("%T %v\n", *v, *v) + //fmt.Printf("%T %v\n", *v, *v) case *ublox.HnrPvt: t, err := time.Parse(time.RFC3339Nano, formatTime(v.Year_y, v.Month_month, v.Day_d, v.Hour_h, v.Min_min, v.Sec_s, v.Nano_ns)) if err != nil { @@ -71,12 +71,12 @@ func (u *ubxReceiver) NextMeasurement() (*Measurement, error) { u.currentMeas.Position[0] = v.Lat_dege7 u.currentMeas.Position[1] = v.Lon_dege7 u.currentMeas.Position[2] = v.Height_mm - fmt.Printf("%T %v\n", *v, *v) + //fmt.Printf("%T %v\n", *v, *v) case *ublox.NavAtt: u.currentMeas.Orientation[0] = v.Pitch_deg u.currentMeas.Orientation[1] = v.Roll_deg u.currentMeas.Orientation[2] = v.Heading_deg - fmt.Printf("%T %v\n", *v, *v) + //fmt.Printf("%T %v\n", *v, *v) //case *ublox.RawMessage: // //fmt.Printf("%T %v\n\n", *v, *v)