~hww3/org.openhab.binding.nadreceiver

59c30183d497e1c653322af5a6ef6f70d717e19b — H. William Welliver III 2 years ago 4e469f3
remove serial support
M ESH-INF/thing/thing-types.xml => ESH-INF/thing/thing-types.xml +0 -11
@@ 18,17 18,6 @@
        </channels>
        
        <config-description>
            <parameter name="isTcpPort" type="boolean">
                <label>Use TCP?</label>
                <description>Is receiver connected via TCP?</description>
                <default>false</default>
            </parameter>
            <parameter name="port" type="text" >
                <label>COM port</label>
                <description>Select serial port</description>
                <default>COM1</default>
                <context>serial-port</context>
            </parameter>        
            <parameter name="hostName" type="text">
                <label>Host Name</label>
                <description>Host name of receiver</description>

M META-INF/MANIFEST.MF => META-INF/MANIFEST.MF +6 -7
@@ 4,17 4,16 @@ Bundle-Name: NadReceiver Binding
Bundle-SymbolicName: org.openhab.binding.nadreceiver;singleton:=true
Bundle-Vendor: openHAB
Bundle-Version: 2.2.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ClassPath: .
Import-Package: 
 com.google.common.collect,
 org.eclipse.jdt.annotation;resolution:=optional,
 gnu.io,
 org.eclipse.smarthome.config.core,
 org.eclipse.smarthome.core.library.types,
 org.eclipse.smarthome.core.thing,
 org.eclipse.smarthome.core.thing.binding,
 org.eclipse.smarthome.core.types,
 org.openhab.config.core,
 org.openhab.core.library.types,
 org.openhab.core.thing,
 org.openhab.core.thing.binding,
 org.openhab.core.types,
 org.slf4j
Service-Component: OSGI-INF/*
Export-Package: org.openhab.binding.nadreceiver,

M pom.xml => pom.xml +4 -4
@@ 1,7 1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>


@@ 26,4 25,5 @@
      <scope>compile</scope>
    </dependency>
  </dependencies>

</project>

M src/main/history/dependencies.xml => src/main/history/dependencies.xml +4 -1
@@ 1,4 1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.6.0" name="org.openhab.binding.nadreceiver">
    <feature version="0.0.0"/>
    <feature version="0.0.0">
        <feature>wrap</feature>
        <bundle>wrap:mvn:org.lastnpe.eea/eea-all/2.2.1</bundle>
    </feature>
</features>

M src/main/java/org/openhab/binding/nadreceiver/NadReceiverBindingConstants.java => src/main/java/org/openhab/binding/nadreceiver/NadReceiverBindingConstants.java +1 -1
@@ 7,7 7,7 @@
 */
package org.openhab.binding.nadreceiver;

import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingTypeUID;

/**
 * The {@link NadReceiverBinding} class defines common constants, which are

M src/main/java/org/openhab/binding/nadreceiver/handler/NadReceiverHandler.java => src/main/java/org/openhab/binding/nadreceiver/handler/NadReceiverHandler.java +26 -38
@@ 7,8 7,8 @@
 */
package org.openhab.binding.nadreceiver.handler;

import static org.eclipse.smarthome.core.library.unit.MetricPrefix.KILO;
import static org.eclipse.smarthome.core.library.unit.MetricPrefix.MEGA;
import static javax.measure.MetricPrefix.KILO;
import static javax.measure.MetricPrefix.MEGA;
import static org.openhab.binding.nadreceiver.NadReceiverBindingConstants.*;

import java.io.IOException;


@@ 22,23 22,22 @@ import java.util.concurrent.TimeUnit;

import javax.measure.quantity.Frequency;

import org.eclipse.smarthome.core.library.types.*;
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.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.core.types.RefreshType;
import org.eclipse.smarthome.core.types.StateOption;
import org.eclipse.smarthome.core.types.UnDefType;
import org.openhab.binding.nadreceiver.internal.NadReceiverDynamicStateDescriptionProvider;
import org.openhab.binding.nadreceiver.internal.PortServer;
import org.openhab.core.library.types.*;
import org.openhab.core.library.unit.Units;
import org.openhab.core.thing.ChannelUID;
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.types.RefreshType;
import org.openhab.core.types.StateOption;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tec.uom.se.unit.Units;

/**
 * The {@link NadReceiverHandler} is responsible for handling commands, which are
 * sent to one of the channels.


@@ 155,34 154,23 @@ public class NadReceiverHandler extends BaseThingHandler implements Runnable {
    private synchronized void connect() {
        if (port == null || !port.isConnected()) {
            logger.warn("connecting");
            boolean tcpMode = (boolean) getThing().getConfiguration().get("isTcpPort");
            if (tcpMode) {
                String hostName = (String) getThing().getConfiguration().get("hostName");
                BigDecimal portNumber = (BigDecimal) getThing().getConfiguration().get("tcpPort");

                if (hostName == null || hostName.isEmpty()) {
                    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No host name specified.");
                    return;
                }

                if (portNumber == null || portNumber.toBigInteger().intValue() == 0) {
                    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No TCP port specified.");
                    return;
                }

                port = new PortServer();
                port.setTcpMode(hostName, portNumber.toBigInteger().intValue());
            String hostName = (String) getThing().getConfiguration().get("hostName");
            BigDecimal portNumber = (BigDecimal) getThing().getConfiguration().get("tcpPort");

            } else {
                String comPort = (String) getThing().getConfiguration().get("port");
                if (comPort == null || comPort.isEmpty()) {
                    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No COM port specified.");
                    return;
                }
            if (hostName == null || hostName.isEmpty()) {
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No host name specified.");
                return;
            }

                port = new PortServer();
                port.setSerialMode(comPort, 115200);
            if (portNumber == null || portNumber.toBigInteger().intValue() == 0) {
                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No TCP port specified.");
                return;
            }

            port = new PortServer();
            port.setTcpMode(hostName, portNumber.toBigInteger().intValue());

            try {
                port.connect();
                OutputStream outStream = port.getOutputStream();

M src/main/java/org/openhab/binding/nadreceiver/internal/NadReceiverDynamicStateDescriptionProvider.java => src/main/java/org/openhab/binding/nadreceiver/internal/NadReceiverDynamicStateDescriptionProvider.java +5 -5
@@ 4,11 4,11 @@ import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.binding.BaseDynamicStateDescriptionProvider;
import org.eclipse.smarthome.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.eclipse.smarthome.core.thing.type.DynamicStateDescriptionProvider;
import org.eclipse.smarthome.core.types.StateDescription;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
import org.openhab.core.types.StateDescription;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

M src/main/java/org/openhab/binding/nadreceiver/internal/NadReceiverHandlerFactory.java => src/main/java/org/openhab/binding/nadreceiver/internal/NadReceiverHandlerFactory.java +1 -0
@@ 22,6 22,7 @@ import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;


M src/main/java/org/openhab/binding/nadreceiver/internal/PortServer.java => src/main/java/org/openhab/binding/nadreceiver/internal/PortServer.java +10 -75
@@ 5,116 5,51 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import gnu.io.NRSerialPort;

// TODO eliminate use of RuntimeException
// TODO better error checking
// TODO should we require a mode to be set, or just assume one?

public class PortServer {
    private NRSerialPort serialPort;
    private Socket tcpPort;

    private boolean serialMode;
    private boolean tcpMode;

    // for TCP mode
    private String hostName;
    private int port;

    // for Serial mode
    private String comPort;
    private int baudRate;

    public boolean isSerialMode() {
        return serialMode;
    }

    public boolean isTcpMode() {
        return tcpMode;
    }

    public void disconnect() {
        if (serialMode) {
            serialPort.disconnect();
        } else if (tcpMode) {
            try {
                tcpPort.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            throw new RuntimeException("No mode set.");
        try {
            tcpPort.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public InputStream getInputStream() throws IOException {
        if (!isConnected()) {
        if (!isConnected())
            throw new RuntimeException("Not Connected.");
        } else if (isTcpMode()) {
            return tcpPort.getInputStream();
        } else {
            return serialPort.getInputStream();
        }
        return tcpPort.getInputStream();
    }

    public boolean isConnected() {
        if (serialMode) {
            return serialPort.isConnected();
        } else if (tcpMode) {
            return tcpPort.isConnected();
        } else {
            throw new RuntimeException("No mode set.");
        }
        return tcpPort.isConnected();
    }

    public OutputStream getOutputStream() throws IOException {
        if (!isConnected()) {
            throw new RuntimeException("Not Connected.");
        } else if (isTcpMode()) {
            return tcpPort.getOutputStream();
        } else {
            return serialPort.getOutputStream();
        }
        return tcpPort.getOutputStream();
    }

    public void connect() throws IOException {
        if (serialMode) {
            serialPort = new NRSerialPort(comPort, baudRate);
            serialPort.connect();
        } else if (tcpMode) {
            tcpPort = new Socket(hostName, port);
        } else {
            throw new RuntimeException("No mode set.");
        }
    }

    public void setSerialMode(String comPort, int baudRate) {
        if (serialPort != null) {
            throw new RuntimeException("Serial mode already set.");
        }
        if (tcpPort != null) {
            throw new RuntimeException("Mode already set to TCP.");
        }

        serialMode = true;
        tcpMode = false;

        this.comPort = comPort;
        this.baudRate = baudRate;
        tcpPort = new Socket(hostName, port);
    }

    public void setTcpMode(String hostName, int port) {
        if (tcpPort != null) {
            throw new RuntimeException("TCP mode already set.");
        }
        if (serialPort != null) {
            throw new RuntimeException("Mode already set to Serial.");
        }

        tcpMode = true;
        serialMode = false;
        tcpPort = new Socket();
        this.hostName = hostName;
        this.port = port;