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!
==== Canali di base: stato generale e comando. ==== <syntaxhighlight lang="xml"><?xml version="1.0" encoding="UTF-8"?> <thing:thing-descriptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:thing="http://openhab.org/schemas/thing-description/v1.0.0" xsi:schemaLocation="http://openhab.org/schemas/thing-description/v1.0.0 http://openhab.org/schemas/thing-description-1.0.0.xsd"> <!-- Bridge: account e-Connect --> <bridge-type id="account"> <label>e-Connect Account</label> <description>Account e-Connect / Metronet</description> <config-description> <parameter name="username" type="text" required="true"> <label>Username</label> <description>Username usato per accedere al portale e-Connect/Metronet</description> </parameter> <parameter name="password" type="text" required="true"> <label>Password</label> <description>Password per l’accesso</description> <context>password</context> </parameter> <parameter name="system" type="text" required="true"> <label>Sistema</label> <description>elmo_econnect oppure iess_metronet</description> <options> <option value="elmo_econnect">Elmo e-Connect</option> <option value="iess_metronet">IESS Metronet</option> </options> </parameter> <parameter name="baseUrl" type="text" required="false"> <label>Base URL</label> <description>Override dell’URL di default (es. https://connect3.elmospa.com)</description> </parameter> <parameter name="domain" type="text" required="false"> <label>Dominio</label> <description>Dominio (es. vendor / nwd). Lascia vuoto per default.</description> </parameter> <parameter name="refreshInterval" type="integer" min="10" max="600" required="false"> <label>Intervallo di aggiornamento</label> <description>Secondi tra un poll e l’altro verso il cloud</description> <default>30</default> </parameter> </config-description> </bridge-type> <!-- Thing: pannello di allarme --> <thing-type id="alarm"> <label>e-Connect Alarm</label> <description>Centralina di allarme collegata all’account e-Connect</description> <channels> <channel id="state" typeId="alarm-state"/> <channel id="command" typeId="alarm-command"/> </channels> </thing-type> <channel-type id="alarm-state"> <item-type>String</item-type> <label>Stato allarme</label> <description>Stato logico dell’allarme (disarmed, armed_away, armed_home, armed_night)</description> <state> <options> <option value="disarmed">Disarmato</option> <option value="armed_away">Totale</option> <option value="armed_home">Parziale casa</option> <option value="armed_night">Notte</option> </options> </state> </channel-type> <channel-type id="alarm-command"> <item-type>String</item-type> <label>Comando</label> <description>Comando da inviare all’allarme (disarm, arm_away, arm_home, arm_night)</description> <state> <options> <option value="disarm">Disarma</option> <option value="arm_away">Totale</option> <option value="arm_home">Casa</option> <option value="arm_night">Notte</option> </options> </state> </channel-type> </thing:thing-descriptions> </syntaxhighlight> ==== <syntaxhighlight lang="java">package org.openhab.binding.econnect; ==== import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.ChannelUID; public class EconnectBindingConstants { public static final String BINDING_ID = "econnect"; // Thing types public static final ThingTypeUID BRIDGE_TYPE_ACCOUNT = new ThingTypeUID(BINDING_ID, "account"); public static final ThingTypeUID THING_TYPE_ALARM = new ThingTypeUID(BINDING_ID, "alarm"); // Channels public static final String CHANNEL_STATE = "state"; public static final String CHANNEL_COMMAND = "command"; public static ChannelUID channelUID(String thingUID, String channelId) { return new ChannelUID(thingUID + ":" + channelId); } } </syntaxhighlight> ==== <syntaxhighlight lang="java">package org.openhab.binding.econnect.internal; ==== import static org.openhab.binding.econnect.EconnectBindingConstants.*; import java.util.Set; import org.openhab.binding.econnect.internal.handler.EconnectAlarmHandler; import org.openhab.binding.econnect.internal.handler.EconnectBridgeHandler; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.binding.BaseThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandler; import org.osgi.service.component.annotations.Component; @Component(service = org.openhab.core.thing.binding.ThingHandlerFactory.class) public class EconnectHandlerFactory extends BaseThingHandlerFactory { private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of( BRIDGE_TYPE_ACCOUNT, THING_TYPE_ALARM ); @Override public boolean supportsThingType(ThingTypeUID thingTypeUID) { return SUPPORTED_THING_TYPES.contains(thingTypeUID); } @Override protected ThingHandler createHandler(Thing thing) { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (BRIDGE_TYPE_ACCOUNT.equals(thingTypeUID)) { return new EconnectBridgeHandler((Bridge) thing); } else if (THING_TYPE_ALARM.equals(thingTypeUID)) { return new EconnectAlarmHandler(thing); } return null; } } </syntaxhighlight> ==== <syntaxhighlight lang="java">package org.openhab.binding.econnect.internal.handler; ==== import static org.openhab.binding.econnect.EconnectBindingConstants.*; import org.openhab.binding.econnect.internal.client.EconnectClient; import org.openhab.core.config.core.Configuration; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.binding.BaseBridgeHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EconnectBridgeHandler extends BaseBridgeHandler { private final Logger logger = LoggerFactory.getLogger(EconnectBridgeHandler.class); private volatile EconnectClient client; public EconnectBridgeHandler(Bridge bridge) { super(bridge); } @Override public void initialize() { updateStatus(ThingStatus.UNKNOWN); Configuration config = getConfig(); String username = (String) config.get("username"); String password = (String) config.get("password"); String system = (String) config.get("system"); String baseUrl = (String) config.get("baseUrl"); String domain = (String) config.get("domain"); try { client = new EconnectClient(username, password, system, baseUrl, domain); client.authenticate(); // <-- usa l’equivalente di client.auth() di econnect-python updateStatus(ThingStatus.ONLINE); } catch (Exception e) { logger.warn("Errore durante l'autenticazione e-Connect", e); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Errore autenticazione: " + e.getMessage()); } } @Override public void dispose() { if (client != null) { client.close(); client = null; } super.dispose(); } public EconnectClient getClient() { return client; } } </syntaxhighlight> ==== <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.thing.binding.builder.ThingStatusInfoBuilder; import org.openhab.core.types.Command; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EconnectAlarmHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(EconnectAlarmHandler.class); private ScheduledFuture<?> pollingJob; 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 config = getThing().getConfiguration(); Integer refreshInterval = (Integer) config.get("refreshInterval"); if (refreshInterval == null || refreshInterval < 10) { refreshInterval = 30; } startPolling(refreshInterval); updateStatus(ThingStatus.ONLINE); } private void startPolling(int refreshInterval) { 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(); // da implementare 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(org.openhab.core.thing.ChannelUID channelUID, Command command) { if (CHANNEL_COMMAND.equals(channelUID.getId()) && command instanceof StringType) { String cmd = ((StringType) command).toString(); EconnectClient client = getClient(); if (client == null) { logger.warn("Comando {} 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: {}", 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)