Search in sources :

Example 1 with OpenEnergyMonitorException

use of org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException in project openhab1-addons by openhab.

the class OpenEnergyMonitorSerialConnector method connect.

@Override
public void connect() throws OpenEnergyMonitorException {
    try {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        in = serialPort.getInputStream();
        logger.debug("Open Energy Monitor Serial Port message listener started");
    } catch (Exception e) {
        throw new OpenEnergyMonitorException(e);
    }
}
Also used : OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) IOException(java.io.IOException)

Example 2 with OpenEnergyMonitorException

use of org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException in project openhab1-addons by openhab.

the class OpenEnergyMonitorSerialConnector method disconnect.

@Override
public void disconnect() throws OpenEnergyMonitorException {
    logger.debug("Disconnecting");
    try {
        logger.debug("Close serial streams");
        if (in != null) {
            in.close();
        }
        in = null;
        if (serialPort != null) {
            logger.debug("Close serial port");
            serialPort.close();
            serialPort = null;
        }
    } catch (IOException e) {
        throw new OpenEnergyMonitorException(e);
    }
    logger.debug("Closed");
}
Also used : OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) IOException(java.io.IOException)

Example 3 with OpenEnergyMonitorException

use of org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException in project openhab1-addons by openhab.

the class OpenEnergyMonitorSimulator method receiveDatagram.

@Override
public byte[] receiveDatagram() throws OpenEnergyMonitorException {
    try {
        Thread.sleep(5000);
        String testData1 = new String("10 224 0 73 1 139 0 68 0 11 0 66 0 28 0 17 0 141 0 175 0 74 0 80 0 122 1 59 2 236 0 227 34 0 0 60 0");
        String testData2 = new String("10 221 0 74 1 139 0 66 0 10 0 63 0 26 0 16 0 136 0 167 0 70 0 81 0 113 1 50 2 236 0 227 34 0 0 60 0");
        String testData3 = new String("10 223 0 71 1 139 0 68 0 11 0 57 0 24 0 19 0 132 0 163 0 69 0 81 0 110 1 36 2 235 0 227 34 0 0 60 0");
        String testData4 = new String("10 129 15 17 17 199 7 90 0 133 15 208 16 170 7 92 0 49 15 10 17 196 7 89 0 55 46 237 50 219 0 222 14 1 0 233 8");
        String testData5 = new String("10 232 15 51 17 203 7 92 0 98 15 140 16 127 7 92 0 227 14 251 16 178 7 87 0 46 46 187 50 220 0 226 14 1 0 248 8");
        String testData6 = new String("10 222 19 192 20 75 8 95 0 17 19 234 19 245 7 95 0 130 19 183 20 71 8 94 0 114 58 99 61 250 0 230 14 1 0 240 8");
        String testData7 = new String("10 78 16 156 17 223 7 92 0 229 15 230 16 141 7 94 0 46 15 224 16 139 7 89 0 97 47 99 51 223 0 235 14 1 0 255 8");
        @SuppressWarnings("unused") String testData8 = new String("10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36");
        @SuppressWarnings("unused") String testData9 = new String("10 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 64 0 0 0 0 0 0 0 64");
        final String[] messages = new String[] { testData1, testData2, testData3, testData4, testData5, testData6, testData7 };
        if (++counter >= messages.length) {
            counter = 0;
        }
        String[] bytes = messages[counter].split(" ");
        ByteBuffer bytebuf = ByteBuffer.allocate(bytes.length);
        for (int i = 0; i < bytes.length; i++) {
            byte b = (byte) Integer.parseInt(bytes[i]);
            bytebuf.put(b);
        }
        return bytebuf.array();
    } catch (InterruptedException e) {
        throw new OpenEnergyMonitorException(e);
    }
}
Also used : OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) ByteBuffer(java.nio.ByteBuffer)

Example 4 with OpenEnergyMonitorException

use of org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException in project openhab1-addons by openhab.

the class OpenEnergyMonitorUDPConnector method connect.

@Override
public void connect() throws OpenEnergyMonitorException {
    if (socket == null) {
        try {
            socket = new DatagramSocket(port);
            logger.debug("Open Energy Monitor UDP message listener started");
        } catch (SocketException e) {
            throw new OpenEnergyMonitorException(e);
        }
    }
}
Also used : SocketException(java.net.SocketException) OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) DatagramSocket(java.net.DatagramSocket)

Example 5 with OpenEnergyMonitorException

use of org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException in project openhab1-addons by openhab.

the class OpenEnergyMonitorUDPConnector method receiveDatagram.

@Override
public byte[] receiveDatagram() throws OpenEnergyMonitorException {
    try {
        if (socket == null) {
            socket = new DatagramSocket(port);
        }
        // Create a packet
        DatagramPacket packet = new DatagramPacket(new byte[MAX_PACKET_SIZE], MAX_PACKET_SIZE);
        // Receive a packet (blocking)
        socket.receive(packet);
        String[] bytes = new String(Arrays.copyOfRange(packet.getData(), 0, packet.getLength() - 1)).split(" ");
        ByteBuffer bytebuf = ByteBuffer.allocate(bytes.length);
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i].isEmpty() == false) {
                byte b = (byte) Integer.parseInt(bytes[i]);
                bytebuf.put(b);
            }
        }
        return bytebuf.array();
    } catch (SocketException e) {
        throw new OpenEnergyMonitorException(e);
    } catch (IOException e) {
        throw new OpenEnergyMonitorException(e);
    } catch (NumberFormatException e) {
        throw new OpenEnergyMonitorException(e);
    }
}
Also used : SocketException(java.net.SocketException) OpenEnergyMonitorException(org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

OpenEnergyMonitorException (org.openhab.binding.openenergymonitor.internal.OpenEnergyMonitorException)5 IOException (java.io.IOException)3 DatagramSocket (java.net.DatagramSocket)2 SocketException (java.net.SocketException)2 ByteBuffer (java.nio.ByteBuffer)2 CommPort (gnu.io.CommPort)1 CommPortIdentifier (gnu.io.CommPortIdentifier)1 DatagramPacket (java.net.DatagramPacket)1