feat(cwmp): device answer timeout set via config

This commit is contained in:
leandrofars 2024-05-30 10:07:19 -03:00
parent 289859624d
commit 8d1ce2529b
4 changed files with 36 additions and 21 deletions

View File

@ -20,6 +20,7 @@ func main() {
natsActions.Publish, natsActions.Publish,
natsActions.Subscribe, natsActions.Subscribe,
h, h,
&c.Acs,
) )
b.StartBridge() b.StartBridge()

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"oktopUSP/backend/services/acs/internal/config"
"oktopUSP/backend/services/acs/internal/server/handler" "oktopUSP/backend/services/acs/internal/server/handler"
"strings" "strings"
"time" "time"
@ -17,6 +18,7 @@ type Bridge struct {
sub func(string, func(*nats.Msg)) error sub func(string, func(*nats.Msg)) error
cpes map[string]handler.CPE cpes map[string]handler.CPE
h *handler.Handler h *handler.Handler
conf *config.Acs
} }
type msgAnswer struct { type msgAnswer struct {
@ -24,18 +26,18 @@ type msgAnswer struct {
Msg any Msg any
} }
const DEVICE_ANSWER_TIMEOUT = 10 * time.Second
func NewBridge( func NewBridge(
pub func(string, []byte) error, pub func(string, []byte) error,
sub func(string, func(*nats.Msg)) error, sub func(string, func(*nats.Msg)) error,
h *handler.Handler, h *handler.Handler,
c *config.Acs,
) *Bridge { ) *Bridge {
return &Bridge{ return &Bridge{
pub: pub, pub: pub,
sub: sub, sub: sub,
cpes: h.Cpes, cpes: h.Cpes,
h: h, h: h,
conf: c,
} }
} }
@ -68,6 +70,7 @@ func (b *Bridge) StartBridge() {
Id: uuid.NewString(), Id: uuid.NewString(),
CwmpMsg: msg.Data, CwmpMsg: msg.Data,
Callback: deviceAnswer, Callback: deviceAnswer,
Time: time.Now(),
}) })
err := b.h.ConnectionRequest(cpe) err := b.h.ConnectionRequest(cpe)
@ -85,12 +88,15 @@ func (b *Bridge) StartBridge() {
select { select {
case response := <-deviceAnswer: case response := <-deviceAnswer:
log.Println("Received response from device: ", string(response)) if b.conf.DebugMode {
log.Printf("Received response from cpe: %s payload: %s ", cpe.SerialNumber, string(response))
}
respondMsg(msg.Respond, http.StatusOK, response) respondMsg(msg.Respond, http.StatusOK, response)
case <-time.After(DEVICE_ANSWER_TIMEOUT): case <-time.After(b.conf.DeviceAnswerTimeout):
log.Println("Device response timed out") log.Println("Device response timed out")
respondMsg(msg.Respond, http.StatusRequestTimeout, "Request timeout") respondMsg(msg.Respond, http.StatusRequestTimeout, "Request timeout")
} }
}) })
} }

View File

@ -21,17 +21,18 @@ type Nats struct {
} }
type Acs struct { type Acs struct {
Port string Port string
Tls bool Tls bool
TlsPort bool TlsPort bool
NoTls bool NoTls bool
KeepAliveInterval time.Duration KeepAliveInterval time.Duration
Username string Username string
Password string Password string
Route string Route string
DebugMode bool DebugMode bool
ConnReqUsername string ConnReqUsername string
ConnReqPassword string ConnReqPassword string
DeviceAnswerTimeout time.Duration
} }
type Config struct { type Config struct {
@ -53,6 +54,7 @@ func NewConfig() *Config {
connReqPasswd := flag.String("connrq_passwd", lookupEnvOrString("CONN_RQ_PASSWD", ""), "Connection Request Password") connReqPasswd := flag.String("connrq_passwd", lookupEnvOrString("CONN_RQ_PASSWD", ""), "Connection Request Password")
acsKeepAliveInterval := flag.Int("acs_keep_alive_interval", lookupEnvOrInt("KEEP_ALIVE_INTERVAL", 300), "keep alive interval in seconds for acs server") acsKeepAliveInterval := flag.Int("acs_keep_alive_interval", lookupEnvOrInt("KEEP_ALIVE_INTERVAL", 300), "keep alive interval in seconds for acs server")
cwmpDebugMode := flag.Bool("debug_mode", lookupEnvOrBool("CWMP_DEBUG", false), "enable or disable cwmp logs in debug mode") cwmpDebugMode := flag.Bool("debug_mode", lookupEnvOrBool("CWMP_DEBUG", false), "enable or disable cwmp logs in debug mode")
deviceAnswerTimeout := flag.Int("device_answer_timeout", lookupEnvOrInt("DEVICE_ANSWER_TIMEOUT", 10), "device answer timeout in seconds")
flHelp := flag.Bool("help", false, "Help") flHelp := flag.Bool("help", false, "Help")
/* /*
@ -79,12 +81,13 @@ func NewConfig() *Config {
Ctx: ctx, Ctx: ctx,
}, },
Acs: Acs{ Acs: Acs{
Port: *acsPort, Port: *acsPort,
Route: *acsRoute, Route: *acsRoute,
KeepAliveInterval: time.Duration(*acsKeepAliveInterval) * time.Second, KeepAliveInterval: time.Duration(*acsKeepAliveInterval) * time.Second,
DebugMode: *cwmpDebugMode, DebugMode: *cwmpDebugMode,
ConnReqUsername: *connReqUser, ConnReqUsername: *connReqUser,
ConnReqPassword: *connReqPasswd, ConnReqPassword: *connReqPasswd,
DeviceAnswerTimeout: time.Duration(*deviceAnswerTimeout) * time.Second,
}, },
} }
} }

View File

@ -63,6 +63,11 @@ type GetParameterNamesResponse struct {
ParameterList []ParameterInfoStruct `xml:"Body>GetParameterNamesResponse>ParameterList>ParameterInfoStruct"` ParameterList []ParameterInfoStruct `xml:"Body>GetParameterNamesResponse>ParameterList>ParameterInfoStruct"`
} }
type Fault struct {
FaultCode uint
FaultString string
}
type CWMPInform struct { type CWMPInform struct {
DeviceId DeviceID `xml:"Body>Inform>DeviceId"` DeviceId DeviceID `xml:"Body>Inform>DeviceId"`
Events []EventStruct `xml:"Body>Inform>Event>EventStruct"` Events []EventStruct `xml:"Body>Inform>Event>EventStruct"`