Example Scripts

Scripts often deal with data coming from an asset, or from a scanned QR code, or from observation steps in a task. The simple case is where an asset is a product A and A has tasks to run. The data will be like this:

"data": {
  "id": {
    "sn": "SN-222",
    "type": "My Product",
    "pid": "123"
  },
  "components": {
    "123": {
      "model": "X13"
    }
  }
}

There is always an id object with pid (product_id), type, and optionally sn if there is an asset. Under components is the pid of this product, then any other data such as the model of this asset.

If a product has components that have tasks and their own data definitions, the structure will be more complicated like this if product 123 has a component 456 that defines the model variable and some additional information.

"data": {
  "id": {
    "sn": "SN-222",
    "type": "My Product",
    "pid": "456"
  },
  "components": {
    "123": {
      "components": {
        "456": {
          "model": "A",
          "pump": {"voltage": 120, "head": 12.5, "flow": 65},
          "motor": {"voltage": 240, "speed": 1500, "power": 450}
        }
      }
    }
  }
}

Using this data, here are some examples. Using the full path to a variable works, but sometimes that is hard or impractical. When the script and the task are in product 456 but it is a component in 123, the script doesn't know the product it is included in. So just using the name of the variable (or key) will find a match if it exists and return that value. This is a search, so if a variable name is used multiple times, the first one will be found. Use the full path if needed for duplicates.

console.log(Inskill.Session.hasData("components.123.components.456.model"));	// true
console.log(Inskill.Session.getData("components.123.components.456.model"));	// "A"

console.log(Inskill.Session.getData("model"));	// "A"
console.log(Inskill.Session.getData("head"));	// 12.5
console.log(Inskill.Session.getData("pump"));	// returns the pump object

console.log(Inskill.Session.getData("voltage"));	// 120
console.log(Inskill.Session.getData("components.123.components.456.motor.voltage"));	// 240

console.log(Inskill.Session.product_id);	// 456
console.log(Inskill.Session.src_product_id);	// 123
let path = `components.${Inskill.Session.product_id}.head`;    // use backtick ` for quotes

If the script is in a copilot that's used as a component in other copilots, the data will be like the example above, where the script is in product 456 but how do you know that "123" is the top level copilot?
Use the attributes Inskill.Session.product_id and Inskill.Session.src_product_id as shown. They will be the same if the product is used alone, and different if one contains another.
Be careful because this is an example that's 2 levels, but components can be many levels deep and you won't know all the ids in between.