use of java.util.Timer in project openhab1-addons by openhab.
the class DmxController method start.
/**
* {@inheritDoc}
*/
@Override
public void start() throws Exception {
logger.trace("Starting Dmx transmitter ...");
transmitter = new DmxTransmitter(this);
transmitterTimer = new Timer(true);
transmitterTimer.schedule(transmitter, 0, TRANSMIT_FREQUENCY_MS);
logger.trace("Dmx transmitter started.");
}
use of java.util.Timer in project openhab1-addons by openhab.
the class MaxCulBinding method internalReceiveCommand.
/**
* @{inheritDoc
*/
@Override
protected void internalReceiveCommand(final String itemName, Command command) {
Timer pairModeTimer = null;
MaxCulBindingConfig bindingConfig = null;
for (MaxCulBindingProvider provider : super.providers) {
bindingConfig = provider.getConfigForItemName(itemName);
if (bindingConfig != null) {
break;
}
}
logger.debug("Received command " + command.toString() + " for item " + itemName);
if (bindingConfig != null) {
logger.debug("Found config for " + itemName);
switch(bindingConfig.getDeviceType()) {
case PAIR_MODE:
if ((command instanceof OnOffType)) {
switch((OnOffType) command) {
case ON:
/*
* turn on pair mode and schedule disabling of pairing
* mode
*/
pairMode = true;
TimerTask task = new TimerTask() {
@Override
public void run() {
logger.debug(itemName + " pairMode timeout executed");
pairMode = false;
eventPublisher.postUpdate(itemName, OnOffType.OFF);
}
};
pairModeTimer = timers.get(itemName);
if (pairModeTimer != null) {
pairModeTimer.cancel();
timers.remove(itemName);
}
pairModeTimer = new Timer();
timers.put(itemName, pairModeTimer);
pairModeTimer.schedule(task, pairModeTimeout);
logger.debug(itemName + " pairMode enabled & timeout scheduled");
break;
case OFF:
/*
* we are manually disabling, so clear the timer and the
* flag
*/
pairMode = false;
pairModeTimer = timers.get(itemName);
if (pairModeTimer != null) {
logger.debug(itemName + " pairMode timer cancelled");
pairModeTimer.cancel();
timers.remove(itemName);
}
logger.debug(itemName + " pairMode cleared");
break;
}
} else {
logger.warn("Command not handled for " + bindingConfig.getDeviceType() + " that is not OnOffType");
}
break;
case LISTEN_MODE:
if (command instanceof OnOffType) {
this.messageHandler.setListenMode(((OnOffType) command == OnOffType.ON));
} else {
logger.warn("Command not handled for " + bindingConfig.getDeviceType() + " that is not OnOffType");
}
break;
case LED_MODE:
if (command instanceof OnOffType) {
this.messageHandler.setLedMode(((OnOffType) command == OnOffType.ON));
} else {
logger.warn("Command not handled for " + bindingConfig.getDeviceType() + " that is not OnOffType");
}
break;
case RADIATOR_THERMOSTAT:
case RADIATOR_THERMOSTAT_PLUS:
case WALL_THERMOSTAT:
if (bindingConfig.getFeature() == MaxCulFeature.THERMOSTAT) {
/* clear out old pacing timer */
if (pacedBindingTransmitTimers.containsKey(bindingConfig)) {
pacedBindingTransmitTimers.get(bindingConfig).cancel();
pacedBindingTransmitTimers.remove(bindingConfig);
}
/* schedule new timer */
Timer pacingTimer = new Timer();
pacedBindingTransmitTimers.put(bindingConfig, pacingTimer);
pacingTimer.schedule(new MaxCulPacedThermostatTransmitTask(command, bindingConfig, messageHandler, super.providers), PACED_TRANSMIT_TIME);
} else if (bindingConfig.getFeature() == MaxCulFeature.DISPLAYSETTING) {
messageHandler.sendSetDisplayActualTemp(bindingConfig.getDevAddr(), ((OnOffType) command == OnOffType.ON));
} else if (bindingConfig.getFeature() == MaxCulFeature.RESET) {
messageHandler.sendReset(bindingConfig.getDevAddr());
} else {
logger.warn("Command not handled for " + bindingConfig.getDeviceType() + " that is not OnOffType or DecimalType");
}
break;
case SHUTTER_CONTACT:
if (bindingConfig.getFeature() == MaxCulFeature.RESET) {
messageHandler.sendReset(bindingConfig.getDevAddr());
}
break;
default:
logger.warn("Command not handled for " + bindingConfig.getDeviceType());
break;
}
}
updateCreditMonitors();
}
use of java.util.Timer in project openhab1-addons by openhab.
the class MaxCulMsgHandler method transmitMessage.
private void transmitMessage(BaseMsg data, SenderQueueItem queueItem) {
try {
cul.send(data.rawMsg);
} catch (CULCommunicationException e) {
logger.error("Unable to send CUL message " + data + " because: " + e.getMessage());
}
/* update surplus credit value */
boolean fastSend = false;
if (data.isPartOfSequence()) {
fastSend = data.getMessageSequencer().useFastSend();
}
enoughCredit(data.requiredCredit(), fastSend);
this.lastTransmit = new Date();
if (this.endOfQueueTransmit.before(this.lastTransmit)) {
/* hit a time after the queue finished tx'ing */
this.endOfQueueTransmit = this.lastTransmit;
}
if (data.msgType != MaxCulMsgType.ACK) {
/* awaiting ack now */
SenderQueueItem qi = queueItem;
if (qi == null) {
qi = new SenderQueueItem();
qi.msg = data;
}
qi.expiry = new Date(this.lastTransmit.getTime() + MESSAGE_EXPIRY_PERIOD);
this.pendingAckQueue.put(qi.msg.msgCount, qi);
/* schedule a check of pending acks */
TimerTask ackCheckTask = new TimerTask() {
@Override
public void run() {
checkPendingAcks();
}
};
Timer timer = new Timer();
timer.schedule(ackCheckTask, qi.expiry);
}
}
use of java.util.Timer in project openhab1-addons by openhab.
the class PowerMaxCommDriver method sendMessage.
/**
* Send a message or delay the sending if time frame for receiving response is not ended
*
* @param msg
* the message to be sent
* @param immediate
* true if the message has to be send without considering timing
* @param waitTime
* the delay in seconds to wait
*
* @return true if the message was sent or the sending is delayed; false in other cases
*/
private synchronized boolean sendMessage(PowerMaxBaseMessage msg, boolean immediate, int waitTime) {
if ((waitTime > 0) && (msg != null)) {
logger.debug("sendMessage(): delay ({} s) sending message (type {})", waitTime, msg.getSendType().toString());
Timer timer = new Timer();
// Don't queue the message
timer.schedule(new DelayedSendTask(msg), waitTime * 1000);
return true;
}
if (msg == null) {
msg = msgQueue.peek();
if (msg == null) {
logger.debug("sendMessage(): nothing to send");
return false;
}
}
// Delay sending if time frame for receiving response is not ended
long delay = WAITING_DELAY_FOR_RESPONSE - (System.currentTimeMillis() - connector.getWaitingForResponse());
PowerMaxBaseMessage msgToSend = msg;
if (!immediate) {
msgToSend = msgQueue.peek();
if (msgToSend != msg) {
logger.debug("sendMessage(): add message in queue (type {})", msg.getSendType().toString());
msgQueue.offer(msg);
msgToSend = msgQueue.peek();
}
if ((msgToSend != msg) && (delay > 0)) {
return true;
} else if ((msgToSend == msg) && (delay > 0)) {
if (delay < 100) {
delay = 100;
}
logger.debug("sendMessage(): delay ({} ms) sending message (type {})", delay, msgToSend.getSendType().toString());
Timer timer = new Timer();
timer.schedule(new DelayedSendTask(null), delay);
return true;
} else {
msgToSend = msgQueue.poll();
}
}
if (logger.isDebugEnabled()) {
logger.debug("sendMessage(): sending {} message {}", msgToSend.getSendType().toString(), DatatypeConverter.printHexBinary(msgToSend.getRawData()));
}
boolean done = sendMessage(msgToSend.getRawData());
if (done) {
lastSendMsg = msgToSend;
connector.setWaitingForResponse(System.currentTimeMillis());
if (!immediate && (msgQueue.peek() != null)) {
logger.debug("sendMessage(): delay sending next message (type {})", msgQueue.peek().getSendType().toString());
Timer timer = new Timer();
timer.schedule(new DelayedSendTask(null), WAITING_DELAY_FOR_RESPONSE);
}
} else {
logger.debug("sendMessage(): failed");
}
return done;
}
use of java.util.Timer in project openhab1-addons by openhab.
the class MqttBrokerConnection method connectionLost.
@Override
public synchronized void connectionLost(Throwable t) {
logger.error("MQTT connection to broker was lost", t);
if (t instanceof MqttException) {
MqttException e = (MqttException) t;
logger.error("MQTT connection to '{}' was lost: {} : ReasonCode {} : Cause : {}", new Object[] { name, e.getMessage(), e.getReasonCode(), (e.getCause() == null ? "Unknown" : e.getCause().getMessage()) });
} else {
logger.error("MQTT connection to '{}' was lost: {}", name, t.getMessage());
}
started = false;
logger.info("Starting connection helper to periodically try restore connection to broker '{}'", name);
MqttBrokerConnectionHelper helper = new MqttBrokerConnectionHelper(this);
reconnectTimer = new Timer(true);
reconnectTimer.schedule(helper, 10000, RECONNECT_FREQUENCY);
}
Aggregations