ot-browser/cmd/ot-browser/ot-browser.go

183 lines
4.2 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"gopkg.in/ini.v1"
)
type Config struct {
wwwDir string
bindAddr string
mqttRemote string
}
var config Config
var mqtt MQTTReceiver
var devices map[string]Location
func (c *Config) SetDefaults() {
c.wwwDir = ""
c.bindAddr = "127.0.0.1:8090"
c.mqttRemote = ""
}
func mqtt_recv(id string, loc Location) {
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")
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 {
return fmt.Errorf("Invalid argument: %s\n", arg)
}
}
return nil
}
func main() {
fmt.Println("ot-browser")
devices = make(map[string]Location, 0)
config.SetDefaults()
parseProgramArguments()
// Connect mqtt
if config.mqttRemote == "" {
fmt.Fprintf(os.Stderr, "No mqtt remote set\n")
os.Exit(1)
}
mqtt.Received = mqtt_recv
if err := mqtt.Connect(config.mqttRemote, "owntracks/#", "", "", "ot-browser"); err != nil {
fmt.Fprintf(os.Stderr, "mqtt error: %s\n", err)
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("/locations", handlerLocations)
http.HandleFunc("/devices/", handlerDeviceQuery)
fmt.Println("Serving: http://" + config.bindAddr)
log.Fatal(http.ListenAndServe(config.bindAddr, nil))
}
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
}
www := cfg.Section("www")
if val := www.Key("remote").String(); val != "" {
c.wwwDir = val
}
if val := mqtt.Key("bind").String(); val != "" {
c.bindAddr = val
}
return nil
}
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 handlerLocations(w http.ResponseWriter, r *http.Request) {
locations := make([]GeoJSON, 0)
for dev, loc := range devices {
point := CreatePoint(loc.Lon, loc.Lat)
point.Properties["name"] = dev
point.Properties["id"] = dev
point.Properties["time"] = fmt.Sprintf("%d", loc.Timestamp)
point.Properties["altitude"] = fmt.Sprintf("%d", loc.Alt)
point.Properties["accuracy"] = fmt.Sprintf("%d", loc.Acc)
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
}