commit 2021-12-17]09:17:23

This commit is contained in:
Godopu 2021-12-17 09:17:26 +09:00
parent 66c1674539
commit 418b5ebad4
3 changed files with 37 additions and 55 deletions

View File

@ -13,7 +13,7 @@ import (
func registerToServer() { func registerToServer() {
var obj map[string]string = make(map[string]string) var obj map[string]string = make(map[string]string)
obj["name"] = "devicemanagerb" obj["name"] = "devicemanagera"
obj["addr"] = constants.MyIP + ":3000" obj["addr"] = constants.MyIP + ":3000"
b, err := json.Marshal(obj) b, err := json.Marshal(obj)

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"devicemanagerb/constants" "devicemanagerb/constants"
"encoding/json" "encoding/json"
"io/ioutil"
"log" "log"
"net/http" "net/http"
@ -29,21 +28,16 @@ func NewRouter() http.Handler {
} }
// sensing data per device // sensing data per device
var s_data = map[string]interface{}{} var state = map[string]interface{}{}
// status from sensor // status from sensor
func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) { func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
b, err := ioutil.ReadAll(r.Body) _status := map[string]interface{}{}
if err != nil { decoder := json.NewDecoder(r.Body)
w.WriteHeader(http.StatusBadRequest) err := decoder.Decode(&_status)
w.Write([]byte(err.Error()))
return
}
status := map[string]interface{}{}
err = json.Unmarshal(b, &status)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
@ -57,21 +51,29 @@ func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
return return
} }
s_data[did] = status // state[did] = _status
log.Println(s_data) // log.Println(state[did])
cdata, ok := c_data[did] _, ok = state[did]
if !ok { if !ok {
w.Write([]byte("I am devicemanagerB")) state[did] = _status
} else { }
encoder := json.NewEncoder(w)
err := encoder.Encode(cdata) b, err := json.Marshal(state[did])
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return 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)) req, err := http.NewRequest("PUT", "http://"+constants.ServerAddr+":3000/device/"+did, bytes.NewReader(b))
if err != nil { if err != nil {
@ -82,11 +84,8 @@ func PutStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
var c_data = map[string]interface{}{}
func GetStatusHandle(w http.ResponseWriter, r *http.Request) { func GetStatusHandle(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
did, ok := vars["id"] did, ok := vars["id"]
@ -95,39 +94,30 @@ func GetStatusHandle(w http.ResponseWriter, r *http.Request) {
return return
} }
status, ok := c_data[did] _status, ok := state[did]
if !ok { if !ok {
status = map[string]interface{}{ _status = map[string]interface{}{
"servo": 0, "msg": "hello world",
"fan": 0,
"light": 0,
} }
} }
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
err := encoder.Encode(status) err := encoder.Encode(_status)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
return return
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func PostStatusChangedHandle(w http.ResponseWriter, r *http.Request) { func PostStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
b, err := ioutil.ReadAll(r.Body) _status := map[string]interface{}{}
if err != nil { decoder := json.NewDecoder(r.Body)
w.WriteHeader(http.StatusBadRequest) err := decoder.Decode(&_status)
w.Write([]byte(err.Error()))
return
}
status := map[string]interface{}{}
err = json.Unmarshal(b, &status)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error())) w.Write([]byte(err.Error()))
@ -141,8 +131,8 @@ func PostStatusChangedHandle(w http.ResponseWriter, r *http.Request) {
return return
} }
c_data[did] = status state[did] = _status
log.Println(c_data) log.Println(_status)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

View File

@ -83,15 +83,6 @@ func GetDeviceWatch(w http.ResponseWriter, r *http.Request) {
return 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) { if !checkDid(did) {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("not exist did")) w.Write([]byte("not exist did"))
@ -99,9 +90,10 @@ func GetDeviceWatch(w http.ResponseWriter, r *http.Request) {
} }
s := make(chan map[string]interface{}) s := make(chan map[string]interface{})
ec := make(chan error)
defer close(s) defer close(s)
ec := make(chan error)
defer close(ec)
watch, ok := watchers[did] watch, ok := watchers[did]
if !ok { if !ok {