Search in sources :

Example 1 with NotInitializedIOException

use of me.retrodaredevil.solarthing.io.NotInitializedIOException in project solarthing by wildmountainfarms.

the class ModbusListUpdaterWrapper method receive.

@Override
public void receive(List<Packet> packets) {
    final long startTimeNanos = System.nanoTime();
    try {
        reloadCache.run();
        packetListReceiver.receive(packets);
    } catch (ModbusRuntimeException e) {
        LOGGER.error("Modbus exception", e);
        if (isSendErrorPackets) {
            LOGGER.debug("Sending error packets");
            packets.add(new ImmutableExceptionErrorPacket(e.getClass().getName(), e.getMessage(), MODBUS_RUNTIME_EXCEPTION_CATCH_LOCATION_IDENTIFIER, errorIdentifierString));
        }
        boolean isTimeout = false;
        if (e.getCause() instanceof NotInitializedIOException) {
            isTimeout = true;
        }
        if (e instanceof ModbusTimeoutException) {
            isTimeout = true;
            // These messages will hopefully help people with problems fix it faster.
            if (hasBeenSuccessful) {
                LOGGER.info("\n\nHey! We noticed you got a ModbusTimeoutException after getting this to work.\n" + "This is likely a fluke and hopefully this message isn't printed a bunch of times. If it is not a fluke, you may want to check your cable.\n");
            } else {
                LOGGER.info("\n\nHey! We noticed you got a ModbusTimeoutException.\n" + "This is likely a problem with your cable. SolarThing is communicating fine with your serial adapter, but it cannot reach the device.\n" + "Make sure the cable you have has the correct pinout, and feel free to open an issue at https://github.com/wildmountainfarms/solarthing/issues if you need help.\n");
            }
        } else if (e instanceof ParsedResponseException) {
            ParsedResponseException parsedResponseException = (ParsedResponseException) e;
            ModbusMessage message = parsedResponseException.getResponse();
            String hexFunctionCode = String.format("%02X", message.getFunctionCode());
            LOGGER.info("Communication with rover working well. Got this response back: function code=0x" + hexFunctionCode + " data='" + dataToSplitHex(message.getByteData()) + "' feel free to open issue at https://github.com/wildmountainfarms/solarthing/issues/");
        } else if (e instanceof RawResponseException) {
            byte[] data = ((RawResponseException) e).getRawData();
            LOGGER.info("Got part of a response back. (Maybe timed out halfway through?) data='" + dataToSplitHex(data) + "' Feel free to open an issue at https://github.com/wildmountainfarms/solarthing/issues/");
        }
        if (isTimeout) {
            successReporter.reportTimeout();
        } else {
            successReporter.reportSuccessWithError();
        }
        return;
    }
    hasBeenSuccessful = true;
    successReporter.reportSuccess();
    final long readDurationNanos = System.nanoTime() - startTimeNanos;
    LOGGER.debug("took " + TimeUtil.nanosToSecondsString(readDurationNanos) + " seconds to read from device");
}
Also used : ImmutableExceptionErrorPacket(me.retrodaredevil.solarthing.misc.error.ImmutableExceptionErrorPacket) ParsedResponseException(me.retrodaredevil.io.modbus.handling.ParsedResponseException) ModbusTimeoutException(me.retrodaredevil.io.modbus.ModbusTimeoutException) RawResponseException(me.retrodaredevil.io.modbus.handling.RawResponseException) ModbusRuntimeException(me.retrodaredevil.io.modbus.ModbusRuntimeException) NotInitializedIOException(me.retrodaredevil.solarthing.io.NotInitializedIOException) ModbusMessage(me.retrodaredevil.io.modbus.ModbusMessage)

Aggregations

ModbusMessage (me.retrodaredevil.io.modbus.ModbusMessage)1 ModbusRuntimeException (me.retrodaredevil.io.modbus.ModbusRuntimeException)1 ModbusTimeoutException (me.retrodaredevil.io.modbus.ModbusTimeoutException)1 ParsedResponseException (me.retrodaredevil.io.modbus.handling.ParsedResponseException)1 RawResponseException (me.retrodaredevil.io.modbus.handling.RawResponseException)1 NotInitializedIOException (me.retrodaredevil.solarthing.io.NotInitializedIOException)1 ImmutableExceptionErrorPacket (me.retrodaredevil.solarthing.misc.error.ImmutableExceptionErrorPacket)1