Search in sources :

Example 1 with TimeRangeActivePeriod

use of me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod in project solarthing by wildmountainfarms.

the class AlterManagerAction method onUpdate.

@Override
protected void onUpdate() {
    super.onUpdate();
    // Supply queried solarthing_open packets to the security packet receiver, which may execute code to put commands requested to be scheduled in solarthing_alter
    List<StoredPacketGroup> packets = openDatabaseCache.getAllCachedPackets();
    securityPacketReceiver.receivePacketGroups(packets);
    // Check packets in solarthing_alter and see if we need to send a command because of a scheduled command packet
    Instant now = Instant.now();
    List<VersionedPacket<StoredAlterPacket>> alterPackets = alterPacketsProvider.getPackets();
    if (alterPackets == null) {
        LOGGER.info("alterPackets is null. Maybe query failed? Maybe additional info in previous logs?");
    } else {
        for (VersionedPacket<StoredAlterPacket> versionedPacket : alterPackets) {
            AlterPacket packet = versionedPacket.getPacket().getPacket();
            if (packet instanceof ScheduledCommandPacket) {
                ScheduledCommandPacket scheduledCommandPacket = (ScheduledCommandPacket) packet;
                ScheduledCommandData data = scheduledCommandPacket.getData();
                if (data.getScheduledTimeMillis() <= now.toEpochMilli()) {
                    if (now.toEpochMilli() - data.getScheduledTimeMillis() > Duration.ofMinutes(5).toMillis()) {
                        LOGGER.warn("Not going to send a command scheduled for more than 5 minutes ago! data: " + data);
                    } else {
                        doSendCommand(versionedPacket, scheduledCommandPacket);
                    }
                }
            } else if (packet instanceof FlagPacket) {
                FlagPacket flagPacket = (FlagPacket) packet;
                FlagData data = flagPacket.getFlagData();
                ActivePeriod activePeriod = data.getActivePeriod();
                if (activePeriod instanceof TimeRangeActivePeriod) {
                    // We only try to "manage" flags that use this type of ActivePeriod
                    TimeRangeActivePeriod period = (TimeRangeActivePeriod) activePeriod;
                    TimeRange timeRange = period.getTimeRange();
                    Instant endTime = timeRange.getEndTime();
                    if (endTime != null && endTime.compareTo(now) < 0) {
                        // If there is an end time, and it is in the past, then we should remove the flag
                        executorService.execute(() -> {
                            try {
                                database.getAlterDatabase().delete(versionedPacket);
                            } catch (SolarThingDatabaseException e) {
                                LOGGER.error("Could not delete a FlagPacket with an expired time", e);
                            // If we cannot delete it, no need to try again, it'll still be here next time around
                            }
                        });
                    }
                }
            }
        }
    }
}
Also used : FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) Instant(java.time.Instant) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) ActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.ActivePeriod) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) TimeRange(me.retrodaredevil.solarthing.util.TimeRange) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) StoredPacketGroup(me.retrodaredevil.solarthing.packets.collection.StoredPacketGroup) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)

Example 2 with TimeRangeActivePeriod

use of me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod in project solarthing by wildmountainfarms.

the class FlagCommandChatBotHandler method setFlag.

private void setFlag(MessageSender messageSender, String flagName) {
    TimeRange timeRange = TimeRange.createAfter(Instant.now());
    FlagData data = new FlagData(flagName, new TimeRangeActivePeriod(timeRange));
    RequestFlagPacket requestFlagPacket = new ImmutableRequestFlagPacket(data);
    PacketCollectionCreator creator = commandHelper.getCommandManager().makeCreator(sourceId, zoneId, null, requestFlagPacket, PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR);
    // TODO We should check if the flag being requested is already active.
    executorService.execute(() -> {
        PacketCollection packetCollection = creator.create(Instant.now());
        boolean success = false;
        try {
            database.getOpenDatabase().uploadPacketCollection(packetCollection, null);
            success = true;
        } catch (SolarThingDatabaseException e) {
            LOGGER.error("Could not upload request flag packet", e);
        }
        if (success) {
            messageSender.sendMessage("Successfully requested flag: '" + flagName + "' to be set.");
        } else {
            messageSender.sendMessage("Was unable to request flag set. See logs for details or try again.");
        }
    });
}
Also used : TimeRange(me.retrodaredevil.solarthing.util.TimeRange) FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) ImmutableRequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestFlagPacket) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) ImmutableRequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestFlagPacket) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Example 3 with TimeRangeActivePeriod

use of me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod in project solarthing by wildmountainfarms.

the class CommandOpenPacketsTest method test.

@Test
void test() throws JsonProcessingException {
    PacketTestUtil.testJson(new ImmutableRequestCommandPacket("GEN OFF"), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableRequestFlagPacket(new FlagData("disable_automations", new TimeRangeActivePeriod(TimeRange.create(Instant.parse("2021-10-05T04:53:22.877307Z"), Instant.parse("2021-10-05T04:53:44.305146Z"))))), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableScheduleCommandPacket(new ScheduledCommandData(System.currentTimeMillis(), "GEN OFF", Collections.singleton(1)), UUID.randomUUID()), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableDeleteAlterPacket("my_document_id", new RevisionUpdateToken("46-9ab7d71841380a36e1ec9e367deae36e")), CommandOpenPacket.class, true);
    PacketTestUtil.testJson(new ImmutableRequestHeartbeatPacket(new HeartbeatData("Hourly Mate Ping", "heartbeat.ping.mate.hourly", Duration.ofHours(1), Duration.ofMinutes(5)), UUID.randomUUID()), CommandOpenPacket.class, true);
}
Also used : FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) HeartbeatData(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData) RevisionUpdateToken(me.retrodaredevil.solarthing.database.couchdb.RevisionUpdateToken) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData) Test(org.junit.jupiter.api.Test)

Aggregations

FlagData (me.retrodaredevil.solarthing.type.alter.flag.FlagData)3 TimeRangeActivePeriod (me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod)3 RequestFlagPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket)2 SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)2 ScheduledCommandData (me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData)2 TimeRange (me.retrodaredevil.solarthing.util.TimeRange)2 Instant (java.time.Instant)1 DeleteAlterPacket (me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket)1 ImmutableRequestFlagPacket (me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestFlagPacket)1 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)1 RevisionUpdateToken (me.retrodaredevil.solarthing.database.couchdb.RevisionUpdateToken)1 UpdateConflictSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException)1 PacketCollection (me.retrodaredevil.solarthing.packets.collection.PacketCollection)1 PacketCollectionCreator (me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator)1 StoredPacketGroup (me.retrodaredevil.solarthing.packets.collection.StoredPacketGroup)1 AlterPacket (me.retrodaredevil.solarthing.type.alter.AlterPacket)1 ImmutableStoredAlterPacket (me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket)1 StoredAlterPacket (me.retrodaredevil.solarthing.type.alter.StoredAlterPacket)1 ActivePeriod (me.retrodaredevil.solarthing.type.alter.flag.ActivePeriod)1 FlagPacket (me.retrodaredevil.solarthing.type.alter.packets.FlagPacket)1