Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions simulator/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package simulator

import (
"encoding/hex"
"encoding/base64"
"errors"
"fmt"
"github.com/arslab/lwnsimulator/shared"
Expand Down Expand Up @@ -352,15 +353,34 @@ func (s *Simulator) ChangePayload(pl socket.NewPayload) (string, bool) {
MType = lorawan.ConfirmedDataUp
}

Payload := &lorawan.DataPayload{
Bytes: []byte(pl.Payload),
}
// -------------------------------
// NEW: decode payload if IsBase64
// -------------------------------
var payloadBytes []byte
var err error

if pl.IsBase64 {
payloadBytes, err = base64.StdEncoding.DecodeString(pl.Payload)
if err != nil {
s.Console.PrintSocket(socket.EventResponseCommand,
"Invalid Base64 payload: "+err.Error())
return devEUIstring, false
}
} else {
// original behavior: raw bytes from string
payloadBytes = []byte(pl.Payload)
}

Payload := &lorawan.DataPayload{
Bytes: payloadBytes,
}

s.Devices[pl.Id].ChangePayload(MType, Payload)
s.Devices[pl.Id].ChangePayload(MType, Payload)

s.Console.PrintSocket(socket.EventResponseCommand, s.Devices[pl.Id].Info.Name+": Payload changed")
s.Console.PrintSocket(socket.EventResponseCommand,
s.Devices[pl.Id].Info.Name+": Payload changed")

return devEUIstring, true
return devEUIstring, true
}

func (s *Simulator) SendUplink(pl socket.NewPayload) {
Expand Down
7 changes: 4 additions & 3 deletions socket/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ type NewStatusDev struct {

// NewPayload represents a structure for handling payload changes with ID, message type, and payload data.
type NewPayload struct {
Id int `json:"id"` // Id is the unique identifier of the payload.
MType string `json:"mtype"` // MType is the message type.
Payload string `json:"payload"` // Payload is the actual payload data.
Id int `json:"id"` // Id is the unique identifier of the payload.
MType string `json:"mtype"` // MType is the message type.
Payload string `json:"payload"` // Payload is the actual payload data.
IsBase64 bool `json:"isbase64"` // If True, payload is decoded as base64.
}

// NewLocation represents the geographical location of a device.
Expand Down