The definition of a data source (device) in Signomix allows you to define code for the data processor. Upon receiving new data from this source, the data processor launches the JavaScript interpreter and executes the code defined for this data source.
When creating code for the processor, the user can use functions from the sgx library built into the data processor. Below is the documentation for this library.
Variables:
Functions:
sgx LibraryWhen the data processor is initialized, variables of the sgx library are assigned values read from the incoming data. In the data processor script, you can read the values of these variables in the same way as for the eui variable below.
var eui = sgx.eui
euiIdentifier (EUI) of the data source
var eui = sgx.eui
dataTimestampTimestamp of data receipt in milliseconds (UTC).
applicationConfigApplication configuration object declared for the data source.
deviceConfigConfiguration object defined for the data source.
deviceGroupsGroups with which the data source is associated.
latitudeLatitude of the data source, specified in its definition (decimal format, as in Google Maps).
longitudeLongitude of the data source, specified in its definition (decimal format, as in Google Maps).
altitudeAltitude of the data source above sea level, specified in its definition.
tagsMap of tags added to the data source definition.
portLoRaWAN port number assigned to the data transmission.
sgx Objectverify(received, receivedStatus)The verify function is used to verify the received data and update the object state.
received (Array): Array of data objects to verify.receivedStatus (String): Status of the received data.void: The function does not return any value.var receivedData = [
{ name: "temperature", value: 22.5, timestamp: 1628765432 },
{ name: "humidity", value: 60, timestamp: 1628765432 }
];
var status = "ok";
sgx.verify(receivedData, status);
accept(name)The accept function is used to accept data with the specified name and save it in the results. Calling it may be necessary when using the put function in the code.
See also: put
name (String): Name of the data to accept.void: The function does not return any value.sgx.accept("temperature");
addCommand(targetEUI, payload, overwrite)The addCommand function is used to add a new command.
targetEUI (String): Unique identifier of the target device.payload (Object): Data (JavaScript object) to be sent as part of the command.overwrite (Boolean): Flag indicating whether to invalidate previous (not yet delivered) commands.void: The function does not return any value.var targetEUI = "00124B0004F12345";
var payload = { command: "activate", parameters: { duration: 10 } };
var overwrite = true;
sgx.addCommand(targetEUI, payload, overwrite);
addPlainCommand(targetEUI, payload, overwrite)The addPlainCommand function is used to add a new command in plain text format.
targetEUI (String): Unique identifier of the target device.payload (String): Text to be sent as part of the command.overwrite (Boolean): Flag indicating whether to invalidate previous (not yet delivered) commands.void: The function does not return any value.var targetEUI = "00124B0004F12345";
var payload = 'start'
var overwrite = false;
sgx.addPlainCommand(targetEUI, payload, overwrite);
addHexCommand(targetEUI, payload, overwrite)The addHexCommand function is used to add a new command with a payload in hexadecimal format.
targetEUI (String): Unique identifier of the target device.payload (String): Data in hexadecimal format to be sent as part of the command.overwrite (Boolean): Flag indicating whether to invalidate previous (not yet delivered) commands.void: The function does not return any value.var targetEUI = "00124B0004F12345";
var payload = "00FFAA01";
var overwrite = true;
sgx.addHexCommand(targetEUI, payload, overwrite);
addNotification(newType, newMessage)The addNotification function is used to add a new notification.
newType (String): Type of the new notification. Accepted values: info, warning, alert.newMessage (String): Content of the new notification.void: The function does not return any value.sgx.addNotification("info", "Device activated successfully.");
addVirtualData(newEUI, newName, newValue)The addVirtualData function is used to add new virtual data.
newEUI (String): Unique identifier of the new device.newName (String): Name of the new data.newValue (Mixed): Value of the new data.void: The function does not return any value.sgx.addVirtualData("00124B0004F67890", "virtualTemperature", 25);
clearData()The clearData function removes all received values from the queue.
None
void: The function does not return any value.getAverage(channelName, scope, newValue)The getAverage function is used to get the average value for a given channel.
channelName (String): Channel name.scope (Number): Value range.newValue (Number, optional): New value to include in calculations.Number: Average value for the given channel.var average = sgx.getAverage("temperature", 10);
var newAverage = sgx.getAverage("temperature", 10, 23.0);
getMinimum(channelName, scope, newValue)The getMinimum function is used to get the minimum value for a given channel.
channelName (String): Channel name.scope (Number): Value range.newValue (Number, optional): New value to include in calculations.Number: Minimum value for the given channel.var minimum = sgx.getMinimum("temperature", 10);
var newMinimum = sgx.getMinimum("temperature", 10, 18);
getMaximum(channelName, scope, newValue)The getMaximum function is used to get the maximum value for a given channel.
channelName (String): Channel name.scope (Number): Value range.newValue (Number, optional): New value to include in calculations.Number: Maximum value for the given channel.var maximum = sgx.getMaximum("temperature", 10);
var newMaximum = sgx.getMaximum("temperature", 10, 27);
getSum(channelName, scope, newValue)The getSum function is used to get the sum of values for a given channel.
channelName (String): Channel name.scope (Number): Value range.newValue (Number, optional): New value to include in calculations.Number: Sum of values for the given channel.var sum = sgx.getSum("temperature", 10);
var newSum = sgx.getSum("temperature", 10, 22);
getLastValue(channelName, skipNull)The getLastValue function is used to get the last value for a given channel.
channelName (String): Channel name.skipNull (Boolean): Skip null values. Optional parameter.Mixed: The last value for the given channel, or null if there is no data.var lastValue = sgx.getLastValue("temperature");
getLastData(channelName, skipNull)The getLastData function is used to get the last data for a given channel.
channelName (String): Channel name.skipNull (Boolean): Skip null values. Optional parameter. Defaults to skipNull==false.Object: The last data for the given channel.var lastData = sgx.getLastData("temperature");
getModulo(value, divider)The getModulo function is used to get the remainder of dividing the value by the divisor.
value (Number): Value to divide.divider (Number): Divisor.Number: Remainder of the division.var modulo = sgx.getModulo(10, 3); // 1
getOutput()The getOutput function is used to get processing results.
Mixed: Processing result.var output = sgx.getOutput();
getTimestamp(channelName)The getTimestamp function is used to get the timestamp for a given channel.
channelName (String): Channel name.Number: Timestamp for the given channel.var timestamp = sgx.getTimestamp("temperature");
getTimestampUTC(y, m, d, h, min, s)The getTimestampUTC function is used to get a UTC timestamp based on the provided parameters.
y (Number): Year.m (Number): Month.d (Number): Day.h (Number): Hour.min (Number): Minute.s (Number): Second.Number: UTC timestamp.var timestampUTC = sgx.getTimestampUTC(2024, 6, 5, 12, 0, 0);
getValue(channelName)The getValue function is used to get the value for a given channel.
channelName (String): Channel name.Mixed: Value for the given channel, or null if there is no data.var value = sgx.getValue("temperature");
getStringValue(channelName)The getStringValue function is used to get the string value for a given channel.
channelName (String): Channel name.String: String value for the given channel, or null if there is no data.var stringValue = sgx.getStringValue("temperature");
put(name, newValue, timestamp)The put function is used to store new data.
name (String): Data name.newValue (Mixed): New data value.timestamp (Number, optional): Timestamp of the data.void: The function does not return any value.sgx.put("temperature", 23.5);
sgx.put("temperature", 23.5, 1628765432);
Using the put function disables the mechanism of automatically accepting and saving received data into the database. If the put function is used in the data processor code, only the data
passed in the put and accept functions will be recorded in the database.
// Signomix received a request with data named `temperature` and `humidity`, but in the data
// processor script we also want to add the value of the `pressure` data
sgx.put("pressure", 1000)
// without the following lines, null values will be saved in the database for `temperature` and `humidity`
sgx.accept("temperature")
sgx.accept("humidity")
See also: accept
setState(newState)The setState function is used to set a new state for the device.
newState (String): New device state.void: The function does not return any value.sgx.setState("active");
setStatus(newStatus)The setStatus function is used to set a new status for the device.
newStatus (String): New device status.void: The function does not return any value.sgx.setStatus("offline");
reverseHex(hexStr)The reverseHex function is used to reverse the order of characters in a hexadecimal string.
hexStr (String): Hexadecimal string.String: Reversed hexadecimal string.var reversedHex = sgx.reverseHex("00FFAA01"); // "01AAFF00"
swap32(val)The swap32 function is used to change the byte order in a 32-bit number.
val (Number): 32-bit number.Number: Number with swapped byte order.var swapped = sgx.swap32(0x12345678); // 0x78563412
distance(latitude1, longitude1, latitude2, longitude2)The distance function is used to calculate the distance between two geographic points.
latitude1 (Number): Latitude of point 1.longitude1 (Number): Longitude of point 1.latitude2 (Number): Latitude of point 2.longitude2 (Number): Longitude of point 2.Number: Distance between the two points in meters.// distance between two points
var dist = sgx.distance(52.2296756, 21.0122287, 41.8919300, 12.5113300);
// distance of a point from the transmitted location
var dist = sgx.distance(2.2296756, 21.0122287, sgx.getValue("latitude"), sgx.getValue("longitude") );
homeDistance(latitude, longitude)The homeDistance function is used to calculate the distance between a given geographic point
and the device location point saved in its configuration.
latitude (Number): Latitude of the point.longitude (Number): Longitude of the point.Number: Distance between the two points in meters.// distance of the specified point from the location specified in the device configuration
var dist = sgx.homeDistance(41.8919300, 12.5113300);
// distance of the transmitted location from the location specified in the device configuration
var dist = sgx.homeDistance( sgx.getValue("latitude"), sgx.getValue("longitude") );