Search in sources :

Example 1 with MessageSender

use of me.retrodaredevil.solarthing.message.MessageSender in project solarthing by wildmountainfarms.

the class SendMessageActionNode method createFromFileAndMessage.

@JsonCreator
public static SendMessageActionNode createFromFileAndMessage(@JsonProperty("send_to") File messageSenderFile, @JsonProperty("message") String message) throws IOException {
    requireNonNull(messageSenderFile, "send_to must be present and cannot be blank!");
    MessageSender messageSender = CONFIG_MAPPER.readValue(messageSenderFile, MessageSender.class);
    return new SendMessageActionNode(messageSender, message);
}
Also used : MessageSender(me.retrodaredevil.solarthing.message.MessageSender) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator)

Example 2 with MessageSender

use of me.retrodaredevil.solarthing.message.MessageSender in project solarthing by wildmountainfarms.

the class MessageSenderActionNode method getMessageSenderFrom.

private MessageSender getMessageSenderFrom(MessageEventNode messageEventNode) {
    List<MessageSender> messageSenders = new ArrayList<>();
    for (String senderName : messageEventNode.getSendTo()) {
        MessageSender sender = messageSenderMap.get(senderName);
        if (sender == null) {
            throw new IllegalArgumentException("senderName: " + senderName + " is not defined!");
        }
        messageSenders.add(sender);
    }
    return new MessageSenderMultiplexer(messageSenders);
}
Also used : MessageSender(me.retrodaredevil.solarthing.message.MessageSender) ArrayList(java.util.ArrayList) MessageSenderMultiplexer(me.retrodaredevil.solarthing.message.MessageSenderMultiplexer)

Example 3 with MessageSender

use of me.retrodaredevil.solarthing.message.MessageSender in project solarthing by wildmountainfarms.

the class MessageSenderActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestFragmentedPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestFragmentedPacketGroupEnvironment.class);
    EventDatabaseCacheEnvironment eventDatabaseCacheEnvironment = actionEnvironment.getInjectEnvironment().get(EventDatabaseCacheEnvironment.class);
    return Actions.createRunOnce(() -> {
        FragmentedPacketGroup packetGroup = latestPacketGroupEnvironment.getFragmentedPacketGroupProvider().getPacketGroup();
        FragmentedPacketGroup last = this.last;
        this.last = packetGroup;
        if (packetGroup == null) {
            LOGGER.warn("packetGroup is null!");
        }
        final boolean statusRun;
        if (packetGroup != null && last != null) {
            if (packetGroup.getDateMillis() <= last.getDateMillis()) {
                LOGGER.debug("No new packet group! Will not try to send any messages based on status packets.");
                statusRun = false;
            } else {
                statusRun = true;
            }
        } else {
            LOGGER.debug("No last packet group yet (this is normal). Will not send any messages until next iteration.");
            statusRun = false;
        }
        // Only process stuff from the last minute
        long afterDateMillis = Instant.now().minusSeconds(60).toEpochMilli();
        List<InstancePacketGroup> unhandledEventInstancePacketGroups = eventProcessedPacketTracker.getUnprocessedPackets(eventDatabaseCacheEnvironment.getEventDatabaseCacheManager(), afterDateMillis).stream().map(PacketGroups::parseToInstancePacketGroupRequireNoDefaults).collect(Collectors.toList());
        for (MessageEventNode messageEventNode : messageEventNodes) {
            MessageSender sender = getMessageSenderFrom(messageEventNode);
            if (statusRun) {
                messageEventNode.getMessageEvent().run(sender, last, packetGroup);
            }
            for (InstancePacketGroup instancePacketGroup : unhandledEventInstancePacketGroups) {
                messageEventNode.getMessageEvent().runForEvent(sender, instancePacketGroup);
            }
        }
    });
}
Also used : EventDatabaseCacheEnvironment(me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) MessageSender(me.retrodaredevil.solarthing.message.MessageSender) MessageEventNode(me.retrodaredevil.solarthing.config.message.MessageEventNode)

Example 4 with MessageSender

use of me.retrodaredevil.solarthing.message.MessageSender in project solarthing by wildmountainfarms.

the class MessageSenderActionNode method getMessageSenderMap.

private static Map<String, MessageSender> getMessageSenderMap(Map<String, String> messageSenderFileMap, FileMapper fileMapper) throws IOException {
    requireNonNull(messageSenderFileMap);
    requireNonNull(fileMapper);
    Map<String, MessageSender> senderMap = new HashMap<>();
    for (Map.Entry<String, String> entry : messageSenderFileMap.entrySet()) {
        String key = entry.getKey();
        File file = fileMapper.map(entry.getValue());
        MessageSender sender = CONFIG_MAPPER.readValue(file, MessageSender.class);
        senderMap.put(key, sender);
    }
    return senderMap;
}
Also used : HashMap(java.util.HashMap) MessageSender(me.retrodaredevil.solarthing.message.MessageSender) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 5 with MessageSender

use of me.retrodaredevil.solarthing.message.MessageSender in project solarthing by wildmountainfarms.

the class FlagCommandChatBotHandler method clearFlag.

private void clearFlag(MessageSender messageSender, String flagName) {
    List<VersionedPacket<StoredAlterPacket>> packets = getPacketsWithFlagName(flagName);
    if (packets == null) {
        messageSender.sendMessage("Could not get flags. Please try again.");
        return;
    }
    Instant now = Instant.now();
    if (packets.isEmpty()) {
        messageSender.sendMessage("Flag: '" + flagName + "' is not set!");
        return;
    }
    List<VersionedPacket<StoredAlterPacket>> activePackets = packets.stream().filter(versionedPacket -> {
        FlagPacket flagPacket = (FlagPacket) versionedPacket.getPacket().getPacket();
        return flagPacket.getFlagData().getActivePeriod().isActive(now);
    }).collect(Collectors.toList());
    if (activePackets.isEmpty()) {
        messageSender.sendMessage("Flag: '" + flagName + "' is not currently active. In a future update we may allow you to clear inactive flags.");
        return;
    }
    // We know that the user wants this flag cleared, so let's delete all the packets that are active.
    for (VersionedPacket<StoredAlterPacket> packetToRequestDeleteFor : activePackets) {
        DeleteAlterPacket deleteAlterPacket = new ImmutableDeleteAlterPacket(packetToRequestDeleteFor.getPacket().getDbId(), packetToRequestDeleteFor.getUpdateToken());
        PacketCollectionCreator creator = commandHelper.getCommandManager().makeCreator(sourceId, zoneId, null, deleteAlterPacket, PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR);
        PacketCollection packetCollection = creator.create(now);
        boolean success = false;
        try {
            database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
            success = true;
        } catch (SolarThingDatabaseException e) {
            LOGGER.error("Could not upload request to delete alter packet for ID: " + packetToRequestDeleteFor.getPacket().getDbId(), e);
        }
        if (success) {
            messageSender.sendMessage("Requested delete for flag: '" + flagName + "' under ID: " + packetToRequestDeleteFor.getPacket().getDbId());
        } else {
            messageSender.sendMessage("Could not request delete for flag: '" + flagName + "' under ID: " + packetToRequestDeleteFor.getPacket().getDbId() + ". See logs for details.");
        }
    }
}
Also used : Arrays(java.util.Arrays) AlterPacketsProvider(me.retrodaredevil.solarthing.AlterPacketsProvider) LoggerFactory(org.slf4j.LoggerFactory) NotNull(me.retrodaredevil.solarthing.annotations.NotNull) MessageSender(me.retrodaredevil.solarthing.message.MessageSender) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) Nullable(me.retrodaredevil.solarthing.annotations.Nullable) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) Instant(java.time.Instant) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) Collectors(java.util.stream.Collectors) PacketCollectionIdGenerator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) List(java.util.List) TimeRange(me.retrodaredevil.solarthing.util.TimeRange) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) ImmutableRequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestFlagPacket) Collections(java.util.Collections) ImmutableDeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableDeleteAlterPacket) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) ImmutableRequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestFlagPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) ImmutableDeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableDeleteAlterPacket) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) Instant(java.time.Instant) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) ImmutableDeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableDeleteAlterPacket) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)

Aggregations

MessageSender (me.retrodaredevil.solarthing.message.MessageSender)5 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 File (java.io.File)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Collectors (java.util.stream.Collectors)1 AlterPacketsProvider (me.retrodaredevil.solarthing.AlterPacketsProvider)1 EventDatabaseCacheEnvironment (me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment)1 LatestFragmentedPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment)1 NotNull (me.retrodaredevil.solarthing.annotations.NotNull)1 Nullable (me.retrodaredevil.solarthing.annotations.Nullable)1 DeleteAlterPacket (me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket)1