Skip to content

Commit

Permalink
remove duplicate update logic in gateway lwm2m (#1092)
Browse files Browse the repository at this point in the history
* remove duplicate update logic in gateway lwm2m

* remove unused function
  • Loading branch information
rhoninl authored Jan 26, 2025
1 parent 377f225 commit 19f5b1a
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions pkg/gateway/lwm2m/client/lwm2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *Client) connect() error {

udpClientOpts = append(
udpClientOpts,
options.WithInactivityMonitor(time.Minute, func(cc *udpClient.Conn) {
options.WithInactivityMonitor(time.Second*time.Duration(c.Settings.UpdateIntervalSec), func(cc *udpClient.Conn) {
logger.Warn("Connection inactive, triggering reconnect")
select {
case c.reconnectCh <- struct{}{}:
Expand Down Expand Up @@ -240,11 +240,6 @@ func (c *Client) Register() error {

c.locationPath = locationPath
c.lastUpdatedTime = time.Now()
go func() {
if err := c.AutoUpdate(); err != nil {
logger.Errorf("failed to auto update registration: %v", err)
}
}()

logger.Infof("register %v success", c.locationPath)
return nil
Expand All @@ -271,26 +266,6 @@ func (c *Client) Delete() error {
return nil
}

// AutoUpdate auto update registration
// Reference: https://www.openmobilealliance.org/release/LightweightM2M/V1_0-20170208-A/OMA-TS-LightweightM2M-V1_0-20170208-A.pdf#page=30
func (c *Client) AutoUpdate() error {
ticker := time.NewTicker(time.Duration(c.Settings.UpdateIntervalSec) * time.Second)
for {
select {
case <-c.ctx.Done():
return nil
case <-ticker.C:
if c.isActivity() {
if err := c.Update(); err != nil {
logger.Errorf("failed to update registration: %v", err)
continue
}
logger.Debug("update registration success")
}
}
}
}

// Update update registration
// Reference: https://www.openmobilealliance.org/release/LightweightM2M/V1_0-20170208-A/OMA-TS-LightweightM2M-V1_0-20170208-A.pdf#page=30
// Reference: https://www.openmobilealliance.org/release/LightweightM2M/V1_0-20170208-A/OMA-TS-LightweightM2M-V1_0-20170208-A.pdf#page=76
Expand Down Expand Up @@ -554,7 +529,3 @@ func (c *Client) CleanUp() {
_ = c.udpConnection.Close()
}
}

func (c *Client) isActivity() bool {
return c.udpConnection != nil && time.Now().Before(c.lastUpdatedTime.Add(time.Duration(c.Settings.LifeTimeSec)*time.Second))
}

0 comments on commit 19f5b1a

Please sign in to comment.