Configuration Options
Energy Manager IoT provides various configuration options to customize the library's behavior according to your application's needs.
EnergyManager Configuration
When creating an instance of the EnergyManager
class, you can provide configuration options to customize its behavior:
import { EnergyManager } from 'energy-manager-iot';
const manager = new EnergyManager({
topicPrefix: 'home/devices/',
mqttOptions: {
clientId: 'my-app-client',
clean: true
},
statusInterval: 30000,
autoReconnect: true
});
Configuration Options
Option | Type | Default | Description |
---|---|---|---|
topicPrefix |
string | 'device/' |
Prefix used for all MQTT topics |
mqttOptions |
object | {} |
MQTT client options (see below) |
statusInterval |
number | 60000 |
Interval (ms) for checking device status |
autoReconnect |
boolean | true |
Whether to automatically reconnect to MQTT |
MQTT Connection Options
When connecting to an MQTT broker, you can provide additional options:
await manager.connect('mqtt://localhost:1883', {
username: 'user',
password: 'password',
keepalive: 30,
reconnectPeriod: 5000,
connectTimeout: 30000
});
MQTT Connection Options
Option | Type | Default | Description |
---|---|---|---|
username |
string | - | Username for authentication |
password |
string | - | Password for authentication |
clientId |
string | auto-generated | Client identifier |
clean |
boolean | true |
Whether to create a clean session |
keepalive |
number | 60 |
Keepalive interval in seconds |
reconnectPeriod |
number | 5000 |
Reconnect period in milliseconds |
connectTimeout |
number | 30000 |
Connection timeout in milliseconds |
will |
object | - | Last Will and Testament message |
Device Configuration
When registering a device, you can provide configuration specific to that device:
manager.registerDevice(
'temp-sensor-01',
'Temperature Sensor',
DeviceType.SENSOR,
{
reportingInterval: 60,
sleepThreshold: 15,
autoWake: true,
securityLevel: 3
}
);
Device Configuration Options
Option | Type | Range | Description |
---|---|---|---|
reportingInterval |
number | 1-86400 | Status reporting interval in seconds |
sleepThreshold |
number | 0-100 | Battery level to enter sleep mode |
autoWake |
boolean | - | Whether to auto wake after sleep |
securityLevel |
number | 1-5 | Security level from low (1) to high (5) |
Logging Configuration
The Energy Manager IoT library uses Winston for logging. You can configure the logging system:
import { Logger } from 'energy-manager-iot';
// Set environment variable to control log level
process.env.LOG_LEVEL = 'debug'; // 'error', 'warn', 'info', 'debug', or 'trace'
// Create module-specific logger
const moduleLogger = Logger.child('my-module');
// Log with correlation ID for tracing related operations
const operationLogger = moduleLogger.withCorrelationId('op-123456');
operationLogger.info('Starting operation');
Log Levels
Level | Description |
---|---|
error |
Critical errors requiring immediate attention |
warn |
Important warnings that don't prevent functionality |
info |
General information about normal operation |
debug |
Detailed information useful for troubleshooting |
trace |
Extremely detailed information for development |
Advanced Configuration Examples
Secure MQTT Connection with TLS
await manager.connect('mqtts://broker.example.com:8883', {
username: 'user',
password: 'password',
rejectUnauthorized: true,
ca: fs.readFileSync('ca-certificate.pem')
});
Custom Last Will Message
await manager.connect('mqtt://localhost:1883', {
will: {
topic: 'system/manager/status',
payload: JSON.stringify({ status: 'offline', timestamp: Date.now() }),
qos: 1,
retain: true
}
});
Different Status Check Intervals Based on Environment
const isProd = process.env.NODE_ENV === 'production';
const manager = new EnergyManager({
statusInterval: isProd ? 300000 : 60000, // 5 minutes in prod, 1 minute in dev
mqttOptions: {
clientId: `energy-manager-${isProd ? 'prod' : 'dev'}-${Math.random().toString(16).substring(2, 8)}`
}
});
Environment Variables
The library recognizes the following environment variables:
Variable | Default | Description |
---|---|---|
LOG_LEVEL |
info |
Sets the logging level |
NODE_ENV |
- | Used to determine default log level |
Best Practices
- Use Unique Client IDs: Always use unique client IDs to avoid conflicts on the MQTT broker.
- Set Appropriate Intervals: Configure reporting intervals based on your application's needs and battery considerations.
- Enable Auto Reconnect: Keep
autoReconnect
enabled for production to handle network interruptions. - Secure Your Connection: Use TLS (mqtts://) with proper authentication in production.
- Tune Status Interval: Adjust
statusInterval
based on your application's needs to balance responsiveness and overhead.