SFA Templates

SCADA Final Aggregator uses jinja2 template engine to serve server-side templates. You can use SFA templates for regular HTML, javascript and JSON data files. Both ui and pvt folders can contain template files, the difference is only that templates in ui are public while templates in pvt are served via SFA PVT.

Template files

All files with .j2 extension are processed as templates, index.j2 has more priority than index.html as the primary interface page.

Templates support all jinja2 functions and features, plus have some specific built-in variables and functions.

Template variables

The following variables are available in all templates:

  • All custom user-defined variables
  • server contains a dict with a system and current API key info (equal to SFA API test function result) plus an additional key remote_ip which contains either request IP address or value of X-Real-IP variable (if set by frontend server).
  • request contains CherryPy request object, e.g. display user agent:
{{ request.headers.get('User-Agent') }}

Template functions

All templates have the following built-in functions. Template functions never raise exceptions, instead they return None values.

groups

Get list of item groups

groups(g=None, p=None, k=None)

where:

The function is similar to SFA API groups except that if API key is not specified, the current key is used.

state

Get list of items and their state

state(i=None, g=None, p=None, k=None):

where:

  • i full item id (group/id), optional
  • g filter by group (use MQTT-style wildcards)
  • p item type (U for unit, S for sensor, LV for lvar), required
  • k API key (use key ID instead of key itself)

The function is similar to SFA API state except that if API key is not specified, the current key is used.

api_call

Allows to call any SFA API method directly.

api_call(method, params={}, k=None)

where:

  • method API method to call
  • params API call parameters
  • k API key (use key ID instead of key itself)

Example. Let’s warn user when specified UC controller is not connected:

{%- set controller = api_call(
            'get_controller', { 'i': 'uc/mws1-v1' }, 'masterkey') %}
{%- if not controller.connected %}
    UC controller is not connected
{%- endif %}