Ich versuche, Daten vom biometrischen Anwesenheitsgerät zkteco k40 von Spring Boot abzurufenJava

Java-Forum
Anonymous
 Ich versuche, Daten vom biometrischen Anwesenheitsgerät zkteco k40 von Spring Boot abzurufen

Post by Anonymous »

Ich habe irgendwo gelesen, dass wir zuerst mit dem IP-Port des Geräts verbunden werden müssen. Ich habe über WebSocket eine Verbindung über Code hergestellt. Dies ist mein Code. Wenn ich das ausführe, bekomme ich eine SocketTimedOut-Ausnahme ... Deshalb habe ich die Zeit von READ_TIMEOUT erhöht. Tritt dieses Problem immer noch auf? Was habe ich falsch gemacht? Was muss ich tun, um eine Verbindung zum BIOMETRIC-Gerät herzustellen und Daten davon abzurufen?

Code: Select all

    public String getAttendanceData() {
StringBuilder response = new StringBuilder();
Socket socket = null;
DataOutputStream dataOutputStream = null;
BufferedReader reader = null;

try {
// Establish a socket connection to the device
socket = new Socket();
socket.connect(new InetSocketAddress(DEVICE_IP, DEVICE_PORT), CONNECTION_TIMEOUT);
socket.setSoTimeout(READ_TIMEOUT); // Set socket read timeout

System.out.println("Connection established to device at " + DEVICE_IP + ":" + DEVICE_PORT);

// Send authentication request with the username and password
String authCommand = "AUTH:" + USERNAME + ":" + PASSWORD;
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataOutputStream.write(authCommand.getBytes());
System.out.println("Sent authentication command to device: " + authCommand);

// Wait briefly before continuing, to ensure the device has time to process
Thread.sleep(1000);

// Read the response from the device
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
response.append(line).append("\n");
}

System.out.println("Received response from device.");
} catch (SocketTimeoutException e) {
System.err.println("Connection timed out: " + e.getMessage());
return "Error: Connection timed out while fetching attendance data.";
} catch (SocketException e) {
System.err.println("Connection reset: " + e.getMessage());
return "Error: Connection reset. The device might have closed the connection unexpectedly.";
} catch (IOException e) {
System.err.println("I/O error while communicating with the device: " + e.getMessage());
return "Error fetching attendance data: " + e.getMessage();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
if (dataOutputStream != null) dataOutputStream.close();
if (socket != null && !socket.isClosed()) socket.close();
} catch (IOException e) {
System.err.println("Error closing resources: " + e.getMessage());
}
}

return response.toString();
}
}
Ich versuche, Anwesenheitsdaten von diesem Gerät abzurufen

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post