Skip to content

Commit

Permalink
dev: Fix chirpstack app ids
Browse files Browse the repository at this point in the history
  • Loading branch information
happyRip committed Jun 19, 2023
1 parent 9ea354b commit 032d31e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
4 changes: 2 additions & 2 deletions pkg/source/chirpstack/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Source struct {

ctx context.Context

applications map[int64]*csapi.Application
applications map[string]*csapi.Application
devProfiles map[string]*csapi.DeviceProfile
}

Expand All @@ -61,7 +61,7 @@ func createNewSource(cfg *config.Config) source.CreateSource {
ctx: ctx,
Config: cfg,

applications: make(map[int64]*csapi.Application),
applications: make(map[string]*csapi.Application),
devProfiles: make(map[string]*csapi.DeviceProfile),
}

Expand Down
26 changes: 9 additions & 17 deletions pkg/source/chirpstack/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package chirpstack

import (
"context"
"strconv"

csapi "github.com/chirpstack/chirpstack/api/go/v4/api"
log "go.thethings.network/lorawan-stack/v3/pkg/log"
Expand Down Expand Up @@ -60,37 +59,30 @@ func (p *Source) getDevice(devEui string) (*csapi.Device, error) {
}

func (p *Source) getApplication(application string) (*csapi.Application, error) {
appID, err := strconv.ParseInt(application, 10, 64)
if err != nil {
appID, err = p.getApplicationIDByName(application)
if err != nil {
return nil, err
}
}
app, err := p.getApplicationByID(appID)
app, err := p.getApplicationByID(application)
if err != nil {
switch status.Code(err) {
case codes.NotFound:
appID, err = p.getApplicationIDByName(application)
appID, err := p.getApplicationIDByName(application)
if err != nil {
return nil, err
}
return p.getApplicationByID(appID)
default:
return nil, err
}
return p.getApplicationByID(appID)
}
return app, nil
}

func (p *Source) getApplicationByID(id int64) (*csapi.Application, error) {
func (p *Source) getApplicationByID(id string) (*csapi.Application, error) {
if app, ok := p.applications[id]; ok {
return app, nil
}

client := csapi.NewApplicationServiceClient(p.ClientConn)
resp, err := client.Get(p.ctx, &csapi.GetApplicationRequest{
Id: strconv.FormatInt(id, 10),
Id: id,
})
if err != nil {
return nil, errAPI.WithCause(err)
Expand All @@ -100,7 +92,7 @@ func (p *Source) getApplicationByID(id int64) (*csapi.Application, error) {
return resp.Application, nil
}

func (p *Source) getApplicationIDByName(name string) (int64, error) {
func (p *Source) getApplicationIDByName(name string) (string, error) {
client := csapi.NewApplicationServiceClient(p.ClientConn)
offset := uint32(0)
for {
Expand All @@ -110,16 +102,16 @@ func (p *Source) getApplicationIDByName(name string) (int64, error) {
Search: name,
})
if err != nil {
return 0, err
return "", err
}
for _, appListItem := range resp.Result {
if appListItem.Name == name {
return strconv.ParseInt(appListItem.Id, 10, 64)
return appListItem.Id, nil
}
}

if offset += limit; offset > resp.TotalCount {
return 0, errAppNotFound.WithAttributes("app", name)
return "", errAppNotFound.WithAttributes("app", name)
}
}
}
Expand Down

0 comments on commit 032d31e

Please sign in to comment.