First commit

This commit is contained in:
2024-11-15 10:58:51 +07:00
parent 2d647fd470
commit 240a7357b6
7 changed files with 167 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import org.pmw.tinylog.Logger;
import java.net.InetAddress;
import java.net.Socket;
@SuppressWarnings("unused")
public class AASMini {
private final String targetIP;
private final int targetPort;
@@ -20,6 +21,14 @@ public class AASMini {
this.targetPort = port;
}
/**
* Check if the AAS Mini is connected
* @return true if connected, false otherwise
*/
public boolean IsConnected(){
return socket != null && socket.isConnected();
}
/**
* Connect to AAS Mini
* @return true if connected, false otherwise
@@ -42,6 +51,61 @@ public class AASMini {
return false;
}
/**
* Disconnect from AAS Mini
*/
public void Disconnect(){
try{
if (socket != null && socket.isConnected()){
socket.close();
Logger.info("Disconnected from {}:{}", targetIP, targetPort);
}
} catch (Exception e){
Logger.error("Failed to disconnect from {}:{}, Error : {}", targetIP, targetPort, e.getMessage());
}
socket = null;
}
//TODO protocol untuk kirim data ke AAS Mini
/**
* Send data to AAS Mini and receive response
* @param data Data to be sent
* @return byte array of response or null if failed
*/
private byte[] SendAndReceive(byte[] data){
if (IsConnected()){
if (data!=null && data.length>0){
try{
socket.getOutputStream().write(data);
byte[] buffer = new byte[1024];
int read = socket.getInputStream().read(buffer);
if (read>0){
byte[] result = new byte[read];
System.arraycopy(buffer, 0, result, 0, read);
return result;
}
} catch (Exception e){
Logger.error("Failed to send data to {}:{}, Error : {}", targetIP, targetPort, e.getMessage());
}
}
}
return null;
}
/**
* Send data to AAS Mini
* @param data Data to be sent
*/
private void SendData(byte[] data){
if (IsConnected()){
if (data!=null && data.length>0){
try{
socket.getOutputStream().write(data);
} catch (Exception e){
Logger.error("Failed to send data to {}:{}, Error : {}", targetIP, targetPort, e.getMessage());
}
}
}
}
}