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);
}
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);
}
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);
}
}
});
}
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;
}
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.");
}
}
}
Aggregations