The logics module manages logic instances — programmable circuits built with the visual Logic module editor that run as standalone processes on the gateway. Each logic instance reacts to inputs, maintains state, and writes outputs through the routing engine.
The API exposes lifecycle control (start/stop/restart), CRUD on logic definitions, and runtime introspection (CPU/memory usage, debugging, manual block triggering).
Load it with:
peer('supervision').require('logics', function(err, logics){
// logics.getLogics(...), logics.startLogic(...), logics.exec(...)
})
getLogics(callback)Return all logic instances as an object keyed by logic id.
logics.getLogics(function(err, all){
Object.keys(all).forEach(function(id){
console.log(id, all[id].name, all[id].state)
})
})
getLogicsLight(callback)Same as getLogics but returns only minimal fields (id, name, description, cluster, log medium, log level). Use this for UI listings to avoid transferring the full configuration.
getLogic(logicKey, callback)Return a single logic by id, name, or object.
getIds(callback)Return only the list of logic ids — fastest way to enumerate available logics.
insertLogic(logic, callback)Create a new logic instance.
logics.insertLogic({
name : 'Boiler regulation',
description : 'PID loop on flow temperature',
json : {
// logic-specific configuration: blockly XML, parameters, references…
}
}, function(err, newId){
console.log('logic created:', newId)
})
updateLogic(logic, callback) / deleteLogic(logicKey, callback)Modify or remove a logic instance.
startLogic(logicKey, callback) / stopLogic(logicKey, callback) / restartLogic(logicKey, callback)Start, stop, or restart a logic process. Each logic runs in its own isolated process supervised by PM2 — restarting one logic does not affect the others.
logics.restartLogic(7, function(err){
if(err) console.error(err)
})
The logicState event fires with 'connecting', then 'connected' once the logic is running.
cpuUsage(logicKey, callback) / memoryUsage(logicKey, callback)Return the CPU and memory usage of a running logic. Both methods return an error if the logic is not currently connected.
logics.cpuUsage(7, function(err, usage){
console.log('CPU:', usage)
})
getStats(callback)Return aggregated statistics across all logics — runtime, restart count, error count.
getCrashLog(logicKey, callback)Retrieve the last crash dump for a logic. Useful when a logic keeps restarting.
These methods let you simulate inputs and gate activations on a running logic — typically used by the visual editor for live debugging.
exec(logicKey, blocId, key, value, type, callback)Inject a value into a specific block of a running logic.
| Param | Description |
|---|---|
logicKey |
Logic to target |
blocId |
Id of the block inside the logic |
key |
Input port name |
value |
Value to inject |
type |
'input' to feed an input, 'output' to override an output |
logics.exec(7, 'block_42', 'setpoint', 21.5, 'input', function(err){
if(err) console.error(err)
})
gateOn(logicKey, blocId, callback) / gateOff(logicKey, blocId, callback)Open or close a gate block — used for conditional execution paths in the logic.
startDebugger(json, callback)Spawn a logic process in debug mode. This boots the logic with a Node.js inspector port attached, allowing the visual editor (or any standard Node.js debugger client) to connect, set breakpoints, and step through the logic code.
The json object must include the logic id and any parameters needed to bootstrap it. The callback returns the debug port the logic is listening on.
| Event | Arguments | Description |
|---|---|---|
logicInserted |
(logic) |
New logic created |
logicUpdated |
(newLogic, oldLogic) |
Logic modified |
logicDeleted |
(logic) |
Logic removed |
logicState <logicId> |
(state) |
Logic state changed ('connecting', 'connected', 'disconnected') |
logicInput <logicId> |
({type, bloc, key, value, isInput}) |
Internal input event — emitted by exec |
logics.getIds(function(err, ids){
var i = 0
function next(){
if(i >= ids.length) return console.log('done')
logics.restartLogic(ids[i++], next)
}
next()
})
logics.getIds(function(err, ids){
setInterval(function(){
ids.forEach(function(id){
logics.cpuUsage(id, function(err, cpu){
if(!err) console.log(id, cpu)
})
})
}, 5000)
})
logics.on('logicState', function(logicId, state){
if(state === 'disconnected'){
logics.getCrashLog(logicId, function(err, log){
console.error('logic', logicId, 'crashed:', log)
})
}
})