Search in sources :

Example 1 with FXStatusPacket

use of me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket 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 FXStatusPacket

use of me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket 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)

Example 3 with FXStatusPacket

use of me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket in project solarthing by wildmountainfarms.

the class SolarThingGraphQLFXService method queryFXCharging.

@GraphQLQuery(description = "Gives the timer values for the master FX of a single fragment over a time range")
public List<DataNode<FXChargingPacket>> queryFXCharging(@GraphQLArgument(name = "from") long from, @GraphQLArgument(name = "to") long to, @GraphQLArgument(name = "fragmentId") int fragmentId) {
    MetaDatabase metaDatabase = simpleQueryHandler.queryMeta();
    FXChargingSettingsPacket fxChargingSettingsPacket = null;
    FXChargingTemperatureAdjustPacket fxChargingTemperatureAdjustPacket = null;
    for (TargetedMetaPacket targetedMetaPacket : metaDatabase.getMeta(to, fragmentId)) {
        if (targetedMetaPacket.getPacketType() == TargetedMetaPacketType.FX_CHARGING_SETTINGS) {
            fxChargingSettingsPacket = (FXChargingSettingsPacket) targetedMetaPacket;
        } else if (targetedMetaPacket.getPacketType() == TargetedMetaPacketType.FX_CHARGING_TEMPERATURE_ADJUST) {
            fxChargingTemperatureAdjustPacket = (FXChargingTemperatureAdjustPacket) targetedMetaPacket;
        }
    }
    if (fxChargingSettingsPacket == null) {
        throw new UnexpectedResponseException("Could not find FX Charging settings in meta!");
    }
    // 3 hours back
    long startTime = from - 3 * 60 * 60 * 1000;
    List<? extends InstancePacketGroup> packets = simpleQueryHandler.queryStatus(startTime, to, null);
    // We make masterIdIgnoreDistance null because we will only be using fragmentId as the master fragment ID
    Map<String, List<FragmentedPacketGroup>> map = // separate based on source ID
    PacketGroups.sortPackets(packets, simpleQueryHandler.getDefaultInstanceOptions(), SolarThingConstants.STANDARD_MAX_TIME_DISTANCE.toMillis(), null, // make fragmentId be the master ID
    FragmentUtil.createPriorityComparator(fragmentId));
    List<FragmentedPacketGroup> sortedPackets = null;
    for (List<FragmentedPacketGroup> fragmentedPacketGroups : map.values()) {
        if (fragmentedPacketGroups.get(0).hasFragmentId(fragmentId)) {
            sortedPackets = fragmentedPacketGroups;
            break;
        }
    }
    if (sortedPackets == null) {
        throw new UnexpectedResponseException("Could not find fragment ID: " + fragmentId);
    }
    FXChargingSettings settings = fxChargingSettingsPacket.getFXChargingSettings();
    FXChargingStateHandler stateHandler = new FXChargingStateHandler(settings);
    Long lastUpdate = null;
    List<DataNode<FXChargingPacket>> r = new ArrayList<>();
    for (FragmentedPacketGroup packetGroup : sortedPackets) {
        List<FXStatusPacket> fxPackets = new ArrayList<>();
        Integer temperature = null;
        for (Packet packet : packetGroup.getPackets()) {
            if (packet instanceof FXStatusPacket && packetGroup.getFragmentId(packet) == fragmentId) {
                fxPackets.add((FXStatusPacket) packet);
            } else if (packet instanceof BatteryTemperature) {
                temperature = Math.round(((BatteryTemperature) packet).getBatteryTemperatureCelsius().floatValue());
            }
        }
        if (fxPackets.isEmpty()) {
            continue;
        }
        if (temperature == null) {
            // we need temperature data for accurate results // TODO add option for systems that don't use temperature compensation
            continue;
        }
        temperature += fxChargingTemperatureAdjustPacket == null ? 0 : fxChargingTemperatureAdjustPacket.getTemperatureAdjustCelsius();
        FXStatusPacket fx = OutbackUtil.getMasterFX(fxPackets);
        if (fx == null) {
            continue;
        }
        final long delta;
        if (lastUpdate == null) {
            delta = 1000;
        } else {
            delta = packetGroup.getDateMillis() - lastUpdate;
        }
        lastUpdate = packetGroup.getDateMillis();
        stateHandler.update(delta, fx, temperature);
        FXChargingPacket fxChargingPacket = new ImmutableFXChargingPacket(fx.getIdentifier(), stateHandler.getMode(), stateHandler.getRemainingAbsorbTimeMillis(), stateHandler.getRemainingFloatTimeMillis(), stateHandler.getRemainingEqualizeTimeMillis(), settings.getAbsorbTimeMillis(), settings.getFloatTimeMillis(), settings.getEqualizeTimeMillis());
        r.add(new DataNode<>(fxChargingPacket, fx, packetGroup.getDateMillis(), packetGroup.getSourceId(fx), fragmentId));
    }
    if (r.isEmpty() && !sortedPackets.isEmpty()) {
        throw new UnexpectedResponseException("There must have been no FX packets or no rover packets!");
    }
    return r;
}
Also used : ArrayList(java.util.ArrayList) ImmutableFXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.ImmutableFXChargingPacket) FXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingPacket) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) FXChargingSettings(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingSettings) DataNode(me.retrodaredevil.solarthing.rest.graphql.packets.nodes.DataNode) ArrayList(java.util.ArrayList) List(java.util.List) FragmentedPacketGroup(me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup) Packet(me.retrodaredevil.solarthing.packets.Packet) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) FXChargingTemperatureAdjustPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingTemperatureAdjustPacket) ImmutableFXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.ImmutableFXChargingPacket) TargetedMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.TargetedMetaPacket) FXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingPacket) FXChargingSettingsPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingSettingsPacket) MetaDatabase(me.retrodaredevil.solarthing.type.closed.meta.MetaDatabase) BatteryTemperature(me.retrodaredevil.solarthing.solar.common.BatteryTemperature) ImmutableFXChargingPacket(me.retrodaredevil.solarthing.solar.outback.fx.charge.ImmutableFXChargingPacket) TargetedMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.TargetedMetaPacket) FXChargingTemperatureAdjustPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingTemperatureAdjustPacket) UnexpectedResponseException(me.retrodaredevil.solarthing.rest.exceptions.UnexpectedResponseException) FXChargingSettingsPacket(me.retrodaredevil.solarthing.solar.outback.fx.meta.FXChargingSettingsPacket) FXChargingStateHandler(me.retrodaredevil.solarthing.solar.outback.fx.charge.FXChargingStateHandler) GraphQLQuery(io.leangen.graphql.annotations.GraphQLQuery)

Example 4 with FXStatusPacket

use of me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket in project solarthing by wildmountainfarms.

the class ACModeActionNode method createAction.

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

        @Override
        protected void onUpdate() {
            super.onUpdate();
            PacketGroup packetGroup = packetGroupProvider.getPacketGroup();
            if (packetGroup == null) {
                LOGGER.warn("packetGroup is null!");
                return;
            }
            FXStatusPacket fxStatusPacket = OutbackUtil.getMasterFX(packetGroup);
            if (fxStatusPacket == null) {
                LOGGER.warn("No master FX Status Packet!");
            } else {
                setDone((fxStatusPacket.getACMode() == acMode) == !not);
            }
        }
    };
}
Also used : PacketGroupProvider(me.retrodaredevil.solarthing.PacketGroupProvider) PacketGroup(me.retrodaredevil.solarthing.packets.collection.PacketGroup) FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) LatestPacketGroupEnvironment(me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment) SimpleAction(me.retrodaredevil.action.SimpleAction)

Example 5 with FXStatusPacket

use of me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket in project solarthing by wildmountainfarms.

the class GeneratorStateEvent method run.

@Override
public void run(MessageSender sender, FragmentedPacketGroup previous, FragmentedPacketGroup current) {
    FXStatusPacket fx = OutbackUtil.getMasterFX(current);
    if (fx == null)
        return;
    ACMode mode = fx.getACMode();
    final Boolean wasGeneratorOn = this.generatorOn;
    if (mode == ACMode.NO_AC) {
        this.generatorOn = false;
        if (Boolean.TRUE.equals(wasGeneratorOn)) {
            sender.sendMessage("Generator has turned off!");
        }
    } else if (mode == ACMode.AC_USE) {
        this.generatorOn = true;
        if (Boolean.FALSE.equals(wasGeneratorOn)) {
            sender.sendMessage("Generator power being used!");
        }
    }
}
Also used : FXStatusPacket(me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket) ACMode(me.retrodaredevil.solarthing.solar.outback.fx.ACMode)

Aggregations

FXStatusPacket (me.retrodaredevil.solarthing.solar.outback.fx.FXStatusPacket)16 Packet (me.retrodaredevil.solarthing.packets.Packet)6 ArrayList (java.util.ArrayList)4 LatestPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestPacketGroupEnvironment)4 SimpleAction (me.retrodaredevil.action.SimpleAction)3 PacketGroupProvider (me.retrodaredevil.solarthing.PacketGroupProvider)3 FragmentedPacketGroup (me.retrodaredevil.solarthing.packets.collection.FragmentedPacketGroup)3 PacketGroup (me.retrodaredevil.solarthing.packets.collection.PacketGroup)3 MXStatusPacket (me.retrodaredevil.solarthing.solar.outback.mx.MXStatusPacket)3 Test (org.junit.jupiter.api.Test)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 File (java.io.File)2 LatestFragmentedPacketGroupEnvironment (me.retrodaredevil.solarthing.actions.environment.LatestFragmentedPacketGroupEnvironment)2 Nullable (me.retrodaredevil.solarthing.annotations.Nullable)2 GraphQLQuery (io.leangen.graphql.annotations.GraphQLQuery)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 List (java.util.List)1 Action (me.retrodaredevil.action.Action)1 ActionNode (me.retrodaredevil.action.node.ActionNode)1