Переосмысление Subscribe

This commit is contained in:
Mark Struchkov 2019-05-23 13:26:52 +03:00
parent 2b518c50e6
commit 4e69f4d561
6 changed files with 106 additions and 68 deletions

View File

@ -6,7 +6,7 @@
<groupId>org.sadtech.vkbot</groupId> <groupId>org.sadtech.vkbot</groupId>
<artifactId>vkbot-core</artifactId> <artifactId>vkbot-core</artifactId>
<version>0.2.0-RELEASE</version> <version>0.2.1-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<build> <build>
@ -23,7 +23,7 @@
</build> </build>
<properties> <properties>
<bot.core.ver>0.6.0-RELEASE</bot.core.ver> <bot.core.ver>0.6.1-SNAPSHOT</bot.core.ver>
<vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver> <vksdk.ver>0.5.13-SNAPSHOT</vksdk.ver>
<log4j.ver>1.2.17</log4j.ver> <log4j.ver>1.2.17</log4j.ver>
</properties> </properties>

View File

@ -0,0 +1,53 @@
package org.sadtech.vkbot.core.distribution;
import java.util.Set;
public abstract class AbstractBasketSubscribe<S> implements EventSubscribe<S> {
private Set<AbstractBasketSubscribe> basketSubscribes;
private AbstractBasketSubscribe prioritySubscribe;
public Set<AbstractBasketSubscribe> getBasketSubscribes() {
return basketSubscribes;
}
public void setBasketSubscribes(Set<AbstractBasketSubscribe> basketSubscribes) {
this.basketSubscribes = basketSubscribes;
}
public AbstractBasketSubscribe getPrioritySubscribe() {
return prioritySubscribe;
}
public void setPrioritySubscribe(AbstractBasketSubscribe prioritySubscribe) {
this.prioritySubscribe = prioritySubscribe;
}
protected abstract boolean check(S object);
private boolean goNextSubscribe(S object) {
boolean flag = false;
if (prioritySubscribe != null && prioritySubscribe.check(object)) {
prioritySubscribe.update(object);
flag = true;
} else if (basketSubscribes != null) {
for (AbstractBasketSubscribe basketSubscribe : basketSubscribes) {
if (basketSubscribe.check(object)) {
basketSubscribe.update(object);
} else {
flag = true;
}
}
}
return flag;
}
@Override
public void update(S object) {
if (!goNextSubscribe(object)) {
processing(object);
}
}
public abstract void processing(S object);
}

View File

@ -0,0 +1,23 @@
package org.sadtech.vkbot.core.distribution;
import com.vk.api.sdk.objects.messages.Message;
import org.sadtech.bot.core.service.AccountService;
public class AccountSubscribe extends AbstractBasketSubscribe<Message> {
private final AccountService accountService;
public AccountSubscribe(AccountService accountService) {
this.accountService = accountService;
}
@Override
protected boolean check(Message userMessage) {
return userMessage.getAttachments().size() > 0 && "Денежный перевод".equals(userMessage.getAttachments().get(0).getLink().getCaption());
}
@Override
public void processing(Message object) {
accountService.pay(0);
}
}

View File

@ -4,15 +4,11 @@ import com.google.gson.JsonObject;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.sadtech.bot.core.service.RawEventService; import org.sadtech.bot.core.service.RawEventService;
import java.util.HashMap; public class EventDistributor extends AbstractBasketSubscribe<JsonObject> implements Runnable {
import java.util.Map;
public class EventDistributor implements Runnable {
private static final Logger log = Logger.getLogger(EventDistributor.class); private static final Logger log = Logger.getLogger(EventDistributor.class);
private final RawEventService rawEventService; private final RawEventService rawEventService;
private final Map<String, EventSubscribe> eventDistributionMap = new HashMap<>();
public EventDistributor(RawEventService rawEventService) { public EventDistributor(RawEventService rawEventService) {
this.rawEventService = rawEventService; this.rawEventService = rawEventService;
@ -21,28 +17,27 @@ public class EventDistributor implements Runnable {
public void update() { public void update() {
while (true) { while (true) {
try { if (rawEventService.getJsonObjects().peek() != null) {
if (rawEventService.getJsonObjects().peek() != null) { JsonObject event = rawEventService.getJsonObjects().poll();
JsonObject event = rawEventService.getJsonObjects().poll(); log.info("Главный дистрибьютор отправил событие дальше");
log.info("Главный дистрибьютор отправил событие дальше"); super.update(event);
if (eventDistributionMap.containsKey(event.get("type").getAsString())) {
eventDistributionMap.get(event.get("type").getAsString()).update(event.getAsJsonObject("object"));
}
}
} catch (Exception e) {
log.error(e.getStackTrace());
break;
} }
} }
} }
public void setEventDistributionMap(String key, EventSubscribe eventSubscribe) {
this.eventDistributionMap.put(key, eventSubscribe);
}
@Override @Override
public void run() { public void run() {
log.info("EventDistributor запущен"); log.info("EventDistributor запущен");
update(); update();
} }
@Override
protected boolean check(JsonObject object) {
return false;
}
@Override
public void processing(JsonObject object) {
}
} }

View File

@ -11,68 +11,37 @@ import org.sadtech.bot.core.domain.attachment.Attachment;
import org.sadtech.bot.core.domain.attachment.AudioMessage; import org.sadtech.bot.core.domain.attachment.AudioMessage;
import org.sadtech.bot.core.service.MailService; import org.sadtech.bot.core.service.MailService;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
public class MailSubscriber implements EventSubscribe<JsonObject> { public class MailSubscriber extends AbstractBasketSubscribe<JsonObject> {
private static final Logger log = Logger.getLogger(MailSubscriber.class); private static final Logger log = Logger.getLogger(MailSubscriber.class);
private final MailService mailService; private final MailService mailService;
private Set<Integer> admins = new HashSet<>();
private final Map<String, EventSubscribe<Message>> eventDistributionMap = new HashMap<>();
public MailSubscriber(MailService mailService) { public MailSubscriber(MailService mailService) {
this.mailService = mailService; this.mailService = mailService;
} }
public void setAdmins(Set<Integer> admins) { @Override
this.admins = admins; protected boolean check(JsonObject object) {
} String type = object.get("type").getAsString();
return "message_new".equals(type);
public Set<Integer> getAdmins() {
return admins;
} }
@Override @Override
public void update(JsonObject object) { public void processing(JsonObject object) {
log.info("Дистрибьютор получил событие - сообщение");
Gson gson = new Gson(); Gson gson = new Gson();
Message userMessage = gson.fromJson(object, Message.class); Message userMessage = gson.fromJson(object.getAsJsonObject("object"), Message.class);
log.info(userMessage); log.info(userMessage);
mailService.add(createMail(userMessage));
if (userMessage.getPeerId() > 2000000000) {
if (eventDistributionMap.containsKey("chat")) {
eventDistributionMap.get("chat").update(userMessage);
}
} else {
if (admins.contains(userMessage.getPeerId()) && eventDistributionMap.containsKey("terminal")) {
log.info("Сообщение отправлено в репозиторий команд");
eventDistributionMap.get("terminal").update(userMessage);
} else {
log.info("Сообщение отправленно на добавление в репозиторий");
mailService.add(createMaail(userMessage));
}
}
} }
public byte[] getBytes(InputStream inputStream) throws IOException { private Mail createMail(Message message) {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
private Mail createMaail(Message message) {
Mail mail = new Mail(); Mail mail = new Mail();
mail.setMessage(message.getText()); mail.setMessage(message.getText());
mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId())); mail.setDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), TimeZone.getDefault().toZoneId()));

View File

@ -9,7 +9,7 @@ import com.vk.api.sdk.objects.messages.Keyboard;
import com.vk.api.sdk.queries.messages.MessagesSendQuery; import com.vk.api.sdk.queries.messages.MessagesSendQuery;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.sadtech.bot.core.domain.BoxAnswer; import org.sadtech.bot.core.domain.BoxAnswer;
import org.sadtech.bot.core.sender.Sent; import org.sadtech.bot.core.service.sender.Sent;
import org.sadtech.vkbot.core.VkConnect; import org.sadtech.vkbot.core.VkConnect;
import org.sadtech.vkbot.core.VkInsertData; import org.sadtech.vkbot.core.VkInsertData;
import org.sadtech.vkbot.core.convert.KeyBoardConvert; import org.sadtech.vkbot.core.convert.KeyBoardConvert;
@ -68,8 +68,6 @@ public class MailSenderVk implements Sent {
} }
private void sendMessage(MessagesSendQuery messages) { private void sendMessage(MessagesSendQuery messages) {
try { try {
messages.execute(); messages.execute();