Skip to main content

Tutorials

Abstract

Create a transform code package and assign it to a "createTransform" action

Tutorial on creating a JavaScript transform

This tutorial creates a transform that processes MQTT messages sent to the topic "robot1_temperature". The transform extracts a Celsius temperature from the JSON payload, converts it to Fahrenheit, and writes each temperature to the appropriate fields in the table "temperature_celsius" and "temperature_fahrenheit". The transform also creates a status field named temperature_status and populates it with information about the temperature, such as "cold" and "normal".

Send an MQTT message

Send a message to the MQTT topic named "robot1_temperature".

{
  "temperature": 21.111
}

When a record is inserted into an integration table, the source_payload field contains JSON data, such as the following JSON object, which contains a Fahrenheit temperature.

Create JavaScript code

Use the following tutorial to convert a Fahrenheit temperature to Celsius

The following code extracts the "temperature_celsius" property from the source_payload field of the current record in an integration table. It converts the Celsius temperature to Fahrenheit and stores the result in the temperature_fahrenheit field in the current record of an integration table. The temperature in Celsius is then stored in the temperature_celsius field in the integration table.

record.temperature_celsius = record.source_payload.temperature
record.temperature_fahrenheit = 
getFahrenheitFromCelsius(record.temperature_celsius)
record.temperature_status = 
calculateTemperatureStatus(record.temperature_fahrenheit)

function getFahrenheitFromCelsius(temperature_celsius){
  return (temperature_celsius * 1.8) + 32;
}
function calculateTemperatureStatus(temperature_fahrenheit){
  switch (true) {
    case (temperature_fahrenheit < 0):
      return "alert: too cold";
    case (temperature_fahrenheit < 32):
      return "cold";
    case (temperature_fahrenheit > 80):
      return "hot";
    case (temperature_fahrenheit > 140):
      return "alert: too hot";
    default:
      return "normal";
  }
}

Create a code package

Run the following "createCodePackage" action to create a JavaScript code package named "convertTemperature".

{
  "authToken": "MyTokenHere",
  "api": "admin",
  "action": "createCodePackage",
  "params": {
    "databaseName": "faircom",
    "ownerName": "admin",
    "codeName": "convertTemperature",
    "codeLanguage": "javascript",
    "codeType": "transform",
    "codeStatus": "developing",
    "description": "1. Copies the value from the temperature property in source_payload to the temperature_fahrenheit field.\n2. Converts temperature_fahrenheit into Celsius and stores it in the temperature_celsius field.\n3. Stores alerts about temperature in temperature_status field.",
    "metadata": {},
    "comment": "optional comment about the current version",
    "codeFormat": "utf8",
    "code": "// The server runs the following JavaScript code, which is registered in the server as a transform method.\nrecord.temperature_fahrenheit = record.source_payload.temperature\nrecord.temperature_celsius = getCelsiusFromFahrenheit(record.temperature_fahrenheit)\nrecord.temperature_status = calculateTemperatureStatus(record.temperature_fahrenheit)\n\nfunction getCelsiusFromFahrenheit(temperature_fahrenheit){\n    return (temperature_fahrenheit - 32) / 1.8;\n}\n\nfunction calculateTemperatureStatus(temperature_fahrenheit){\n    switch (true) {\n        case (temperature_fahrenheit < 0):\n            return \"alert: too cold\";\n        case (temperature_fahrenheit < 32):\n            return \"cold\";\n        case (temperature_fahrenheit > 80):\n            return \"hot\";\n        case (temperature_fahrenheit > 140):\n            return \"alert: too hot\";\n        default:\n            return \"normal\";\n    }\n}"  
  }
}

Create a transform

{
  "authToken": "MyTokenHere",
  "api": "hub",
  "action": "createTransform",
  "params": {
    "transformName": "transformFahrenheitToCelsius",
    "transformActions": [
      {
        "transformActionName": "transformStep-ConvertAndCategorizeTemperature",
        "transformService": "javascript",
        "inputFields": ["source_payload"],
        "outputFields": [
          "temperature_celsius",
          "temperature_fahrenheit",
          "temperature_status"
        ],
        "transformParams": {
          "codeName": "convertTemperature",
          "databaseName": "ctreeSQL",
          "ownerName": "admin",
          "codePackagePoperties": {},
          "outputFieldDefinitions": [
            {
              "name": "temperature_celsius",
              "type": "double"
            },
            {
              "name": "temperature_fahrenheit",
              "type": "double"
            },
            {
              "name": "temperature_status",
              "type": "varchar",
              "length": 100
            }
          ]
        }
      }
    ]
  }
}

"codePackageProperties" is an optional parameter that lets the user add run-time properties and values to JavaScript before the server runs the code When the value is an object, it contains properties and values that the server adds to the JavaScript engine.

"codePackageProperties": {
  "myCustomProperty1": "a string that the JavaScript code will use",
  "myCustomProperty2": 42
},