commit 2021-11-25]10:11:43
This commit is contained in:
parent
8834e3986d
commit
d36d7a8653
@ -20,6 +20,7 @@ func getIP() string {
|
|||||||
|
|
||||||
return addrs[0].String()
|
return addrs[0].String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerToServer() {
|
func registerToServer() {
|
||||||
var exist bool
|
var exist bool
|
||||||
server_addr, exist = os.LookupEnv("SERVER_ADDR")
|
server_addr, exist = os.LookupEnv("SERVER_ADDR")
|
||||||
@ -45,7 +46,6 @@ func registerToServer() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -23,6 +23,8 @@ type DBHandler interface {
|
|||||||
AddService(name string) error
|
AddService(name string) error
|
||||||
UpdateService(name, addr string) (*Service, error)
|
UpdateService(name, addr string) (*Service, error)
|
||||||
GetAddr(sid string) (string, error)
|
GetAddr(sid string) (string, error)
|
||||||
|
GetSID(name string) (string, error)
|
||||||
|
IsExistService(name string) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDBHandler(dbtype, path string) (DBHandler, error) {
|
func NewDBHandler(dbtype, path string) (DBHandler, error) {
|
||||||
|
@ -52,6 +52,13 @@ func (s *dbHandler) AddService(name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *dbHandler) IsExistService(name string) bool {
|
||||||
|
var service Service
|
||||||
|
tx := s.db.Select("sid").First(&service, "sname=?", name)
|
||||||
|
|
||||||
|
return tx.Error == nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *dbHandler) UpdateService(name, addr string) (*Service, error) {
|
func (s *dbHandler) UpdateService(name, addr string) (*Service, error) {
|
||||||
tx := s.db.Model(&Service{}).Where("sname = ?", name).Updates(Service{SName: name, SID: uuid.NewString(), Addr: addr})
|
tx := s.db.Model(&Service{}).Where("sname = ?", name).Updates(Service{SName: name, SID: uuid.NewString(), Addr: addr})
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
@ -63,6 +70,16 @@ func (s *dbHandler) UpdateService(name, addr string) (*Service, error) {
|
|||||||
return &service, nil
|
return &service, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *dbHandler) GetSID(name string) (string, error) {
|
||||||
|
var service Service
|
||||||
|
tx := s.db.Select("sid").First(&service, "sname=?", name)
|
||||||
|
if tx.Error != nil {
|
||||||
|
return "", tx.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return service.SID, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *dbHandler) GetAddr(sid string) (string, error) {
|
func (s *dbHandler) GetAddr(sid string) (string, error) {
|
||||||
var service Service
|
var service Service
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ func PostController(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
err = encoder.Encode(controller)
|
err = encoder.Encode(controller)
|
||||||
|
|
||||||
@ -166,27 +166,41 @@ func GetNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
||||||
noti := make(chan string, 1)
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
mutex.Lock()
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||||
discoveredNotifications = append(discoveredNotifications, noti)
|
devices, _, err := db.GetDevices()
|
||||||
mutex.Unlock()
|
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte(err.Error()))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
encoder := json.NewEncoder(w)
|
||||||
<-noti
|
encoder.Encode(devices)
|
||||||
// fmt.Println("Write!!", discoveredDevices)
|
|
||||||
if conn.WriteJSON(discoveredDevices) != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func GetDiscoveredDevices(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// noti := make(chan string, 1)
|
||||||
|
// mutex.Lock()
|
||||||
|
// discoveredNotifications = append(discoveredNotifications, noti)
|
||||||
|
// mutex.Unlock()
|
||||||
|
// conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
// if err != nil {
|
||||||
|
// w.WriteHeader(http.StatusBadRequest)
|
||||||
|
// w.Write([]byte(err.Error()))
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for {
|
||||||
|
// <-noti
|
||||||
|
// // fmt.Println("Write!!", discoveredDevices)
|
||||||
|
// if conn.WriteJSON(discoveredDevices) != nil {
|
||||||
|
// log.Println(err)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
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
|
||||||
@ -225,32 +239,49 @@ func PostDevice(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 등록된 제어기로 부터 전송된 요청 메시지임을 확인
|
||||||
if !db.IsExistController(device.CID) {
|
if !db.IsExistController(device.CID) {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("Wrong Controller ID"))
|
w.Write([]byte("Wrong Controller ID"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 장치 ID 생성 및 탐색된 장치 추가
|
||||||
device.DID = uuid.NewString()
|
device.DID = uuid.NewString()
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
waitPermission[device.DID] = make(chan bool)
|
waitPermission[device.DID] = make(chan bool)
|
||||||
discoveredDevices = append(discoveredDevices, device)
|
discoveredDevices = append(discoveredDevices, device)
|
||||||
|
|
||||||
|
// 관리자에게 탐색을 알림
|
||||||
for _, noti := range discoveredNotifications {
|
for _, noti := range discoveredNotifications {
|
||||||
noti <- device.DID
|
noti <- device.DID
|
||||||
}
|
}
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
|
|
||||||
timer := time.NewTimer(20 * time.Second)
|
timer := time.NewTimer(20 * time.Second)
|
||||||
select {
|
select {
|
||||||
case <-timer.C:
|
case <-r.Context().Done():
|
||||||
|
fmt.Println("Done!!")
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
|
delete(waitPermission, device.DID)
|
||||||
|
removeDevice(device)
|
||||||
|
|
||||||
|
for _, noti := range discoveredNotifications {
|
||||||
|
noti <- device.DID
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte("This operation is not permitted"))
|
||||||
|
return
|
||||||
|
case <-timer.C:
|
||||||
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
delete(waitPermission, device.DID)
|
delete(waitPermission, device.DID)
|
||||||
removeDevice(device)
|
removeDevice(device)
|
||||||
|
|
||||||
for _, noti := range discoveredNotifications {
|
for _, noti := range discoveredNotifications {
|
||||||
noti <- device.DID
|
noti <- device.DID
|
||||||
}
|
}
|
||||||
mutex.Unlock()
|
|
||||||
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
|
||||||
@ -262,24 +293,25 @@ func PostDevice(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// 디바이스 등록 알림
|
// 디바이스 등록 알림
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
json.NewEncoder(w).Encode(device)
|
||||||
sendNotification(&Notification{Msg: "Added Device"})
|
sendNotification(&Notification{Msg: "Added Device"})
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
delete(waitPermission, device.DID)
|
delete(waitPermission, device.DID)
|
||||||
removeDevice(device)
|
removeDevice(device)
|
||||||
|
|
||||||
for _, noti := range discoveredNotifications {
|
for _, noti := range discoveredNotifications {
|
||||||
noti <- device.DID
|
noti <- device.DID
|
||||||
}
|
}
|
||||||
mutex.Unlock()
|
|
||||||
} else {
|
} else {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
delete(waitPermission, device.DID)
|
delete(waitPermission, device.DID)
|
||||||
removeDevice(device)
|
removeDevice(device)
|
||||||
|
|
||||||
for _, noti := range discoveredNotifications {
|
for _, noti := range discoveredNotifications {
|
||||||
noti <- device.DID
|
noti <- device.DID
|
||||||
}
|
}
|
||||||
mutex.Unlock()
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("This operation is not permitted"))
|
w.Write([]byte("This operation is not permitted"))
|
||||||
}
|
}
|
||||||
@ -312,6 +344,24 @@ func PutDevice(w http.ResponseWriter, r *http.Request) {
|
|||||||
func GetServices(w http.ResponseWriter, r *http.Request) {
|
func GetServices(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||||
|
|
||||||
|
if r.ContentLength != 0 {
|
||||||
|
obj := map[string]string{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(&obj)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sid, err := db.GetSID(obj["sname"])
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write([]byte(sid))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
l, err := db.GetServices()
|
l, err := db.GetServices()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
@ -362,6 +412,7 @@ func PostService(w http.ResponseWriter, r *http.Request) {
|
|||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
err := decoder.Decode(&obj)
|
err := decoder.Decode(&obj)
|
||||||
|
|
||||||
|
db.IsExistService(obj["name"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
@ -376,9 +427,6 @@ func PostService(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
|
||||||
// result := containermgmt.CreateContainer(context.Background(), cont)
|
|
||||||
// b := result.Value(containermgmt.ReturnKey).([]byte)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RouteRequestToService(w http.ResponseWriter, r *http.Request) {
|
func RouteRequestToService(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Loading…
Reference in New Issue
Block a user