The definition of a data source (device) in Signomix allows you to define code for the data processor. When new data is received from this source, the data processor runs the JavaScript interpreter and executes the code defined for this data source.
When creating code for the processor, the user can use the functions of the sgx
library built into the data processor. Below is the documentation of this library.
verify(received, receivedStatus)
The verify
function is used to verify the received data and update the status of the object.
received
(Array): An array of data objects for verification.receivedStatus
(String): The 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 store it in the results. Its call may be necessary when the put
function is used in the code.
See also: put
name
(String): The 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): The unique identifier of the target device.payload
(Object): The data (JavaScript object) to be sent as part of the command.overwrite
(Boolean): Flag indicating whether to invalidate previous (not yet transmitted) 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): The text to be sent as part of the command.overwrite
(Boolean): Flag indicating whether to invalidate previous (not yet transmitted) 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 payload in hexadecimal format.
targetEUI
(String): The unique identifier of the target device.payload
(String): Hexadecimal format data to be sent as part of the command.overwrite
(Boolean): Flag indicating whether to invalidate previous (not yet transmitted) 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): The type of the new notification. Accepted values: info
,warning
,alert
.newMessage
(String): The 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): The unique identifier of the new device.newName
(String): The name of the new data.newValue
(Mixed): The value of the new data.void
: The function does not return any value.sgx.addVirtualData("00124B0004F67890", "virtualTemperature", 25)
getAverage(channelName, scope, newValue)
The getAverage
function is used to get the average value for a given channel.
channelName
(String): The name of the channel.scope
(Number): The range of the value.newValue
(Number, optional): The new value to be included in the calculation.Number
: The average value for a 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): The name of the channel.scope
(Number): The range of the value.newValue
(Number, optional): The new value to be included in the calculation.Number
: The minimum value for a 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): The name of the channel.scope
(Number): The range of the value.newValue
(Number, optional): The new value to be included in the calculation.Number
: The maximum value for a 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): The name of the channel.scope
(Number): The range of values.newValue
(Number, optional): The new value to be included in the calculation.Number
: The sum of the values for a 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 channel.
channelName
(String): The name of the channel.skipNull
(Boolean): Skip null
values. Optional parameter.Mixed
: The last value for the 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): The name of the channel.skipNull
(Boolean): Skip null
values. Optional parameter. If missing, 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 from dividing the value by the divider.
value
(Number): The value to be divided.divider
(Number): The divider.Number
: The remainder of the divider.var modulo = sgx.getModulo(10, 3); // 1
getOutput()
The getOutput
function is used to get the results of processing.
Mixed
: the result of the processing.var output = sgx.getOutput()
getTimestamp(channelName)
The getTimestamp
function is used to get the timestamp for a given channel.
channelName
(String): The name of the channel.Number
: The timestamp for the given channel.var timestamp = sgx.getTimestamp("temperature")
getTimestampUTC(y, m, d, h, min, s)
The getTimestampUTC
function is used to get the UTC timestamp based on the given 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): The name of the channel.Mixed
: The value for the channel, or null
if no data is available.var value = sgx.getValue("temperature")
getStringValue(channelName)
The getStringValue
function is used to get the text value for a given channel.
channelName
(String): The name of the channel.String
: The text value for the given channel, or null
if no data is available.var stringValue = sgx.getStringValue("temperature")
put(name, newValue, timestamp)
The put
function is used to put new data.
name
(String): Name of the data.newValue
(Mixed): The new value of the data.timestamp
(Number, optional): The 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 for automatically accepting and storing received data in the database. If the put
function is used in the code of the data processor then in the database
only the data passed in the put
and accept
functions will be recorded.
// Signomix received a request with data named `temperature` and `humidity`, but in the // data processor script we want to add a value for the data `pressure` sgx.put("pressure", 1000) // without the following lines, null values for `temperature` and `humidity` will be recorded in the database sgx.accept("temperature") sgx.accept("humidity")
See also accept
setState(newState)
The setState
function is used to set the new state of the device.
newState
(String): The new state of the device.void
: The function does not return any value.sgx.setState("active")
setStatus(newStatus)
The setStatus
function is used to set the new status of 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
: The inverted hexadecimal string.var reversedHex = sgx.reverseHex("00FFAA01"); // "01AAFF00"
swap32(val)
The swap32
function is used to change the byte order of a 32-bit number.
val
(Number): A 32-bit number.Number
: A number with the byte order changed.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): The longitude of point 1.latitude2
(Number): Latitude of point 2.longitude2
(Number): Longitude of point 2.Number
: The distance between two points in meters.// the distance between two points var dist = sgx.distance(52.2296756, 21.0122287, 41.8919300, 12.5113300); // the distance of a point from the uploaded 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 location point of the device stored in its configuration.
latitude
(Number): Latitude of the point.longitude
(Number): The longitude of the point.Number
: The distance between 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 uploaded location from the location specified in the device configuration var dist = sgx.homeDistance( sgx.getValue("latitude"), sgx.getValue("longitude") )