2022-01-16 10:55:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-16 11:27:54 +00:00
|
|
|
_ "embed"
|
2022-01-16 10:55:37 +00:00
|
|
|
"encoding/json"
|
2022-01-16 12:25:40 +00:00
|
|
|
"github.com/sms77io/go-client/sms77api"
|
2022-01-16 11:27:54 +00:00
|
|
|
"html/template"
|
2022-01-16 10:55:37 +00:00
|
|
|
"net/http"
|
2022-01-16 12:25:40 +00:00
|
|
|
"os"
|
2022-01-16 12:12:21 +00:00
|
|
|
"time"
|
2022-01-16 10:55:37 +00:00
|
|
|
)
|
|
|
|
|
2022-01-16 11:39:26 +00:00
|
|
|
var lastData map[string]interface{}
|
2022-01-16 10:55:37 +00:00
|
|
|
|
2022-01-16 11:27:54 +00:00
|
|
|
//go:embed notfall.tmpl
|
|
|
|
var templateContent []byte
|
|
|
|
|
2022-01-16 12:04:33 +00:00
|
|
|
var active = false
|
2022-01-16 12:12:21 +00:00
|
|
|
var activeSince time.Time
|
2022-01-16 12:04:33 +00:00
|
|
|
var getData = false
|
|
|
|
|
2022-01-16 11:27:54 +00:00
|
|
|
|
2022-01-16 10:55:37 +00:00
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/owntrack", func(writer http.ResponseWriter, request *http.Request) {
|
2022-01-16 11:27:54 +00:00
|
|
|
var p map[string]interface{}
|
2022-01-16 10:55:37 +00:00
|
|
|
err := json.NewDecoder(request.Body).Decode(&p)
|
|
|
|
if err != nil {
|
2022-01-16 10:56:34 +00:00
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2022-01-16 10:55:37 +00:00
|
|
|
}
|
2022-01-16 11:27:54 +00:00
|
|
|
|
|
|
|
if p["_type"] == "location"{
|
|
|
|
lastData = p
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:04:33 +00:00
|
|
|
getData = true
|
2022-01-16 11:27:54 +00:00
|
|
|
|
|
|
|
writer.Write([]byte("[]"))
|
2022-01-16 10:55:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
|
2022-01-16 11:27:54 +00:00
|
|
|
templ := template.Must(template.New("page").Parse(string(templateContent)))
|
|
|
|
templ.Execute(writer, map[string]interface{}{
|
|
|
|
})
|
2022-01-16 10:55:37 +00:00
|
|
|
})
|
|
|
|
|
2022-01-16 12:12:21 +00:00
|
|
|
http.HandleFunc("/activate", func(writer http.ResponseWriter, request *http.Request) {
|
2022-01-16 12:25:40 +00:00
|
|
|
if active == true {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var klient = sms77api.New(sms77api.Options{
|
|
|
|
ApiKey: os.Getenv("sms77key"),
|
|
|
|
})
|
|
|
|
|
|
|
|
sms := sms77api.SmsBaseParams{
|
|
|
|
Text: "Something",
|
|
|
|
To: "004917655424258",
|
|
|
|
From: "Notfall-Portal"
|
|
|
|
}
|
|
|
|
klient.Sms.Text(sms77api.SmsTextParams{
|
|
|
|
SmsBaseParams: sms,
|
|
|
|
Details: false,
|
|
|
|
ReturnMessageId: false,
|
|
|
|
})
|
2022-01-16 12:12:21 +00:00
|
|
|
active = true
|
|
|
|
activeSince = time.Now()
|
|
|
|
})
|
|
|
|
|
2022-01-16 11:39:26 +00:00
|
|
|
http.HandleFunc("/json", func(writer http.ResponseWriter, request *http.Request) {
|
2022-01-16 12:04:33 +00:00
|
|
|
if getData == false || active == false {
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-01-16 12:12:21 +00:00
|
|
|
|
|
|
|
diff := time.Now().Sub(activeSince)
|
|
|
|
if diff.Minutes() > 2 {
|
|
|
|
active = false
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-16 11:41:06 +00:00
|
|
|
data := make(map[string]interface{})
|
2022-01-16 11:39:26 +00:00
|
|
|
|
|
|
|
data["lat"] = lastData["lat"]
|
|
|
|
data["lon"] = lastData["lon"]
|
|
|
|
data["tst"] = lastData["tst"]
|
2022-01-16 11:46:43 +00:00
|
|
|
data["acc"] = lastData["acc"]
|
2022-01-16 11:39:26 +00:00
|
|
|
|
|
|
|
r, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Write(r)
|
|
|
|
})
|
|
|
|
|
2022-01-16 10:55:37 +00:00
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|