fix: info api dynamic broker connection

This commit is contained in:
leandrofars 2023-12-13 09:03:12 -03:00
parent 8a0fe71470
commit f96ff1d075
2 changed files with 8 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/leandrofars/oktopus/internal/api/cors"
"github.com/leandrofars/oktopus/internal/api/middleware"
"github.com/leandrofars/oktopus/internal/db"
"github.com/leandrofars/oktopus/internal/mqtt"
"github.com/leandrofars/oktopus/internal/mtp"
usp_msg "github.com/leandrofars/oktopus/internal/usp_message"
"github.com/leandrofars/oktopus/internal/utils"
@ -23,6 +24,7 @@ type Api struct {
Broker mtp.Broker
MsgQueue map[string](chan usp_msg.Msg)
QMutex *sync.Mutex
Mqtt mqtt.Mqtt
}
const REQUEST_TIMEOUT = time.Second * 30
@ -32,13 +34,14 @@ const (
AdminUser
)
func NewApi(port string, db db.Database, b mtp.Broker, msgQueue map[string](chan usp_msg.Msg), m *sync.Mutex) Api {
func NewApi(port string, db db.Database, mqtt *mqtt.Mqtt, msgQueue map[string](chan usp_msg.Msg), m *sync.Mutex) Api {
return Api{
Port: port,
Db: db,
Broker: b,
Broker: mqtt,
MsgQueue: msgQueue,
QMutex: m,
Mqtt: *mqtt,
}
}

View File

@ -60,24 +60,21 @@ func (a *Api) generalInfo(w http.ResponseWriter, r *http.Request) {
result.VendorsCount = vendorcount
result.ProductClassCount = productclasscount
/* ------------ TODO: [mqtt rtt] create common function for this ------------ */
//TODO: address with value from env or something like that
conn, err := net.Dial("tcp", "127.0.0.1:1883")
conn, err := net.Dial("tcp", a.Mqtt.Addr+":"+a.Mqtt.Port)
if err != nil {
json.NewEncoder(w).Encode("Error to connect to broker")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode("Error to connect to broker: " + err.Error())
return
}
defer conn.Close()
info, err := tcpInfo(conn.(*net.TCPConn))
if err != nil {
json.NewEncoder(w).Encode("Error to get TCP socket info")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode("Error to get TCP socket info")
return
}
rtt := time.Duration(info.Rtt) * time.Microsecond
/* -------------------------------------------------------------------------- */
result.MqttRtt = rtt / 1000
@ -85,8 +82,6 @@ func (a *Api) generalInfo(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println(err)
}
return
}
func (a *Api) vendorsInfo(w http.ResponseWriter, r *http.Request) {
@ -100,8 +95,6 @@ func (a *Api) vendorsInfo(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println(err)
}
return
}
func (a *Api) productClassInfo(w http.ResponseWriter, r *http.Request) {
@ -115,8 +108,6 @@ func (a *Api) productClassInfo(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println(err)
}
return
}
func (a *Api) statusInfo(w http.ResponseWriter, r *http.Request) {
@ -141,6 +132,4 @@ func (a *Api) statusInfo(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println(err)
}
return
}