Skip to content

Commit

Permalink
Bugfixes (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
silamon authored Nov 30, 2020
1 parent dfce14c commit 60cd742
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.openhab.binding.vm208.internal.handler.VM208BusHandler;
import org.openhab.binding.vm208.internal.handler.VM208ExHandler;
import org.openhab.binding.vm208.internal.handler.VM208IntHandler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -44,9 +46,16 @@ public class VM208HandlerFactory extends BaseThingHandlerFactory {

private final Logger logger = LoggerFactory.getLogger(VM208HandlerFactory.class);

private final VM208BusHandler busHandler;

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>(
Arrays.asList(THING_TYPE_VM208INT, THING_TYPE_VM208EX));

@Activate
public VM208HandlerFactory() {
this.busHandler = new VM208BusHandler();
}

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
Expand All @@ -60,7 +69,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
if (THING_TYPE_VM208EX.equals(thingTypeUID)) {
return new VM208ExHandler(thing);
} else if (THING_TYPE_VM208INT.equals(thingTypeUID)) {
return new VM208IntHandler((Bridge) thing);
return new VM208IntHandler(busHandler, (Bridge) thing);
}
logger.debug("No handler match for {}", thingTypeUID.getAsString());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2010-2020 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.vm208.internal.handler;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link VM208BusHandler} class is defined to lock the bus.
*
* @author Simon Lamon - Initial contribution
*/
@NonNullByDefault
public class VM208BusHandler {

@SuppressWarnings("unused")
private final Logger logger = LoggerFactory.getLogger(VM208BusHandler.class);

public synchronized void claimBus(Runnable command) {
command.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ public void initialize() {

gateway.sendToSocket(this, () -> {
// set all pins to output
tcaProvider.setDirectionSettings(0x00, 0xFF, 0x00);
boolean deviceNeedsInitialization = tcaProvider.setDirectionSettings(0x00, 0xFF, 0x00);
if (deviceNeedsInitialization) {
// turn channels off
tcaProvider.setOutput0Settings(0x00);
tcaProvider.setOutput2Settings(0xFF);
}
});

this.fetchInitialStates();
Expand Down Expand Up @@ -195,8 +200,10 @@ private void turnRelayOnWithoutLock(int channel) {
// turn relay on
Pin pin = VM208ExHandler.RELAY_PIN_MAP[channel];
PinState pinState = PinState.HIGH;
logger.debug(pin.getName());
this.tcaProvider.setState(pin, pinState);

// update state
this.updateState(new ChannelUID(thing.getUID(), RELAY_CHANNELS[channel], RELAY), OnOffType.ON);
}

@Override
Expand All @@ -215,8 +222,10 @@ private void turnRelayOffWithoutLock(int channel) {
// turn relay off
Pin pin = VM208ExHandler.RELAY_PIN_MAP[channel];
PinState pinState = PinState.LOW;
logger.debug(pin.getName());
this.tcaProvider.setState(pin, pinState);

// update state
this.updateState(new ChannelUID(thing.getUID(), RELAY_CHANNELS[channel], RELAY), OnOffType.OFF);
}

@Override
Expand All @@ -240,6 +249,9 @@ private void turnLedOnWithoutLock(int channel) {
Pin pin = VM208ExHandler.LED_PIN_MAP[channel];
PinState pinState = PinState.LOW; // active low so result is inverted
this.tcaProvider.setState(pin, pinState);

// update state
this.updateState(new ChannelUID(thing.getUID(), RELAY_CHANNELS[channel], LED), OnOffType.ON);
}

@Override
Expand All @@ -255,6 +267,9 @@ private void turnLedOffWithoutLock(int channel) {
Pin pin = VM208ExHandler.LED_PIN_MAP[channel];
PinState pinState = PinState.HIGH;
this.tcaProvider.setState(pin, pinState);

// update state
this.updateState(new ChannelUID(thing.getUID(), RELAY_CHANNELS[channel], LED), OnOffType.OFF);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class VM208IntHandler extends BaseBridgeHandler implements GpioPinListene

private final Logger logger = LoggerFactory.getLogger(getClass());

private final VM208BusHandler bus;

private @NonNullByDefault({}) VM208IntConfiguration config;

private int busNumber;
Expand All @@ -57,8 +59,9 @@ public class VM208IntHandler extends BaseBridgeHandler implements GpioPinListene

private @Nullable GpioPinDigitalInput interruptPinInput;

public VM208IntHandler(Bridge bridge) {
public VM208IntHandler(VM208BusHandler bus, Bridge bridge) {
super(bridge);
this.bus = bus;

this.sockets = new VM208BaseHandler[4];
}
Expand Down Expand Up @@ -140,26 +143,31 @@ protected void checkConfiguration() {
interruptPin = config.getInterruptPin();
}

// Only one thread can access this method at a time
// Only one socket can access this method at a time
public synchronized void sendToSocket(VM208BaseHandler vm208baseHandler, Runnable command) {
int socket = vm208baseHandler.getSocket();
boolean channelHasChanged = false;
try {
tcaProvider.changeChannel((byte) socket);
channelHasChanged = true;

command.run();
} catch (IOException ex) {
logger.error("{}", ex);
} finally {
if (channelHasChanged) {
try {
tcaProvider.changeChannel((byte) 0);
} catch (IOException ex) {
logger.error("{}", ex);

// Only one interface can communicate with the bus,
// since each device has the same address
bus.claimBus(() -> {
boolean channelHasChanged = false;
try {
tcaProvider.changeChannel((byte) socket);
channelHasChanged = true;

command.run();
} catch (IOException ex) {
logger.error("", ex);
} finally {
if (channelHasChanged) {
try {
tcaProvider.changeChannel((byte) 0);
} catch (IOException ex) {
logger.error("", ex);
}
}
}
}
});
}

@Override
Expand All @@ -186,7 +194,7 @@ public void handleGpioPinDigitalStateChangeEvent(@Nullable GpioPinDigitalStateCh
}
}
} catch (Exception ex) {
logger.error("{}", ex);
logger.error("", ex);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public class GPIODataHolder {
GpioUtil.enableNonPrivilegedAccess();
}
public static final GpioController GPIO = GpioFactory.getInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ public class HexUtils {
public static String toHex(int number) {
return String.format("%02X", number);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,59 @@ public TCA6424AProvider(I2CBus bus, int address, int pollingTime) throws IOExcep
device = bus.getDevice(address);
}

public void setDirectionSettings(int direction0, int direction1, int direction2) {
public boolean setDirectionSettings(int direction0, int direction1, int direction2) {
boolean wroteNewSettings = false;

try {
readSettings();

if (currentDirection0 != direction0) {
writeToDevice(REGISTER_DIRECTION0, (byte) direction0);
currentDirection0 = direction0;
wroteNewSettings = true;
}
if (currentDirection1 != direction1) {
writeToDevice(REGISTER_DIRECTION1, (byte) direction1);
currentDirection1 = direction1;
wroteNewSettings = true;
}
if (currentDirection2 != direction2) {
writeToDevice(REGISTER_DIRECTION2, (byte) direction2);
currentDirection2 = direction2;
wroteNewSettings = true;
}
} catch (Exception ex) {
logger.error("{}", ex.toString());
}

return wroteNewSettings;
}

public void setOutput0Settings(int output0) {
try {
writeToDevice(REGISTER_OUTPUT0, (byte) output0);
currentOutputStates0 = output0;
} catch (Exception ex) {
logger.error("{}", ex.toString());
}
}

public void setOutput1Settings(int output1) {
try {
writeToDevice(REGISTER_OUTPUT1, (byte) output1);
currentOutputStates1 = output1;
} catch (Exception ex) {
logger.error("{}", ex.toString());
}
}

public void setOutput2Settings(int output2) {
try {
writeToDevice(REGISTER_OUTPUT2, (byte) output2);
currentOutputStates2 = output2;
} catch (Exception ex) {
logger.error("{}", ex.toString());
}
}

public void readSettings() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/ESH-INF/binding/binding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
xmlns:binding="https://openhab.org/schemas/binding/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">

<name>vm208 Binding</name>
<name>VM208 Binding</name>
<description>This is the binding for the VM208 ecosystem.</description>
<author>Simon Lamon</author>

</binding:binding>
3 changes: 3 additions & 0 deletions src/main/resources/ESH-INF/thing/channels.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<description>Relay channel</description>
<category>Switch</category>
<state readOnly="false"/>
<autoUpdatePolicy>veto</autoUpdatePolicy>
</channel-type>

<channel-type id="led">
Expand All @@ -98,6 +99,7 @@
<description>Led channel</description>
<category>Switch</category>
<state readOnly="false"/>
<autoUpdatePolicy>veto</autoUpdatePolicy>
</channel-type>

<channel-type id="button">
Expand All @@ -106,6 +108,7 @@
<description>Button channel</description>
<category>Switch</category>
<state readOnly="true"/>
<autoUpdatePolicy>veto</autoUpdatePolicy>
</channel-type>

</thing:thing-descriptions>
20 changes: 10 additions & 10 deletions src/main/resources/ESH-INF/thing/thing-types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@

<thing-type id="vm208ex">
<supported-bridge-type-refs>
<bridge-type-ref id="vm208int" />
<bridge-type-ref id="vm208int"/>
</supported-bridge-type-refs>
<label>VM208Ex</label>
<description>Din-rail extension relay card</description>
<channel-groups>
<channel-group id="relay1" typeId="relay1" />
<channel-group id="relay2" typeId="relay2" />
<channel-group id="relay3" typeId="relay3" />
<channel-group id="relay4" typeId="relay4" />
<channel-group id="relay5" typeId="relay5" />
<channel-group id="relay6" typeId="relay6" />
<channel-group id="relay7" typeId="relay7" />
<channel-group id="relay8" typeId="relay8" />
<channel-group id="relay1" typeId="relay1"/>
<channel-group id="relay2" typeId="relay2"/>
<channel-group id="relay3" typeId="relay3"/>
<channel-group id="relay4" typeId="relay4"/>
<channel-group id="relay5" typeId="relay5"/>
<channel-group id="relay6" typeId="relay6"/>
<channel-group id="relay7" typeId="relay7"/>
<channel-group id="relay8" typeId="relay8"/>
</channel-groups>
<config-description>
<parameter name="socket" type="integer" required="true" min="1" max="4">
Expand All @@ -49,7 +49,7 @@
<parameter name="ledReflectsRelayStatus" type="boolean" required="true">
<label>Led reflects Relay status</label>
<description>Led reflects Relay status</description>
<default>FALSE</default>
<default>true</default>
</parameter>
</config-description>
</thing-type>
Expand Down

0 comments on commit 60cd742

Please sign in to comment.