Skip to content

Commit

Permalink
Add_bus_command
Browse files Browse the repository at this point in the history
Risolve in parte #29
  • Loading branch information
Gozilla01 committed Jul 31, 2019
1 parent 783d364 commit 26bafe2
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class OpenWebNetBindingConstants {
public static final ThingTypeUID THING_TYPE_BUS_MOTION_DETECTOR = new ThingTypeUID(BINDING_ID,
"bus_motion_detector");
public static final String THING_LABEL_BUS_DETECTOR = "Motion Detector";
public static final ThingTypeUID THING_TYPE_BUS_COMMAND = new ThingTypeUID(BINDING_ID, "bus_command");
public static final String THING_LABEL_BUS_COMMAND = "Command";

// ZIGBEE
public static final ThingTypeUID THING_TYPE_ZB_ON_OFF_SWITCH = new ThingTypeUID(BINDING_ID, "zb_on_off_switch");
Expand Down Expand Up @@ -123,12 +125,15 @@ public class OpenWebNetBindingConstants {
// ## Motion Detector
public static final Set<ThingTypeUID> MOTION_DETECTOR_SUPPORTED_THING_TYPES = new HashSet<>(
Arrays.asList(THING_TYPE_BUS_MOTION_DETECTOR));
// ## Command
public static final Set<ThingTypeUID> COMMAND_SUPPORTED_THING_TYPES = new HashSet<>(
Arrays.asList(THING_TYPE_BUS_COMMAND));
// ## Groups
public static final Set<ThingTypeUID> DEVICE_SUPPORTED_THING_TYPES = Stream
.of(LIGHTING_SUPPORTED_THING_TYPES, AUTOMATION_SUPPORTED_THING_TYPES,
THERMOREGULATION_SUPPORTED_THING_TYPES, ENERGY_SUPPORTED_THING_TYPES,
SCENARIO_SUPPORTED_THING_TYPES, GENERIC_SUPPORTED_THING_TYPES, AUX_SUPPORTED_THING_TYPES,
MOTION_DETECTOR_SUPPORTED_THING_TYPES)
MOTION_DETECTOR_SUPPORTED_THING_TYPES, COMMAND_SUPPORTED_THING_TYPES)
.flatMap(Collection::stream).collect(Collectors.toCollection(HashSet::new));

// Sets.union(LIGHTING_SUPPORTED_THING_TYPES,
Expand Down Expand Up @@ -183,6 +188,10 @@ public class OpenWebNetBindingConstants {
// motion detector
public static final String CHANNEL_MOTION_DETECTOR_SWITCH = "switch";
public static final String CHANNEL_MOTION_DETECTOR_VALUE = "value";
// command
public static final String CHANNEL_COMMAND_SWITCH = "switch";
public static final String CHANNEL_COMMAND_CONTACT = "contact";
public static final String CHANNEL_COMMAND_WHAT = "what";

// devices config properties
public static final String CONFIG_PROPERTY_WHERE = "where";
Expand All @@ -194,6 +203,9 @@ public class OpenWebNetBindingConstants {
public static final String CONFIG_PROPERTY_MINUTE = "minute";
public static final String CONFIG_PROPERTY_SECOND = "second";
public static final String CONFIG_PROPERTY_AUTOMATICTOOFF = "automaticToOff";
public static final String CONFIG_PROPERTY_WHO = "who";
public static final String CONFIG_PROPERTY_WHATOFF = "whatOff";
public static final String CONFIG_PROPERTY_COMPARE = "compare";
// BUS gw config properties
public static final String CONFIG_PROPERTY_SERIAL_PORT = "serialPort";
public static final String CONFIG_PROPERTY_HOST = "host";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ public void onMessage(OpenMessage msg) {
}
}
getThingAssociated(msg);
callAllThingCommandHandlerMessage(msg);
}

/** Get the Thing associated with this message... */
Expand Down Expand Up @@ -457,6 +458,17 @@ private void callAllThingHandlerMessage(OpenMessage msg, String who) {
}
}

private void callAllThingCommandHandlerMessage(OpenMessage msg) {
BaseOpenMessage baseMsg = (BaseOpenMessage) msg;
for (String ownIdkey : registeredDevices.keySet()) {
OpenWebNetThingHandler deviceHandler = registeredDevices.get(ownIdkey);
// Bus Command
if (deviceHandler != null && deviceHandler.ownIdPrefix().indexOf("C") == 0) {
deviceHandler.handleMessage(baseMsg);
}
}
}

@Override
public void onConnected() {
isGatewayConnected = true;
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openhab.binding.openwebnet.handler.OpenWebNetAutomationHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetAuxHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetBridgeHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetCommandHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetEnergyHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetGenericHandler;
import org.openhab.binding.openwebnet.handler.OpenWebNetLightingHandler;
Expand Down Expand Up @@ -94,6 +95,9 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
} else if (OpenWebNetMotionDetectorHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
logger.debug("==OWN:HandlerFactory== creating NEW Motion Detector Handler");
return new OpenWebNetMotionDetectorHandler(thing);
} else if (OpenWebNetCommandHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
logger.debug("==OWN:HandlerFactory== creating NEW Command Handler");
return new OpenWebNetCommandHandler(thing);
}
logger.warn("==OWN:HandlerFactory== ThingType: {} is not supported by this binding", thing.getThingTypeUID());
return null;
Expand Down
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>
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,20 @@
<category></category>
<state readOnly="true" pattern="%.1f lx"></state>
</channel-type>

<!-- Command Channel -->
<channel-type id="what">
<item-type>String</item-type>
<label>What</label>
<description>String What</description>
<category></category>
</channel-type>
<channel-type id="contact">
<item-type>Contact</item-type>
<label>Contact</label>
<description>Switch the power OPEN and Closed</description>
<category></category>
<state pattern="%s" readOnly="true"></state>
</channel-type>

</thing:thing-descriptions>

0 comments on commit 26bafe2

Please sign in to comment.