2021-11-16 12:28:52 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
2021-12-15 08:07:52 +00:00
|
|
|
"bytes"
|
|
|
|
"devicemanagerb/constants"
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
2021-11-16 12:28:52 +00:00
|
|
|
"net/http"
|
2021-12-15 08:07:52 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/urfave/negroni"
|
2021-11-16 12:28:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewRouter() http.Handler {
|
2021-12-15 08:07:52 +00:00
|
|
|
mux := mux.NewRouter()
|
|
|
|
// mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// w.WriteHeader(http.StatusOK)
|
|
|
|
// w.Write([]byte("I am devicemanagerB"))
|
|
|
|
// })
|
|
|
|
|
|
|
|
mux.HandleFunc("/{id}", PutStatusChangedHandle).Methods("PUT")
|
|
|
|
mux.HandleFunc("/{id}", PostStatusChangedHandle).Methods("POST")
|
|
|
|
mux.HandleFunc("/{id}", GetStatusHandle).Methods("GET")
|
|
|
|
|
|
|
|
n := negroni.Classic()
|
|
|
|
n.UseHandler(mux)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// sensing data per device
|
2021-12-17 00:17:26 +00:00
|
|
|
var state = map[string]interface{}{}
|
2021-12-15 08:07:52 +00:00
|
|
|
|
|
|
|
// status from sensor
|
|
|
|
func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
_status := map[string]interface{}{}
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&_status)
|
|
|
|
|
2021-12-15 08:07:52 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
did, ok := vars["id"]
|
|
|
|
if !ok {
|
2021-12-15 08:07:52 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
// state[did] = _status
|
|
|
|
// log.Println(state[did])
|
|
|
|
|
|
|
|
_, ok = state[did]
|
2021-12-15 08:07:52 +00:00
|
|
|
if !ok {
|
2021-12-17 00:17:26 +00:00
|
|
|
state[did] = _status
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(state[did])
|
|
|
|
if err != nil {
|
2021-12-15 08:07:52 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
w.Write(b)
|
2021-12-15 08:07:52 +00:00
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
2021-12-15 08:07:52 +00:00
|
|
|
}
|
2021-12-17 00:17:26 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-12-15 08:07:52 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", "http://"+constants.ServerAddr+":3000/device/"+did, bytes.NewReader(b))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStatusHandle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
did, ok := vars["id"]
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
_status, ok := state[did]
|
2021-12-15 08:07:52 +00:00
|
|
|
if !ok {
|
2021-12-17 00:17:26 +00:00
|
|
|
_status = map[string]interface{}{
|
|
|
|
"msg": "hello world",
|
2021-12-15 08:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
err := encoder.Encode(_status)
|
2021-12-15 08:07:52 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
_status := map[string]interface{}{}
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err := decoder.Decode(&_status)
|
2021-12-15 08:07:52 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2021-11-16 12:28:52 +00:00
|
|
|
|
2021-12-15 08:07:52 +00:00
|
|
|
did, ok := vars["id"]
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-17 00:17:26 +00:00
|
|
|
state[did] = _status
|
|
|
|
log.Println(_status)
|
2021-12-15 08:07:52 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-11-16 12:28:52 +00:00
|
|
|
}
|
2021-12-15 08:07:52 +00:00
|
|
|
|
|
|
|
// status from user per device
|