MEJORAS

parent 1ca0d994
package sacooliveros.whatsappweb.api;
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 org.apache.catalina.connector.Response;
import org.springframework.http.MediaType;
......@@ -26,54 +23,43 @@ import sacooliveros.whatsappweb.utils.WhatsappUtils;
public class WhatsappRest {
Logger logger = Logger.getLogger(WhatsappRest.class.getName());
WhatsappBean whatsappBean;
Whatsapp whatsapp;
WhatsappService whatsappService;
public WhatsappRest(WhatsappBean whatsappBean, WhatsappService whatsappService) {
this.whatsappBean = whatsappBean;
WhatsappUtils utils;
public WhatsappRest(WhatsappBean whatsapp, WhatsappService whatsappService) {
this.whatsapp = whatsapp.getWhatsapp();
this.whatsappService = whatsappService;
}
@GetMapping("qr")
public ResponseEntity<?> getQR() throws ExecutionException, InterruptedException, TimeoutException {
Whatsapp w = whatsappBean.getWhatsapp();
WhatsappUtils utils = new WhatsappUtils();
if (w == null) {
public ResponseEntity<?> getQR() {
if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body(utils.generateQR());
}
w.connect().get(5, TimeUnit.SECONDS);
if (w.isConnected()) {
whatsapp.connect().join();
if (whatsapp.isConnected()) {
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{
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Try logging out first");
return ResponseEntity.status(Response.SC_OK).body(utils.generateQR());
}
}
@GetMapping("logout")
public ResponseEntity<?> logout() {
Whatsapp w = whatsappBean.getWhatsapp();
if (w == null) {
if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body("Not connected");
}
System.out.println(whatsapp.store().about());
try {
w.disconnect().get(5, TimeUnit.SECONDS);
w.logout().get(5, TimeUnit.SECONDS);
if(whatsapp.isConnected()){
whatsapp.disconnect().join();
whatsapp.logout().join();
return ResponseEntity.status(Response.SC_OK).body("Logged out");
} catch (InterruptedException | ExecutionException | TimeoutException e) {
}else{
return ResponseEntity.status(Response.SC_OK).body("Not connected");
}
} catch (Exception e) {
logger.severe(e.getMessage());
return ResponseEntity.status(Response.SC_INTERNAL_SERVER_ERROR).body("Logout failed");
}
......@@ -81,15 +67,13 @@ public class WhatsappRest {
@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) {
Whatsapp w = whatsappBean.getWhatsapp();
if (w == null) {
if (whatsapp == null) {
return ResponseEntity.status(Response.SC_OK).body("Not connected");
}
try{
w.reconnect().get(5, TimeUnit.SECONDS);
System.out.println(w.store().about());
if (w.isConnected()) {
whatsappService.send(payload, files, w);
whatsapp.connect().join();
if (whatsapp.isConnected()) {
whatsappService.send(payload, files, whatsapp);
return ResponseEntity.status(Response.SC_OK).body(payload);
}else{
return ResponseEntity.status(Response.SC_OK).body("Not connected");
......
......@@ -9,10 +9,15 @@ import it.auties.whatsapp.api.Whatsapp;
@Component
public class WhatsappBean {
Optional<Whatsapp> w = Whatsapp.webBuilder().lastConnection().registered();
Optional<Whatsapp> w = Whatsapp.webBuilder().firstConnection().registered();
public Whatsapp getWhatsapp() {
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;
public class WhatsappUtils {
Whatsapp whatsapp;
Logger logger = Logger.getLogger(WhatsappUtils.class.getName());
public WhatsappUtils(Whatsapp whatsapp) {
this.whatsapp = whatsapp;
}
public String generateQR() {
StringBuilder qr = new StringBuilder();
CountDownLatch latch = new CountDownLatch(1);
......@@ -26,11 +31,7 @@ public class WhatsappUtils {
try {
CompletableFuture.supplyAsync(() ->
Whatsapp.webBuilder()
.lastConnection()
.unregistered(QrHandler.toString(smallQrConsumer))
.connect()
.join()
Whatsapp.webBuilder().newConnection().unregistered(QrHandler.toString(smallQrConsumer)).connect().join()
).get(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