Smart Site
Home Docs Smart Data Collector Plugin Development Rules

Collector Plugin Development Rules

This article collects all hard rules for collector plugin development, for developers and AI coding assistants to follow when adding or modifying collector plugins and device-template variable configs. Two plugin kinds are covered:

Kind Form Typical protocols
Built-in collector TS package in the workspace, extends BaseCollectorService, compiled with the host modbus / mqtt / opcua / s7 / iec104 / bacnet / virtual
External collector Self-contained CommonJS package (package.json#collectorPlugin + index.js), uploaded & installed, forced child_process isolation custom protocols (e.g. jt808)

These rules are a quick reference; see Authoritative docs at the end. If any red line conflicts with an older document, the .qoder/rules/ contract files and AGENTS.md §5.2 rules 11/15 take precedence.


1. Device Template Config Contract (8 red lines)

Quick ref: .qoder/rules/template-config-contract.md; authoritative: Device Template Protocol-specific Configuration Specification.

Core principle: top-level fields of a template variable are a cross-protocol fixed contract; any protocol-specific parameter must live only in variable.config.<pluginId> and be passed through untouched end-to-end.

# Forbidden / Required
R1 Never add protocol-specific top-level fields to a template variable (e.g. mqttTopic)
R2 Never write keys outside the loader whitelist in a template — loadTemplateFile() silently drops them; config is the only extension slot
R3 New protocol parameters must go into config.<pluginId> and be registered in spec §4
R4 The loader must pass config through (same level as calc/simulation)
R5 DataTag.config must be simple-json — write objects, not strings
R6 Task registration must forward tag.config → tagConfig.config (together with transform/scale/offset)
R7 Plugins must centralize parsing in resolveXxxTagConfig() and fall back for legacy templates without config
R8 Never send read requests (or wait for private replies) on a business topic in the main collection path — report-only devices always time out

Adding a protocol parameter, 4 steps: register in spec §4 → write config.<pluginId>.<field> in the template → add parsing + fallback in resolveXxxTagConfig() → add 3 tests (with / without / missing field).


2. External Collector Plugin Contract (10 red lines)

Quick ref: .qoder/rules/collector-plugin-contract.md; authoritative: 00-方案-自定义协议插件 v1.5.

Core principle: external plugin = self-contained CommonJS package + mandatory child_process; connect() returns a real protocol handle; report-oriented protocols read only from cache; any new .js source must be explicitly registered in build & publish whitelists.

# Forbidden / Required
R1 Never require('@ss-node/*'); only Node built-ins + bundled dependencies (E-PLUGIN-012)
R2 protocol must be a safe identifier (lowercase letters/digits/underscore, 2–64 chars), not conflicting with BUILTIN_PROTOCOLS (modbus/mqtt/opcua/s7/iec104/bacnet/node_red/virtual etc.) or installed plugins (E-PLUGIN-005)
R3 isolationMode may only declare child_process; in_process/worker_thread are rejected or force-corrected (E-PLUGIN-009)
R4 i18n must be inlined in package.json#collectorPlugin.i18n; the current implementation does not read i18n/*.json files
R5 connect() must return a real protocol client object, never a {type,deviceId,config} wrapper; state goes in WeakMap<client,state>
R6 Never connect/disconnect inside readTag; reuse the same client handle the host passes in
R7 Report-oriented protocols (MQTT/JT808/IEC104) must not send read requests on the main collection path; background callbacks write cache, readTag only reads cache
R8 Data details ([RECV] etc.) must go through the setDataLogEmitter/emitDataLog data-log channel; the main log keeps only lifecycle + exceptions
R9 Adding any .js under ss-collector-sdk/src/** requires syncing two whitelists: ① copyFileSync in package.json#scripts.build; ② MINIFY_SKIP_BASENAMES in bin/publish-smart-site/module-exclusions.cjs
R10 Protocol parameters may only go into config.<pluginId> via tagConfigSchema; never write a protocol-specific form in the frontend

3. Connection Handle Contract (most common pitfall)

  • The connect() return value is the connection handle; the host passes it verbatim to readTag/writeTag/readTags/writeTags/disconnect and never inspects its structure.
  • Must return the real protocol client (mqtt.Client, net.Server, opcua session, etc.), never a wrapper {type,deviceId,config}. Otherwise client.subscribe/readArea/... are all undefined and collection fails silently with no error log (built-in mqtt/modbus/s7/opcua all hit this).
  • Handle lifecycle is managed by the host: all reads/writes reuse the same handle after connect; disconnect releases it.
  • Mandatory assertions: client received by readTag === return value of connect(); not a wrapper object; two consecutive reads do not rebuild the connection.
  • Types: SDK IProtocolClient is an opaque handle interface; ITagContext/IDeviceContext are plain objects safe to serialize across process boundaries.

4. i18n Rules

  • Fallback language is English: schema title/description are written in English (= en fallback).
  • Other languages live in CollectorPluginMeta.i18n (inlined manifest.i18n for external plugins); key naming:
    • meta.name / meta.description
    • conn.<field>.title|desc|ph|enum.<value> (device connection config)
    • tag.<field>.title|desc|ph|enum.<value> (variable-level config.<pluginId>)
  • Two independent prefixes: conn.*, tag.*; unknown locales fall back to en.
  • enum values are config values and are not rewritten — only same-order enumLabels are generated (missing label falls back to the raw value); the frontend fills the raw value back (numeric qos is restored by matching enum, not by the select's string).
  • After changes run node ss-services/ss-node/scripts/audit-plugin-i18n.cjs to verify all six locales have identical key sets.

5. Isolation, Logs and Data Channel

  • Isolation: external plugins are forced to child_process (shared memory is uncontrollable; CPU/memory cannot be limited); missing or invalid manifest values are normalized and corrected.
  • Three guards: ① normalize missing/invalid values to child_process; ② loading plugin-configs/<protocol>.json at startup must not override to in_process; ③ PUT /plugins/:protocol/config rejects non-child_process for external plugins (E-PLUGIN-009).
  • Data-log channel: collection details ([RECV]) go to a separate file data/logs/plugins/<protocol>/<protocol>-data-YYYY-MM-DD.log; in_process writes directly, isolated mode uses setDataLogEmitter → IPC back to the host; the main log keeps only lifecycle + exceptions.
  • On-disk layout: <SS_NODE_ROOT>/plugins/collectors/<protocol>/{current.json, <version>/...}; runtime config data/plugin-configs/<protocol>.json.
  • readTags/writeTags: optional; when absent the host degrades to per-point calls — correctness is unaffected, only protocol-level batching is lost.

6. Build & Publish Whitelist (high-frequency pitfall)

tsc does not compile .js source files — it only copies .ts output. The SDK's two standalone .js scripts must be registered in both places:

Script Purpose Registration
src/plugins/isolation/collector-worker.js isolation worker/child executor entry copyFileSync in package.json#scripts.build + MINIFY_SKIP_BASENAMES
src/plugin-kit/validator/smoke-runner.js install-validation smoke test (spawned process) same as above

Omission consequence: upload validation reports E-PLUGIN-007 Smoke runner not found: .../dist/plugin-kit/validator/smoke-runner.js (actual incident during JT808 plugin install on 2026-11-20). Register any new .js in both places per R9.


7. Development Checklist (confirm each item after every change)

  1. node --test test/*.test.js all green (including the three handle-contract assertions).
  2. Verify the entry with the dist smoke runner: node <sdk>/dist/plugin-kit/validator/smoke-runner.js <abs entry> → {"ok":true,"warnings":[]}.
  3. After pnpm --filter @ss-node/ss-collector-sdk build, confirm both .js scripts are present in dist.
  4. Validate the zip with Expand-Archive / the host installer; the package contains only deliverables.
  5. Upload in the admin UI → validation ok=true (E-PLUGIN-011 warning is expected and non-blocking) → install → source=external appears in /plugins → selecting the protocol on a device reads points.
  6. After i18n changes run audit-plugin-i18n.cjs to verify all six locales.
  7. Publishing is manual (bin\ci_ss_backand.bat); AI never publishes automatically.

Authoritative Docs

  • AGENTS.md §5.2 rule 11 (device template structure lock) and rule 15 (external collector plugin contract)
  • .qoder/rules/template-config-contract.md (8 red lines)
  • .qoder/rules/collector-plugin-contract.md (10 red lines + build whitelist table)
  • Device Template Protocol-specific Configuration Specification v1.1 (04-template)
  • 00-方案-自定义协议插件 v1.5 (05-collector)
  • Collector Plugin - Multi-mode Isolation Design, Collector Plugin - Data Log Channel Design, 01-设计-采集插件元数据国际化
  • Reference implementation: ss-services/ss-node/external-collectors/jt808/ (dynamic plugin source projects go under ss-node/external-collectors/<protocol>/; after install they land at <SS_NODE_ROOT>/plugins/collectors/<protocol>/<version>/)
  • Unified packer: bin/pack-external-collectors.bat [protocol] (no arg = all, --list/--check/--out <dir>; output at ss-run/ss/tools/external-collectors/, gitignored)