Script types and usage boundaries

Page scripts come from HQ VISU Designer screen packages and run in the browser for the current page. Use them for page initialization, page variables, template values, object color/visibility/text changes, and page-local interactions. Do not use page scripts as server-side scheduled jobs or direct database connectors.

Scripts

Scripts: Script types and usage boundaries

HQControl projects can contain page scripts, page-global scripts, and server-side system scripts. These script types have different execution locations, permission boundaries, and use cases.

Page scripts run with one screen page

Page scripts come from HQ VISU Designer screen packages and run in the browser for the current page. Use them for page initialization, page variables, template values, object color/visibility/text changes, and page-local interactions. Do not use page scripts as server-side scheduled jobs or direct database connectors.

Page-global scripts share browser helpers

Page-global scripts also run in the browser and are shared across pages in the same screen package. Use them for common functions, constants, navigation wrappers, reusable status transforms, and multi-page helpers. Page-specific workflows should remain in page scripts.

System scripts run in Integration Script

System scripts are HQControl Integration Script jobs. They run in the server-side Python environment and are suited for HTTP, MQTT, SQL, scheduled jobs, event triggers, and cross-system automation. They can call ReadTag, ReadTags, and WriteTag, but they do not manipulate browser DOM directly.

Common Python script examples

Use these templates in host-side Integration Script jobs for scheduled checks, HTTP notifications, third-party synchronization, and small field automations. Before enabling a trigger, run the script manually once and check output, recent execution records, and failure logs. For writes, include a reason and confirm the target tag allows control.

  • Read current values with typed helpers such as ReadTagBoolean / ReadTagDouble and always provide a fallback.
  • Guard data quality with ReadTagQualityGood so bad quality is not treated as a normal process value.
  • HTTP integration can use the bundled requests package; always set a timeout.
  • For batch reads, set deviceCodes, groupCodes, or limit instead of reading the whole site.
from datetime import datetime
import requests

tag_name = "Line01.MainMotor.SpeedPV"
speed = ReadTagDouble(tag_name, 0.0)
running = ReadTagBoolean("Line01.MainMotor.Running", False)
quality_good = ReadTagQualityGood(tag_name)
now_text = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

if not quality_good:
    print(f"{now_text} {tag_name} quality is bad, skip action")
elif running and speed > 80:
    payload = {
        "tag": tag_name,
        "speed": speed,
        "time": now_text,
        "message": "main motor speed high",
    }
    requests.post("http://127.0.0.1:9000/api/alarm", json=payload, timeout=3)

# Batch-read a bounded group for inspection or reporting
items = ReadTags({"deviceCodes": ["Line01"], "limit": 100})
print(f"read {len(items or [])} tags at {now_text}")

# Keep a reason for audit whenever the script writes a value
WriteTag("Line01.MainMotor.SpeedSP", 12.5, "number", reason="integration script setpoint update")

Python examples: network content and SQL read/write

Host-side scripts often read external HTTP/JSON content, synchronize data into a business database, or read recipes, schedules, and shift plans from SQL. Keep timeouts explicit, use parameterized SQL, close connections, and avoid hard-coding database passwords in the script. Prefer environment variables, script connection settings, or controlled config files.

  • Network content: use requests.get(..., timeout=3) and raise_for_status() so HTTP errors fail clearly.
  • SQL reads: use parameterized queries and handle empty results explicitly.
  • SQL writes: commit transactions and store tag value, quality, and timestamp together.
  • Drivers: use psycopg for PostgreSQL, pymysql for MySQL, and pyodbc for ODBC / SQL Server.
import os
from datetime import datetime
import requests
import psycopg

# 1) Fetch external JSON content
resp = requests.get('https://example.com/api/shift-plan', timeout=3)
resp.raise_for_status()
plan = resp.json()
target_speed = float(plan.get('targetSpeed', 0))

# 2) Read current HQControl tag state
tag_name = 'Line01.MainMotor.SpeedPV'
speed = ReadTagDouble(tag_name, 0.0)
quality = ReadTagQuality(tag_name, 'bad')
sample_time = datetime.now()

# 3) Read and write PostgreSQL; keep the DSN in env/config, not hard-coded
dsn = os.getenv('MES_PG_DSN', 'host=127.0.0.1 port=5432 dbname=mes user=mes password=change-me')
with psycopg.connect(dsn) as conn:
    with conn.cursor() as cur:
        cur.execute(
            'select order_no, product_code from work_orders where line_code = %s order by created_at desc limit 1',
            ('Line01',),
        )
        row = cur.fetchone()
        order_no = row[0] if row else ''

        cur.execute(
            'insert into hq_samples(tag_name, value, quality, target_speed, order_no, sampled_at) values (%s, %s, %s, %s, %s, %s)',
            (tag_name, speed, quality, target_speed, order_no, sample_time),
        )
    conn.commit()

print(f'synced {tag_name}={speed}, quality={quality}, target={target_speed}')

Where system script files are stored

Integration Script files are stored under configs/integration-scripts/integration-script/. Each script usually has a script content file and a metadata file: script-code.py stores the script content, while script-code.toml stores the name, runtime settings, connections, and editor metadata. Trigger configuration is stored as trigger-code.trigger.toml.

  • When copying a script to another project, copy both the .py file and the matching .toml file.
  • When copying a trigger, copy the .trigger.toml file and confirm the referenced script code exists in the target project.
  • Script codes must be unique inside the same module directory.
  • After copying, open Integration Script and check scripts, connections, triggers, and recent execution records.
configs/integration-scripts/
  integration-script/
    script-code.py
    script-code.toml
    trigger-code.trigger.toml

How Python packages are installed

The production installer bundles common Python packages offline: requests, psycopg, pymysql, pyodbc, paho-mqtt, websocket-client, and cryptography. Normal field scripts should import these packages directly without manual pip. Projects can also install third-party Python packages, or put project-local packages under the script working directory in libs, packages, or site-packages, then set Working Dir in the script configuration.

Can users install packages with pip

Yes, but pip must target the Python actually used by Integration Script. Check the Environment panel, then execute python -m pip install package-name with the corresponding interpreter. Do not copy a fixed path or put packages in arbitrary folders. Production environments should prefer offline wheels or maintained local package folders; online pip can be affected by network, proxy, permissions, native compilation requirements, and dependency conflicts. Restart Integration Script after package changes.

How local packages are discovered

At runtime, HQControl adds the temporary script directory, Working Dir, and libs, packages, and site-packages under Working Dir to PYTHONPATH. Put project-specific shared code in those folders and point Working Dir to that directory. Do not install dependencies dynamically in production scripts or bypass HQControl by editing the database, cache, or driver layer directly.

Where the offline package files live

The official installer already bundles the common Python dependencies offline, so they work right after installation; no internet download and no need to locate these packages by hand. For special third-party packages, add them through the local package directory described above.

Configure first, script only when needed

Devices, tags, alarms, trends, menus, popup templates, permissions, and language bundles should be configured first. Scripts should handle cross-system logic, condition combinations, external APIs, and limited automation that configuration cannot express cleanly.

HQControl Integration Script page
Figure 1: System script entry

Integration Script is the server-side system script entry, not the same as browser page scripts.