Skip to content

Commit

Permalink
feat(cli): binding info from object and devices
Browse files Browse the repository at this point in the history
  • Loading branch information
vbeaucha committed Aug 9, 2023
1 parent 07fc637 commit 4bd2e15
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 44 deletions.
8 changes: 4 additions & 4 deletions ARANGO_API/handlers/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/gin-gonic/gin"
)

// swagger:operation GET /Devices Devices Devices
// swagger:operation GET /devices Devices Devices
// Get Devices list
//
// ---
Expand Down Expand Up @@ -61,7 +61,7 @@ func GetDevices(c *gin.Context) {
c.IndentedJSON(http.StatusOK, devices)
}

// swagger:operation GET /Devices/ConnecteTo/{device} Devices GetDevicesConnectedTo
// swagger:operation GET /devices/ConnecteTo/{device} Devices GetDevicesConnectedTo
// Get Devices connected to a device
//
// ---
Expand Down Expand Up @@ -120,7 +120,7 @@ func GetDevicesConnectedTo(c *gin.Context) {
c.IndentedJSON(http.StatusOK, devices)
}

// swagger:operation POST /Devices Devices CreateDevices
// swagger:operation POST /devices Devices CreateDevices
// Create new Devices
//
// ---
Expand Down Expand Up @@ -168,7 +168,7 @@ func PostDevices(c *gin.Context) {
c.IndentedJSON(http.StatusCreated, result)
}

// swagger:operation DELETE /Devices/{device} Devices DeleteDevices
// swagger:operation DELETE /devices/{device} Devices DeleteDevices
// Delete Devices by key
//
// ---
Expand Down
8 changes: 4 additions & 4 deletions ARANGO_API/services/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func InitRouter(db driver.Database, addr string) *gin.Engine {
router.Use(DBMiddleware(db, addr))
proteted := router.Group("/api")
//proteted.Use(JwtAuthMiddleware())
proteted.GET("/Devices", handlers.GetDevices)
proteted.POST("/Devices", handlers.PostDevices)
proteted.DELETE("/Devices/:key", handlers.DeleteDevice)
proteted.GET("/Devices/ConnecteTo/:key", handlers.GetDevicesConnectedTo)
proteted.GET("/:devices", handlers.GetDevices)
proteted.POST("/:devices", handlers.PostDevices)
proteted.DELETE(":devices/:key", handlers.DeleteDevice)
proteted.GET(":devices/ConnecteTo/:key", handlers.GetDevicesConnectedTo)

proteted.GET("/Connections", handlers.GetConnection)
proteted.POST("/Connections", handlers.PostConnection)
Expand Down
6 changes: 3 additions & 3 deletions ARANGO_API/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
}
}
},
"/Devices": {
"/devices": {
"get": {
"security": [
{
Expand Down Expand Up @@ -247,7 +247,7 @@
}
}
},
"/Devices/ConnecteTo/{device}": {
"/devices/ConnecteTo/{device}": {
"get": {
"security": [
{
Expand Down Expand Up @@ -312,7 +312,7 @@
}
}
},
"/Devices/{device}": {
"/devices/{device}": {
"delete": {
"security": [
{
Expand Down
89 changes: 89 additions & 0 deletions BFF/controllers/devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package controllers

import (

"net/http"
"ogree-bff/utils/token"

"github.com/gin-gonic/gin"
)



func DeviceBindingObject(c *gin.Context,database string) {

objAttr:= c.Param("objAttr")

obj := c.Param("obj")

//MONGO Check
mongoURL, ok := c.Value("mongo").(string)
if !ok {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Failed to get api connection"})
return
}
key := token.ExtractToken(c)
mongoResp,err := Send("GET",mongoURL+"/api/objects/"+obj,"",key,nil)
if err != nil {
if mongoResp != nil {
result := GetJSONBody(mongoResp)
c.IndentedJSON(mongoResp.StatusCode,result.Message)
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}
mongoBody:=GetJSONBody(mongoResp)
if mongoBody.StatusCode != http.StatusOK {
c.IndentedJSON(mongoBody.StatusCode,mongoBody.Message)
return
}
mongoDataResult := mongoBody.Message.(map[string]interface{})
mongoResult := mongoDataResult["data"].(map[string]interface{})
if mongoResult["category"] != "device"{
c.IndentedJSON(http.StatusBadRequest, gin.H{"message":obj +" is not a device"})
return
}
mongoAttr := mongoResult["attributes"].(map[string]interface{})
if mongoAttr[objAttr] == nil && mongoResult[objAttr] == nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message":obj +" has not attributes :" + objAttr})
return
}
var attributeNeed string
if mongoResult[objAttr] != nil {
attributeNeed = mongoResult[objAttr].(string)
}else{
attributeNeed = mongoAttr[objAttr].(string)
}

//ARANGO
deviceAttr:= c.Param("deviceAttr")
deviceURL, ok := c.Value(database).(string)
if !ok {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Failed to get api connection"})
return
}
query := GetQueryString(c)
if query == "" {
query +="?"+deviceAttr+"="+attributeNeed
}else{
query +="&"+deviceAttr+"="+attributeNeed
}

deviceResp,err := Send("GET",deviceURL+"/api/devices",query,key,nil)
if err != nil {
if deviceResp != nil {
result := GetJSONBody(deviceResp)
c.IndentedJSON(deviceResp.StatusCode,result.Message)
return
}
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}
result := GetJSONBody(deviceResp)
c.IndentedJSON(deviceResp.StatusCode, result.Message)
return



}
67 changes: 63 additions & 4 deletions BFF/handlers/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gin-gonic/gin"
)

// swagger:operation GET /Devices Devices Devices
// swagger:operation GET /devices Devices Devices
// Get Devices list
//
// ---
Expand Down Expand Up @@ -49,7 +49,7 @@ func GetDevices(c *gin.Context) {
controllers.Get(c,"arango")
}

// swagger:operation GET /Devices/ConnecteTo/{device} Devices GetDevicesConnectedTo
// swagger:operation GET /devices/ConnecteTo/{device} Devices GetDevicesConnectedTo
// Get Devices connected to a device
//
// ---
Expand Down Expand Up @@ -95,7 +95,7 @@ func GetDevices(c *gin.Context) {
func GetDevicesConnectedTo(c *gin.Context) {
controllers.Get(c,"arango")
}
// swagger:operation POST /Devices Devices CreateDevices
// swagger:operation POST /devices Devices CreateDevices
// Create new Devices
//
// ---
Expand Down Expand Up @@ -123,7 +123,7 @@ func CreateDevices(c *gin.Context) {
controllers.Post(c,"arango")
}

// swagger:operation DELETE /Devices/{device} Devices DeleteDevices
// swagger:operation DELETE /devices/{device} Devices DeleteDevices
// Delete Devices by key
//
// ---
Expand Down Expand Up @@ -240,4 +240,63 @@ func CreateConnections(c *gin.Context){
// "$ref": "#/definitions/ErrorResponse"
func DeleteConnections(c *gin.Context){
controllers.Delete(c,"arango")
}



// swagger:operation GET /devices/{obj}/objAttr/{objAttr}/deviceAttr/{deviceAttr} Devices GetDeviceBindingObject
// Get Devices list
//
// ---
// parameters:
// - name: obj
// in: path
// description: object for binding
// required: true
// type: string
// - name: objAttr
// in: path
// description: object attribute for binding
// required: true
// type: string
// - name: deviceAttr
// in: path
// description: devices attribute for binding
// required: true
// type: string
// - name: _key
// in: query
// description: Key of device
// required: false
// type: string
// - name: _name
// in: query
// description: Name of device
// required: false
// type: string
// - name: group_name
// in: query
// description: Group_name of device
// required: false
// type: string
// - name: serial
// in: query
// description: Serial number of device
// required: false
// type: string
// security:
// - Bearer: []
// responses:
// '200':
// description: successful
// schema:
// items:
// "$ref": "#/definitions/SuccessResponse"
// '500':
// description: Error
// schema:
// items:
// "$ref": "#/definitions/ErrorResponse"
func GetDeviceBindingObject(c*gin.Context){
controllers.DeviceBindingObject(c,"arango")
}
10 changes: 6 additions & 4 deletions BFF/services/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ func APIMiddleware(apiList []models.API) gin.HandlerFunc {
}
func initDevices(protected, unprotected *gin.RouterGroup){

protected.GET("/Devices",handlers.GetDevices)
protected.GET("/Devices/ConnecteTo/:key",handlers.GetDevicesConnectedTo)
protected.POST("/Devices",handlers.CreateDevices)
protected.DELETE("/Devices/:id",handlers.DeleteDevice)
protected.GET("/devices",handlers.GetDevices)
protected.GET("/devices/ConnecteTo/:key",handlers.GetDevicesConnectedTo)
protected.POST("/devices",handlers.CreateDevices)
protected.DELETE("/devices/:id",handlers.DeleteDevice)

protected.GET("/Connections",handlers.GetConnections)
protected.POST("/Connections",handlers.CreateConnections)
protected.DELETE("/Connections/:id",handlers.DeleteConnections)

protected.GET("/devices/:obj/objAttr/:objAttr/deviceAttr/:deviceAttr",handlers.GetDeviceBindingObject)
}

func initAuth(protected, unprotected *gin.RouterGroup){
Expand Down
Loading

0 comments on commit 4bd2e15

Please sign in to comment.