2016-05-14 10:31:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-05-14 11:21:10 +00:00
|
|
|
"encoding/json"
|
2016-05-29 19:41:58 +00:00
|
|
|
"fmt"
|
2016-05-14 11:21:10 +00:00
|
|
|
"github.com/FreifunkBremen/respond-collector/models"
|
2016-05-29 19:41:58 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"net/http"
|
2016-05-14 10:31:43 +00:00
|
|
|
)
|
2016-05-14 11:21:10 +00:00
|
|
|
|
2016-07-09 17:14:04 +00:00
|
|
|
// 7 nachkommerstellen sollten genug sein (7cm genau)
|
|
|
|
// http://blog.3960.org/post/7309573249/genauigkeit-bei-geo-koordinaten
|
|
|
|
|
|
|
|
const GEOROUND = 0.0000001
|
|
|
|
|
|
|
|
func geoEqual(a, b float64) bool {
|
|
|
|
if (a-b) < GEOROUND && (b-a) < GEOROUND {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-14 10:31:43 +00:00
|
|
|
type ApiAliases struct {
|
2016-05-14 11:21:10 +00:00
|
|
|
aliases *models.Aliases
|
2016-05-29 19:41:58 +00:00
|
|
|
config *models.Config
|
|
|
|
nodes *models.Nodes
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|
2016-05-14 11:21:10 +00:00
|
|
|
|
2016-05-29 19:41:58 +00:00
|
|
|
func NewAliases(config *models.Config, router *httprouter.Router, prefix string, nodes *models.Nodes) {
|
2016-05-14 11:21:10 +00:00
|
|
|
api := &ApiAliases{
|
|
|
|
aliases: models.NewAliases(config),
|
2016-05-29 19:41:58 +00:00
|
|
|
nodes: nodes,
|
|
|
|
config: config,
|
2016-05-14 11:21:10 +00:00
|
|
|
}
|
|
|
|
router.GET(prefix, api.GetAll)
|
|
|
|
router.GET(prefix+"/ansible", api.AnsibleDiff)
|
2016-05-16 10:24:50 +00:00
|
|
|
router.GET(prefix+"/cleanup", api.Cleanup)
|
2016-05-29 19:41:58 +00:00
|
|
|
router.GET(prefix+"/auth", BasicAuth(api.Cleanup, []byte(config.Webserver.Api.Passphrase)))
|
|
|
|
router.GET(prefix+"/alias/:nodeid", api.GetOne)
|
|
|
|
router.POST(prefix+"/alias/:nodeid", BasicAuth(api.SaveOne, []byte(config.Webserver.Api.Passphrase)))
|
2016-05-16 10:24:50 +00:00
|
|
|
}
|
2016-05-29 19:41:58 +00:00
|
|
|
|
2016-05-17 08:37:15 +00:00
|
|
|
// clean up the aliases by correct values in nodes
|
2016-05-29 19:41:58 +00:00
|
|
|
func (api *ApiAliases) cleaner() {
|
|
|
|
for key, alias := range api.aliases.List {
|
|
|
|
if node := api.nodes.List[key]; node != nil {
|
2016-06-18 22:01:12 +00:00
|
|
|
//counter for the diffrent attribute
|
|
|
|
count := 0
|
2016-05-29 19:41:58 +00:00
|
|
|
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
|
2016-07-09 17:14:04 +00:00
|
|
|
if len(alias.Hostname) > 0 {
|
|
|
|
count += 1
|
|
|
|
if alias.Hostname == nodeinfo.Hostname {
|
2016-05-29 20:49:40 +00:00
|
|
|
count -= 1
|
2016-07-09 17:14:04 +00:00
|
|
|
alias.Hostname = ""
|
2016-05-29 20:49:40 +00:00
|
|
|
}
|
2016-07-09 17:14:04 +00:00
|
|
|
}
|
|
|
|
if len(alias.Owner) > 0 {
|
|
|
|
count += 1
|
|
|
|
if nodeinfo.Owner != nil && alias.Owner == nodeinfo.Owner.Contact {
|
2016-05-29 20:49:40 +00:00
|
|
|
count -= 1
|
2016-07-09 17:14:04 +00:00
|
|
|
alias.Owner = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if alias.Location != nil {
|
|
|
|
count += 2
|
|
|
|
if nodeinfo.Location != nil {
|
|
|
|
if geoEqual(alias.Location.Latitude, nodeinfo.Location.Latitude) {
|
|
|
|
count -= 1
|
|
|
|
if geoEqual(alias.Location.Longtitude, nodeinfo.Location.Longtitude) {
|
|
|
|
count -= 1
|
|
|
|
alias.Location = nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if geoEqual(alias.Location.Longtitude, nodeinfo.Location.Longtitude) {
|
|
|
|
count -= 1
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 20:49:40 +00:00
|
|
|
}
|
2016-05-29 19:41:58 +00:00
|
|
|
}
|
2016-06-29 00:14:34 +00:00
|
|
|
if nodeinfo.Wireless != nil && alias.Wireless != nil {
|
2016-06-28 22:04:33 +00:00
|
|
|
count += 4
|
|
|
|
if alias.Wireless.Channel24 == nodeinfo.Wireless.Channel24 {
|
|
|
|
count -= 1
|
|
|
|
}
|
|
|
|
if alias.Wireless.TxPower24 == nodeinfo.Wireless.TxPower24 {
|
|
|
|
count -= 1
|
2016-05-29 19:41:58 +00:00
|
|
|
}
|
2016-06-28 22:04:33 +00:00
|
|
|
if alias.Wireless.Channel5 == nodeinfo.Wireless.Channel5 {
|
|
|
|
count -= 1
|
|
|
|
}
|
|
|
|
if alias.Wireless.TxPower5 == nodeinfo.Wireless.TxPower5 {
|
|
|
|
count -= 1
|
2016-05-29 19:41:58 +00:00
|
|
|
}
|
2016-07-09 17:14:04 +00:00
|
|
|
if alias.Wireless.Channel24 == nodeinfo.Wireless.Channel24 && alias.Wireless.TxPower24 == nodeinfo.Wireless.TxPower24 && alias.Wireless.Channel5 == nodeinfo.Wireless.Channel5 && alias.Wireless.TxPower5 == nodeinfo.Wireless.TxPower5 {
|
|
|
|
alias.Wireless = nil
|
|
|
|
}
|
2016-05-17 08:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-09 17:14:04 +00:00
|
|
|
|
2016-06-18 22:01:12 +00:00
|
|
|
//delete element
|
|
|
|
if count <= 0 {
|
|
|
|
delete(api.aliases.List, key)
|
2016-07-09 17:14:04 +00:00
|
|
|
fmt.Print("[api] node updated '", key, "'\n")
|
2016-06-18 22:01:12 +00:00
|
|
|
}
|
2016-05-17 08:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|
|
|
|
func (api *ApiAliases) GetAll(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2016-05-29 19:41:58 +00:00
|
|
|
jsonOutput(w, r, api.aliases.List)
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|
2016-05-14 11:21:10 +00:00
|
|
|
|
2016-05-14 10:31:43 +00:00
|
|
|
func (api *ApiAliases) GetOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
2016-05-29 19:41:58 +00:00
|
|
|
if alias := api.aliases.List[ps.ByName("nodeid")]; alias != nil {
|
|
|
|
jsonOutput(w, r, alias)
|
2016-05-14 11:21:10 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-29 19:41:58 +00:00
|
|
|
fmt.Fprint(w, "Not found: ", ps.ByName("nodeid"), "\n")
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|
2016-05-14 11:21:10 +00:00
|
|
|
|
2016-05-14 10:31:43 +00:00
|
|
|
func (api *ApiAliases) SaveOne(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
2016-05-14 11:21:10 +00:00
|
|
|
var alias models.Alias
|
|
|
|
|
2016-05-16 10:24:50 +00:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&alias)
|
2016-05-29 19:41:58 +00:00
|
|
|
if err != nil {
|
2016-05-14 11:21:10 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2016-05-29 19:41:58 +00:00
|
|
|
fmt.Fprint(w, "Decode: ", ps.ByName("nodeid"), "\n")
|
2016-05-14 11:21:10 +00:00
|
|
|
return
|
|
|
|
}
|
2016-05-29 19:41:58 +00:00
|
|
|
api.aliases.Update(ps.ByName("nodeid"), &alias)
|
2016-07-09 17:14:04 +00:00
|
|
|
fmt.Print("[api] node updated '", ps.ByName("nodeid"), "'\n")
|
2016-05-29 19:41:58 +00:00
|
|
|
jsonOutput(w, r, alias)
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|
2016-05-14 11:21:10 +00:00
|
|
|
|
2016-05-16 10:24:50 +00:00
|
|
|
func (api *ApiAliases) Cleanup(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
api.cleaner()
|
2016-05-29 19:41:58 +00:00
|
|
|
jsonOutput(w, r, api.aliases.List)
|
2016-05-16 10:24:50 +00:00
|
|
|
}
|
2016-05-14 10:31:43 +00:00
|
|
|
func (api *ApiAliases) AnsibleDiff(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
2016-07-09 17:14:04 +00:00
|
|
|
fmt.Print("[api] ansible\n")
|
2016-05-16 10:24:50 +00:00
|
|
|
api.cleaner()
|
2016-05-29 19:41:58 +00:00
|
|
|
jsonOutput(w, r, models.GenerateAnsible(api.nodes, api.aliases.List))
|
2016-05-14 10:31:43 +00:00
|
|
|
}
|