feat(gateway): added reply to the POST request with the latest measurement
Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
_ "gateway-softweng/docs"
|
||||
"time"
|
||||
|
||||
_ "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -24,25 +25,25 @@ type Command struct {
|
||||
// @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"
|
||||
// @Success 200 {object} map[string]interface{} "response or status:ok"
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /raclette [post]
|
||||
func (gh *Gateway) publishCommand(c *gin.Context) error {
|
||||
func (gh *Gateway) publishCommand(c *gin.Context) (string, error) {
|
||||
// Get the user from the authenticated context
|
||||
//userid := c.MustGet(gin.AuthUserKey).(string)
|
||||
|
||||
// Get user, room and device from the query parameters
|
||||
user, ret := c.GetQuery("user")
|
||||
if !ret {
|
||||
return errors.New("no user found")
|
||||
return "", errors.New("no user found")
|
||||
}
|
||||
room, ret := c.GetQuery("room")
|
||||
if !ret {
|
||||
return errors.New(`no room found`)
|
||||
return "", errors.New(`no room found`)
|
||||
}
|
||||
device, ret := c.GetQuery("device")
|
||||
if !ret {
|
||||
return errors.New(`no device found`)
|
||||
return "", errors.New(`no device found`)
|
||||
}
|
||||
|
||||
// Define the JSON structure for the command
|
||||
@@ -51,14 +52,38 @@ func (gh *Gateway) publishCommand(c *gin.Context) error {
|
||||
// Bind the JSON payload to the structure
|
||||
err := c.Bind(&json)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Set the pending request fields to track this request
|
||||
gh.pendingUser = user
|
||||
gh.pendingRoom = room
|
||||
gh.pendingDevice = device
|
||||
|
||||
// Publish the command to the MQTT broker
|
||||
topic := user + "/" + room + "/" + device + "/cmd/" + json.Command
|
||||
token := gh.mqtt.Publish(topic, 1, false, "")
|
||||
cmdTopic := user + "/" + room + "/" + device + "/cmd/" + json.Command
|
||||
token := gh.mqtt.Publish(cmdTopic, 1, false, "")
|
||||
if token.Wait() && token.Error() != nil {
|
||||
return token.Error()
|
||||
// Clear pending request fields
|
||||
gh.pendingUser = ""
|
||||
gh.pendingRoom = ""
|
||||
gh.pendingDevice = ""
|
||||
return "", token.Error()
|
||||
}
|
||||
|
||||
// Wait for response with a 1-second timeout
|
||||
select {
|
||||
case response := <-gh.pendingChan:
|
||||
// Clear pending request fields
|
||||
gh.pendingUser = ""
|
||||
gh.pendingRoom = ""
|
||||
gh.pendingDevice = ""
|
||||
return response, nil
|
||||
case <-time.After(1 * time.Second):
|
||||
// Timeout occurred, clear pending request fields
|
||||
gh.pendingUser = ""
|
||||
gh.pendingRoom = ""
|
||||
gh.pendingDevice = ""
|
||||
return "", nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user