Keep previous value state

Prerequisites

Modules

Checkout branches

git checkout origin/osp-scripts-configuration .
git checkout origin/osp-collections-configuration .
git checkout origin/osp-variables-configuration .
git checkout origin/osp-web-configuration .

Description

In this tutorial you will learn how to create a script that get the previous value and save the current one to check if the state changed.

The full example is available on the example-keep-previous-value-state branch.

git checkout origin/example-keep-previous-value-state .

Steps

1. Create a collection to store previous values

root/previous_state_collection/schema.ospp

{
    "schema": {
        "type": "object",
        "properties": {
            "valueId": {
                "type": "string"
            },
            "content": {
                "type": ["integer", "boolean", "string", "number"]
            }
        }
    },
    "filters": [
        {
            "id": "all",
            "name": "All"
        }
    ]
}

root/previous_state_collection/schema.collections

{
    "moduleId": "modules.collections.collections-1",
    "collectionName": "values_previous",
    "indexes": [
        {
            "name": "valueId",
            "index": {
                "valueId": 1
            }
        },
        {
            "name": "content",
            "index": {
                "content": 1
            }
        }
    ],
    "filters": [
        {
            "id": "all",
            "query": {}
        }
    ],
    "autoIncrementFields": []
}

2. Create a library script to handle the get and set of the value

root/lib/valueStore.mjs

let currentId = null;

export function getPreviousContent(valueId) {
    const result = collections.listCustomFilter("root.value_previous_collection", 10, 0, "{valueId: \"" + valueId + "\"}");
    if (result.success === false) {
        log.warn("Fail to get content for value [{}] with error [{}].", valueId, result.message);
        return null;
    }

    const count = result.content.totalCount;
    if (count <= 0) {
        return null;
    }

    currentId = result.content.collections[0]._id;

    return result.content.collections[0].content;
}

export function saveCurrentContent(valueId, content) {
    if (currentId == null) {
        const result = collections.insert("root.value_previous_collection", { "valueId": valueId, "content": content }, "user");
        if (result.success === false) {
            log.warn("Fail to save content [{}] for value [{}] with error [{}].", content, valueId, result.message);
        }
    } else {
        const result = collections.update("root.value_previous_collection", currentId, [{ "field": "content", "operation": 'SET', "content": content }], "user")
        if (result.success === false) {
            log.warn("Fail to save content [{}] for value [{}] with error [{}].", content, valueId, result.message);
        }
    }
}

3. Create some values to show the behavior

Note

This example only shows the integer value, but the branch also contains the text, decimal and boolean values

root/value/integer/value.ospp

{
    "name" : "integer",
    "description" : "",
    "type" : "INTEGER"
}

root/value/integer/owner.variables

{
    "moduleId": "modules.variables.variables-1",
    "persistent": true,
    "default": 0
}

4. Create a script displaying the current and previous values

root/scripts/detached.scripts

{
    "moduleId": "modules.scripts.scripts-1",
    "sourceFile": "root/scripts/script.mjs"
}

root/scripts/script.mjs

import { getPreviousContent, saveCurrentContent } from '../lib/valueStore.mjs';

function main() {
    if (trigger.content != null) {
        const previousContent = getPreviousContent(trigger.id);

        log.info("Value [{}] has content [{}] and previously [{}]", trigger.id, trigger.content, previousContent);

        saveCurrentContent(trigger.id, trigger.content);
    }
}

main();

root/scripts/output.scripts

{
    "scriptId": "root.scripts"
}

4. Create the callback to call the script when the value changes

root/value/integer/callback.ospp

{
    "linkedOutputs": [
        {
            "outputId": "root.scripts"
        }
    ]
}

5. Add a dashboard to control the value

root/dashboard.web

{
    "moduleId": "modules.web.web-1",
    "title": "Home",
    "description": "OnSphere home",
    "tags": []
}

root/dashboard.view

Note

The dashboard also contains a view of the collection.

{
  "configuration": [
    {
      "type": "ObjectBrowser",
      "id": "pdwNZoi2",
      "title": ""
    },
    {
      "type": "BasicInputOutputValue",
      "id": "CasixrEW",
      "title": "",
      "valueSubscriptions": {
        "values": [
          {
            "id": "root.value.integer",
            "type": "INTEGER",
            "right": "READ_WRITE"
          }
        ]
      },
      "basicWidgetSettings": {
        "showLabel": true,
        "showTooltip": true,
        "showIcon": true,
        "showValue": true
      }
    },
    {
      "type": "CollectionTable",
      "id": "zi23FU7j",
      "title": "Test",
      "collectionTableWidgetSettings": {
        "defaultSchema": "root.previous_state_collection",
        "defaultFilter": "all",
        "disableDisplayTypes": [],
        "defaultDisplayType": "table",
        "disableFormPicker": false,
        "disableFormCreate": false,
        "disableFormEdit": false,
        "pageSize": 50,
        "pageSizes": [
          50,
          100,
          200
        ],
        "resizeMode": "widget",
        "disableToolbarTableRefresh": false,
        "disableSchemaUpdate": false,
        "disableViewUpdate": false,
        "disableFilterUpdate": false,
        "disableToolbar": false,
        "disableToolbarMenu": false,
        "disableSidePanel": false,
        "disableToolbarExport": false,
        "disableToolbarColumnShowHide": false,
        "disableToolbarFilterShowHide": false,
        "disableToolbarSummaryShowHide": false,
        "disableToolbarSearch": false,
        "disableToolbarColumnChooser": false,
        "disableToolbarClearFilter": false
      }
    },
    {
      "type": "BasicInputOutputValue",
      "id": "OKSnkwTG",
      "title": "",
      "valueSubscriptions": {
        "values": [
          {
            "id": "root.value.boolean",
            "type": "BOOLEAN",
            "right": "READ_WRITE"
          }
        ]
      },
      "basicWidgetSettings": {
        "showLabel": true,
        "showTooltip": true,
        "showIcon": true,
        "showValue": true
      }
    },
    {
      "type": "BasicInputOutputValue",
      "id": "FwNUfHIV",
      "title": "",
      "valueSubscriptions": {
        "values": [
          {
            "id": "root.value.string",
            "type": "TEXT",
            "right": "READ_WRITE"
          }
        ]
      },
      "basicWidgetSettings": {
        "showLabel": true,
        "showTooltip": true,
        "showIcon": true,
        "showValue": true
      }
    },
    {
      "valueSubscriptions": {
        "values": [
          {
            "id": "root.value.decimal",
            "type": "DECIMAL",
            "right": "READ_WRITE"
          }
        ]
      },
      "basicWidgetSettings": {
        "showLabel": true,
        "showTooltip": true,
        "showIcon": true,
        "showValue": true
      },
      "id": "cki4-4lr",
      "type": "BasicInputOutputValue",
      "title": ""
    }
  ],
  "layout": {
    "lg": [
      {
        "w": 6,
        "h": 4,
        "x": 0,
        "y": 0,
        "i": "pdwNZoi2"
      },
      {
        "w": 6,
        "h": 1,
        "x": 6,
        "y": 0,
        "i": "CasixrEW"
      },
      {
        "w": 12,
        "h": 2,
        "x": 0,
        "y": 4,
        "i": "zi23FU7j"
      },
      {
        "w": 6,
        "h": 1,
        "x": 6,
        "y": 1,
        "i": "OKSnkwTG"
      },
      {
        "w": 6,
        "h": 1,
        "x": 6,
        "y": 2,
        "i": "FwNUfHIV"
      },
      {
        "w": 6,
        "h": 1,
        "x": 6,
        "y": 3,
        "i": "cki4-4lr"
      }
    ]
  },
  "breakpoints": {
    "lg": 1200,
    "md": 996,
    "sm": 768,
    "xs": 480,
    "xxs": 0
  },
  "cols": {
    "lg": 12,
    "md": 10,
    "sm": 6,
    "xs": 4,
    "xxs": 2
  },
  "rowHeight": 150
}