Unable to decode the data with JavaScript codec


#1

the json data that i am getting in base64 format. I am unable to decode that data inside the javascript codec.


#2

Could you give us more details? What have you tried? Why does it fail?


#3

Whenever i am trying to receive the json data through mqtt i am unable to decode the data.
suppose this is my data… “data”: “ABDJGDKHSKA/==” which must be in base64 encoded format.
I am trying to decode the data so that i get the result in a format like {“humidity”: 30}
So while registering the device in loraApp server’s UI i need to write a javascript code…

// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// The function must return an object, e.g. {“temperature”: 22.5}
function Decode(fPort, bytes) {
return {};
}

But the problem is that i am unable to decode this data into my desired format correctly.


#4

@Sobhan_Thakur Why don’t you post your code? Without that it is really hard to help you.


#5
function Decode(fPort, bytes) {
	var result = "";
	for(var i = 0; i < bytes.length; ++i){
		result+= (String.fromCharCode(bytes[i]));
	}
	
 	var raw = Base64.decode(result);//Base64 has the definitions of the functions. I haven't mentioned here because its too long.
  var HEX = ' ';
  for ( i = 0; i < raw.length; i++ ) {
   var _hex = raw.charCodeAt(i).toString(16)
    HEX += (_hex.length==2?_hex:'0'+_hex);
  }
  var op= HEX.toUpperCase();
	op=op.substr(6,4)
return {"temperature":op};
}

#6

Why are you turning the bytes into a string and then trying to base64 decode it? Decode receives the decoded base64 data as an array of bytes, i.e., the original bytes that are sent from the device (I confirmed this just now), so basically Decode should be the inverse of whatever your encoding is at the device.

As an example, we encode our data as 2’s complement hex, and here is how we decode it at the lora-app-server side:

func ReadFloatData(dataType DataType, dataArray []byte) (float64, error) {
	if dataType.NumBytes != int32(len(dataArray)) {
		return 0.0, ErrNumBytes
	}

	intData := new(big.Int)
	intData.SetBytes(dataArray)

	//Precision is given by the max repesentable number minus 1.
	precision := float64((dataType.NumBytes * 8) - 1)
	var fData float64

	floatInt, _ := new(big.Float).SetInt(intData).Float64()

	if dataType.AllowsNegative {
		fData = (floatInt / (math.Pow(2, precision) - 1.0)) * dataType.MaxVal
		if fData > dataType.MaxVal {
			fData = (2*dataType.MaxVal - fData) * -1.0
		}

	} else {
		//If no negatives are allowd, we don't need a sign bit, so we divide by the whole precision.
		fData = (floatInt / math.Pow(2, precision+1)) * dataType.MaxVal
		if fData > dataType.MaxVal {
			return 0.0, ErrDataTypeRange
		}
	}

	//Check for min val, return n error if the value is outside the range.
	if fData < dataType.MinVal {
		return 0.0, ErrDataTypeRange
	}

	return fData, nil

}

#7

Thanks @iegomez It’s working Now …


#8

Hello everyone and have a nice week.

I am trying to achieve code decoding, using Lora-App Server’s decoder, as @Sobhan_Thakur.

Where can i make this?

Regards,
Nikos.


#9

within the application configuration


#10

Thanks for the immediate reply @Sobhan_Thakur.

In Payload codec i use Cayenne LPP.

May i have to change it, to Custom Javascript codec functions, in order to make a decode, as you mentioned above?

Regards,
Nikos.


#11

Yes… Inside Custom Javascript Codec you need to write your own decoding logic …


#12

And the results will be shown into which page?


#13

You can verify your decoding using HTTP integration or by executing mqtt_sub command


#14

Using above code, as @iegomez posted is any solution?


#15

Note that my code is specific for our encoding, apart from being directly coded at our modified version of lora-app-server source (so written in Go), so take it only as an example of what a decoding function could look like. Again, it depends entirely on your encoding.


#16

Hello @iegomez and thanks for your reply.

I made questions in a lot of posts, as i have a specific problem with authentication so in mosquitto_subs,as in HTTP Integration, which i believe that create the main issue of non-decoding.

Do you believe that there is the problem?


#17

Sorry, I’d have to take a look at those other questions and I’m quite busy right now, so hopefully someone else can help you in each topic.


#18

Hello! I thought of creating a new post about the subject, but hopefully that i will receive any reply from here, i post again my problem:

From my nodes, i sent a data “Hello World”, which decrypted in base64 gives “SGVsbG8gV29ybGQ=”

,which actually also receive using HTTP Integration or MQTT_subs:

My issue is, that despite inserting my custom decode javascript code, i receive “null” in object field,where i should take “Hello World”…any idea?

My code is
function Decode(fPort, bytes) {
var s = ‘0x’;
bytes.forEach(function(byte) {
s += (‘0’ + (byte & 0xFF).toString(16)).slice(-2);
});
var p=s.substr(8,4);
return {“temperature”:parseInt(p, 16)};//Your decoding Logic Goes here
}

Thanks in advance,
Nikos.


#19

I also used below code to decode plain text, but nothing:

function Decoder(bytes, port) {
// Decode plain text; not recommended
var text = String.fromCharCode.apply(null, bytes);
return{
text: text,
}
}


#20

@brocaar would be any problem, if i opened a new post with issue?