package pa; import lombok.NonNull; import org.tinylog.Logger; import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; import java.util.function.Consumer; public class VX3K { private final InetSocketAddress inetSocketAddress; public VX3K(String ipaddress, int port){ this.inetSocketAddress = new InetSocketAddress(ipaddress, port); } public void PseudoContactInput(VX3KPseudoContactInput input, @NonNull Consumer<@NonNull VX3KCommandResult> callback){ try(SocketChannel channel = SocketChannel.open(inetSocketAddress)){ channel.write(input.getBuffer()); input.getBuffer().clear(); int read = channel.read(input.getBuffer()); if (read>0){ short command = input.getBuffer().getShort(0); short responsecode = input.getBuffer().getShort(1); if (command == input.getCommandID()){ if (responsecode == 0x0000){ callback.accept(new VX3KCommandResult(input.getCommandID(), true, "success")); } else callback.accept(new VX3KCommandResult(input.getCommandID(), false , "Code:"+responsecode )); } else callback.accept(new VX3KCommandResult(input.getCommandID(), false, "Invalid CommandID reply")); } else callback.accept( new VX3KCommandResult(input.getCommandID(), false, "Read 0")); } catch (Exception e){ Logger.error("PseudoContactInput failed, Message {}", e.getMessage()); callback.accept(new VX3KCommandResult(input.getCommandID(), false, "Exception")); } } }