mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Improve 3A+ case, making it easier to take the pi out. Improve the fit of the Z2 case.
Make it possible for a hub to reload a config automatically when it changes without being restarted. Prevent the auto-calibration on first install from being stomped by the app. Allow updating the hub software via the app.
This commit is contained in:
@@ -2,6 +2,8 @@ package com.lanternsoftware.dataaccess.currentmonitor;
|
||||
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Account;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerConfig;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.ChargeSummary;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.ChargeTotal;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergySummary;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergyTotal;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Sequence;
|
||||
@@ -10,14 +12,15 @@ import com.lanternsoftware.datamodel.rules.FcmDevice;
|
||||
import com.lanternsoftware.datamodel.rules.Rule;
|
||||
import com.lanternsoftware.util.DebugTimer;
|
||||
import com.lanternsoftware.util.LanternFiles;
|
||||
import com.lanternsoftware.util.dao.DaoQuery;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Backup {
|
||||
public static void main(String[] args) {
|
||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.OPS_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_SOURCE + "mongo.cfg"));
|
||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_DEST + "mongo.cfg"));
|
||||
|
||||
DebugTimer t1 = new DebugTimer("Query Accounts");
|
||||
List<Account> accounts = dao.getProxy().queryAll(Account.class);
|
||||
@@ -34,17 +37,39 @@ public class Backup {
|
||||
t4.stop();
|
||||
|
||||
DebugTimer t5 = new DebugTimer("Query Energy");
|
||||
List<EnergySummary> energy = dao.getProxy().queryAll(EnergySummary.class);
|
||||
for (Account a : accounts) {
|
||||
List<EnergySummary> energy = dao.getProxy().query(EnergySummary.class, new DaoQuery("account_id", a.getId()));
|
||||
DebugTimer t = new DebugTimer("Save Energy");
|
||||
backupDao.getProxy().save(energy);
|
||||
t.stop();
|
||||
}
|
||||
t5.stop();
|
||||
DebugTimer t6 = new DebugTimer("Save Energy");
|
||||
backupDao.getProxy().save(energy);
|
||||
|
||||
DebugTimer t6 = new DebugTimer("Query Energy Totals");
|
||||
for (Account a : accounts) {
|
||||
List<EnergyTotal> total = dao.getProxy().query(EnergyTotal.class, new DaoQuery("account_id", a.getId()));
|
||||
DebugTimer t = new DebugTimer("Save Summary");
|
||||
backupDao.getProxy().save(total);
|
||||
t.stop();
|
||||
}
|
||||
t6.stop();
|
||||
|
||||
DebugTimer t7 = new DebugTimer("Query Summaries");
|
||||
List<EnergyTotal> summary = dao.getProxy().queryAll(EnergyTotal.class);
|
||||
DebugTimer t7 = new DebugTimer("Query Charges");
|
||||
for (Account a : accounts) {
|
||||
List<ChargeSummary> charges = dao.getProxy().query(ChargeSummary.class, new DaoQuery("account_id", a.getId()));
|
||||
DebugTimer t = new DebugTimer("Save Charges");
|
||||
backupDao.getProxy().save(charges);
|
||||
t.stop();
|
||||
}
|
||||
t7.stop();
|
||||
DebugTimer t8 = new DebugTimer("Save Summaries");
|
||||
backupDao.getProxy().save(summary);
|
||||
|
||||
DebugTimer t8 = new DebugTimer("Query Charge Totals");
|
||||
for (Account a : accounts) {
|
||||
List<ChargeTotal> charges = dao.getProxy().query(ChargeTotal.class, new DaoQuery("account_id", a.getId()));
|
||||
DebugTimer t = new DebugTimer("Save Charge Totals");
|
||||
backupDao.getProxy().save(charges);
|
||||
t.stop();
|
||||
}
|
||||
t8.stop();
|
||||
|
||||
DebugTimer t9 = new DebugTimer("Query Events");
|
||||
|
||||
@@ -16,8 +16,8 @@ import java.util.TimeZone;
|
||||
|
||||
public class BackupMinutes {
|
||||
public static void main(String[] args) {
|
||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.OPS_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_PATH + "mongo.cfg"));
|
||||
CurrentMonitorDao dao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_SOURCE + "mongo.cfg"));
|
||||
CurrentMonitorDao backupDao = new MongoCurrentMonitorDao(MongoConfig.fromDisk(LanternFiles.BACKUP_DEST + "mongo.cfg"));
|
||||
Date now = new Date();
|
||||
for (Account a : dao.getProxy().queryAll(Account.class)) {
|
||||
if (a.getId() == 0)
|
||||
@@ -30,12 +30,11 @@ public class BackupMinutes {
|
||||
HubPowerMinute minute = dao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sort("minute"));
|
||||
if (minute == null)
|
||||
continue;
|
||||
Date minStart = DateUtils.addDays(DateUtils.getMidnightBeforeNow(tz), -60, tz);
|
||||
Date start = DateUtils.getMidnightBefore(minute.getMinuteAsDate(), tz);
|
||||
if (minStart.after(start))
|
||||
start = minStart;
|
||||
HubPowerMinute lastBackup = backupDao.getProxy().queryOne(HubPowerMinute.class, new DaoQuery("account_id", a.getId()), DaoSort.sortDesc("minute"));
|
||||
Date start = lastBackup == null ? DateUtils.getMidnightBefore(minute.getMinuteAsDate(), tz) : lastBackup.getMinuteAsDate();
|
||||
// Date start = DateUtils.date(10,16,2021,tz);
|
||||
Date end = DateUtils.addDays(start, 1, tz);
|
||||
while (end.before(now)) {
|
||||
while (start.before(now)) {
|
||||
DebugTimer t2 = new DebugTimer("Account Id: " + a.getId() + " Query Day " + DateUtils.format("MM/dd/yyyy", tz, start));
|
||||
List<HubPowerMinute> minutes = dao.getProxy().query(HubPowerMinute.class, new DaoQuery("account_id", a.getId()).andBetweenInclusiveExclusive("minute", (int) (start.getTime() / 60000), (int) (end.getTime() / 60000)));
|
||||
t2.stop();
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.lanternsoftware.datamodel.currentmonitor.BreakerConfig;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerPower;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergySummary;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergyViewMode;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.HubCommand;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.HubPowerMinute;
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoProxy;
|
||||
@@ -48,5 +49,9 @@ public interface CurrentMonitorDao {
|
||||
TimeZone getTimeZoneForAccount(int _accountId);
|
||||
String getTimeZoneForAccount(String _authCode);
|
||||
|
||||
void putHubCommand(HubCommand _command);
|
||||
List<HubCommand> getAllHubCommands();
|
||||
void deleteHubCommand(String _id);
|
||||
|
||||
MongoProxy getProxy();
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@ import com.lanternsoftware.datamodel.currentmonitor.BillingRate;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Breaker;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerConfig;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerGroup;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerHub;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerPower;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.ChargeSummary;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.ChargeTotal;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergySummary;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergyTotal;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.EnergyViewMode;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.HubCommand;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.HubPowerMinute;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.Sequence;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
@@ -404,6 +406,7 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
if (config == null) {
|
||||
config = new BreakerConfig();
|
||||
config.setAccountId(_authCode.getAccountId());
|
||||
config.setVersion(config.getVersion());
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -416,6 +419,7 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
config.setMeters(CollectionUtils.aggregate(configs, BreakerConfig::getMeters));
|
||||
config.setBillingPlans(CollectionUtils.aggregate(configs, BreakerConfig::getBillingPlans));
|
||||
config.setBillingRates(CollectionUtils.aggregate(configs, BreakerConfig::getBillingRates));
|
||||
config.setVersion(CollectionUtils.getLargest(CollectionUtils.transform(configs, BreakerConfig::getVersion)));
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -424,6 +428,16 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
DaoQuery configQuery = new DaoQuery("_id", String.valueOf(_config.getAccountId()));
|
||||
BreakerConfig oldConfig = proxy.queryOne(BreakerConfig.class, configQuery);
|
||||
if (oldConfig != null) {
|
||||
logger.info("old version: {}, new version: {}", oldConfig.getVersion(), _config.getVersion());
|
||||
if (oldConfig.getVersion() > _config.getVersion()) {
|
||||
for (BreakerHub hub : CollectionUtils.makeNotNull(_config.getBreakerHubs())) {
|
||||
BreakerHub oldHub = oldConfig.getHub(hub.getHub());
|
||||
if (oldHub != null) {
|
||||
logger.info("Prevent overwrite of voltage calibration");
|
||||
hub.setVoltageCalibrationFactor(oldHub.getRawVoltageCalibrationFactor());
|
||||
}
|
||||
}
|
||||
}
|
||||
_config.setVersion(oldConfig.getVersion() + 1);
|
||||
if (NullUtils.isNotIdentical(_config, oldConfig)) {
|
||||
DaoEntity oldEntity = DaoSerializer.toDaoEntity(oldConfig);
|
||||
@@ -441,6 +455,9 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
});
|
||||
}
|
||||
}
|
||||
for (BreakerHub hub : CollectionUtils.makeNotNull(_config.getBreakerHubs())) {
|
||||
logger.info("voltage calibration hub {}: {}", hub.getHub(), hub.getVoltageCalibrationFactor());
|
||||
}
|
||||
proxy.save(_config);
|
||||
}
|
||||
|
||||
@@ -459,7 +476,7 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
AuthCode code = decryptAuthCode(_authCode);
|
||||
if (code == null)
|
||||
return null;
|
||||
return proxy.queryOne(Account.class, new DaoQuery("_id", code.getAccountId()));
|
||||
return proxy.queryOne(Account.class, new DaoQuery("_id", String.valueOf(code.getAccountId())));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -572,6 +589,23 @@ public class MongoCurrentMonitorDao implements CurrentMonitorDao {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putHubCommand(HubCommand _command) {
|
||||
BreakerConfig config = getConfig(_command.getAccountId());
|
||||
if (config != null)
|
||||
proxy.save(_command.forAllHubs(config));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<HubCommand> getAllHubCommands() {
|
||||
return proxy.queryAll(HubCommand.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteHubCommand(String _id) {
|
||||
proxy.delete(HubCommand.class, new DaoQuery("_id", _id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public MongoProxy getProxy() {
|
||||
return proxy;
|
||||
|
||||
Reference in New Issue
Block a user