Codec Error: JS error: Error: unexpected token in expression: 'var' at eval_script:35:10

Viewed 23

I have tried to adapt a working codec for a differemnt device which does not have a published codec. The device is a Tek766 Oil Tank Sensor, and I have tried to understand and reuse some code, but I now get thrown an error that doesn’t seem to refer to the codec specifically, but rather to the script that uses the function defined in my codec.

I am not proficient in JS, my background is more Bash and Python, and so I am not sure what I have missed. If anyone could give me some pointers, that would be really useful.

I did paste the whole code from Notepad++ with only the LF newline in case the Windows line feed was an issue, but I am still getting the same error.

The Error is:

JS error: Error: unexpected token in expression: 'var' at eval_script:35:10

My code is as follows:

// Decode uplink function.
//
// Input is an object with the following fields:
// - bytes = Byte array containing the uplink payload, e.g. [255, 230, 255, 0]
// - fPort = Uplink fPort.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - data = Object representing the decoded payload.
const PORT_SONIC_MEASUREMENT = 0x10;
const PORT_STATUS = 0x30;
const PORT_ALARM_NOTOFICATION = 0x45;

function decodeUplink(input) {
    var bytes = input.bytes;
	var port = input.fPort;
	var data = {};

	if ( port===PORT_SONIC_MEASUREMENT && bytes.length === 20) {

		var ullage0=bytes.readUInt16BE(4);
        var temp0=bytes.readInt8(6);
        data.TYPE="measurement",
        data.ULLAGE=ullage0,
        data.TEMP=temp0
        
    
		return {
			data: data,
		};
	} else if (port===PORT_STATUS && bytes.length === 18) {

        data.TYPE="status",
        var rssi=bytes.readUInt8(8);
        data.RSSI=-rssi,
        var battery=bytes.readUInt8(10);
        data.BATTERYLEVEL=battery,
        var ullage=bytes.readUInt16BE(14);
        data.ULLAGE=ullage,
        var temperature=bytes.readInt8(16);
        data.TEMP=temperature,
		return {
			data: data,
		}
	}
}

// Encode downlink function.
//
// Input is an object with the following fields:
// - data = Object representing the payload that must be encoded.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - bytes = Byte array containing the downlink payload.
function encodeDownlink(input) {
  return {
    bytes: [225, 230, 255, 0]
  };
}

If anyone can give me any ideas where my problem lies, I’d be grateful.

2 Answers

My lack of JS experience definitely shows in how lines are terminated, but I've made a few changes, including using semi-colons and the following works much better and decodes correctly.

function decodeUplink(input) {
    var bytes = input.bytes;
	var port = input.fPort;
	var data = {};
	switch (input.fPort) {
case 16:
	{
	data.TYPE="measurement";
	data.ULLAGE=(bytes[4]<<24>>16 | bytes[5]);
	data.TEMP=bytes[6];
	data.SRC=((bytes[7] & 0xF0)>>4);
	data.SRSSI=(bytes[7] & 0x07);
	data.ULLAGE_PREV=(bytes[8]<<24>>16 | bytes[9]);
	}
	return {
		data: data,
	}
	break;
case 48:
    {
	data.TYPE="status";
	data.RSSI=-bytes[8];
	data.BATTERYLEVEL=bytes[10];
	data.ULLAGE=(bytes[14]<<24>>16 | bytes[15]);
	data.TEMP=bytes[16];
	data.SRC=((bytes[17] & 0xF0)>>4);
	data.SRSSI=(bytes[17] & 0x07);
    }
	return {
		data: data,
	}
	break;
default:
	return {
		errors: ["unknown FPort"]
	}
}
}

I have not fully debugged your code, but this looks incorrect:

        data.TYPE="measurement",
        data.ULLAGE=ullage0,
        data.TEMP=temp0

I think you probably want to end the lines with ; instead of ,