Performance improvement for returning energy summaries from DB. Change the way MQTT values are posted. Add a BOM utf-8 char to the BOM csv, yo dawg, bom.

This commit is contained in:
MarkBryanMilligan
2021-08-29 22:53:41 -05:00
parent 77ceec745c
commit d63f6df1fd
12 changed files with 71 additions and 31 deletions

View File

@@ -350,11 +350,8 @@ public class MonitorApp {
}
}
}
if (mqttPoster != null) {
for (BreakerPower p : mqttReadings) {
monitor.submit(() -> mqttPoster.postPower(p));
}
}
if (mqttPoster != null)
monitor.submit(() -> mqttPoster.postPower(mqttReadings));
if (DateUtils.diffInSeconds(new Date(), lastUpdateCheck) >= config.getUpdateInterval()) {
lastUpdateCheck = new Date();
monitor.submit(new UpdateChecker());

View File

@@ -1,12 +1,18 @@
package com.lanternsoftware.currentmonitor;
import com.lanternsoftware.datamodel.currentmonitor.BreakerPower;
import com.lanternsoftware.util.CollectionUtils;
import com.lanternsoftware.util.NullUtils;
import com.lanternsoftware.util.dao.DaoSerializer;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class MqttPoster {
private static final Logger LOG = LoggerFactory.getLogger(MqttPoster.class);
@@ -15,6 +21,7 @@ public class MqttPoster {
public MqttPoster(MonitorConfig _config) {
IMqttClient c = null;
try {
LOG.info("Attempting to connect to MQTT broker at {}", _config.getMqttBrokerUrl());
c = new MqttClient(_config.getMqttBrokerUrl(), String.format("Lantern_Power_Monitor_Hub_%d", _config.getHub()));
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
@@ -25,21 +32,23 @@ public class MqttPoster {
if (NullUtils.isNotEmpty(_config.getMqttPassword()))
options.setPassword(_config.getMqttPassword().toCharArray());
c.connect(options);
} catch (MqttException e) {
} catch (Exception e) {
LOG.error("Failed to create MQTT client", e);
}
client = c;
}
public void postPower(BreakerPower _power) {
String topic = "lantern_power_monitor/breaker_power/" + _power.getKey();
MqttMessage msg = new MqttMessage(NullUtils.toByteArray(DaoSerializer.toJson(_power)));
msg.setQos(2);
msg.setRetained(true);
try {
client.publish(topic, msg);
} catch (MqttException e) {
LOG.error("Failed to publish message to {}", topic, e);
public void postPower(List<BreakerPower> _power) {
for (BreakerPower power : CollectionUtils.makeNotNull(_power)) {
String topic = "lantern_power_monitor/breaker_power/" + power.getKey();
MqttMessage msg = new MqttMessage(NullUtils.toByteArray(DaoSerializer.toJson(power)));
msg.setQos(2);
msg.setRetained(true);
try {
client.publish(topic, msg);
} catch (Exception e) {
LOG.error("Failed to publish message to {}", topic, e);
}
}
}
}