commit 2021-12-01]09:21:33
This commit is contained in:
parent
d36d7a8653
commit
3ade3a2d62
@ -165,20 +165,6 @@ func GetNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
|
||||||
devices, _, err := db.GetDevices()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
|
||||||
encoder.Encode(devices)
|
|
||||||
}
|
|
||||||
|
|
||||||
// func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
// func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
||||||
// noti := make(chan string, 1)
|
// noti := make(chan string, 1)
|
||||||
// mutex.Lock()
|
// mutex.Lock()
|
||||||
@ -203,8 +189,15 @@ func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var waitPermission = map[string]chan bool{}
|
var waitPermission = map[string]chan bool{}
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
var discoveredDevices []*model.Device
|
var discoveredDevices = []*model.Device{}
|
||||||
var discoveredNotifications []chan string
|
|
||||||
|
func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||||
|
|
||||||
|
encoder := json.NewEncoder(w)
|
||||||
|
encoder.Encode(discoveredDevices)
|
||||||
|
}
|
||||||
|
|
||||||
func removeDevice(device *model.Device) {
|
func removeDevice(device *model.Device) {
|
||||||
for i, e := range discoveredDevices {
|
for i, e := range discoveredDevices {
|
||||||
@ -253,23 +246,19 @@ func PostDevice(w http.ResponseWriter, r *http.Request) {
|
|||||||
discoveredDevices = append(discoveredDevices, device)
|
discoveredDevices = append(discoveredDevices, device)
|
||||||
|
|
||||||
// 관리자에게 탐색을 알림
|
// 관리자에게 탐색을 알림
|
||||||
for _, noti := range discoveredNotifications {
|
sendNotification(&Notification{Msg: "Add discovered device"})
|
||||||
noti <- device.DID
|
|
||||||
}
|
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
|
|
||||||
timer := time.NewTimer(20 * time.Second)
|
timer := time.NewTimer(20 * time.Second)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-r.Context().Done():
|
case <-r.Context().Done():
|
||||||
fmt.Println("Done!!")
|
fmt.Println("^^")
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
delete(waitPermission, device.DID)
|
delete(waitPermission, device.DID)
|
||||||
removeDevice(device)
|
removeDevice(device)
|
||||||
|
sendNotification(&Notification{Msg: "Remove discovered device"})
|
||||||
for _, noti := range discoveredNotifications {
|
|
||||||
noti <- device.DID
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("This operation is not permitted"))
|
w.Write([]byte("This operation is not permitted"))
|
||||||
return
|
return
|
||||||
@ -278,10 +267,7 @@ func PostDevice(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
delete(waitPermission, device.DID)
|
delete(waitPermission, device.DID)
|
||||||
removeDevice(device)
|
removeDevice(device)
|
||||||
|
sendNotification(&Notification{Msg: "Remove discovered device"})
|
||||||
for _, noti := range discoveredNotifications {
|
|
||||||
noti <- device.DID
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("This operation is not permitted"))
|
w.Write([]byte("This operation is not permitted"))
|
||||||
return
|
return
|
||||||
@ -299,19 +285,13 @@ func PostDevice(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
delete(waitPermission, device.DID)
|
delete(waitPermission, device.DID)
|
||||||
removeDevice(device)
|
removeDevice(device)
|
||||||
|
sendNotification(&Notification{Msg: "Add device"})
|
||||||
for _, noti := range discoveredNotifications {
|
|
||||||
noti <- device.DID
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
delete(waitPermission, device.DID)
|
delete(waitPermission, device.DID)
|
||||||
removeDevice(device)
|
removeDevice(device)
|
||||||
|
sendNotification(&Notification{Msg: "Remove discovered device"})
|
||||||
for _, noti := range discoveredNotifications {
|
|
||||||
noti <- device.DID
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("This operation is not permitted"))
|
w.Write([]byte("This operation is not permitted"))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user