Skip to content

Commit

Permalink
fix: Use proper enums and guards
Browse files Browse the repository at this point in the history
  • Loading branch information
happyRip committed Jul 3, 2023
1 parent 6efaee3 commit 27b400a
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions pkg/source/chirpstack/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

csapi "github.com/chirpstack/chirpstack/api/go/v4/api"
"github.com/chirpstack/chirpstack/api/go/v4/common"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/types"
Expand Down Expand Up @@ -157,32 +158,32 @@ func (p *Source) ExportDevice(devEui string) (*ttnpb.EndDevice, error) {
dev.FrequencyPlanId = p.FrequencyPlanID

// General
switch devProfile.MacVersion.String() {
case "LORAWAN_1_0_0":
switch devProfile.MacVersion {
case common.MacVersion_LORAWAN_1_0_0:
dev.LorawanVersion = ttnpb.MACVersion_MAC_V1_0
dev.LorawanPhyVersion = ttnpb.PHYVersion_TS001_V1_0
case "LORAWAN_1_0_1":
case common.MacVersion_LORAWAN_1_0_1:
dev.LorawanVersion = ttnpb.MACVersion_MAC_V1_0_1
dev.LorawanPhyVersion = ttnpb.PHYVersion_TS001_V1_0_1
case "LORAWAN_1_0_2":
case common.MacVersion_LORAWAN_1_0_2:
dev.LorawanVersion = ttnpb.MACVersion_MAC_V1_0_2
switch devProfile.RegParamsRevision.String() {
case "A":
switch devProfile.RegParamsRevision {
case common.RegParamsRevision_A:
dev.LorawanPhyVersion = ttnpb.PHYVersion_RP001_V1_0_2
case "B":
case common.RegParamsRevision_B:
dev.LorawanPhyVersion = ttnpb.PHYVersion_RP001_V1_0_2_REV_B
default:
return nil, errInvalidPHYVersion.WithAttributes("phy_version", devProfile.RegParamsRevision)
}
case "LORAWAN_1_0_3":
case common.MacVersion_LORAWAN_1_0_3:
dev.LorawanVersion = ttnpb.MACVersion_MAC_V1_0_3
dev.LorawanPhyVersion = ttnpb.PHYVersion_RP001_V1_0_3_REV_A
case "LORAWAN_1_1_0":
case common.MacVersion_LORAWAN_1_1_0:
dev.LorawanVersion = ttnpb.MACVersion_MAC_V1_1
switch devProfile.RegParamsRevision.String() {
case "A":
switch devProfile.RegParamsRevision {
case common.RegParamsRevision_A:
dev.LorawanPhyVersion = ttnpb.PHYVersion_RP001_V1_1_REV_A
case "B":
case common.RegParamsRevision_B:
dev.LorawanPhyVersion = ttnpb.PHYVersion_RP001_V1_1_REV_B
default:
return nil, errInvalidPHYVersion.WithAttributes("phy_version", devProfile.RegParamsRevision)
Expand All @@ -204,11 +205,15 @@ func (p *Source) ExportDevice(devEui string) (*ttnpb.EndDevice, error) {
Value: ttnpb.RxDelay(delay),
}
}
dev.MacSettings.DesiredRx1DataRateOffset = &ttnpb.DataRateOffsetValue{
Value: ttnpb.DataRateOffset(devProfile.AbpRx1DrOffset),
if offset := devProfile.AbpRx1DrOffset; offset > 0 {
dev.MacSettings.Rx1DataRateOffset = &ttnpb.DataRateOffsetValue{
Value: ttnpb.DataRateOffset(offset),
}
}
dev.MacSettings.DesiredRx2DataRateIndex = &ttnpb.DataRateIndexValue{
Value: ttnpb.DataRateIndex(devProfile.AbpRx2Dr),
if dataRate := devProfile.AbpRx2Dr; dataRate > 0 {
dev.MacSettings.Rx2DataRateIndex = &ttnpb.DataRateIndexValue{
Value: ttnpb.DataRateIndex(dataRate),
}
}
}

Expand Down Expand Up @@ -266,11 +271,11 @@ func (p *Source) ExportDevice(devEui string) (*ttnpb.EndDevice, error) {
}

// Payload formatters
switch devProfile.PayloadCodecRuntime.String() {
case "CAYENNE_LPP":
switch devProfile.PayloadCodecRuntime {
case csapi.CodecRuntime_CAYENNE_LPP:
dev.Formatters.UpFormatter = ttnpb.PayloadFormatter_FORMATTER_CAYENNELPP
dev.Formatters.DownFormatter = ttnpb.PayloadFormatter_FORMATTER_CAYENNELPP
case "JS":
case csapi.CodecRuntime_JS:
if s := devProfile.PayloadCodecScript; s != "" {
dev.Formatters.UpFormatter = ttnpb.PayloadFormatter_FORMATTER_JAVASCRIPT
dev.Formatters.UpFormatterParameter = fmt.Sprintf(encoderFormat, s)
Expand Down

0 comments on commit 27b400a

Please sign in to comment.