-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Risolve in parte #29
- Loading branch information
Showing
6 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
...webnet/src/main/java/org/openhab/binding/openwebnet/handler/OpenWebNetCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/** | ||
* Copyright (c) 2010-2019 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.openwebnet.handler; | ||
|
||
import static org.openhab.binding.openwebnet.OpenWebNetBindingConstants.*; | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.smarthome.core.library.types.OnOffType; | ||
import org.eclipse.smarthome.core.library.types.OpenClosedType; | ||
import org.eclipse.smarthome.core.thing.ChannelUID; | ||
import org.eclipse.smarthome.core.thing.Thing; | ||
import org.eclipse.smarthome.core.thing.ThingStatus; | ||
import org.eclipse.smarthome.core.thing.ThingTypeUID; | ||
import org.eclipse.smarthome.core.types.Command; | ||
import org.eclipse.smarthome.core.types.UnDefType; | ||
import org.openhab.binding.openwebnet.OpenWebNetBindingConstants; | ||
import org.openwebnet.message.BaseOpenMessage; | ||
import org.openwebnet.message.Lighting; | ||
import org.openwebnet.message.OpenMessage; | ||
import org.openwebnet.message.OpenMessageFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link OpenWebNetCommandHandler} is responsible for handling commands/messages for a Command OpenWebNet device. | ||
* It extends the abstract {@link OpenWebNetThingHandler}. | ||
* | ||
* @author Massimo Valla - Initial contribution | ||
*/ | ||
|
||
public class OpenWebNetCommandHandler extends OpenWebNetThingHandler { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(OpenWebNetCommandHandler.class); | ||
|
||
public final static Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.COMMAND_SUPPORTED_THING_TYPES; | ||
protected Lighting.Type lightingType = Lighting.Type.ZIGBEE; | ||
|
||
// protected Command.Type commandType = Command.Type.ZIGBEE; | ||
public OpenWebNetCommandHandler(@NonNull Thing thing) { | ||
super(thing); | ||
logger.debug("==OWN:CommandHandler== constructor"); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
super.initialize(); | ||
logger.debug("==OWN:CommandHandler== initialize() thing={}", thing.getUID()); | ||
if (bridgeHandler != null && bridgeHandler.isBusGateway()) { | ||
lightingType = Lighting.Type.POINT_TO_POINT; | ||
} | ||
} | ||
|
||
@Override | ||
protected void requestChannelState(ChannelUID channel) { | ||
logger.debug("==OWN:CommandHandler== requestChannelState() thingUID={} channel={}", thing.getUID(), | ||
channel.getId()); | ||
// is not possible to request channel state for Command buttons | ||
updateStatus(ThingStatus.ONLINE); | ||
updateState(channel, UnDefType.UNDEF); | ||
} | ||
|
||
@Override | ||
protected void handleChannelCommand(ChannelUID channel, Command command) { | ||
String who = (String) getConfig().get(CONFIG_PROPERTY_WHO); | ||
String what = (String) getConfig().get(CONFIG_PROPERTY_WHAT); | ||
String whatOff = (String) getConfig().get(CONFIG_PROPERTY_WHATOFF); | ||
String where = (String) getConfig().get(CONFIG_PROPERTY_WHERE); | ||
switch (channel.getId()) { | ||
case CHANNEL_SWITCH: | ||
handleCommandSwitch(channel, command, who, what, whatOff, where); | ||
break; | ||
case CHANNEL_COMMAND_WHAT: | ||
handleCommandWhat(channel, command, who, where); | ||
break; | ||
default: { | ||
logger.warn("==OWN:CommandHandler== Unsupported channel UID {}", channel); | ||
} | ||
} | ||
// TODO | ||
// Note: if communication with thing fails for some reason, | ||
// indicate that by setting the status with detail information | ||
// updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, | ||
// "Could not control device at IP address x.x.x.x"); | ||
} | ||
|
||
/** | ||
* Handles Command what command | ||
* | ||
* @param channel | ||
* @param command | ||
* @param who | ||
* @param where | ||
*/ | ||
private void handleCommandWhat(ChannelUID channel, Command command, String who, String where) { | ||
logger.debug("==OWN:CommandHandler== handleCommandWhat() (who={} - command={} - channel={})", who, command, | ||
channel); | ||
if (command != null) { | ||
SendOWN(who, command.toString(), where); | ||
} else { | ||
logger.debug("==OWN:CommandHandler== handleCommandWhat() command is null"); | ||
} | ||
} | ||
|
||
/** | ||
* Handles Command switch command ON/OFF | ||
* | ||
* @param channel | ||
* @param command | ||
* @param who | ||
* @param what | ||
* @param whatOff | ||
* @param where | ||
*/ | ||
private void handleCommandSwitch(ChannelUID channel, Command command, String who, String what, String whatOff, | ||
String where) { | ||
logger.debug("==OWN:CommandHandler== handleCommandSwitch() (who={} - what={} - where={} - channel={})", who, | ||
what, where, channel); | ||
if (command instanceof OnOffType) { | ||
if (OnOffType.ON.equals(command)) { | ||
if (what != null) { | ||
SendOWN(who, what, where); | ||
updateState(CHANNEL_COMMAND_CONTACT, OpenClosedType.OPEN); | ||
} else { | ||
logger.debug("==OWN:CommandHandler== handleCommandSwitch() What is null"); | ||
} | ||
} else if (OnOffType.OFF.equals(command)) { | ||
if (whatOff != null) { | ||
SendOWN(who, whatOff, where); | ||
updateState(CHANNEL_COMMAND_CONTACT, OpenClosedType.CLOSED); | ||
} else { | ||
logger.debug("==OWN:CommandHandler== handleCommandSwitch() WhatOff is null"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Send to bus | ||
* | ||
* @param channel | ||
* @param command | ||
* @param who | ||
* @param what | ||
* @param where | ||
*/ | ||
private void SendOWN(String who, String what, String where) { | ||
String commandOWN = OpenMessage.FRAME_START + who + OpenMessage.FRAME_START + what + OpenMessage.FRAME_START | ||
+ where + OpenMessage.FRAME_END; | ||
bridgeHandler.gateway.send(OpenMessageFactory.parse(commandOWN)); | ||
} | ||
|
||
@Override | ||
protected String ownIdPrefix() { | ||
String compare = ""; | ||
if (getConfig().get(CONFIG_PROPERTY_COMPARE) != null) { | ||
compare = (String) getConfig().get(CONFIG_PROPERTY_COMPARE); | ||
compare = "." + compare.replace('#', 'h').replace('*', 'h'); | ||
} | ||
return "C." + getConfig().get(CONFIG_PROPERTY_WHO).toString() + compare; | ||
} | ||
|
||
@Override | ||
protected void handleMessage(BaseOpenMessage msg) { | ||
super.handleMessage(msg); | ||
String who = (String) getConfig().get(CONFIG_PROPERTY_WHO); | ||
String what = (String) getConfig().get(CONFIG_PROPERTY_WHAT); | ||
String where = (String) getConfig().get(CONFIG_PROPERTY_WHERE); | ||
String compare = "<" + getConfig().get(CONFIG_PROPERTY_COMPARE) + ">"; | ||
if (compare.equals(msg.toString())) { | ||
SendOWN(who, what, where); | ||
updateState(CHANNEL_COMMAND_SWITCH, OnOffType.ON); | ||
updateState(CHANNEL_COMMAND_CONTACT, OpenClosedType.OPEN); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a WHERE address (string) based on bridge type and unit (optional) | ||
* | ||
* @param unit device unit | ||
**/ | ||
protected String toWhere(String unit) { | ||
logger.debug("==OWN:CommandHandler== toWhere(unit) ownId={}", ownId); | ||
if (bridgeHandler.isBusGateway()) { | ||
return deviceWhere; | ||
} else { | ||
return deviceWhere + unit; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a WHERE address based on channel | ||
* | ||
* @param channel channel | ||
**/ | ||
protected String toWhere(ChannelUID channel) { | ||
logger.debug("==OWN:CommandHandler== toWhere(ChannelUID) ownId={}", ownId); | ||
if (bridgeHandler.isBusGateway()) { | ||
return deviceWhere; | ||
} else if (channel.getId().equals(CHANNEL_SWITCH_02)) { | ||
return deviceWhere + BaseOpenMessage.UNIT_02; | ||
} else { // CHANNEL_SWITCH_01 or other channels | ||
return deviceWhere + BaseOpenMessage.UNIT_01; | ||
} | ||
} | ||
|
||
} // class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
org.openhab.binding.openwebnet/src/main/resources/ESH-INF/thing/BusCommand.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<thing:thing-descriptions bindingId="openwebnet" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0" | ||
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd"> | ||
|
||
<!-- Thing for BUS Command (BTicino xxx/xxx/...) --> | ||
<thing-type id="bus_command"> | ||
<supported-bridge-type-refs> | ||
<bridge-type-ref id="bus_gateway" /> | ||
</supported-bridge-type-refs> | ||
|
||
<label>Command</label> | ||
<description>A OpenWebNet BUS/SCS Command. BTicino models: xxx/yyyy/etc.</description> | ||
|
||
<channels> | ||
<channel id="what" typeId="what" /> | ||
<channel id="switch" typeId="switch" /> | ||
<channel id="contact" typeId="contact" /> | ||
</channels> | ||
|
||
<properties> | ||
<property name="vendor">BTicino/Legrand</property> | ||
<property name="model">BTI-xxxx/yyyy/etc.</property> | ||
<property name="ownDeviceType">---</property> | ||
</properties> | ||
|
||
<config-description> | ||
<parameter name="who" type="text"> | ||
<label>OpenWebNet Device Address (WHO)</label> | ||
<description>Example: Lighting Who = 1 , Automation Who = 2</description> | ||
<required>true</required> | ||
</parameter> | ||
<parameter name="what" type="text"> | ||
<label>OpenWebNet Device Address (WHAT)</label> | ||
<description>OpenWebNet code sent with the ON command. Example: Lighting ON -> what = "1".</description> | ||
<required>true</required> | ||
</parameter> | ||
<parameter name="where" type="text"> | ||
<label>OpenWebNet Device Address (WHERE)</label> | ||
<description>Example: A/PL address: A=1 PL=3 --> WHERE=13. On local bus: WHERE=13#4#01</description> | ||
<required>true</required> | ||
</parameter> | ||
<parameter name="whatOff" type="text"> | ||
<label>OpenWebNet Device Address (WHATOFF)</label> | ||
<description>OpenWebNet code sent with the OFF command, if omitted no code is sent to the BUS/SCS. Example: Lighting OFF -> what = "0".</description> | ||
<required>false</required> | ||
</parameter> | ||
<parameter name="compare" type="text"> | ||
<label>OpenWebNet Frame OWN</label> | ||
<description>OpenWebNet frame code, if the same code is received on the BUS/SCS the code *who*what*where## is sent to the BUS / SCS. If omitted, no comparison is performed. Example: appears = "*1*1*23##".</description> | ||
<required>false</required> | ||
</parameter> | ||
</config-description> | ||
|
||
</thing-type> | ||
</thing:thing-descriptions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters