From 53fbc87af66c59ec9acd8abada41dd251e833547 Mon Sep 17 00:00:00 2001 From: Klagarge Date: Sat, 30 May 2026 20:40:48 +0200 Subject: [PATCH] feat(db): add co2-status endpoint in REST API This endpoint get the co2 status and return for each room if the co2 is too high or in the acceptable level Assisted-by: Junie:gemini-3-flash Signed-off-by: Klagarge --- db/get-db.http | 4 ++++ db/src/docs/docs.go | 44 ++++++++++++++++++++++++++++++++++++++++ db/src/docs/swagger.json | 44 ++++++++++++++++++++++++++++++++++++++++ db/src/docs/swagger.yaml | 27 ++++++++++++++++++++++++ db/src/rest/rest.go | 27 ++++++++++++++++++++++++ 5 files changed, 146 insertions(+) diff --git a/db/get-db.http b/db/get-db.http index 8f80077..446e645 100644 --- a/db/get-db.http +++ b/db/get-db.http @@ -12,6 +12,10 @@ Authorization: Basic {{username}} {{password}} GET {{host}}/api/v1/rooms/{{room-id}}/history?window={{window}} Authorization: Basic {{username}} {{password}} +### GET CO2 status of all rooms +GET {{host}}/api/v1/rooms/co2-status +Authorization: Basic {{username}} {{password}} + ### GET all rooms GET {{host}}/api/v1/rooms Authorization: Basic {{username}} {{password}} diff --git a/db/src/docs/docs.go b/db/src/docs/docs.go index 92620fa..0b15831 100644 --- a/db/src/docs/docs.go +++ b/db/src/docs/docs.go @@ -159,6 +159,34 @@ const docTemplate = `{ } } }, + "/rooms/co2-status": { + "get": { + "security": [ + { + "BasicAuth": [] + } + ], + "description": "Get a list of all rooms with their CO2 status and value", + "produces": [ + "application/json" + ], + "tags": [ + "rooms" + ], + "summary": "Get CO2 status for all rooms", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/rest.RoomCO2Status" + } + } + } + } + } + }, "/rooms/{room-id}/current": { "get": { "security": [ @@ -267,6 +295,22 @@ const docTemplate = `{ } } }, + "definitions": { + "rest.RoomCO2Status": { + "type": "object", + "properties": { + "co2": { + "type": "integer" + }, + "is_high": { + "type": "boolean" + }, + "room": { + "type": "string" + } + } + } + }, "securityDefinitions": { "BasicAuth": { "type": "basic" diff --git a/db/src/docs/swagger.json b/db/src/docs/swagger.json index 2215d30..7227d07 100644 --- a/db/src/docs/swagger.json +++ b/db/src/docs/swagger.json @@ -153,6 +153,34 @@ } } }, + "/rooms/co2-status": { + "get": { + "security": [ + { + "BasicAuth": [] + } + ], + "description": "Get a list of all rooms with their CO2 status and value", + "produces": [ + "application/json" + ], + "tags": [ + "rooms" + ], + "summary": "Get CO2 status for all rooms", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/rest.RoomCO2Status" + } + } + } + } + } + }, "/rooms/{room-id}/current": { "get": { "security": [ @@ -261,6 +289,22 @@ } } }, + "definitions": { + "rest.RoomCO2Status": { + "type": "object", + "properties": { + "co2": { + "type": "integer" + }, + "is_high": { + "type": "boolean" + }, + "room": { + "type": "string" + } + } + } + }, "securityDefinitions": { "BasicAuth": { "type": "basic" diff --git a/db/src/docs/swagger.yaml b/db/src/docs/swagger.yaml index 7658347..24d4bb5 100644 --- a/db/src/docs/swagger.yaml +++ b/db/src/docs/swagger.yaml @@ -1,4 +1,14 @@ basePath: /api/v1 +definitions: + rest.RoomCO2Status: + properties: + co2: + type: integer + is_high: + type: boolean + room: + type: string + type: object host: api.db.e.kb28.ch info: contact: {} @@ -168,6 +178,23 @@ paths: summary: Get history for a room tags: - rooms + /rooms/co2-status: + get: + description: Get a list of all rooms with their CO2 status and value + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/rest.RoomCO2Status' + type: array + security: + - BasicAuth: [] + summary: Get CO2 status for all rooms + tags: + - rooms securityDefinitions: BasicAuth: type: basic diff --git a/db/src/rest/rest.go b/db/src/rest/rest.go index 90b440c..d31a734 100644 --- a/db/src/rest/rest.go +++ b/db/src/rest/rest.go @@ -107,6 +107,7 @@ func (g *RestGateway) setupRoutes() { v1.GET("/rooms", g.getRooms) v1.GET("/rooms/:room-id/current", g.getRoomCurrent) v1.GET("/rooms/:room-id/history", g.getRoomHistory) + v1.GET("/rooms/co2-status", g.getRoomCO2Status) v1.GET("/export/csv", g.getExportCSV) } @@ -557,3 +558,29 @@ func (g *RestGateway) checkCO2() { } } } + +// GET /api/v1/rooms/co2-status +// getRoomCO2Status godoc +// @Summary Get CO2 status for all rooms +// @Description Get a list of all rooms with their CO2 status and value +// @Tags rooms +// @Produce json +// @Success 200 {array} RoomCO2Status +// @Security BasicAuth +// @Router /rooms/co2-status [get] +func (g *RestGateway) getRoomCO2Status(c *gin.Context) { + g.statusMu.RLock() + defer g.statusMu.RUnlock() + + var result []RoomCO2Status + for _, status := range g.roomStatus { + result = append(result, *status) + } + + // Sort by room ID for stability + slices.SortFunc(result, func(a, b RoomCO2Status) int { + return strings.Compare(a.RoomID, b.RoomID) + }) + + c.JSON(http.StatusOK, result) +}