Search in sources :

Example 1 with MessageEventNode

use of me.retrodaredevil.solarthing.config.message.MessageEventNode 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)

Aggregations

EventDatabaseCacheEnvironment (me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment)1 LatestFragmentedPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment)1 MessageEventNode (me.retrodaredevil.solarthing.config.message.MessageEventNode)1 MessageSender (me.retrodaredevil.solarthing.message.MessageSender)1