feat(gateway): added first implement of the Gateway (MQTT, Influx, REST)
Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
59
gateway/src/PublishCommand.go
Normal file
59
gateway/src/PublishCommand.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
_ "gateway-softweng/docs"
|
||||
|
||||
_ "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Command represents the structure of the command to be published
|
||||
// @Description Command structure for publishing
|
||||
type Command struct {
|
||||
Command string `json:"command" example:"UP" binding:"required"`
|
||||
}
|
||||
|
||||
// @Summary Publish command
|
||||
// @Description Publish a command to a specific device in a room
|
||||
// @Tags commands
|
||||
// @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 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)
|
||||
|
||||
// Get room and device from the query parameters
|
||||
room, ret := c.GetQuery("room")
|
||||
if !ret {
|
||||
return errors.New(`no room found`)
|
||||
}
|
||||
device, ret := c.GetQuery("device")
|
||||
if !ret {
|
||||
return errors.New(`no device found`)
|
||||
}
|
||||
|
||||
// Define the JSON structure for the command
|
||||
var json Command
|
||||
|
||||
// Bind the JSON payload to the structure
|
||||
err := c.Bind(&json)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Publish the command to the MQTT broker
|
||||
topic := user + "/" + room + "/" + device + "/command/" + json.Command
|
||||
token := gh.mqtt.Publish(topic, 1, false, "")
|
||||
if token.Wait() && token.Error() != nil {
|
||||
return token.Error()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user