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