From f4d088decd697694430a93c62eb6308565cf911a Mon Sep 17 00:00:00 2001 From: Klagarge Date: Tue, 15 Apr 2025 01:03:57 +0200 Subject: [PATCH] feat(gateway): small adaption for softweng project Signed-off-by: Klagarge --- docker-compose.yml | 2 ++ gateway/src/ProcessUpdate.go | 2 +- gateway/src/PublishCommand.go | 13 ++++++--- gateway/src/RequestInflux.go | 51 ++++++++++++++++----------------- gateway/src/docs/docs.go | 53 +++++++++++++++-------------------- gateway/src/docs/swagger.json | 53 +++++++++++++++-------------------- gateway/src/docs/swagger.yaml | 42 ++++++++++++--------------- 7 files changed, 100 insertions(+), 116 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index b4f056c..2b14b10 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,8 @@ services: restart: unless-stopped ports: - "8080:8080" + networks: + - kb28_default environment: - INFLUXDB_TOKEN=$INFLUXDB_TOKEN - INFLUXDB_ORG=$INFLUXDB_ORG diff --git a/gateway/src/ProcessUpdate.go b/gateway/src/ProcessUpdate.go index c61dfaf..ce0fd05 100644 --- a/gateway/src/ProcessUpdate.go +++ b/gateway/src/ProcessUpdate.go @@ -15,7 +15,7 @@ import ( type dataPoints = map[string]interface{} type updateEvent struct { - State string `json:"state"` + //State string `json:"state"` DataPoints dataPoints `json:"values"` } diff --git a/gateway/src/PublishCommand.go b/gateway/src/PublishCommand.go index 087a648..41e8b6b 100644 --- a/gateway/src/PublishCommand.go +++ b/gateway/src/PublishCommand.go @@ -20,17 +20,22 @@ type Command struct { // @Accept json // @Produce json // @Security BasicAuth -// @Param room query string true "Room name" example(Garage) -// @Param device query string true "Device name" example(Door) +// @Param user query string true "User name" example(remi) +// @Param room query string true "Room name" example(Bedroom) +// @Param device query string true "Device name" example(DoorSensor) // @Param command body main.Command true "Command to publish" // @Success 200 {object} map[string]string "status:ok" // @Failure 500 {object} gin.H // @Router /raclette [post] func (gh *Gateway) publishCommand(c *gin.Context) error { // Get the user from the authenticated context - user := c.MustGet(gin.AuthUserKey).(string) + //userid := c.MustGet(gin.AuthUserKey).(string) - // Get room and device from the query parameters + // Get user, room and device from the query parameters + user, ret := c.GetQuery("user") + if !ret { + return errors.New("no user found") + } room, ret := c.GetQuery("room") if !ret { return errors.New(`no room found`) diff --git a/gateway/src/RequestInflux.go b/gateway/src/RequestInflux.go index a194884..77cd242 100644 --- a/gateway/src/RequestInflux.go +++ b/gateway/src/RequestInflux.go @@ -11,37 +11,27 @@ import ( "os" ) -// swagger:model -// @Description Example response for Garage Door status -type GarageDoorExample struct { - // example: true - IsClosed bool `json:"IsClosed"` - // example: false - IsDownButtonPressed bool `json:"IsDownButtonPressed"` - // example: false - IsIRSensor bool `json:"IsIRSensor"` - // example: false - IsOpen bool `json:"IsOpen"` - // example: false - IsUpButtonPressed bool `json:"IsUpButtonPressed"` -} - // @Summary Request Influx data // @Description Request data from InfluxDB for a specific device in a room // @Tags request // @Accept json // @Produce json // @Security BasicAuth -// @Param room query string true "Room name" example(Garage) -// @Param device query string true "Device name" example(Door) -// @Success 200 {object} GarageDoorExample "Returns a map of field names to their latest values." +// @Param user query string true "User name" example(remi) +// @Param room query string true "Room name" example(Bedroom) +// @Param device query string true "Device name" example(DoorSensor) +// @Success 200 {object} map[string]interface{} "Returns a map of field names to their latest values." // @Failure 500 {object} gin.H // @Router /raclette [get] func (gh *Gateway) requestInflux(c *gin.Context) error { // Get the user from the authenticated context - user := c.MustGet(gin.AuthUserKey).(string) + //userId := c.MustGet(gin.AuthUserKey).(string) // Get room and device from the query parameters + user, ret := c.GetQuery("user") + if !ret { + return errors.New("no user found") + } room, ret := c.GetQuery("room") if !ret { return errors.New(`no room found`) @@ -51,24 +41,35 @@ func (gh *Gateway) requestInflux(c *gin.Context) error { return errors.New(`no device found`) } - org := user - bucket := user + // Get env variables and set default values if not set + INFLUXDB_ORG, ok := os.LookupEnv("INFLUXDB_ORG") + if !ok { + log.Error("INFLUXDB_ORG not set, using default value: raclette") + INFLUXDB_ORG = "raclette" + } - queryAPI := gh.influx.QueryAPI(org) + INFLUXDB_BUCKET, ok := os.LookupEnv("INFLUXDB_BUCKET") + if !ok { + log.Error("INFLUXDB_BUCKET not set, using default value: raclette") + INFLUXDB_BUCKET = "raclette" + } + + queryAPI := gh.influx.QueryAPI(INFLUXDB_ORG) MEASUREMENT_NAME, ok := os.LookupEnv("MEASUREMENT_NAME") if !ok { log.Error("MEASUREMENT_NAME not set, using default value: softweng") - MEASUREMENT_NAME = "softweng" + MEASUREMENT_NAME = "THC" } // The Flux query uses a large range (-1000d) and aggregates the latest values. // This ensures we always get the most recent data, even if the database contains old entries. query := fmt.Sprintf(`from(bucket: %q) |> range(start: -1000d) |> filter(fn: (r) => r["_measurement"] == %q) + |> filter(fn: (r) => r["user"] == %q) |> filter(fn: (r) => r["room"] == %q) |> filter(fn: (r) => r["device"] == %q) - |> aggregateWindow(every: 1000d, fn: last, createEmpty: false) - `, bucket, MEASUREMENT_NAME, room, device) + //|> aggregateWindow(every: 1000d, fn: mean, createEmpty: false) + `, INFLUXDB_BUCKET, MEASUREMENT_NAME, user, room, device) results, err := queryAPI.Query(context.Background(), query) if err != nil { log.Fatal(err) diff --git a/gateway/src/docs/docs.go b/gateway/src/docs/docs.go index 7132925..9baf7b4 100644 --- a/gateway/src/docs/docs.go +++ b/gateway/src/docs/docs.go @@ -59,7 +59,15 @@ const docTemplate = `{ "parameters": [ { "type": "string", - "example": "Garage", + "example": "remi", + "description": "User name", + "name": "user", + "in": "query", + "required": true + }, + { + "type": "string", + "example": "Bedroom", "description": "Room name", "name": "room", "in": "query", @@ -67,7 +75,7 @@ const docTemplate = `{ }, { "type": "string", - "example": "Door", + "example": "DoorSensor", "description": "Device name", "name": "device", "in": "query", @@ -78,7 +86,8 @@ const docTemplate = `{ "200": { "description": "Returns a map of field names to their latest values.", "schema": { - "$ref": "#/definitions/main.GarageDoorExample" + "type": "object", + "additionalProperties": true } }, "500": { @@ -109,7 +118,15 @@ const docTemplate = `{ "parameters": [ { "type": "string", - "example": "Garage", + "example": "remi", + "description": "User name", + "name": "user", + "in": "query", + "required": true + }, + { + "type": "string", + "example": "Bedroom", "description": "Room name", "name": "room", "in": "query", @@ -117,7 +134,7 @@ const docTemplate = `{ }, { "type": "string", - "example": "Door", + "example": "DoorSensor", "description": "Device name", "name": "device", "in": "query", @@ -170,32 +187,6 @@ const docTemplate = `{ "example": "UP" } } - }, - "main.GarageDoorExample": { - "description": "Example response for Garage Door status", - "type": "object", - "properties": { - "IsClosed": { - "description": "example: true", - "type": "boolean" - }, - "IsDownButtonPressed": { - "description": "example: false", - "type": "boolean" - }, - "IsIRSensor": { - "description": "example: false", - "type": "boolean" - }, - "IsOpen": { - "description": "example: false", - "type": "boolean" - }, - "IsUpButtonPressed": { - "description": "example: false", - "type": "boolean" - } - } } }, "securityDefinitions": { diff --git a/gateway/src/docs/swagger.json b/gateway/src/docs/swagger.json index 72ac85f..9135020 100644 --- a/gateway/src/docs/swagger.json +++ b/gateway/src/docs/swagger.json @@ -53,7 +53,15 @@ "parameters": [ { "type": "string", - "example": "Garage", + "example": "remi", + "description": "User name", + "name": "user", + "in": "query", + "required": true + }, + { + "type": "string", + "example": "Bedroom", "description": "Room name", "name": "room", "in": "query", @@ -61,7 +69,7 @@ }, { "type": "string", - "example": "Door", + "example": "DoorSensor", "description": "Device name", "name": "device", "in": "query", @@ -72,7 +80,8 @@ "200": { "description": "Returns a map of field names to their latest values.", "schema": { - "$ref": "#/definitions/main.GarageDoorExample" + "type": "object", + "additionalProperties": true } }, "500": { @@ -103,7 +112,15 @@ "parameters": [ { "type": "string", - "example": "Garage", + "example": "remi", + "description": "User name", + "name": "user", + "in": "query", + "required": true + }, + { + "type": "string", + "example": "Bedroom", "description": "Room name", "name": "room", "in": "query", @@ -111,7 +128,7 @@ }, { "type": "string", - "example": "Door", + "example": "DoorSensor", "description": "Device name", "name": "device", "in": "query", @@ -164,32 +181,6 @@ "example": "UP" } } - }, - "main.GarageDoorExample": { - "description": "Example response for Garage Door status", - "type": "object", - "properties": { - "IsClosed": { - "description": "example: true", - "type": "boolean" - }, - "IsDownButtonPressed": { - "description": "example: false", - "type": "boolean" - }, - "IsIRSensor": { - "description": "example: false", - "type": "boolean" - }, - "IsOpen": { - "description": "example: false", - "type": "boolean" - }, - "IsUpButtonPressed": { - "description": "example: false", - "type": "boolean" - } - } } }, "securityDefinitions": { diff --git a/gateway/src/docs/swagger.yaml b/gateway/src/docs/swagger.yaml index a26f015..6c8f4ad 100644 --- a/gateway/src/docs/swagger.yaml +++ b/gateway/src/docs/swagger.yaml @@ -12,25 +12,6 @@ definitions: required: - command type: object - main.GarageDoorExample: - description: Example response for Garage Door status - properties: - IsClosed: - description: 'example: true' - type: boolean - IsDownButtonPressed: - description: 'example: false' - type: boolean - IsIRSensor: - description: 'example: false' - type: boolean - IsOpen: - description: 'example: false' - type: boolean - IsUpButtonPressed: - description: 'example: false' - type: boolean - type: object host: rest.mse.kb28.ch info: contact: {} @@ -59,14 +40,20 @@ paths: - application/json description: Request data from InfluxDB for a specific device in a room parameters: + - description: User name + example: remi + in: query + name: user + required: true + type: string - description: Room name - example: Garage + example: Bedroom in: query name: room required: true type: string - description: Device name - example: Door + example: DoorSensor in: query name: device required: true @@ -77,7 +64,8 @@ paths: "200": description: Returns a map of field names to their latest values. schema: - $ref: '#/definitions/main.GarageDoorExample' + additionalProperties: true + type: object "500": description: Internal Server Error schema: @@ -92,14 +80,20 @@ paths: - application/json description: Publish a command to a specific device in a room parameters: + - description: User name + example: remi + in: query + name: user + required: true + type: string - description: Room name - example: Garage + example: Bedroom in: query name: room required: true type: string - description: Device name - example: Door + example: DoorSensor in: query name: device required: true