commit 2021-12-17]09:17:23
This commit is contained in:
parent
66c1674539
commit
418b5ebad4
@ -13,7 +13,7 @@ import (
|
||||
func registerToServer() {
|
||||
|
||||
var obj map[string]string = make(map[string]string)
|
||||
obj["name"] = "devicemanagerb"
|
||||
obj["name"] = "devicemanagera"
|
||||
obj["addr"] = constants.MyIP + ":3000"
|
||||
|
||||
b, err := json.Marshal(obj)
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"devicemanagerb/constants"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@ -29,21 +28,16 @@ func NewRouter() http.Handler {
|
||||
}
|
||||
|
||||
// sensing data per device
|
||||
var s_data = map[string]interface{}{}
|
||||
var state = map[string]interface{}{}
|
||||
|
||||
// status from sensor
|
||||
func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
_status := map[string]interface{}{}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&_status)
|
||||
|
||||
status := map[string]interface{}{}
|
||||
err = json.Unmarshal(b, &status)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
@ -57,21 +51,29 @@ func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s_data[did] = status
|
||||
log.Println(s_data)
|
||||
// state[did] = _status
|
||||
// log.Println(state[did])
|
||||
|
||||
cdata, ok := c_data[did]
|
||||
_, ok = state[did]
|
||||
if !ok {
|
||||
w.Write([]byte("I am devicemanagerB"))
|
||||
} else {
|
||||
encoder := json.NewEncoder(w)
|
||||
err := encoder.Encode(cdata)
|
||||
state[did] = _status
|
||||
}
|
||||
|
||||
b, err := json.Marshal(state[did])
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
req, err := http.NewRequest("PUT", "http://"+constants.ServerAddr+":3000/device/"+did, bytes.NewReader(b))
|
||||
if err != nil {
|
||||
@ -82,11 +84,8 @@ func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var c_data = map[string]interface{}{}
|
||||
|
||||
func GetStatusHandle(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
did, ok := vars["id"]
|
||||
@ -95,39 +94,30 @@ func GetStatusHandle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
status, ok := c_data[did]
|
||||
_status, ok := state[did]
|
||||
if !ok {
|
||||
status = map[string]interface{}{
|
||||
"servo": 0,
|
||||
"fan": 0,
|
||||
"light": 0,
|
||||
_status = map[string]interface{}{
|
||||
"msg": "hello world",
|
||||
}
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(w)
|
||||
|
||||
err := encoder.Encode(status)
|
||||
err := encoder.Encode(_status)
|
||||
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)
|
||||
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
status := map[string]interface{}{}
|
||||
err = json.Unmarshal(b, &status)
|
||||
_status := map[string]interface{}{}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&_status)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
@ -141,8 +131,8 @@ func PostStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
c_data[did] = status
|
||||
log.Println(c_data)
|
||||
state[did] = _status
|
||||
log.Println(_status)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
@ -83,15 +83,6 @@ func GetDeviceWatch(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// payload := map[string]interface{}{}
|
||||
// decoder := json.NewDecoder(r.Body)
|
||||
// err = decoder.Decode(&payload)
|
||||
// if err != nil {
|
||||
// w.WriteHeader(http.StatusBadRequest)
|
||||
// w.Write([]byte(err.Error()))
|
||||
// return
|
||||
// }
|
||||
|
||||
if !checkDid(did) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("not exist did"))
|
||||
@ -99,9 +90,10 @@ func GetDeviceWatch(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
s := make(chan map[string]interface{})
|
||||
ec := make(chan error)
|
||||
|
||||
defer close(s)
|
||||
ec := make(chan error)
|
||||
defer close(ec)
|
||||
|
||||
watch, ok := watchers[did]
|
||||
|
||||
if !ok {
|
||||
|
Loading…
Reference in New Issue
Block a user