Паттерн цепочка с автоподтягиванием классов-команд
parent
c3711bb571
commit
0fe1d7e670
|
@ -1,101 +1,42 @@
|
|||
package ru.ldeloff.servermonitorbot.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
import org.telegram.telegrambots.meta.api.objects.Message;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.Command;
|
||||
import ru.ldeloff.servermonitorbot.model.TelegramBot;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.command.FirstUseCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.GetStatusSessions;
|
||||
import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu;
|
||||
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempAggregateCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.mdadm.MdadmStatusAggregateCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.mdadm.MdadmStatusCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramBotController extends TelegramLongPollingBot {
|
||||
//private final Map<String, CommandTemplate> commands;
|
||||
private final Map<String, CommandTemplate> commands;
|
||||
final TelegramBot telegramBot;
|
||||
final SshRepository sshRepository;
|
||||
final FirstUseCommand firstUseCommand;
|
||||
final GetStatusSessions getStatusSessions;
|
||||
final UnameAggregateCommand unameAggregateCommand;
|
||||
final SwitchToMainMenu switchToMainMenu;
|
||||
final UnameCommand unameCommand;
|
||||
final CpuTempAggregateCommand cpuTempAggregateCommand;
|
||||
final CpuTempCommand cpuTempCommand;
|
||||
final HddTempAggregateCommand hddTempAggregateCommand;
|
||||
final HddTempCommand hddTempCommand;
|
||||
final MdadmStatusAggregateCommand mdadmStatusAggregateCommand;
|
||||
final MdadmStatusCommand mdadmStatusCommand;
|
||||
|
||||
@Override
|
||||
public void onUpdateReceived(Update update) {
|
||||
if (update.hasMessage()) {
|
||||
if (update.getMessage().hasText()) {
|
||||
String messageText = update.getMessage().getText();
|
||||
switch (messageText) {
|
||||
case "/start" -> {
|
||||
firstUseCommand.execute(update, this);
|
||||
boolean result = false;
|
||||
for (Map.Entry<String, CommandTemplate> entry : commands.entrySet()) {
|
||||
result = entry.getValue().execute(update, this);
|
||||
if (result) {
|
||||
break;
|
||||
}
|
||||
case "Статус" -> {
|
||||
getStatusSessions.execute(update, this);
|
||||
}
|
||||
case "uname" -> {
|
||||
unameAggregateCommand.execute(update, this);
|
||||
if (!result) {
|
||||
Message message = update.getMessage();
|
||||
if (message!=null) {
|
||||
message.setText("Not found");
|
||||
}
|
||||
case "CPU.temp" -> {
|
||||
cpuTempAggregateCommand.execute(update, this);
|
||||
}
|
||||
case "HDD.temp" -> {
|
||||
hddTempAggregateCommand.execute(update, this);
|
||||
}
|
||||
case "MDADM" -> {
|
||||
mdadmStatusAggregateCommand.execute(update,this);
|
||||
}
|
||||
default -> {
|
||||
update.setMessage(message);
|
||||
switchToMainMenu.execute(update, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
String [] tags = update.getCallbackQuery().getData().split(":");
|
||||
if (tags.length > 1) {
|
||||
switch (tags[0]) {
|
||||
case "uname" -> {
|
||||
unameCommand.execute(update, this);
|
||||
}
|
||||
case "CPU.temp" -> {
|
||||
cpuTempCommand.execute(update, this);
|
||||
}
|
||||
case "HDD.temp" -> {
|
||||
hddTempCommand.execute(update, this);
|
||||
}
|
||||
case "MDADM" -> {
|
||||
mdadmStatusCommand.execute(update,this);
|
||||
}
|
||||
default -> {
|
||||
switchToMainMenu.execute(update, this);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
switchToMainMenu.execute(update, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String getBotUsername() {
|
||||
return telegramBot.getBotUsername();
|
||||
|
|
|
@ -14,6 +14,7 @@ import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
|||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||
|
||||
import static ru.ldeloff.servermonitorbot.init.AddCommands.fillCommandTable;
|
||||
|
||||
|
@ -22,14 +23,15 @@ import static ru.ldeloff.servermonitorbot.init.AddCommands.fillCommandTable;
|
|||
@NoArgsConstructor
|
||||
public class StartBot implements ApplicationRunner {
|
||||
private TelegramBotController telegramBot;
|
||||
private TelegramBotKeyboard telegramBotKeyboard;
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
public StartBot(TelegramBotController telegramBot, UserService userService) {
|
||||
public StartBot(TelegramBotController telegramBot, TelegramBotKeyboard telegramBotKeyboard, UserService userService) {
|
||||
this.telegramBot = telegramBot;
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
try {
|
||||
|
@ -42,11 +44,10 @@ public class StartBot implements ApplicationRunner {
|
|||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void SendInitMessage(User user) {
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText("Бот перезапущен");
|
||||
CommandTemplate.sendMessage(answer, telegramBot);
|
||||
CommandTemplate.sendMessage(telegramBotKeyboard.uiForm(answer), telegramBot);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
public interface CommandService {
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import org.jvnet.hk2.annotations.Service;
|
||||
|
||||
@Service
|
||||
public class CommandServiceImpl implements CommandService {
|
||||
|
||||
}
|
|
@ -1,54 +1,158 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Data
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public abstract class CommandTemplate {
|
||||
final UserService userService;
|
||||
final RoleService roleService;
|
||||
long expectedRole = 2L;
|
||||
public abstract SendMessage actionForAuth(User user, SendMessage message);
|
||||
private final UserService userService;
|
||||
private final RoleService roleService;
|
||||
private final ServerListButtons serverListButtons;
|
||||
private final SshService sshService;
|
||||
@Getter
|
||||
@Setter
|
||||
private long expectedRole = 1L;
|
||||
@Getter
|
||||
@Setter
|
||||
private String name;
|
||||
|
||||
public SendMessage actionForNotAuth(User user, SendMessage message) {
|
||||
message.setText("У пользователя " + user.getLogin() + " (" + user.getTelegramId()
|
||||
+ ") недостаточно прав для выполнения этой команды");
|
||||
return message;
|
||||
public boolean execute(Update update, TelegramBotController bot) {
|
||||
if (!isExecute(update)) {
|
||||
return false;
|
||||
}
|
||||
public void execute(Update update, TelegramBotController bot) {
|
||||
|
||||
User user = getUser(update);
|
||||
String message = getMessage(update);
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText(message);
|
||||
if (user.getRole().getId() <= expectedRole) {
|
||||
logSuccess(user, message);
|
||||
sendMessage(actionForAuth(user, answer), bot);
|
||||
} else {
|
||||
|
||||
if (!isAuth(update)) {
|
||||
logNotAuth(user, message);
|
||||
sendMessage(actionForNotAuth(user, answer), bot);
|
||||
return true;
|
||||
}
|
||||
|
||||
String type = checkTypeCommand(update);
|
||||
switch (type) {
|
||||
case "aggregate":
|
||||
logSuccess(user, message);
|
||||
sendMessage(executeAggregate(user, answer), bot);
|
||||
return true;
|
||||
case "command":
|
||||
logSuccess(user, message);
|
||||
sendMessage(executeCommand(user, answer), bot);
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected void logSuccess(User user, String message) {
|
||||
// Методы для проверки команды
|
||||
private boolean isExecute(Update update) {
|
||||
if (update.hasMessage()) {
|
||||
if (update.getMessage().hasText()) {
|
||||
String messageText = update.getMessage().getText();
|
||||
if (Objects.equals(messageText, name)) {
|
||||
log.debug("Команда " + messageText + " будет выполнена");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
String [] tags = update.getCallbackQuery().getData().split(":");
|
||||
if (tags.length > 1) {
|
||||
if (Objects.equals(tags[0], name)) {
|
||||
log.debug("Команда " + tags[0] + " будет выполнена");
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
log.debug("Команда не будет выполнена");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
log.debug("Команда не будет выполнена");
|
||||
return false;
|
||||
}
|
||||
private String checkTypeCommand(Update update) {
|
||||
if (update.hasMessage()) {
|
||||
if (update.getMessage().hasText()) {
|
||||
return "aggregate";
|
||||
}
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
String [] tags = update.getCallbackQuery().getData().split(":");
|
||||
if (tags.length > 1) {
|
||||
return "command";
|
||||
} else {
|
||||
return "error";
|
||||
}
|
||||
}
|
||||
return "error";
|
||||
}
|
||||
private boolean isAuth(Update update) {
|
||||
User user = getUser(update);
|
||||
return user.getRole().getId() <= expectedRole;
|
||||
}
|
||||
|
||||
// Методы при верных условиях
|
||||
void logSuccess(User user, String message) {
|
||||
log.info("Получена команда '" + message + "' от " + user.getLogin()
|
||||
+ " (" + user.getTelegramId() + "). OK");
|
||||
}
|
||||
protected void logNotAuth(User user, String message) {
|
||||
SendMessage executeAggregate(User user, SendMessage message) {
|
||||
return serverListButtons.uiForm(message);
|
||||
}
|
||||
SendMessage executeCommand(User user, SendMessage message) {
|
||||
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||
.map(String::trim)
|
||||
.toArray(String[]::new);
|
||||
|
||||
if (tags[1].equals("all")) {
|
||||
message.setText(executeCommandAllHost());
|
||||
} else {
|
||||
message.setText(Objects.requireNonNull(executeCommandSpecificHost(tags[1])));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
private String executeCommandAllHost() {
|
||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> response.append(executeCommandSpecificHost(server.getName())).append("\n"));
|
||||
return response.toString();
|
||||
}
|
||||
public abstract String executeCommandSpecificHost(String serverName);
|
||||
|
||||
// Методы при неверных условиях
|
||||
void logNotAuth(User user, String message) {
|
||||
log.warn("Получена команда '" + message + "' от " + user.getLogin()
|
||||
+ " (" + user.getTelegramId() + "). Нет прав");
|
||||
}
|
||||
private User getUser(Update update) {
|
||||
private SendMessage actionForNotAuth(User user, SendMessage message) {
|
||||
message.setText("У пользователя " + user.getLogin() + " (" + user.getTelegramId()
|
||||
+ ") недостаточно прав для выполнения этой команды");
|
||||
return message;
|
||||
}
|
||||
|
||||
// Общее
|
||||
// TODO: вынести в UpdateUtils
|
||||
User getUser(Update update) {
|
||||
long id = -1L;
|
||||
String login = null;
|
||||
if (update.hasMessage()) {
|
||||
|
@ -67,8 +171,7 @@ public abstract class CommandTemplate {
|
|||
user.setLogin(login);
|
||||
return user;
|
||||
}
|
||||
|
||||
private String getMessage(Update update) {
|
||||
String getMessage(Update update) {
|
||||
if (update.hasMessage()) {
|
||||
return update.getMessage().getText();
|
||||
} else if (update.hasCallbackQuery()) {
|
||||
|
@ -77,6 +180,7 @@ public abstract class CommandTemplate {
|
|||
return "";
|
||||
}
|
||||
|
||||
// TODO: вынести
|
||||
public static void sendMessage(SendMessage message, TelegramBotController bot) {
|
||||
try {
|
||||
bot.execute(message);
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.cputemp;
|
||||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -21,33 +16,14 @@ import java.util.Optional;
|
|||
@Slf4j
|
||||
public class CpuTempCommand extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public CpuTempCommand(UserService userService, RoleService roleService, SshService sshService) {
|
||||
super(userService, roleService);
|
||||
public CpuTempCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
setName("CPU.temp");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||
.map(String::trim)
|
||||
.toArray(String[]::new);
|
||||
|
||||
if (tags[1].equals("all")) {
|
||||
message.setText(hddTempAllHost());
|
||||
} else {
|
||||
message.setText(Objects.requireNonNull(hddTempSpecificHost(tags[1])));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private String hddTempAllHost() {
|
||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> response.append(hddTempSpecificHost(server.getName())).append("\n"));
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
private String hddTempSpecificHost(String serverName) {
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
|
@ -2,22 +2,36 @@ package ru.ldeloff.servermonitorbot.service.command;
|
|||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||
|
||||
@Service
|
||||
public class FirstUseCommand extends CommandTemplate {
|
||||
final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public FirstUseCommand(UserService userService, RoleService roleService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(userService, roleService);
|
||||
public FirstUseCommand(UserService userService,
|
||||
RoleService roleService,
|
||||
SshService sshService,
|
||||
ServerListButtons serverListButtons,
|
||||
TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("/start");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
message.setText("Добро пожаловать " + user.getLogin() + "!");
|
||||
return telegramBotKeyboard.uiForm(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,18 +6,22 @@ import ru.ldeloff.servermonitorbot.model.User;
|
|||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class GetStatusSessions extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
|
||||
public GetStatusSessions(UserService userService, RoleService roleService, SshService sshService) {
|
||||
super(userService, roleService);
|
||||
public GetStatusSessions(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
setName("Статус");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
public SendMessage executeAggregate(User user, SendMessage message) {
|
||||
return sshService.getStatusSessions(message);
|
||||
}
|
||||
@Override
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.hddtemp;
|
||||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -21,33 +16,13 @@ import java.util.Optional;
|
|||
@Slf4j
|
||||
public class HddTempCommand extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public HddTempCommand(UserService userService, RoleService roleService, SshService sshService) {
|
||||
super(userService, roleService);
|
||||
public HddTempCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
setName("HDD.temp");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||
.map(String::trim)
|
||||
.toArray(String[]::new);
|
||||
|
||||
if (tags[1].equals("all")) {
|
||||
message.setText(hddTempAllHost());
|
||||
} else {
|
||||
message.setText(Objects.requireNonNull(hddTempSpecificHost(tags[1])));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private String hddTempAllHost() {
|
||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> response.append(hddTempSpecificHost(server.getName())).append("\n"));
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
private String hddTempSpecificHost(String serverName) {
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
|
@ -1,52 +1,27 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.mdadm;
|
||||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class MdadmStatusCommand extends CommandTemplate {
|
||||
|
||||
final SshService sshService;
|
||||
|
||||
public MdadmStatusCommand(UserService userService, RoleService roleService, SshService sshService) {
|
||||
super(userService, roleService);
|
||||
public MdadmStatusCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
setName("MDADM");
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||
.map(String::trim)
|
||||
.toArray(String[]::new);
|
||||
|
||||
if (tags[1].equals("all")) {
|
||||
message.setText(mdadmStatusAllHost());
|
||||
} else {
|
||||
message.setText(Objects.requireNonNull(mdadmStatusSpecificHost(tags[1])));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private String mdadmStatusAllHost() {
|
||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> response.append(mdadmStatusSpecificHost(server.getName())).append("\n"));
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
private String mdadmStatusSpecificHost(String serverName) {
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
||||
|
@ -78,7 +53,6 @@ public class MdadmStatusCommand extends CommandTemplate {
|
|||
.forEach(names::add);
|
||||
return names;
|
||||
}
|
||||
|
||||
private List<String> parseHddState(SshCommand result) {
|
||||
List<String> names = new ArrayList<>();
|
||||
names.add("Number Name State");
|
|
@ -3,9 +3,13 @@ package ru.ldeloff.servermonitorbot.service.command;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||
|
||||
@Service
|
||||
|
@ -13,23 +17,31 @@ import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
|||
public class SwitchToMainMenu extends CommandTemplate {
|
||||
final TelegramBotKeyboard telegramBotKeyboard;
|
||||
|
||||
public SwitchToMainMenu(UserService userService, RoleService roleService, TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(userService, roleService);
|
||||
public SwitchToMainMenu(UserService userService,
|
||||
RoleService roleService,
|
||||
SshService sshService,
|
||||
ServerListButtons serverListButtons,
|
||||
TelegramBotKeyboard telegramBotKeyboard) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
setName("Not found");
|
||||
}
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
message.setText("Неверная команда. Попробуем сначала.");
|
||||
return telegramBotKeyboard.uiForm(message);
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logSuccess(User user, String message) {
|
||||
void logSuccess(User user, String message) {
|
||||
log.warn("Получена несуществующая команда '" + message + "' от " + user.getLogin()
|
||||
+ " (" + user.getTelegramId() + "). OK");
|
||||
}
|
||||
@Override
|
||||
public void logNotAuth(User user, String message) {
|
||||
log.warn("Получена несуществующая команда '" + message + "' от " + user.getLogin()
|
||||
+ " (" + user.getTelegramId() + "). Нет прав");
|
||||
SendMessage executeAggregate(User user, SendMessage message) {
|
||||
SendMessage answer = new SendMessage();
|
||||
answer.setChatId(user.getTelegramId());
|
||||
answer.setText("Неверная команда. Попробуем сначала.");
|
||||
return telegramBotKeyboard.uiForm(answer);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.uname;
|
||||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -21,32 +16,13 @@ import java.util.Optional;
|
|||
@Slf4j
|
||||
public class UnameCommand extends CommandTemplate {
|
||||
final SshService sshService;
|
||||
public UnameCommand(UserService userService, RoleService roleService, SshService sshService) {
|
||||
super(userService, roleService);
|
||||
public UnameCommand(UserService userService, RoleService roleService, SshService sshService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService, serverListButtons, sshService);
|
||||
this.sshService = sshService;
|
||||
setName("uname");
|
||||
}
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
String [] tags = Arrays.stream(message.getText().split(":"))
|
||||
.map(String::trim)
|
||||
.toArray(String[]::new);
|
||||
|
||||
if (tags[1].equals("all")) {
|
||||
message.setText(unameAllHost());
|
||||
} else {
|
||||
message.setText(Objects.requireNonNull(unameSpecificHost(tags[1])));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private String unameAllHost() {
|
||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> response.append(unameSpecificHost(server.getName())).append("\n"));
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
private String unameSpecificHost(String serverName) {
|
||||
public String executeCommandSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
.stream()
|
||||
.filter(x -> x.getName().equals(serverName))
|
|
@ -1,24 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.cputemp;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class CpuTempAggregateCommand extends CommandTemplate {
|
||||
final ServerListButtons serverListButtons;
|
||||
|
||||
public CpuTempAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService);
|
||||
this.serverListButtons = serverListButtons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
return serverListButtons.uiForm(message);
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.hddtemp;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class HddTempAggregateCommand extends CommandTemplate {
|
||||
final ServerListButtons serverListButtons;
|
||||
|
||||
public HddTempAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService);
|
||||
this.serverListButtons = serverListButtons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
return serverListButtons.uiForm(message);
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.mdadm;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class MdadmStatusAggregateCommand extends CommandTemplate {
|
||||
|
||||
final ServerListButtons serverListButtons;
|
||||
|
||||
public MdadmStatusAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService);
|
||||
this.serverListButtons = serverListButtons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
return serverListButtons.uiForm(message);
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command.uname;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.role.RoleService;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class UnameAggregateCommand extends CommandTemplate {
|
||||
final ServerListButtons serverListButtons;
|
||||
|
||||
public UnameAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
|
||||
super(userService, roleService);
|
||||
this.serverListButtons = serverListButtons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendMessage actionForAuth(User user, SendMessage message) {
|
||||
return serverListButtons.uiForm(message);
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
CREATE TABLE commands
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(128),
|
||||
level integer
|
||||
);
|
||||
|
||||
COMMENT ON TABLE commands IS 'Команды';
|
||||
COMMENT ON COLUMN commands.id IS 'ID';
|
||||
COMMENT ON COLUMN commands.name IS 'Имя команды';
|
||||
COMMENT ON COLUMN commands.level IS 'Уровень доступа';
|
Loading…
Reference in New Issue