147 lines
3.1 KiB
Go
147 lines
3.1 KiB
Go
package routes
|
|
|
|
import (
|
|
"basic-sensor-receiver/app"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type postOrPutMQTTBrokerRequest struct {
|
|
Name string `json:"name"`
|
|
Address string `json:"address"`
|
|
Username *string `json:"username"`
|
|
Password *string `json:"password"`
|
|
ClientId *string `json:"client_id"`
|
|
}
|
|
|
|
type postMQTTPublishRequest struct {
|
|
Retain *bool `json:"retain"`
|
|
Qos *float64 `json:"qos"`
|
|
Topic string `json:"topic" binding:"required"`
|
|
Message string `json:"message" binding:"required"`
|
|
}
|
|
|
|
func GetMQTTBrokers(s *app.Server) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
brokers, err := s.Services.MQTTBrokers.GetList()
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, brokers)
|
|
}
|
|
}
|
|
|
|
func PostMQTTBroker(s *app.Server) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
body := postOrPutMQTTBrokerRequest{}
|
|
if err := bindJSONBodyOrAbort(c, &body); err != nil {
|
|
return
|
|
}
|
|
|
|
broker, err := s.Services.MQTTBrokers.Create(body.Name, body.Address, body.Username, body.Password, body.ClientId)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, broker)
|
|
}
|
|
}
|
|
|
|
func PutMQTTBroker(s *app.Server) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
brokerId, err := getIntParamOrAbort(c, "brokerId")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
body := postOrPutMQTTBrokerRequest{}
|
|
if err := bindJSONBodyOrAbort(c, &body); err != nil {
|
|
return
|
|
}
|
|
|
|
broker, err := s.Services.MQTTBrokers.Update(brokerId, body.Name, body.Address, body.Username, body.Password, body.ClientId)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, broker)
|
|
}
|
|
}
|
|
|
|
func GetMQTTBroker(s *app.Server) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
brokerId, err := getIntParamOrAbort(c, "brokerId")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
broker, err := s.Services.MQTTBrokers.GetById(brokerId)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, broker)
|
|
}
|
|
}
|
|
|
|
func DeleteMQTTBroker(s *app.Server) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
brokerId, err := getIntParamOrAbort(c, "brokerId")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
err = s.Services.MQTTBrokers.Delete(brokerId)
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
}
|
|
}
|
|
|
|
func PostMQTTBrokerPublish(s *app.Server) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
brokerId, err := getIntParamOrAbort(c, "brokerId")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
body := postMQTTPublishRequest{}
|
|
|
|
if err := bindJSONBodyOrAbort(c, &body); err != nil {
|
|
return
|
|
}
|
|
|
|
qos := byte(0)
|
|
retain := false
|
|
|
|
if body.Retain != nil {
|
|
retain = *body.Retain
|
|
}
|
|
|
|
if body.Qos != nil {
|
|
qos = byte(*body.Qos)
|
|
}
|
|
|
|
if err := s.Services.MQTTBrokers.PublishTopic(brokerId, body.Topic, body.Message, qos, retain); err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.Writer.WriteHeader(http.StatusOK)
|
|
}
|
|
}
|