/* * enable() method * * enable <node> * */ int baselineEnable(nbCELL context,void *skillHandle,BTree *tree){ int remaining; time_t utime; if(tree->options&BTREE_OPTION_TRACE) nbLogMsg(context,0,'T',"baselineEnable() called for baseline %s",tree->directory); tree->synapse=nbSynapseOpen(context,skillHandle,tree,NULL,treeAlarm); treeLoad(context,skillHandle,tree); // load baseline // set timer time(&utime); remaining=tree->interval-utime%tree->interval; nbSynapseSetTimer(context,tree->synapse,remaining); nbLogMsg(context,0,'I',"Enabled baseline %s",tree->directory); nbLogFlush(context); return(0); }
static void treeAlarm(nbCELL context,void *skillHandle,void *nodeHandle,nbCELL cell){ BTree *tree=(BTree *)nodeHandle; int remaining; time_t utime; if(tree->period>=0){ // store baseline treeStore(context,skillHandle,tree,NULL); } treeLoad(context,skillHandle,tree); // load baseline // reset timer time(&utime); remaining=tree->interval-utime%tree->interval; //nbLogMsg(context,0,'T',"treeAlarm() called for baseline %s - interval=%d,remaining=%d",tree->directory,tree->interval,remaining); nbSynapseSetTimer(context,tree->synapse,remaining); }
int main( int argc, char *argv[] ) { nbCELL context; nbCELL xCell; nbCELL synapseAlertCell; nbCELL synapseAlarmCell; int i; context = nbStart( argc, argv ); TEST( "Testing a synapse alert - response to cell change" ); nbCmd( context, "define x cell a + b;", NB_CMDOPT_ECHO ); nbCmd( context, "show a,b,x;", NB_CMDOPT_ECHO ); nbCmd( context, "assert a=13,b=100;", NB_CMDOPT_ECHO ); nbCmd( context, "show a,b,x;", NB_CMDOPT_ECHO ); showTermValue( context, "x" ); xCell = nbTermLocate( context, "x" ); if( xCell == NULL ) { nbLogMsg( context, 0, 'E', "Unable to local x cell" ); return( 1 ); } synapseAlertCell = nbSynapseOpen( context, NULL, NULL, xCell, myAlert ); nbCmd( context, "show a,b,x;", NB_CMDOPT_ECHO ); nbCmd( context, "assert a=14;", NB_CMDOPT_ECHO ); nbCmd( context, "show a,b,x;", NB_CMDOPT_ECHO ); TEST( "Testing synapse alert on time condition along with a synapse alarm" ); nbCmd( context, "assert x==~(4s);", NB_CMDOPT_ECHO ); synapseAlarmCell = nbSynapseOpen( context, NULL, NULL, NULL, myAlarm ); nbSynapseSetTimer( context, synapseAlarmCell, 3 ); // because we have not called nbServe yet, we need to tell the rule engine about advancing time for(i=0;i<20;i++) { sleep(1); nbClockAlert(); // WARNING: This function is deprecated and will be replaced in 0.9.04 } TEST( "Testing a synapse alert and alarm with the rule engine in control of time" ); nbCmd( context, "define EndIt when(~(10s)):stop;", NB_CMDOPT_ECHO ); nbSynapseSetTimer( context, synapseAlarmCell, 5 ); char *argvServe[2]={"eSynapse","-s"}; nbServe( context, 2, argvServe ); nbSynapseClose( context, synapseAlertCell ); // release the synapse nbSynapseClose( context, synapseAlarmCell ); // release the synapse return ( nbStop( context ) ); }