Search in sources :

Example 1 with LatestFragmentedPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class HomeAssistantActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestFragmentedPacketGroupEnvironment latestFragmentedPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestFragmentedPacketGroupEnvironment.class);
    return Actions.createRunOnce(() -> {
        FragmentedPacketGroup packetGroup = latestFragmentedPacketGroupEnvironment.getFragmentedPacketGroupProvider().getPacketGroup();
        if (packetGroup == null) {
            LOGGER.warn("packetGroup is null!");
            return;
        }
        List<Call<SensorInfo>> calls = new ArrayList<>();
        for (Packet packet : packetGroup.getPackets()) {
            int fragmentId = packetGroup.getFragmentId(packet);
            String sourceId = packetGroup.getSourceId(packet);
            String nameBase = "solarthing_" + sourceId + "_" + fragmentId + "_";
            if (packet instanceof FXStatusPacket) {
                FXStatusPacket fx = (FXStatusPacket) packet;
                nameBase += "fx_" + fx.getAddress() + "_";
                calls.add(service.updateSensor(nameBase + "acmode", new SensorState(fx.getACMode().getModeName(), new AttributeBuilder().friendlyName("AC Mode").build())));
                calls.add(service.updateBinarySensor(nameBase + "acpresent", new BinarySensorState(fx.getACMode() != ACMode.NO_AC, new AttributeBuilder().friendlyName("AC Present").build())));
                calls.add(service.updateSensor(nameBase + "batteryvoltage", new SensorState(FORMAT.format(fx.getBatteryVoltage()), new AttributeBuilder().friendlyName("Battery Voltage").build())));
            }
        }
        for (Call<SensorInfo> call : calls) {
            try {
                // TODO move into a separate thread
                Response<SensorInfo> response = call.execute();
                if (!response.isSuccessful()) {
                    LOGGER.error("Unsuccessful response! " + requireNonNull(response.errorBody()).string());
                }
            } catch (IOException e) {
                LOGGER.error("Home assistant error", e);
            }
        }
    });
}
Also used : FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) Call(retrofit2.Call) Packet(me.retrodaredevil.solarthing.packets.Packet) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) AttributeBuilder(me.retrodaredevil.solarthing.homeassistant.data.AttributeBuilder) BinarySensorState(me.retrodaredevil.solarthing.homeassistant.data.BinarySensorState) BinarySensorState(me.retrodaredevil.solarthing.homeassistant.data.BinarySensorState) SensorState(me.retrodaredevil.solarthing.homeassistant.data.SensorState) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) SensorInfo(me.retrodaredevil.solarthing.homeassistant.data.SensorInfo)

Example 2 with LatestFragmentedPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment 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 3 with LatestFragmentedPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class RequireFullOutputActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestFragmentedPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestFragmentedPacketGroupEnvironment.class);
    return new SimpleAction(false) {

        @Override
        protected void onUpdate() {
            super.onUpdate();
            FragmentedPacketGroup latestPacketGroup = latestPacketGroupEnvironment.getFragmentedPacketGroupProvider().getPacketGroup();
            if (latestPacketGroup != null) {
                setDone(isFullOutput(latestPacketGroup));
            }
        }
    };
}
Also used : FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) SimpleAction(me.retrodaredevil.action.SimpleAction)

Example 4 with LatestFragmentedPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class RequiredIdentifierActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestFragmentedPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestFragmentedPacketGroupEnvironment.class);
    return new SimpleAction(false) {

        @Override
        protected void onUpdate() {
            super.onUpdate();
            FragmentedPacketGroup packetGroup = latestPacketGroupEnvironment.getFragmentedPacketGroupProvider().getPacketGroup();
            if (packetGroup == null) {
                return;
            }
            String reason = IdentifierUtil.getRequirementNotMetReason(requiredIdentifierMap, packetGroup);
            if (reason != null) {
                if (log) {
                    LOGGER.info("Requirement not met: " + reason);
                }
            } else {
                setDone(true);
            }
        }
    };
}
Also used : FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) SimpleAction(me.retrodaredevil.action.SimpleAction)

Example 5 with LatestFragmentedPacketGroupEnvironment

use of me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment in project solarthing by wildmountainfarms.

the class SlackChatBotActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    LatestFragmentedPacketGroupEnvironment latestPacketGroupEnvironment = actionEnvironment.getInjectEnvironment().get(LatestFragmentedPacketGroupEnvironment.class);
    AlterPacketsEnvironment alterPacketsEnvironment = actionEnvironment.getInjectEnvironment().get(AlterPacketsEnvironment.class);
    SolarThingDatabaseEnvironment solarThingDatabaseEnvironment = actionEnvironment.getInjectEnvironment().get(SolarThingDatabaseEnvironment.class);
    SourceIdEnvironment sourceIdEnvironment = actionEnvironment.getInjectEnvironment().get(SourceIdEnvironment.class);
    TimeZoneEnvironment timeZoneEnvironment = actionEnvironment.getInjectEnvironment().get(TimeZoneEnvironment.class);
    EventDatabaseCacheEnvironment eventDatabaseCacheEnvironment = actionEnvironment.getInjectEnvironment().get(EventDatabaseCacheEnvironment.class);
    // Note that all objects listed here must be thread safe, as data will be accessed from them on a separate thread
    FragmentedPacketGroupProvider packetGroupProvider = latestPacketGroupEnvironment.getFragmentedPacketGroupProvider();
    AlterPacketsProvider alterPacketsProvider = alterPacketsEnvironment.getAlterPacketsProvider();
    SolarThingDatabase database = solarThingDatabaseEnvironment.getSolarThingDatabase();
    String sourceId = sourceIdEnvironment.getSourceId();
    ZoneId zoneId = timeZoneEnvironment.getZoneId();
    ResourceManager<? extends DatabaseCache> eventDatabaseCacheManager = eventDatabaseCacheEnvironment.getEventDatabaseCacheManager();
    Slack slack = Slack.getInstance(new SlackConfig(), new SlackHttpClient(new OkHttpClient.Builder().callTimeout(Duration.ofSeconds(10)).connectTimeout(Duration.ofSeconds(4)).build()));
    ChatBotCommandHelper commandHelper = new ChatBotCommandHelper(permissionMap, packetGroupProvider, new CommandManager(keyDirectory, sender));
    return new SlackChatBotAction(appToken, new SlackMessageSender(authToken, channelId, slack), slack, new HelpChatBotHandler(new ChatBotHandlerMultiplexer(Arrays.asList(// note: this isn't applied to "help" commands
    new StaleMessageHandler(), new ScheduleCommandChatBotHandler(commandHelper, database, sourceId, zoneId), new CancelCommandChatBotHandler(commandHelper, database, sourceId, zoneId, alterPacketsProvider), new FlagCommandChatBotHandler(commandHelper, database, sourceId, zoneId, alterPacketsProvider), new CommandChatBotHandler(commandHelper, database, sourceId, zoneId), new StatusChatBotHandler(packetGroupProvider, alterPacketsProvider), new HeartbeatCommandChatBotHandler(eventDatabaseCacheManager), (message, messageSender) -> {
        messageSender.sendMessage("Unknown command!");
        return true;
    }))));
}
Also used : EventDatabaseCacheEnvironment(me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment) SlackHttpClient(com.slack.api.util.http.SlackHttpClient) CommandManager(me.retrodaredevil.solarthing.commands.util.CommandManager) Arrays(java.util.Arrays) Slack(com.slack.api.Slack) me.retrodaredevil.solarthing.chatbot(me.retrodaredevil.solarthing.chatbot) AlterPacketsProvider(me.retrodaredevil.solarthing.AlterPacketsProvider) SourceIdEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceIdEnvironment) TimeZoneEnvironment(me.retrodaredevil.solarthing.actions.environment.TimeZoneEnvironment) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) ActionEnvironment(me.retrodaredevil.action.node.environment.ActionEnvironment) SlackMessageSender(me.retrodaredevil.solarthing.message.implementations.SlackMessageSender) ResourceManager(me.retrodaredevil.solarthing.util.sync.ResourceManager) FragmentedPacketGroupProvider(me.retrodaredevil.solarthing.FragmentedPacketGroupProvider) Action(me.retrodaredevil.action.Action) Duration(java.time.Duration) Map(java.util.Map) SolarThingDatabaseEnvironment(me.retrodaredevil.solarthing.actions.environment.SolarThingDatabaseEnvironment) EventDatabaseCacheEnvironment(me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) ActionNode(me.retrodaredevil.action.node.ActionNode) DatabaseCache(me.retrodaredevil.solarthing.database.cache.DatabaseCache) File(java.io.File) ZoneId(java.time.ZoneId) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) SlackConfig(com.slack.api.SlackConfig) AlterPacketsEnvironment(me.retrodaredevil.solarthing.actions.environment.AlterPacketsEnvironment) SourceIdEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceIdEnvironment) TimeZoneEnvironment(me.retrodaredevil.solarthing.actions.environment.TimeZoneEnvironment) OkHttpClient(okhttp3.OkHttpClient) SlackHttpClient(com.slack.api.util.http.SlackHttpClient) FragmentedPacketGroupProvider(me.retrodaredevil.solarthing.FragmentedPacketGroupProvider) SlackMessageSender(me.retrodaredevil.solarthing.message.implementations.SlackMessageSender) LatestFragmentedPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment) SlackConfig(com.slack.api.SlackConfig) CommandManager(me.retrodaredevil.solarthing.commands.util.CommandManager) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) ZoneId(java.time.ZoneId) AlterPacketsEnvironment(me.retrodaredevil.solarthing.actions.environment.AlterPacketsEnvironment) SolarThingDatabaseEnvironment(me.retrodaredevil.solarthing.actions.environment.SolarThingDatabaseEnvironment) Slack(com.slack.api.Slack) AlterPacketsProvider(me.retrodaredevil.solarthing.AlterPacketsProvider)

Aggregations

LatestFragmentedPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment)7 FragmentedPacketGroup (me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup)5 File (java.io.File)2 IOException (java.io.IOException)2 Duration (java.time.Duration)2 Action (me.retrodaredevil.action.Action)2 SimpleAction (me.retrodaredevil.action.SimpleAction)2 ActionNode (me.retrodaredevil.action.node.ActionNode)2 ActionEnvironment (me.retrodaredevil.action.node.environment.ActionEnvironment)2 FragmentedPacketGroupProvider (me.retrodaredevil.solarthing.FragmentedPacketGroupProvider)2 EventDatabaseCacheEnvironment (me.retrodaredevil.solarthing.actions.environment.EventDatabaseCacheEnvironment)2 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)2 Slack (com.slack.api.Slack)1 SlackConfig (com.slack.api.SlackConfig)1 SlackHttpClient (com.slack.api.util.http.SlackHttpClient)1 ZoneId (java.time.ZoneId)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1