Added toString metods, and some robustness fixes in TriggerManager

This commit is contained in:
Ziver Koc 2017-03-06 17:59:38 +01:00
parent 2608ad0bfd
commit 74bdd33a9c
3 changed files with 23 additions and 7 deletions

View file

@ -6,7 +6,6 @@ import se.hal.struct.TriggerFlow;
import zutil.log.LogUtil;
import zutil.plugin.PluginManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@ -40,7 +39,9 @@ public class TriggerManager {
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
evaluateAndExecute();
try {
evaluateAndExecute();
} catch (Exception e){ logger.log(Level.SEVERE, "Trigger Evaluation Thread has Crashed", e); }
}
}, 0, interval, TimeUnit.MILLISECONDS);
}
@ -74,7 +75,7 @@ public class TriggerManager {
* evaluation of a trigger returns true then its execute method will be called.
*/
public synchronized void evaluateAndExecute() {
for (int i = 0; i < triggerFlows.size(); i++) { // avoid foreach as triggerFlow can change while we are running
for (int i = 0; i < triggerFlows.size(); i++) { // avoid foreach as triggerFlows can change while we are running
TriggerFlow flow = triggerFlows.get(i);
if (flow.evaluate()) {
logger.fine("Flow "+ flow.getId() +" evaluated true, executing actions");

View file

@ -45,6 +45,11 @@ public class SendEventAction implements HalAction {
public String toString(){
return "Send event: "+ eventId +" with data: "+ data;
DBConnection db = HalContext.getDB();
Event event = null;
try{ event = Event.getEvent(db, eventId); } catch (Exception e){} //ignore exception
return "Send event: "+ eventId +
" ("+(event!=null ? event.getName() : null)+")" +
" with data: "+ data;
}
}

View file

@ -5,6 +5,8 @@ import zutil.CronTimer;
import zutil.ui.Configurator;
import zutil.ui.Configurator.PreConfigurationActionListener;
import java.text.SimpleDateFormat;
/**
*
*/
@ -30,19 +32,27 @@ public class DateTimeTrigger implements HalTrigger,PreConfigurationActionListene
@Override
public void preConfigurationAction(Configurator configurator, Object obj) {
cronTimer = new CronTimer(minute, hour, dayOfMonth, month, dayOfWeek, year);
reset();
}
@Override
public boolean evaluate() {
if (cronTimer == null)
return false;
if (timeOut < 0)
if (System.currentTimeMillis()-timeOut > 60*1000) // have we passed the timeout by one minute?
reset();
return timeOut < System.currentTimeMillis();
return timeOut <= System.currentTimeMillis();
}
@Override
public void reset() {
timeOut = cronTimer.next();
if (cronTimer != null)
timeOut = cronTimer.next();
}
public String toString(){
return //"Cron: \""+minute+" "+hour+" "+dayOfMonth+" "+month+" "+dayOfWeek+" "+year+"\" "+
"Next timeout: "+
(timeOut>0 ? new SimpleDateFormat("yyyy-MM-dd HH:mm").format(timeOut) : timeOut);
}
}