ot-browser/cmd/ot-browser/main.go
Felix Niederwanger d534856fb6
Introduce storage module
Add a storage module to store all received geopoints into a sqlite3
database.
2024-02-17 15:10:40 +01:00

312 lines
7.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"gopkg.in/ini.v1"
)
// Config contains the main program configuration
type Config struct {
wwwDir string
bindAddr string
mqttRemote string
mqttClientId string
mqttTopic string
db string
}
var config Config
var mqtt MQTTReceiver
var db Storage
var devices map[string]Location
// SetDefaults sets the configuration defaults
func (c *Config) SetDefaults() {
c.wwwDir = "./www"
c.bindAddr = "127.0.0.1:8090"
c.mqttRemote = "127.0.0.1"
c.mqttTopic = "owntracks/#"
c.mqttClientId = "ot-browser"
c.db = ""
}
func mqttRecv(id string, loc Location) {
if !loc.IsPlausible() { // Ignore stupid locations
return
}
// Write all datapoints before doing additional checks on known devices
if config.db != "" {
if err := db.InsertLocation(id, loc); err != nil {
fmt.Fprintf(os.Stderr, "error writing location to database: %s\n", err)
}
}
// Additional checks on known devices
if old, ok := devices[id]; ok {
// Ignore, if the new location is less precise, and the old one lies within it's accuracy
// This might happen, if there is a provider switch on the phone (e.g. GPX -> coarse location)
if loc.Acc > old.Acc {
distance := old.Distance(loc)
if distance <= loc.Acc {
return
}
}
}
devices[id] = loc
}
func parseProgramArguments() error {
args := os.Args[1:]
for i := 0; i < len(args); i++ {
arg := args[i]
if len(arg) == 0 {
continue
}
if arg == "-h" || arg == "--help" {
fmt.Printf("Usage: %s [OPTIONS]", os.Args[0])
fmt.Println(" -h,--help Print this help message")
fmt.Println(" -w,--www DIR Set WWW content directory")
fmt.Println(" -c,--config FILE Read configuration from the ini FILE")
fmt.Println(" -b,--bind ADDR Bind webserver to ADDR")
fmt.Println(" --mqtt ADDR Set MQTT remote address")
fmt.Println(" --clientid CLIENTID Set MQTT client id")
fmt.Println(" --db FILENAME Set storage database")
os.Exit(0)
} else if arg == "-w" || arg == "--www" {
i++
config.wwwDir = args[i]
} else if arg == "-c" || arg == "--config" {
i++
if err := config.ReadFile(args[i]); err != nil {
return err
}
} else if arg == "-b" || arg == "--bind" {
i++
config.bindAddr = args[i]
} else if arg == "--mqtt" {
i++
config.mqttRemote = args[i]
} else if arg == "--clientid" {
i++
config.mqttClientId = args[i]
} else if arg == "--db" {
i++
config.db = args[i]
} else {
return fmt.Errorf("invalid argument: %s", arg)
}
}
return nil
}
func main() {
var err error
devices = make(map[string]Location, 0)
config.SetDefaults()
parseProgramArguments()
// Setup database
if config.db != "" {
db, err = CreateDatabase(config.db)
if err != nil {
fmt.Fprintf(os.Stderr, "database error: %s\n", err)
os.Exit(1)
}
}
// Connect mqtt
if config.mqttRemote == "" {
fmt.Fprintf(os.Stderr, "No mqtt remote set\n")
os.Exit(1)
}
mqtt.Received = mqttRecv
if err = mqtt.Connect(config.mqttRemote, config.mqttTopic, "", "", config.mqttClientId); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
fmt.Fprintf(os.Stderr, "error: mqtt connection failed\n")
os.Exit(1)
}
// Setup webserver
fs := http.FileServer(http.Dir(config.wwwDir))
http.Handle("/", http.StripPrefix("/", fs))
http.HandleFunc("/devices", handlerDevices)
http.HandleFunc("/devices.json", handlerDevices)
http.HandleFunc("/health.json", handlerHealth)
http.HandleFunc("/health", handlerHealth)
http.HandleFunc("/locations", handlerLocations)
http.HandleFunc("/devices/", handlerDeviceQuery)
fmt.Println("ot-browser serving: http://" + config.bindAddr)
log.Fatal(http.ListenAndServe(config.bindAddr, nil))
}
// ReadFile reads a configuration file
func (c *Config) ReadFile(filename string) error {
cfg, err := ini.InsensitiveLoad(filename)
if err != nil {
return err
}
mqtt := cfg.Section("mqtt")
if val := mqtt.Key("remote").String(); val != "" {
c.mqttRemote = val
}
if val := mqtt.Key("clientid").String(); val != "" {
c.mqttClientId = val
}
www := cfg.Section("www")
if val := www.Key("remote").String(); val != "" {
c.wwwDir = val
}
if val := www.Key("bind").String(); val != "" {
c.bindAddr = val
}
storage := cfg.Section("storage")
if val := storage.Key("database").String(); val != "" {
c.db = val
}
return nil
}
// handlerHealth - health endpoint - writes a health status message to the client
func handlerHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"ok"}`))
}
func handlerDevices(w http.ResponseWriter, r *http.Request) {
devs := make([]string, 0)
for dev := range devices {
devs = append(devs, dev)
}
buf, err := json.Marshal(devs)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Server error"))
fmt.Fprintf(os.Stderr, "json marshal error: %s\n", err)
return
}
w.Write(buf)
}
func timeDeltastr(now int64, tsmp int64) string {
delta := now - tsmp
if delta >= 0 {
if delta < 10 {
// Practically fresh
return "now"
} else if delta < 60 {
return fmt.Sprintf("%d seconds ago", delta)
} else {
minutes := delta / 60
if minutes == 1 {
return "1 minute ago"
} else if minutes < 60 {
return fmt.Sprintf("%d minutes ago", minutes)
} else {
hours := minutes / 60
if hours == 1 {
return "1 hour ago"
} else if hours < 24 {
return fmt.Sprintf("%d hours ago", hours)
} else {
days := hours / 24
if days == 1 {
return "yesterday"
} else if days < 7 {
return fmt.Sprintf("%d days ago", days)
}
}
}
}
}
tUnix := time.Unix(tsmp, 0)
return tUnix.Format("2006-01-02-15:04:05")
}
func nameFormat(name string) string {
// Return "dev" for names like "dev/dev"
i := strings.Index(name, "/")
if i > 0 && i < (len(name)-1) {
left, right := name[:i], name[i+1:]
if left == right {
return left
}
}
return name
}
func handlerLocations(w http.ResponseWriter, r *http.Request) {
locations := make([]GeoJSON, 0)
now := time.Now().Unix()
for dev, loc := range devices {
point := CreatePoint(loc.Lon, loc.Lat)
point.Properties["name"] = nameFormat(dev)
point.Properties["id"] = dev
point.Properties["time"] = fmt.Sprintf("%d", loc.Timestamp)
point.Properties["altitude"] = fmt.Sprintf("%f", loc.Alt)
point.Properties["accuracy"] = fmt.Sprintf("%f", loc.Acc)
description := timeDeltastr(now, loc.Timestamp)
if loc.Velocity > 1 {
description += fmt.Sprintf(" %.2fkm/h", loc.Velocity)
}
if loc.Acc > 5 {
description += fmt.Sprintf(" +/- %.0fm", loc.Acc)
}
point.Properties["description"] = description
locations = append(locations, point)
}
buf, err := json.Marshal(locations)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Server error"))
fmt.Fprintf(os.Stderr, "json marshal error: %s\n", err)
return
}
w.Write([]byte("{ \"features\": "))
w.Write(buf)
w.Write([]byte("}"))
}
func handlerDeviceQuery(w http.ResponseWriter, r *http.Request) {
// extract device name from the path
path := r.URL.Path
device := ""
if i := strings.Index(path, "devices/"); i > 0 {
device = path[i+8:]
} else {
goto server_Error
}
if loc, ok := devices[device]; ok {
buf, err := json.Marshal(loc)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Server error"))
fmt.Fprintf(os.Stderr, "json marshal error: %s\n", err)
return
}
w.Write(buf)
} else {
w.WriteHeader(404)
w.Write([]byte("device not found"))
}
return
server_Error:
w.WriteHeader(500)
w.Write([]byte("Server error"))
return
}