mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Allow binary and dimmer switches to be grouped together. Allow a custom host to be sent to the power monitor hubs.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<groupId>com.lanternsoftware.currentmonitor</groupId>
|
||||
<artifactId>lantern-currentmonitor</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0.0</version>
|
||||
<version>0.9.5</version>
|
||||
<name>lantern-currentmonitor</name>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -9,10 +9,8 @@ import com.lanternsoftware.datamodel.currentmonitor.HubConfigService;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class BluetoothConfig implements Runnable {
|
||||
private final AtomicBoolean running = new AtomicBoolean(true);
|
||||
public class BluetoothConfig {
|
||||
private final BleApplication app;
|
||||
|
||||
public BluetoothConfig(String _hubName, BleCharacteristicListener _listener) {
|
||||
@@ -25,15 +23,11 @@ public class BluetoothConfig implements Runnable {
|
||||
app = new BleApplication("Lantern", _hubName, new BleService("HubConfig", service.getServiceUUID(), chars));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public void start() {
|
||||
app.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
synchronized (running) {
|
||||
running.set(false);
|
||||
}
|
||||
app.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,59 +90,65 @@ public class MonitorApp {
|
||||
HubConfigCharacteristic ch = NullUtils.toEnum(HubConfigCharacteristic.class, _name);
|
||||
LOG.info("Char Received, Name: {} Value: {}", _name, _value);
|
||||
monitor.submit(()->{
|
||||
switch (ch) {
|
||||
case HubIndex:
|
||||
if ((_value.length > 0)) {
|
||||
config.setHub(_value[0]);
|
||||
ResourceLoader.writeFile(WORKING_DIR + "config.json", DaoSerializer.toJson(config));
|
||||
}
|
||||
break;
|
||||
case AuthCode:
|
||||
String value = NullUtils.toString(_value);
|
||||
if (NullUtils.isNotEmpty(value)) {
|
||||
authCode = value;
|
||||
config.setAuthCode(value);
|
||||
ResourceLoader.writeFile(WORKING_DIR + "config.json", DaoSerializer.toJson(config));
|
||||
}
|
||||
break;
|
||||
case WifiCredentials:
|
||||
String ssid = HubConfigService.decryptWifiSSID(_value);
|
||||
String pwd = HubConfigService.decryptWifiPassword(_value);
|
||||
if (NullUtils.isNotEmpty(ssid) && NullUtils.isNotEmpty(pwd))
|
||||
WifiConfig.setCredentials(ssid, pwd);
|
||||
break;
|
||||
case Flash:
|
||||
if ((CollectionUtils.length(_value) == 0) || (_value[0] == 0)) {
|
||||
if (flasher != null) {
|
||||
flasher.stop();
|
||||
flasher = null;
|
||||
synchronized (monitor) {
|
||||
switch (ch) {
|
||||
case Host:
|
||||
if ((_value.length > 0)) {
|
||||
config.setHost(NullUtils.terminateWith(NullUtils.toString(_value), "/") + "currentmonitor/");
|
||||
ResourceLoader.writeFile(WORKING_DIR + "config.json", DaoSerializer.toJson(config));
|
||||
}
|
||||
else
|
||||
LEDFlasher.setLEDOn(false);
|
||||
}
|
||||
else {
|
||||
if (flasher == null) {
|
||||
flasher = new LEDFlasher();
|
||||
monitor.submit(flasher);
|
||||
break;
|
||||
case HubIndex:
|
||||
if ((_value.length > 0)) {
|
||||
config.setHub(_value[0]);
|
||||
ResourceLoader.writeFile(WORKING_DIR + "config.json", DaoSerializer.toJson(config));
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Restart:
|
||||
LOG.info("Restarting Current Monitor...");
|
||||
try {
|
||||
Runtime.getRuntime().exec("echo \"sudo systemctl restart currentmonitor\" | at now + 1 minute");
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Exception occurred while trying to restart", _e);
|
||||
}
|
||||
break;
|
||||
case Reboot:
|
||||
LOG.info("Rebooting Pi...");
|
||||
try {
|
||||
Runtime.getRuntime().exec("sudo reboot now");
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Exception occurred while trying to reboot", _e);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case AuthCode:
|
||||
String value = NullUtils.toString(_value);
|
||||
if (NullUtils.isNotEmpty(value)) {
|
||||
authCode = value;
|
||||
config.setAuthCode(value);
|
||||
ResourceLoader.writeFile(WORKING_DIR + "config.json", DaoSerializer.toJson(config));
|
||||
}
|
||||
break;
|
||||
case WifiCredentials:
|
||||
String ssid = HubConfigService.decryptWifiSSID(_value);
|
||||
String pwd = HubConfigService.decryptWifiPassword(_value);
|
||||
if (NullUtils.isNotEmpty(ssid) && NullUtils.isNotEmpty(pwd))
|
||||
WifiConfig.setCredentials(ssid, pwd);
|
||||
break;
|
||||
case Flash:
|
||||
if ((CollectionUtils.length(_value) == 0) || (_value[0] == 0)) {
|
||||
if (flasher != null) {
|
||||
flasher.stop();
|
||||
flasher = null;
|
||||
} else
|
||||
LEDFlasher.setLEDOn(false);
|
||||
} else {
|
||||
if (flasher == null) {
|
||||
flasher = new LEDFlasher();
|
||||
monitor.submit(flasher);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Restart:
|
||||
LOG.info("Restarting Current Monitor...");
|
||||
try {
|
||||
Runtime.getRuntime().exec(new String[]{"systemctl","restart","currentmonitor"});
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Exception occurred while trying to restart", _e);
|
||||
}
|
||||
break;
|
||||
case Reboot:
|
||||
LOG.info("Rebooting Pi...");
|
||||
try {
|
||||
Runtime.getRuntime().exec(new String[]{"reboot","now"});
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Exception occurred while trying to reboot", _e);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -159,7 +165,7 @@ public class MonitorApp {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
monitor.submit(bluetoothConfig);
|
||||
bluetoothConfig.start();
|
||||
if (NullUtils.isNotEmpty(config.getAuthCode()))
|
||||
authCode = config.getAuthCode();
|
||||
else {
|
||||
@@ -190,7 +196,8 @@ public class MonitorApp {
|
||||
}
|
||||
List<Breaker> breakers = breakerConfig.getBreakersForHub(config.getHub());
|
||||
LOG.info("Monitoring {} breakers for hub {}", CollectionUtils.size(breakers), hub.getHub());
|
||||
monitor.monitorPower(hub, breakers, 1000, logger);
|
||||
if (CollectionUtils.size(breakers) > 0)
|
||||
monitor.monitorPower(hub, breakers, 1000, logger);
|
||||
}
|
||||
monitor.submit(new PowerPoster());
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
@@ -341,7 +348,7 @@ public class MonitorApp {
|
||||
ResourceLoader.writeFile(WORKING_DIR + "lantern-currentmonitor.jar", jar);
|
||||
ConcurrencyUtils.sleep(10000);
|
||||
try {
|
||||
Runtime.getRuntime().exec("echo \"sudo systemctl restart currentmonitor\" | at now + 1 minute");
|
||||
Runtime.getRuntime().exec(new String[]{"systemctl","restart","currentmonitor"});
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Exception occurred while trying to restart", _e);
|
||||
}
|
||||
@@ -375,9 +382,9 @@ public class MonitorApp {
|
||||
else if (NullUtils.isEqual(command, "extend_filesystem")) {
|
||||
LOG.info("Extending filesystem and rebooting");
|
||||
try {
|
||||
Runtime.getRuntime().exec("sudo raspi-config --expand-rootfs");
|
||||
Runtime.getRuntime().exec(new String[]{"sudo","raspi-config","--expand-rootfs"});
|
||||
ConcurrencyUtils.sleep(5000);
|
||||
Runtime.getRuntime().exec("sudo reboot now");
|
||||
Runtime.getRuntime().exec(new String[]{"reboot","now"});
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Exception occurred while trying to extend filesystem", _e);
|
||||
}
|
||||
@@ -386,7 +393,7 @@ public class MonitorApp {
|
||||
else if (NullUtils.isEqual(command, "restart")) {
|
||||
LOG.info("Restarting...");
|
||||
try {
|
||||
Runtime.getRuntime().exec("echo \"sudo systemctl restart currentmonitor\" | at now + 1 minute");
|
||||
Runtime.getRuntime().exec(new String[]{"systemctl","restart","currentmonitor"});
|
||||
} catch (IOException _e) {
|
||||
LOG.error("Exception occurred while trying to restart", _e);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.lanternsoftware.currentmonitor.wifi;
|
||||
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.ResourceLoader;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
@@ -11,28 +13,17 @@ public abstract class WifiConfig {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WifiConfig.class);
|
||||
|
||||
private static final String WIFI_CONFIG_PATH = "/etc/wpa_supplicant/wpa_supplicant.conf";
|
||||
private static final String CONF_FORMAT = "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\ncountry=US\nnetwork={\n\tssid=\"%s\"\n\t%s\n}\n";
|
||||
private static final String CONF_FORMAT = "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\ncountry=US\n";
|
||||
|
||||
public static void setCredentials(String _ssid, String _password) {
|
||||
String[] commands = {"wpa_passphrase", _ssid, _password};
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = Runtime.getRuntime().exec(commands).getInputStream();
|
||||
String newConf = IOUtils.toString(is);
|
||||
is = Runtime.getRuntime().exec(new String[]{"wpa_passphrase", _ssid, _password}).getInputStream();
|
||||
String newConf = CollectionUtils.delimit(CollectionUtils.filter(CollectionUtils.asArrayList(NullUtils.cleanSplit(IOUtils.toString(is), "\\r?\\n")), _s->!_s.trim().startsWith("#")), "\n");
|
||||
if (newConf == null)
|
||||
return;
|
||||
int idx = newConf.indexOf("psk=");
|
||||
if (idx > 0) {
|
||||
if (newConf.charAt(idx-1) == '#')
|
||||
idx = newConf.indexOf("psk=", idx+1);
|
||||
if (idx > 0) {
|
||||
int endIdx = newConf.indexOf("\n", idx);
|
||||
if (endIdx > 0) {
|
||||
String finalConf = String.format(CONF_FORMAT, _ssid, newConf.substring(idx, endIdx));
|
||||
ResourceLoader.writeFile(WIFI_CONFIG_PATH, finalConf);
|
||||
}
|
||||
}
|
||||
}
|
||||
ResourceLoader.writeFile(WIFI_CONFIG_PATH, CONF_FORMAT+newConf);
|
||||
Runtime.getRuntime().exec(new String[]{"wpa_cli","-i","wlan0","reconfigure"});
|
||||
}
|
||||
catch (Exception _e) {
|
||||
LOG.error("Failed to write wifi credentials", _e);
|
||||
|
||||
Reference in New Issue
Block a user