A relatively functional v0.1

This commit is contained in:
Adriano Caloiaro 2020-07-16 16:47:01 -06:00
parent 0c1093519d
commit bfe265e71e
No known key found for this signature in database
GPG key ID: 9FFD0E7601F166AB
3 changed files with 74 additions and 31 deletions

37
main.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/acaloiaro/roam-location/receiver"
"github.com/acaloiaro/roam-location/service"
)
var dbPath string
func init() {
var ok bool
if dbPath, ok = os.LookupEnv("ROAM_LOCATION_DB_PATH"); !ok {
log.Fatalf("ROAM_LOCATION_DB_PATH environment variable not set. Please set it before running")
}
}
func main() {
modePtr := flag.String("mode", "listener", "set application mode to either 'listener' (listening for NMEA sentences) or 'service' (providing current coordinates)")
flag.Parse()
switch *modePtr {
case "listener":
fmt.Println("Listener mode")
receiver.Receive(dbPath)
case "service":
fmt.Println("Service mode")
service.Listen(dbPath)
}
}

View file

@ -1,4 +1,4 @@
package main
package receiver
import (
"fmt"
@ -10,13 +10,19 @@ import (
"github.com/adrianmo/go-nmea"
)
func main() {
listen()
}
var dbFile *os.File
var err error
func listen() {
// Receive receives NMEA sentences from the Sierra Wireless RV55 router and writes parsed sentences to
// ROAM_LOCATION_DB_PATH
func Receive(dbPath string) {
addr, _ := net.ResolveUDPAddr("udp", ":22335")
sock, _ := net.ListenUDP("udp", addr)
dbFile, err = os.OpenFile(dbPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("unable to open DB file: '%v'", err)
}
defer dbFile.Close()
for {
buf := make([]byte, 2048)
@ -51,16 +57,8 @@ func handlePacket(buf []byte, rlen int) {
}
func appendLog(lat, lon float64, nmeaSentence string) {
dbPath := os.Getenv("ROAM_LOCATION_DB_PATH")
f, err := os.OpenFile(dbPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
row := fmt.Sprintf("%f,%f,%s\n", lat, lon, nmeaSentence)
if _, err := f.WriteString(row); err != nil {
log.Println(err)
if _, err := dbFile.WriteString(row); err != nil {
log.Println("unable to write to database:", err)
}
}

View file

@ -1,4 +1,4 @@
package main
package service
import (
"encoding/json"
@ -10,29 +10,39 @@ import (
"strings"
)
var port = 22495
// Listen listens for location requests from clients and return my current coordinates from the db located at dbPath
// my last known location is always the tail end of the file
func Listen(dbPath string) {
http.HandleFunc("/current_location", listenHandler(dbPath))
log.Printf("Listening: 0.0.0.0:%d", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
func handler(w http.ResponseWriter, r *http.Request) {
location := currentLocation()
byteArray, err := json.MarshalIndent(location, "", " ")
if err != nil {
log.Fatalf("error providing current location: %v", err)
}
func listenHandler(dbPath string) (handler func(http.ResponseWriter, *http.Request)) {
return func(w http.ResponseWriter, r *http.Request) {
location := currentLocation(dbPath)
byteArray, err := json.MarshalIndent(location, "", " ")
if err != nil {
log.Fatalf("error providing current location: %v", err)
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, string(byteArray))
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, string(byteArray))
}
}
func currentLocation() (location Location) {
dbPath := os.Getenv("ROAM_LOCATION_DB_PATH")
func currentLocation(dbPath string) (location Location) {
var err error
fileHandle, err := os.Open(dbPath)
if err != nil {
panic("Cannot open file")
log.Fatalf("unable to open db file: %v", err)
}
defer fileHandle.Close()
@ -71,6 +81,4 @@ func currentLocation() (location Location) {
}
func main() {
http.HandleFunc("/current_location", handler)
log.Fatal(http.ListenAndServe(":22495", nil))
}