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.Subscribe,
h,
&c.Acs,
)
b.StartBridge()

View File

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

View File

@ -21,17 +21,18 @@ type Nats struct {
}
type Acs struct {
Port string
Tls bool
TlsPort bool
NoTls bool
KeepAliveInterval time.Duration
Username string
Password string
Route string
DebugMode bool
ConnReqUsername string
ConnReqPassword string
Port string
Tls bool
TlsPort bool
NoTls bool
KeepAliveInterval time.Duration
Username string
Password string
Route string
DebugMode bool
ConnReqUsername string
ConnReqPassword string
DeviceAnswerTimeout time.Duration
}
type Config struct {
@ -53,6 +54,7 @@ func NewConfig() *Config {
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")
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")
/*
@ -79,12 +81,13 @@ func NewConfig() *Config {
Ctx: ctx,
},
Acs: Acs{
Port: *acsPort,
Route: *acsRoute,
KeepAliveInterval: time.Duration(*acsKeepAliveInterval) * time.Second,
DebugMode: *cwmpDebugMode,
ConnReqUsername: *connReqUser,
ConnReqPassword: *connReqPasswd,
Port: *acsPort,
Route: *acsRoute,
KeepAliveInterval: time.Duration(*acsKeepAliveInterval) * time.Second,
DebugMode: *cwmpDebugMode,
ConnReqUsername: *connReqUser,
ConnReqPassword: *connReqPasswd,
DeviceAnswerTimeout: time.Duration(*deviceAnswerTimeout) * time.Second,
},
}
}

View File

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