MEJORAS

parent 1ca0d994
package sacooliveros.whatsappweb.api; package sacooliveros.whatsappweb.api;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.catalina.connector.Response; import org.apache.catalina.connector.Response;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -26,54 +23,43 @@ import sacooliveros.whatsappweb.utils.WhatsappUtils; ...@@ -26,54 +23,43 @@ import sacooliveros.whatsappweb.utils.WhatsappUtils;
public class WhatsappRest { public class WhatsappRest {
Logger logger = Logger.getLogger(WhatsappRest.class.getName()); Logger logger = Logger.getLogger(WhatsappRest.class.getName());
WhatsappBean whatsappBean; Whatsapp whatsapp;
WhatsappService whatsappService; WhatsappService whatsappService;
WhatsappUtils utils;
public WhatsappRest(WhatsappBean whatsappBean, WhatsappService whatsappService) { public WhatsappRest(WhatsappBean whatsapp, WhatsappService whatsappService) {
this.whatsappBean = whatsappBean; this.whatsapp = whatsapp.getWhatsapp();
this.whatsappService = whatsappService; this.whatsappService = whatsappService;
} }
@GetMapping("qr") @GetMapping("qr")
public ResponseEntity<?> getQR() throws ExecutionException, InterruptedException, TimeoutException { public ResponseEntity<?> getQR() {
Whatsapp w = whatsappBean.getWhatsapp(); if (whatsapp == null) {
WhatsappUtils utils = new WhatsappUtils();
if (w == null) {
return ResponseEntity.status(Response.SC_OK).body(utils.generateQR()); return ResponseEntity.status(Response.SC_OK).body(utils.generateQR());
} }
whatsapp.connect().join();
w.connect().get(5, TimeUnit.SECONDS); if (whatsapp.isConnected()) {
if (w.isConnected()) {
return ResponseEntity.status(Response.SC_OK).body("Already connected"); return ResponseEntity.status(Response.SC_OK).body("Already connected");
}
try {
w.connect().get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.severe(e.getMessage());
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Connection failed");
}
if (!w.isConnected()) {
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Connection failed");
}else{ }else{
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Try logging out first"); return ResponseEntity.status(Response.SC_OK).body(utils.generateQR());
} }
} }
@GetMapping("logout") @GetMapping("logout")
public ResponseEntity<?> logout() { public ResponseEntity<?> logout() {
Whatsapp w = whatsappBean.getWhatsapp(); if (whatsapp == null) {
if (w == null) {
return ResponseEntity.status(Response.SC_OK).body("Not connected"); return ResponseEntity.status(Response.SC_OK).body("Not connected");
} }
System.out.println(whatsapp.store().about());
try { try {
w.disconnect().get(5, TimeUnit.SECONDS); if(whatsapp.isConnected()){
w.logout().get(5, TimeUnit.SECONDS); whatsapp.disconnect().join();
return ResponseEntity.status(Response.SC_OK).body("Logged out"); whatsapp.logout().join();
} catch (InterruptedException | ExecutionException | TimeoutException e) { return ResponseEntity.status(Response.SC_OK).body("Logged out");
}else{
return ResponseEntity.status(Response.SC_OK).body("Not connected");
}
} catch (Exception e) {
logger.severe(e.getMessage()); logger.severe(e.getMessage());
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Logout failed"); return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Logout failed");
} }
...@@ -81,15 +67,13 @@ public class WhatsappRest { ...@@ -81,15 +67,13 @@ public class WhatsappRest {
@PostMapping(value = "send", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @PostMapping(value = "send", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> send(@RequestParam Map<String, String> payload, @RequestParam Map<String, MultipartFile> files) { public ResponseEntity<?> send(@RequestParam Map<String, String> payload, @RequestParam Map<String, MultipartFile> files) {
Whatsapp w = whatsappBean.getWhatsapp(); if (whatsapp == null) {
if (w == null) {
return ResponseEntity.status(Response.SC_OK).body("Not connected"); return ResponseEntity.status(Response.SC_OK).body("Not connected");
} }
try{ try{
w.reconnect().get(5, TimeUnit.SECONDS); whatsapp.connect().join();
System.out.println(w.store().about()); if (whatsapp.isConnected()) {
if (w.isConnected()) { whatsappService.send(payload, files, whatsapp);
whatsappService.send(payload, files, w);
return ResponseEntity.status(Response.SC_OK).body(payload); return ResponseEntity.status(Response.SC_OK).body(payload);
}else{ }else{
return ResponseEntity.status(Response.SC_OK).body("Not connected"); return ResponseEntity.status(Response.SC_OK).body("Not connected");
......
...@@ -9,10 +9,15 @@ import it.auties.whatsapp.api.Whatsapp; ...@@ -9,10 +9,15 @@ import it.auties.whatsapp.api.Whatsapp;
@Component @Component
public class WhatsappBean { public class WhatsappBean {
Optional<Whatsapp> w = Whatsapp.webBuilder().lastConnection().registered(); Optional<Whatsapp> w = Whatsapp.webBuilder().firstConnection().registered();
public Whatsapp getWhatsapp() { public Whatsapp getWhatsapp() {
return w.orElse(null); if(w.isEmpty()){
w = Whatsapp.webBuilder().lastConnection().registered();
return w.orElse(null);
}else{
return w.get();
}
} }
} }
...@@ -13,8 +13,13 @@ import it.auties.whatsapp.api.Whatsapp; ...@@ -13,8 +13,13 @@ import it.auties.whatsapp.api.Whatsapp;
public class WhatsappUtils { public class WhatsappUtils {
Whatsapp whatsapp;
Logger logger = Logger.getLogger(WhatsappUtils.class.getName()); Logger logger = Logger.getLogger(WhatsappUtils.class.getName());
public WhatsappUtils(Whatsapp whatsapp) {
this.whatsapp = whatsapp;
}
public String generateQR() { public String generateQR() {
StringBuilder qr = new StringBuilder(); StringBuilder qr = new StringBuilder();
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
...@@ -26,11 +31,7 @@ public class WhatsappUtils { ...@@ -26,11 +31,7 @@ public class WhatsappUtils {
try { try {
CompletableFuture.supplyAsync(() -> CompletableFuture.supplyAsync(() ->
Whatsapp.webBuilder() Whatsapp.webBuilder().newConnection().unregistered(QrHandler.toString(smallQrConsumer)).connect().join()
.lastConnection()
.unregistered(QrHandler.toString(smallQrConsumer))
.connect()
.join()
).get(10, TimeUnit.SECONDS); ).get(10, TimeUnit.SECONDS);
if (!latch.await(10, TimeUnit.SECONDS)) { if (!latch.await(10, TimeUnit.SECONDS)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment