Search in sources :

Example 1 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class ExecutingCommandFeedbackActionNode method createAction.

@Override
public Action createAction(ActionEnvironment actionEnvironment) {
    SourceEnvironment sourceEnvironment = actionEnvironment.getInjectEnvironment().get(SourceEnvironment.class);
    EventReceiverEnvironment eventReceiverEnvironment = actionEnvironment.getInjectEnvironment().get(EventReceiverEnvironment.class);
    OpenSource source = sourceEnvironment.getSource();
    PacketListReceiverHandler packetListReceiverHandler = eventReceiverEnvironment.getEventPacketListReceiverHandler();
    List<Packet> packets = Arrays.asList(new ImmutableExecutionFeedbackPacket(message, category, new OpenSourceExecutionReason(source)));
    return Actions.createRunOnce(() -> {
        LOGGER.debug("Going to upload an execution feedback packet.");
        Instant now = Instant.now();
        packetListReceiverHandler.uploadSimple(now, packets);
    });
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) ImmutableExecutionFeedbackPacket(me.retrodaredevil.solarthing.type.event.feedback.ImmutableExecutionFeedbackPacket) SourceEnvironment(me.retrodaredevil.solarthing.actions.environment.SourceEnvironment) EventReceiverEnvironment(me.retrodaredevil.solarthing.actions.environment.EventReceiverEnvironment) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) ImmutableExecutionFeedbackPacket(me.retrodaredevil.solarthing.type.event.feedback.ImmutableExecutionFeedbackPacket) Instant(java.time.Instant) PacketListReceiverHandler(me.retrodaredevil.solarthing.program.PacketListReceiverHandler) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)

Example 2 with Packet

use of me.retrodaredevil.solarthing.packets.Packet 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 3 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class RoverAnalyticsHandler method send.

private void send(PacketCollection packetCollection) {
    for (Packet packet : packetCollection.getPackets()) {
        if (packet instanceof RoverStatusPacket) {
            RoverStatusPacket rover = (RoverStatusPacket) packet;
            String data = "(" + rover.getNumber() + ") " + rover.getProductModel() + "," + rover.getRatedChargingCurrentValue();
            if (!rover.supportsMesLoad()) {
                data += ",no MES Load";
            }
            analyticsManager.sendRoverStatus(data, timer.getUptimeHours());
        }
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) RoverStatusPacket(me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket) RoverStatusPacket(me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket)

Example 4 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class HeartbeatCommandChatBotHandler method displayHeartbeats.

// I really don't care at the moment. Extracting a method will make this less readable IMO
@SuppressWarnings("DuplicatedCode")
private void displayHeartbeats(MessageSender messageSender) {
    Map<HeartbeatIdentifier, HeartbeatNode> map = new HashMap<>();
    eventDatabaseCacheManager.access(eventDatabaseCache -> {
        eventDatabaseCache.createCachedPacketsInRangeStream(TimeRange.createAfter(Instant.now().minus(Duration.ofHours(3))), false).map(// We are accessing new packets, so they should have source ID and fragment ID
        PacketGroups::parseToInstancePacketGroupRequireNoDefaults).forEach(instancePacketGroup -> {
            long dateMillis = instancePacketGroup.getDateMillis();
            for (Packet packet : instancePacketGroup.getPackets()) {
                if (packet instanceof HeartbeatPacket) {
                    HeartbeatPacket heartbeatPacket = (HeartbeatPacket) packet;
                    HeartbeatIdentifier identifier = new HeartbeatIdentifier(heartbeatPacket.getData().getIdentifier(), instancePacketGroup.getFragmentId());
                    HeartbeatNode node = new HeartbeatNode(dateMillis, heartbeatPacket);
                    map.put(identifier, node);
                }
            }
        });
    });
    List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> activeHeartbeats = new ArrayList<>();
    List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> expectedHeartbeats = new ArrayList<>();
    List<Map.Entry<HeartbeatIdentifier, HeartbeatNode>> deadHeartbeats = new ArrayList<>();
    Instant now = Instant.now();
    map.entrySet().stream().sorted(// iterate over oldest first
    Comparator.comparingLong(entry -> entry.getValue().getDateMillis())).forEach(entry -> {
        HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
        Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
        if (lastHeartbeat.plus(data.getExpectedDurationToNextHeartbeat()).compareTo(now) > 0) {
            activeHeartbeats.add(entry);
        } else if (lastHeartbeat.plus(data.getExpectedDurationToNextHeartbeat()).plus(data.getBufferDuration()).compareTo(now) > 0) {
            expectedHeartbeats.add(entry);
        } else {
            deadHeartbeats.add(entry);
        }
    });
    StringBuilder builder = new StringBuilder();
    if (!activeHeartbeats.isEmpty()) {
        builder.append("Active:\n");
        activeHeartbeats.forEach(entry -> {
            int fragmentId = entry.getKey().getFragmentId();
            HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
            Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
            builder.append("  ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
        });
    }
    if (!expectedHeartbeats.isEmpty()) {
        builder.append("Expected:\n");
        expectedHeartbeats.forEach(entry -> {
            int fragmentId = entry.getKey().getFragmentId();
            HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
            Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
            builder.append("  ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
        });
    }
    if (!deadHeartbeats.isEmpty()) {
        builder.append("Dead:\n");
        deadHeartbeats.forEach(entry -> {
            int fragmentId = entry.getKey().getFragmentId();
            HeartbeatData data = entry.getValue().getHeartbeatPacket().getData();
            Instant lastHeartbeat = Instant.ofEpochMilli(entry.getValue().getDateMillis());
            builder.append("  ").append(buildLineForHeartbeat(fragmentId, data, lastHeartbeat)).append('\n');
        });
    }
    String message = builder.toString();
    if (message.isEmpty()) {
        messageSender.sendMessage("No heartbeats");
    } else {
        messageSender.sendMessage(message);
    }
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) HashMap(java.util.HashMap) HeartbeatData(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatData) HeartbeatPacket(me.retrodaredevil.solarthing.type.event.feedback.HeartbeatPacket) Instant(java.time.Instant) ArrayList(java.util.ArrayList) HeartbeatNode(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatNode) HeartbeatIdentifier(me.retrodaredevil.solarthing.util.heartbeat.HeartbeatIdentifier)

Example 5 with Packet

use of me.retrodaredevil.solarthing.packets.Packet in project solarthing by wildmountainfarms.

the class MateAnalyticsHandler method send.

private void send(PacketCollection packetCollection) {
    int fxCount = 0;
    List<Integer> fxOperationalModes = new ArrayList<>();
    int mxfmCount = 0;
    int fmCount = 0;
    Boolean isOldFirmware = null;
    for (Packet packet : packetCollection.getPackets()) {
        if (packet instanceof FXStatusPacket) {
            fxCount++;
            FXStatusPacket fx = (FXStatusPacket) packet;
            fxOperationalModes.add(fx.getOperationalModeValue());
        } else if (packet instanceof MXStatusPacket) {
            mxfmCount++;
            MXStatusPacket mx = (MXStatusPacket) packet;
            if (Boolean.TRUE.equals(mx.isFlexMax())) {
                fmCount++;
            }
            Boolean oldMate = mx.isOldMateFirmware();
            if (Boolean.TRUE.equals(oldMate)) {
                isOldFirmware = true;
            } else if (Boolean.FALSE.equals(oldMate)) {
                isOldFirmware = false;
            }
        }
    }
    String data = "";
    if (fxCount > 0) {
        data += "fx=" + fxCount + ",fx_op=" + fxOperationalModes + ",";
    }
    if (mxfmCount > 0) {
        data += "mxfm=" + mxfmCount + ",";
    }
    if (fmCount > 0) {
        data += "fm=" + fmCount + ",";
    }
    if (isOldFirmware != null) {
        if (isOldFirmware) {
            data += "old_mate,";
        } else {
            data += "new_mate,";
        }
    }
    if (!data.isEmpty()) {
        // remove comma
        data = data.substring(0, data.length() - 1);
    }
    analyticsManager.sendMateStatus(data, timer.getUptimeHours());
}
Also used : Packet(me.retrodaredevil.solarthing.packets.Packet) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) ArrayList(java.util.ArrayList) MXStatusPacket(me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket)

Aggregations

Packet (me.retrodaredevil.solarthing.packets.Packet)45 ArrayList (java.util.ArrayList)17 Instant (java.time.Instant)7 List (java.util.List)7 FragmentedPacketGroup (me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup)6 FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)6 Identifiable (me.retrodaredevil.solarthing.packets.identification.Identifiable)5 IdentifierFragment (me.retrodaredevil.solarthing.packets.identification.IdentifierFragment)5 RoverStatusPacket (me.retrodaredevil.solarthing.solar.renogy.rover.RoverStatusPacket)5 OpenSource (me.retrodaredevil.solarthing.type.open.OpenSource)5 Map (java.util.Map)4 CommandOpenPacket (me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket)4 InstancePacketGroup (me.retrodaredevil.solarthing.packets.collection.InstancePacketGroup)4 InstanceSourcePacket (me.retrodaredevil.solarthing.packets.instance.InstanceSourcePacket)4 ExecutionReason (me.retrodaredevil.solarthing.reason.ExecutionReason)4 OpenSourceExecutionReason (me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)3 RequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket)3 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)3