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',
driver : 'logic.js',
active : 1, // 0 (default): created but not started
json : { scenario: scenarioString } // the graph saved by the logic editor, as a JSON string
}, function(err, newId){
console.log('logic created:', newId)
})
driver: 'logic.js' is required, and name must be a text that is not a number. Draw the graph in the logic editor. Through the API, copy json.scenario from an existing logic (getLogic) rather than writing it by hand.
updateLogic(logic, callback) / deleteLogic(logicKey, callback)Modify or remove a logic instance.
updateLogic merges the given fields into the stored logic and restarts the logic when json, active or name change (add doNotRestartDriver: true to avoid the restart).
startLogic(logicKey, callback) / stopLogic(logicKey, callback) / restartLogic(logicKey, callback)Start, stop, or restart a logic process. startLogic and stopLogic also save the logic's active flag: a stopped logic stays stopped after a reboot. restartLogic stops then starts the logic, so it is active afterwards. Logic processes are started by the supervision; logics that share a cluster run in the same process.
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 the error not ready if the logic is not currently connected.
cpuUsage returns { user, system } in cumulated microseconds (compare two calls to get a load); memoryUsage returns { rss, heapTotal, heapUsed, … } in bytes.
logics.cpuUsage(7, function(err, usage){
console.log('CPU:', usage)
})
getStats(callback)Returns, for each logic id, how many times the logic entered each state since the supervision started, and when it last did: { "7": { connecting: 4, connecting_date: 1764000000000, connected: 4, connected_date: …, disconnected: 3, disconnected_date: … } }. A growing disconnected count means the logic keeps failing. Takes only a callback.
getCrashLog(logicKey, callback)Retrieve the last crash dump for a logic. Useful when a logic keeps restarting. See also Running, monitoring and troubleshooting circuits.
getLogicLogLevel(logicKey, callback) / setLogicLogLevel(logicKey, level, callback) / getDriverLogs(logicKey, options, callback)Read or change the log level of a logic while it runs, without a restart. Levels are error, warn, info and log.
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 |
Index of the block in the graph |
key |
Port name (input0, set0, output0…) |
value |
Value to inject. Pass 'Ø' for undefined. |
type |
'input': the value is emitted by the block output that feeds that input (every block linked to that output receives it). Any other type forces the output key of block blocId. |
logics.exec(7, 3, 'input0', 21.5, 'input', function(err){
if(err) console.error(err)
})
The callback only confirms that the command was queued.
gateOn(logicKey, blocId, callback) / gateOff(logicKey, blocId, callback)Enable or disable a block of the logic.
startDebugger(json, callback)Runs a temporary copy of a logic in debug mode (used by the logic editor). json is the logic configuration ({ scenario: … }). The callback returns a controller: { id, stop(cb), pause(cb), cont(cb), pauseLink(linkId, cb), contLink(linkId, cb), exec(blocId, key, value, type, cb), gateOn(blocId, cb), gateOff(blocId, cb), on(event, fn), off(event, fn) }. The debug session stops when the caller disconnects.
exec, gateOn and gateOff work as described in 5. Triggering blocks manually.
| 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.getLogicsLight(function(err, all){
var ids = Object.keys(all).filter(function(id){ return all[id].active })
var i = 0
function next(){
if(i >= ids.length) return console.log('done')
logics.restartLogic(ids[i++], next)
}
next()
})
Only active logics are restarted, so logics stopped on purpose stay stopped.
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)
})
}
})