Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/691a15e5-9180-8001-a5a0-9b83ec27e6fe
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== 8. EconnectAlarmHandler.java ==== File: <code>src/main/java/org/openhab/binding/econnect/internal/handler/EconnectAlarmHandler.java</code> <syntaxhighlight lang="java">package org.openhab.binding.econnect.internal.handler; import static org.openhab.binding.econnect.EconnectBindingConstants.*; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.openhab.binding.econnect.internal.client.EconnectClient; import org.openhab.binding.econnect.internal.client.model.AlarmStatus; import org.openhab.core.config.core.Configuration; import org.openhab.core.library.types.StringType; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.types.Command; import org.openhab.core.thing.ChannelUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EconnectAlarmHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(EconnectAlarmHandler.class); private ScheduledFuture<?> pollingJob; private int refreshInterval; public EconnectAlarmHandler(Thing thing) { super(thing); } @Override public void initialize() { updateStatus(ThingStatus.UNKNOWN); Bridge bridge = getBridge(); if (bridge == null || !(bridge.getHandler() instanceof EconnectBridgeHandler)) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "Bridge e-Connect non disponibile"); return; } Configuration cfg = getThing().getConfiguration(); Integer cfgRefresh = (Integer) cfg.get("refreshInterval"); refreshInterval = (cfgRefresh != null && cfgRefresh >= 10) ? cfgRefresh : 30; startPolling(); updateStatus(ThingStatus.ONLINE); } private void startPolling() { stopPolling(); pollingJob = scheduler.scheduleWithFixedDelay(this::pollStatus, 5, refreshInterval, TimeUnit.SECONDS); } private void stopPolling() { if (pollingJob != null && !pollingJob.isCancelled()) { pollingJob.cancel(true); pollingJob = null; } } private EconnectClient getClient() { Bridge bridge = getBridge(); if (bridge == null) return null; if (!(bridge.getHandler() instanceof EconnectBridgeHandler)) return null; return ((EconnectBridgeHandler) bridge.getHandler()).getClient(); } private void pollStatus() { EconnectClient client = getClient(); if (client == null) { logger.debug("Nessun client disponibile per il polling"); return; } try { AlarmStatus status = client.getAlarmStatus(); if (status != null) { updateState(CHANNEL_STATE, new StringType(status.getModeAsString())); } } catch (Exception e) { logger.warn("Errore leggendo lo stato e-Connect", e); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Errore di comunicazione: " + e.getMessage()); } } @Override public void handleCommand(ChannelUID channelUID, Command command) { if (!CHANNEL_COMMAND.equals(channelUID.getId())) { return; } if (!(command instanceof StringType)) { logger.warn("Comando non valido (atteso StringType): {}", command); return; } String cmd = ((StringType) command).toString(); EconnectClient client = getClient(); if (client == null) { logger.warn("Comando {} ricevuto ma client nullo", cmd); return; } try { switch (cmd) { case "disarm": client.disarmAll(); break; case "arm_away": client.armAway(); break; case "arm_home": client.armHome(); break; case "arm_night": client.armNight(); break; default: logger.warn("Comando sconosciuto per e-Connect: {}", cmd); return; } // Dopo il comando, forza un refresh pollStatus(); } catch (Exception e) { logger.warn("Errore eseguendo comando {} su e-Connect", cmd, e); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Errore comando: " + e.getMessage()); } } @Override public void dispose() { stopPolling(); super.dispose(); } } </syntaxhighlight>
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)