Skip to content

Commit

Permalink
Charset improvements, --validate option
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Mar 28, 2023
1 parent 9fa60d6 commit 735fdcd
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public AnalyticsManager(boolean isEnabled, Path dataDirectory) {
.withTrackingId("UA-70767765-2")
.build();
} else {
// TODO when the --validate argument is present, this is always printed. Think about if we want to let the user know what the actual configuration is
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Google Analytics is disabled");
googleAnalytics = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import static java.util.Objects.requireNonNull;

Expand All @@ -16,13 +17,12 @@ public class SocketSimpleConnection implements SimpleConnection {
private final BufferedReader bufferedReader;
private final PrintWriter writer;

@SuppressWarnings("DefaultCharset")
public SocketSimpleConnection(Socket socket) throws IOException {
this.socket = socket;
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
writer = new PrintWriter(outputStream);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
writer = new PrintWriter(outputStream, false, StandardCharsets.UTF_8);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public final class AutomationMain {

private static final Logger LOGGER = LoggerFactory.getLogger(AutomationMain.class);

public static int startAutomation(AutomationProgramOptions options) throws IOException {
return startAutomation(ActionUtil.createActionNodeEntries(options), options, options.getPeriodMillis());
public static int startAutomation(AutomationProgramOptions options, boolean isValidate) throws IOException {
return startAutomation(ActionUtil.createActionNodeEntries(options), options, options.getPeriodMillis(), isValidate);
}

private static void queryAndFeed(MillisDatabase millisDatabase, ResourceManager<SimpleDatabaseCache> databaseCacheManager, boolean useEndDate) {
Expand Down Expand Up @@ -81,7 +81,7 @@ private static void queryAndFeed(MillisDatabase millisDatabase, ResourceManager<
}

@SuppressWarnings("InconsistentOverloads")
public static int startAutomation(List<ActionNodeEntry> originalActionNodeEntries, DatabaseTimeZoneOptionBase options, long periodMillis) {
public static int startAutomation(List<ActionNodeEntry> originalActionNodeEntries, DatabaseTimeZoneOptionBase options, long periodMillis, boolean isValidate) {
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Starting automation program.");
final CouchDbDatabaseSettings couchSettings;
try {
Expand Down Expand Up @@ -123,6 +123,10 @@ public static int startAutomation(List<ActionNodeEntry> originalActionNodeEntrie
.add(new AuthorizationEnvironment(new DatabaseDocumentKeyMap(authorizationPacketCache)))
.build();

if (isValidate) {
return 0;
}

ActionMultiplexer multiplexer = new Actions.ActionMultiplexerBuilder().build();
List<ActionNodeEntry> actionNodeEntries = new ArrayList<>(originalActionNodeEntries); // entries may be removed from this list
while (!Thread.currentThread().isInterrupted()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public interface CommandOptions {
@Option(longName = "to", defaultToNull = true, description = "Use with --base and a pvoutput-upload program type")
String getPVOutputToDate();

@Option(longName = "validate", description = "When present, the program does not run but only validates the configuration.")
boolean isValidate();

@Unparsed(name = "LEGACY ARGUMENTS")
List<String> getLegacyOptionsRaw();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import me.retrodaredevil.solarthing.util.JacksonUtil;

import java.io.PrintStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
Expand All @@ -42,11 +43,9 @@ public CouchDbSetupMain(CouchDbInstance instance, PrintStream out, Prompt prompt
this.out = out;
this.prompt = prompt;
}
@SuppressWarnings("DefaultCharset")
public static CouchDbSetupMain createFrom(CouchDbDatabaseSettings settings) {
CouchDbInstance instance = CouchDbUtil.createInstance(settings.getCouchProperties(), settings.getOkHttpProperties());
// TODO when upgrade to Java 11, use Charset.defaultCharset() here for explicitness
Scanner scanner = new Scanner(System.in);
Scanner scanner = new Scanner(System.in, Charset.defaultCharset());
PrintStream out = System.out;
return new CouchDbSetupMain(instance, out, new ScannerPrompt(scanner));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class OutbackMateMain {
private static final Collection<MateCommand> ALLOWED_COMMANDS = EnumSet.of(MateCommand.AUX_OFF, MateCommand.AUX_ON, MateCommand.USE, MateCommand.DROP);

@SuppressWarnings("SameReturnValue")
public static int connectMate(MateProgramOptions options, Path dataDirectory) throws Exception {
public static int connectMate(MateProgramOptions options, Path dataDirectory, boolean isValidate) throws Exception {
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Beginning mate program");
AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled(), dataDirectory);
analyticsManager.sendStartUp(ProgramType.MATE);
Expand Down Expand Up @@ -103,6 +103,9 @@ public static int connectMate(MateProgramOptions options, Path dataDirectory) th
dataRequesterResults.stream().map(DataRequesterResult::getStatusEndPacketListReceiver).forEachOrdered(packetListReceiverList::add);
packetListReceiverList.addAll(bundle.createDefaultPacketListReceivers());

if (isValidate) {
return 0;
}
return SolarMain.initReader(
requireNonNull(ioBundle.getInputStream()),
ioBundle::reload,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public class RequestMain {

private static final Logger LOGGER = LoggerFactory.getLogger(RequestMain.class);

public static int startRequestProgram(RequestProgramOptions options, Path dataDirectory) throws Exception {
public static int startRequestProgram(RequestProgramOptions options, Path dataDirectory, boolean isValidate) throws Exception {
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Beginning request program");
AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled(), dataDirectory);
AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled() && !isValidate, dataDirectory);
analyticsManager.sendStartUp(ProgramType.REQUEST);
return startRequestProgram(options, analyticsManager, options.getPeriod(), options.getMinimumWait());
return startRequestProgram(options, analyticsManager, options.getPeriod(), options.getMinimumWait(), isValidate);
}

private static int startRequestProgram(RequestProgramOptions options, AnalyticsManager analyticsManager, Duration period, Duration minimumWait) throws Exception {
private static int startRequestProgram(RequestProgramOptions options, AnalyticsManager analyticsManager, Duration period, Duration minimumWait, boolean isValidate) throws Exception {
// Note this is very similar to code in OutbackMateMain and could eventually be refactored
EnvironmentUpdater[] environmentUpdaterReference = new EnvironmentUpdater[1];
PacketHandlerInit.Result handlersResult = PacketHandlerInit.initHandlers(
Expand Down Expand Up @@ -71,6 +71,9 @@ private static int startRequestProgram(RequestProgramOptions options, AnalyticsM
dataRequesterResults.stream().map(DataRequesterResult::getStatusEndPacketListReceiver).forEachOrdered(packetListReceiverList::add);
packetListReceiverList.addAll(bundle.createDefaultPacketListReceivers());

if (isValidate) {
return 0;
}
return doRequest(new PacketListReceiverMultiplexer(packetListReceiverList), period, minimumWait);
}
private static int doRequest(PacketListReceiver packetListReceiver, Duration period, Duration minimumWait) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ public class RoverMain {
private static final Logger LOGGER = LoggerFactory.getLogger(RoverMain.class);


public static int connectRoverSetup(RoverSetupProgramOptions options) {
return doRoverProgram(options, RoverSetupProgram::startRoverSetup);
public static int connectRoverSetup(RoverSetupProgramOptions options, boolean isValidate) {
return doRoverProgram(options, RoverSetupProgram::startRoverSetup, isValidate);
}
private static int doRoverProgram(RoverOption options, RoverProgramRunner runner) {
private static int doRoverProgram(RoverOption options, RoverProgramRunner runner, boolean isValidate) {
IOConfig ioConfig = ConfigUtil.parseIOConfig(options.getIOBundleFilePath(), RoverReadTable.SERIAL_CONFIG);
try(ReloadableIOBundle ioBundle = new ReloadableIOBundle(ioConfig::createIOBundle)) {
ModbusSlaveBus modbus = new IOModbusSlaveBus(ioBundle, new RtuDataEncoder(2000, 20, 4));
MutableAddressModbusSlave slave = new MutableAddressModbusSlave(options.getModbusAddress(), modbus);
RoverReadTable read = new RoverModbusSlaveRead(slave);
RoverWriteTable write = new RoverModbusSlaveWrite(slave);
if (isValidate) {
return 0;
}
return runner.doProgram(slave, read, write, () -> {}, ioBundle::reload);
} catch (Exception e) {
LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Got exception!", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
import me.retrodaredevil.solarthing.solar.renogy.rover.special.SpecialPowerControl_E02D;
import me.retrodaredevil.solarthing.util.StringUtil;

import java.nio.charset.Charset;
import java.util.Scanner;

@UtilityClass
public final class RoverSetupProgram {
private RoverSetupProgram(){ throw new UnsupportedOperationException(); }

@SuppressWarnings("DefaultCharset")
public static int startRoverSetup(@Nullable MutableAddressModbusSlave slave, RoverReadTable read, RoverWriteTable write, Runnable reloadCache, Runnable reloadIO){
System.out.println("Starting rover setup! This is deprecated and will be removed in a future version!.");
Scanner scanner = new Scanner(System.in);
Scanner scanner = new Scanner(System.in, Charset.defaultCharset());
while (scanner.hasNextLine()) {
String command = scanner.nextLine();
String[] split = StringUtil.terminalSplit(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private static String getJarInfo() {
+ " Java version: " + System.getProperty("java.version");
}

@SuppressWarnings("DefaultCharset")
public static int doMainCommand(CommandOptions commandOptions, Path baseConfigFile) {
String user = System.getProperty("user.name");
if (!user.equals("solarthing")) {
Expand Down Expand Up @@ -134,15 +133,15 @@ public static int doMainCommand(CommandOptions commandOptions, Path baseConfigFi
final ProgramType programType = options.getProgramType();
try {
if(programType == ProgramType.MATE) {
return OutbackMateMain.connectMate((MateProgramOptions) options, dataDirectory);
return OutbackMateMain.connectMate((MateProgramOptions) options, dataDirectory, commandOptions.isValidate());
} else if(programType == ProgramType.ROVER_SETUP){
return RoverMain.connectRoverSetup((RoverSetupProgramOptions) options);
return RoverMain.connectRoverSetup((RoverSetupProgramOptions) options, commandOptions.isValidate());
} else if(programType == ProgramType.PVOUTPUT_UPLOAD){
return PVOutputUploadMain.startPVOutputUpload((PVOutputUploadProgramOptions) options, commandOptions, dataDirectory);
return PVOutputUploadMain.startPVOutputUpload((PVOutputUploadProgramOptions) options, commandOptions, dataDirectory, commandOptions.isValidate());
} else if(programType == ProgramType.REQUEST) {
return RequestMain.startRequestProgram((RequestProgramOptions) options, dataDirectory);
return RequestMain.startRequestProgram((RequestProgramOptions) options, dataDirectory, commandOptions.isValidate());
} else if(programType == ProgramType.AUTOMATION) {
return AutomationMain.startAutomation((AutomationProgramOptions) options);
return AutomationMain.startAutomation((AutomationProgramOptions) options, commandOptions.isValidate());
}
throw new AssertionError("Unknown program type... type=" + programType + " programOptions=" + options);
} catch (ConfigException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class PVOutputUploadMain {


@SuppressWarnings({"SameReturnValue", "deprecation"})
public static int startPVOutputUpload(PVOutputUploadProgramOptions options, CommandOptions commandOptions, Path dataDirectory){
public static int startPVOutputUpload(PVOutputUploadProgramOptions options, CommandOptions commandOptions, Path dataDirectory, boolean isValidate){
final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); // Not thread safe, otherwise this would be a static field

LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Starting PV Output upload program");
Expand Down Expand Up @@ -102,6 +102,9 @@ public static int startPVOutputUpload(PVOutputUploadProgramOptions options, Comm
System.err.println("Unable to parser either from date or to date. Use the yyyy-MM-dd format");
return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
}
if (isValidate) {
return 0;
}
return startRangeUpload(
fromDate, toDate,
options, database, handler, service, options.getZoneId()
Expand All @@ -110,9 +113,12 @@ public static int startPVOutputUpload(PVOutputUploadProgramOptions options, Comm
LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)You need to define both from and to, or define neither to do the normal PVOutput program!");
return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
}
AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled(), dataDirectory);
AnalyticsManager analyticsManager = new AnalyticsManager(options.isAnalyticsEnabled() && !isValidate, dataDirectory);
analyticsManager.sendStartUp(ProgramType.PVOUTPUT_UPLOAD);

if (isValidate) {
return 0;
}
return startRealTimeProgram(options, database, handler, service, options.getZoneId());
}
@SuppressWarnings("CatchAndPrintStackTrace")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ public class IntegrationSetup {
private IntegrationSetup() { throw new UnsupportedOperationException(); }

public static void setup(CouchDbInstance instance) throws CouchDbException {
// Once we get Java 11 features, use OutputStream.nullOutputStream() instead
OutputStream nullOutputStream = new OutputStream() {
@Override
public void write(int i) {}
};
CouchDbSetupMain couchDbSetupMain = new CouchDbSetupMain(instance, new PrintStream(nullOutputStream), new IntegrationPrompt());
CouchDbSetupMain couchDbSetupMain = new CouchDbSetupMain(instance, new PrintStream(OutputStream.nullOutputStream()), new IntegrationPrompt());
int status = couchDbSetupMain.doCouchDbSetupMain();
assertEquals(0, status);
}
Expand Down

0 comments on commit 735fdcd

Please sign in to comment.