commit 2021-11-18]00:11:43

This commit is contained in:
Godopu 2021-11-18 00:11:54 +09:00
parent 2c046ebb32
commit 8834e3986d
6 changed files with 296 additions and 13 deletions

View File

@ -0,0 +1,235 @@
{
"info": {
"_postman_id": "6f0d9b87-1c8c-475e-9498-0786a8a423b4",
"name": "ETRI-SMARTFARM-POC",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "1. (post) controller-A",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"cname\": \"controller-A\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{server}}/controllers",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"controllers"
]
}
},
"response": []
},
{
"name": "2. (post) controller-B",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"cname\": \"controller-B\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{server}}/controllers",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"controllers"
]
}
},
"response": []
},
{
"name": "3. (post) device-A",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"dname\": \"device-A\",\n \"type\": \"sensor\",\n \"cid\": \"{{cid-A}}\",\n \"sname\": \"devicemanagera\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{server}}/devices",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"devices"
]
}
},
"response": []
},
{
"name": "4. (post) device-B",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"dname\": \"device-A\",\n \"type\": \"sensor\",\n \"cid\": \"{{cid-A}}\",\n \"sname\": \"devicemanagera\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{server}}/devices",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"devices"
]
}
},
"response": []
},
{
"name": "5. (post) device-C",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"dname\": \"device-A\",\n \"type\": \"sensor\",\n \"cid\": \"{{cid-A}}\",\n \"sname\": \"devicemanagera\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{server}}/devices",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"devices"
]
}
},
"response": []
},
{
"name": "6. (post) device-D",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"dname\": \"device-A\",\n \"type\": \"sensor\",\n \"cid\": \"{{cid-A}}\",\n \"sname\": \"devicemanagerb\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{server}}/devices",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"devices"
]
}
},
"response": []
},
{
"name": "7. (post) device-E",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"dname\": \"device-A\",\n \"type\": \"sensor\",\n \"cid\": \"{{cid-A}}\",\n \"sname\": \"devicemanagerb\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://{{server}}/devices",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"devices"
]
}
},
"response": []
},
{
"name": "8. (get) Request-to-ServiceA",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{server}}/services/{{sid-A}}",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"services",
"{{sid-A}}"
]
}
},
"response": []
},
{
"name": "9. (get) Request-to-ServiceB",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://{{server}}/{{sid-A}}",
"protocol": "http",
"host": [
"{{server}}"
],
"path": [
"{{sid-A}}"
]
}
},
"response": []
}
]
}

View File

@ -0,0 +1,34 @@
{
"id": "80fd9b0c-0696-4c64-810c-4ba804f822b4",
"name": "POC-ENV",
"values": [
{
"key": "server",
"value": "localhost:3000",
"enabled": true
},
{
"key": "cid-A",
"value": "562f71b4-1267-49bf-ba55-2d00add1f0f6",
"enabled": true
},
{
"key": "cid-B",
"value": "23f65d70-78d4-4b17-b0ac-906fe01f3090",
"enabled": true
},
{
"key": "sid-A",
"value": "8e8f6de3-94e7-4fe0-a0e9-8d473674aced",
"enabled": true
},
{
"key": "sid-B",
"value": "7a43133f-9c01-46f9-9fd9-9e8f157ec604",
"enabled": true
}
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2021-11-17T03:10:47.279Z",
"_postman_exported_using": "Postman/8.12.5"
}

BIN
dump.db

Binary file not shown.

View File

@ -26,6 +26,6 @@ type DBHandler interface {
} }
func NewDBHandler(dbtype, path string) (DBHandler, error) { func NewDBHandler(dbtype, path string) (DBHandler, error) {
// return newSqliteHandler(path) return newSqliteHandler(path)
return newPostgresqlHandler(path) // return newPostgresqlHandler(path)
} }

View File

@ -77,6 +77,7 @@ func GetControllerList(w http.ResponseWriter, r *http.Request) {
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
} }
fmt.Println(list) fmt.Println(list)
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
@ -85,6 +86,7 @@ func GetControllerList(w http.ResponseWriter, r *http.Request) {
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
} }
} }
@ -96,6 +98,7 @@ func PostController(w http.ResponseWriter, r *http.Request) {
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
} }
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
@ -104,6 +107,7 @@ func PostController(w http.ResponseWriter, r *http.Request) {
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
} }
sendNotification(&Notification{Msg: "Added Controller"}) sendNotification(&Notification{Msg: "Added Controller"})
@ -116,14 +120,19 @@ var upgrader = websocket.Upgrader{
} }
var notifications []chan *Notification var notifications []chan *Notification
var notiMutex sync.Mutex
func sendNotification(noti *Notification) { func sendNotification(noti *Notification) {
// notiMutex.Lock()
// defer notiMutex.Unlock()
for _, ch := range notifications { for _, ch := range notifications {
ch <- noti ch <- noti
} }
} }
func removeNotification(noti chan *Notification) { func removeNotification(noti chan *Notification) {
notiMutex.Lock()
defer notiMutex.Unlock()
for i, e := range notifications { for i, e := range notifications {
if e == noti { if e == noti {
discoveredDevices[i] = discoveredDevices[len(discoveredDevices)-1] discoveredDevices[i] = discoveredDevices[len(discoveredDevices)-1]
@ -133,11 +142,14 @@ func removeNotification(noti chan *Notification) {
} }
func GetNotification(w http.ResponseWriter, r *http.Request) { func GetNotification(w http.ResponseWriter, r *http.Request) {
notiChan := make(chan *Notification, 1) notiChan := make(chan *Notification, 1)
notiMutex.Lock()
notifications = append(notifications, notiChan) notifications = append(notifications, notiChan)
notiMutex.Unlock()
conn, err := upgrader.Upgrade(w, r, nil) 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())) w.Write([]byte(err.Error()))
return
} }
for { for {
@ -146,7 +158,6 @@ func GetNotification(w http.ResponseWriter, r *http.Request) {
if conn.WriteJSON(notification) != nil { if conn.WriteJSON(notification) != nil {
log.Println(err) log.Println(err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
removeNotification(notiChan) removeNotification(notiChan)
notiChan = nil notiChan = nil
return return
@ -156,11 +167,14 @@ 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) noti := make(chan string, 1)
mutex.Lock()
discoveredNotifications = append(discoveredNotifications, noti) discoveredNotifications = append(discoveredNotifications, noti)
mutex.Unlock()
conn, err := upgrader.Upgrade(w, r, nil) 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())) w.Write([]byte(err.Error()))
return
} }
for { for {
@ -221,24 +235,25 @@ func PostDevice(w http.ResponseWriter, r *http.Request) {
mutex.Lock() mutex.Lock()
waitPermission[device.DID] = make(chan bool) waitPermission[device.DID] = make(chan bool)
discoveredDevices = append(discoveredDevices, device) discoveredDevices = append(discoveredDevices, device)
mutex.Unlock()
for _, noti := range discoveredNotifications { for _, noti := range discoveredNotifications {
noti <- device.DID noti <- device.DID
} }
mutex.Unlock()
timer := time.NewTimer(3 * time.Second) timer := time.NewTimer(20 * time.Second)
select { select {
case <-timer.C: case <-timer.C:
mutex.Lock() mutex.Lock()
delete(waitPermission, device.DID) delete(waitPermission, device.DID)
removeDevice(device) removeDevice(device)
mutex.Unlock()
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
case b := <-waitPermission[device.DID]: case b := <-waitPermission[device.DID]:
if b { if b {
// 디바이스 등록 절차 수행 // 디바이스 등록 절차 수행
@ -251,19 +266,20 @@ func PostDevice(w http.ResponseWriter, r *http.Request) {
mutex.Lock() mutex.Lock()
delete(waitPermission, device.DID) delete(waitPermission, device.DID)
removeDevice(device) removeDevice(device)
mutex.Unlock()
for _, noti := range discoveredNotifications { for _, noti := range discoveredNotifications {
noti <- device.DID noti <- device.DID
} }
mutex.Unlock()
} else { } else {
mutex.Lock() mutex.Lock()
delete(waitPermission, device.DID) delete(waitPermission, device.DID)
removeDevice(device) removeDevice(device)
mutex.Unlock()
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"))
} }
@ -278,7 +294,6 @@ func PutDevice(w http.ResponseWriter, r *http.Request) {
err := decoder.Decode(&msg) err := decoder.Decode(&msg)
fmt.Println("msg[did]: ", msg["did"]) fmt.Println("msg[did]: ", msg["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()))
@ -290,7 +305,6 @@ func PutDevice(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Wrong Device ID is Sended")) w.Write([]byte("Wrong Device ID is Sended"))
return return
} }
ch <- true ch <- true
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

BIN
server Executable file

Binary file not shown.