diff --git a/simulator/api.go b/simulator/api.go index 54dcdfa..3b9fa46 100644 --- a/simulator/api.go +++ b/simulator/api.go @@ -2,6 +2,7 @@ package simulator import ( "encoding/hex" + "encoding/base64" "errors" "fmt" "github.com/arslab/lwnsimulator/shared" @@ -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) { diff --git a/socket/models.go b/socket/models.go index 4876591..2eec525 100644 --- a/socket/models.go +++ b/socket/models.go @@ -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.