Level 4 - Create a Device Type Template Worker

Level 4 - Create a Device Type Template Worker

Level 4 workers define device category templates that serve as abstract bases for brand-specific implementations.

A.1. Repository Naming Convention

Level 4 repositories follow strict naming patterns (see Naming Conventions).

miningos-tpl-wrk-{devicetype}

Examples:

  • miningos-tpl-wrk-powermeter — Power monitoring equipment

  • miningos-tpl-wrk-miner — Mining hardware

  • miningos-tpl-wrk-sensor — Environmental sensors

  • miningos-tpl-wrk-container — Mining containers/racks

The tpl (template) designation distinguishes Level 4 workers from concrete implementations.

A.2. Directory Structure

A Level 4 template repository must follow this structure (see Repository Structure — Standard Directory Structure):

miningos-tpl-wrk-{devicetype}/
├── config/
│   ├── base.thing.json.example       Device-specific configuration template
│   ├── common.json.example           Common worker settings
│   └── facs/
│       ├── net.config.json.example   Network/RPC configuration
│       └── store.config.json.example  Storage configuration
├── mock/
│   └── mock-control-agent.js         Mock device for testing
├── tests/
│   ├── cases/
│   │   └── {feature}.js              Test case definitions
│   ├── schema/
│   │   └── {feature}.js              Test schema validators
│   ├── {devicetype}.spec.js          Main test file (*.spec.js pattern)
│   ├── executors.js                  Test executors
│   └── utils.js                      Test utilities
├── workers/
│   ├── lib/
│   │   ├── alerts.js                 Alert processing logic
│   │   ├── base.js                   Device base class (optional)
│   │   ├── constants.js              Device-type constants
│   │   ├── stats.js                  Statistics processing
│   │   └── utils.js                  Utility functions
│   └── rack.{devicetype}.wrk.js      Main template worker
├── LICENSE                            Apache-2.0
├── README.md                          Documentation
├── package.json                       Dependencies
├── setup-config.sh                    Configuration setup script
└── worker.js                          Entry point

For testing structure details, see Testing & Linting Guidelines — Test Directory Structure.

A.3. Package.json Structure

For linting and test script conventions, see Testing & Linting Guidelines.

A.4. Main Template Worker Implementation

The main worker file (workers/rack.{devicetype}.wrk.js) extends Level 3. For JSDoc annotation standards, see Code Documentation Standards:

A.5. Abstract Methods Contract

Level 4 templates inherit abstract methods from Level 3 that must be implemented by Level 5 workers:

Method
Purpose
Base Behavior
Level 5 Requirement

async connectThing(thg)

Establish connection to physical device

No-op (empty stub)

Implement connection logic; return 0 on failure

async collectThingSnap(thg)

Collect current device state snapshot

Throws ERR_IMPL_UNKNOWN

Must implement; return Object with stats and config

selectThingInfo()

Select device info for API responses

Returns empty object {}

Override to return device-specific details

async disconnectThing(thg)

Gracefully disconnect from device

Calls thg.ctrl.close() if available

Override only if custom cleanup needed

async releaseIpThing(thg)

Release IP/network resources for device

No-op (empty stub)

Override if device requires IP resource cleanup

Note: connectThing is a no-op stub in Level 3. Level 5 implementations should return 0 from connectThing when connection fails to signal the failure to the snapshot collection loop.

Optional Lifecycle Hooks:

Hook
When Called
Base Behavior

async registerThingHook0(thg)

After new device registration

No-op

async updateThingHook0(thg, thgPrev)

After device configuration update

Stores info changes to DB

async forgetThingHook0(thg)

Before device removal

No-op

async setupThingHook0(thg)

During device setup/loading

No-op

async setupThingHook1(thg)

After loading last state from DB

No-op

async collectSnapsHook0()

After snapshot collection cycle

No-op

async tailLogHook0(logs, req)

During log tail operations

No-op

Optional Extension Methods:

Method
Purpose
Base Behavior

_getWrkExtData(args)

Provide custom worker extension data for getWrkExtData RPC

Returns empty object {}

A.6. Statistics Timeframe Configuration

Level 4 templates can configure additional statistics collection timeframes through two mechanisms:

Code-Defined Timeframes (scheduleAddlStatTfs)

Set in the init() method for timeframes that are integral to the device type:

Config-Defined Timeframes (scheduleAddlStatConfigTfs)

Set in base.thing.json for operator-customizable timeframes:

Both arrays are merged with the default timeframes from miningos-lib-stats during startup.

A.7. Configuration Templates

For complete configuration file documentation, see Repository Structure — Key Files Explained.

base.thing.json.example

common.json.example

A.8. Testing Infrastructure

Level 4 templates must provide testing infrastructure that Level 5 implementations inherit. Test files must use the .spec.js extension to match the glob pattern in package.json. For complete testing guidelines, see Testing & Linting Guidelines.

tests/utils.js

tests/{devicetype}.spec.js

A.9. Worker Entry Point

worker.js

A.10. Level 4 Checklist


Level 4 - Create a Device Type Template Worker

Level 4 workers define device category templates that serve as abstract bases for brand-specific implementations.

A.1. Repository Naming Convention

Level 4 repositories follow strict naming patterns:

Examples:

  • miningos-tpl-wrk-powermeter — Power monitoring equipment

  • miningos-tpl-wrk-miner — Mining hardware

  • miningos-tpl-wrk-sensor — Environmental sensors

  • miningos-tpl-wrk-container — Mining containers or racks

The tpl (template) designation distinguishes Level 4 workers from concrete implementations.

A.2. Directory Structure

A Level 4 template repository must follow this structure:

A.3. Package.json Structure

To create a package.json file for a Level 4 template:

  1. Open a text editor and create the following JSON structure:

  2. Replace {devicetype} with your specific device category (e.g., powermeter).

  3. Update the author and maintainers fields with your information.

  4. Save the file as package.json in your repository root.

A.4. Main Template Worker Implementation

The main worker file (workers/rack.{devicetype}.wrk.js) extends Level 3: To implement the main worker file:

  1. Create the file at workers/rack.{devicetype}.wrk.js.

  2. Add the following structure, replacing {DeviceType} with your device category (e.g., PowerMeter):

A.5. Abstract Methods Contract

Level 4 templates inherit abstract methods from Level 3 that must be implemented by Level 5 workers:

Method
Purpose
Base Behavior
Level 5 Requirement

async connectThing(thg)

Establish connection to physical device

No-op (empty stub)

Implement connection logic; return 0 on failure

async collectThingSnap(thg)

Collect current device state snapshot

Throws ERR_IMPL_UNKNOWN

Must implement; return Object with stats and config

selectThingInfo()

Select device info for API responses

Returns empty object {}

Override to return device-specific details

async disconnectThing(thg)

Gracefully disconnect from device

Calls thg.ctrl.close() if available

Override only if custom cleanup needed

async releaseIpThing(thg)

Release IP/network resources for device

No-op (empty stub)

Override if device requires IP resource cleanup

Note: connectThing is a no-op stub in Level 3. Level 5 implementations should return 0 from connectThing when connection fails to signal the failure to the snapshot collection loop.

Optional Lifecycle Hooks:

Hook
When Called
Base Behavior

async registerThingHook0(thg)

After new device registration

No-op

async updateThingHook0(thg, thgPrev)

After device configuration update

Stores info changes to DB

async forgetThingHook0(thg)

Before device removal

No-op

async setupThingHook0(thg)

During device setup or loading

No-op

async setupThingHook1(thg)

After loading last state from DB

No-op

async collectSnapsHook0()

After snapshot collection cycle

No-op

async tailLogHook0(logs, req)

During log tail operations

No-op

Optional Extension Methods:

Method
Purpose
Base Behavior

_getWrkExtData(args)

Provide custom worker extension data for getWrkExtData RPC

Returns empty object {}

A.6. Statistics Timeframe Configuration

Level 4 templates can configure additional statistics collection timeframes through two mechanisms:

Code-Defined Timeframes (scheduleAddlStatTfs)

Set in the init() method for timeframes that are integral to the device type:

  1. In your main worker file, modify the init() method:

Config-Defined Timeframes (scheduleAddlStatConfigTfs)

Set in base.thing.json for operator-customizable timeframes:

  1. Edit your config/base.thing.json.example file:

Both arrays are merged with the default timeframes from miningos-lib-stats during startup.

A.7. Configuration Templates

base.thing.json.example

  1. Create or edit the config/base.thing.json.example file:

  2. Replace {devicetype} with your device category.

common.json.example

  1. Create the config/common.json.example file:

A.8. Testing Infrastructure

Level 4 templates must provide testing infrastructure that Level 5 implementations inherit. Test files must use the .spec.js extension to match the glob pattern in package.json.

tests/utils.js

To extend the parent test infrastructure:

  1. Create tests/utils.js:

tests/{devicetype}.spec.js

To create the main test file create a tests/{devicetype}.spec.js file:

A.9. Worker Entry Point

worker.js

  1. Create the worker.js entry point file:

A.10. Level 4 Checklist


Last updated