Skip to content

Commit

Permalink
Nicer error messages, removed rpi-cpu-temp, fixed some errorprone war…
Browse files Browse the repository at this point in the history
…nings
  • Loading branch information
retrodaredevil committed Mar 18, 2024
1 parent a319278 commit b09fccc
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -116,7 +117,7 @@ private void cancelCommand(MessageSender messageSender, UUID schedulingId) {

@Override
public boolean handleMessage(Message message, MessageSender messageSender) {
String text = message.getText().toLowerCase();
String text = message.getText().toLowerCase(Locale.ENGLISH);
String[] split = text.split(" ");
if (split.length == 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.retrodaredevil.solarthing.annotations.UtilityClass;

import java.util.Iterator;
import java.util.Locale;
import java.util.function.Function;

@UtilityClass
Expand All @@ -13,7 +14,7 @@ public final class ChatBotUtil {
private static final double SIMILARITY_CONSTANT = 0.86;

public static double similarity(String s1, String s2) {
return MATCHER.similarity(s1.toLowerCase(), s2.toLowerCase());
return MATCHER.similarity(s1.toLowerCase(Locale.ENGLISH), s2.toLowerCase(Locale.ENGLISH));
}
public static boolean isSimilar(String s1, String s2) {
return similarity(s1, s2) > SIMILARITY_CONSTANT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -259,7 +260,7 @@ private static Stream<FlagAliasPacket> findAliases(Stream<? extends VersionedPac
@Override
public boolean handleMessage(Message message, MessageSender messageSender) {
String text = message.getText();
String lowerText = text.toLowerCase();
String lowerText = text.toLowerCase(Locale.ENGLISH);
String[] split = lowerText.split(" ");
if (split.length == 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -117,7 +118,7 @@ private void displayHeartbeats(MessageSender messageSender) {
@Override
public boolean handleMessage(Message message, MessageSender messageSender) {
String text = message.getText();
String lowerText = text.toLowerCase();
String lowerText = text.toLowerCase(Locale.ENGLISH);
String[] split = lowerText.split(" ");
if (split.length == 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -50,7 +51,7 @@ private boolean canScheduleAnyCommands(Message message) {

@Override
public boolean handleMessage(Message message, MessageSender messageSender) {
String text = message.getText().toLowerCase();
String text = message.getText().toLowerCase(Locale.ENGLISH);
String[] split = text.split(" ");
if (split.length == 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* through commands. Although these can also be used for other purposes
*/
@JsonSubTypes({
@JsonSubTypes.Type(RaspberryPiCpuTemperatureDataRequester.class),
@JsonSubTypes.Type(CpuTemperatureDataRequester.class),
@JsonSubTypes.Type(W1TemperatureDataRequester.class),
@JsonSubTypes.Type(BatteryVoltageIODataRequester.class),
Expand Down

This file was deleted.

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

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

@UtilityClass
Expand All @@ -38,7 +39,7 @@ private static void handleSplit(String[] split, @Nullable MutableAddressModbusSl
break;
case 1:
// display data
String request = split[0].toLowerCase();
String request = split[0].toLowerCase(Locale.ENGLISH);
switch(request){
case "maxvoltage":
System.out.println(read.getMaxVoltage().getModeName());
Expand Down Expand Up @@ -286,8 +287,8 @@ private static void handleSplit(String[] split, @Nullable MutableAddressModbusSl
break;
case 2:
// set most data
String toSet = split[1].toLowerCase();
switch(split[0].toLowerCase()) {
String toSet = split[1].toLowerCase(Locale.ENGLISH);
switch(split[0].toLowerCase(Locale.ENGLISH)) {
case "factoryreset":
if(toSet.equals("!!")){
write.factoryReset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static int doMainCommand(CommandOptions commandOptions, Path baseConfigFi
options = ConfigUtil.readConfig(baseConfigFile, ProgramOptions.class);
} catch (ConfigException e) {
LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Error while parsing ProgramOptions.", e);
LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "Hey! The error you got above might be scary, but this message might be helpful:\n\n" + e.getMessage());
return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;

@JsonTypeName("acmode")
public class ACModeActionNode implements ActionNode {
private static final Logger LOGGER = LoggerFactory.getLogger(ACModeActionNode.class);
Expand All @@ -32,7 +34,7 @@ public ACModeActionNode(ACMode acMode, boolean not) {
this.not = not;
}
private static ACMode parseMode(String modeName) {
modeName = modeName.replaceAll(" ", "").toLowerCase();
modeName = modeName.replaceAll(" ", "").toLowerCase(Locale.ENGLISH);
switch (modeName) {
case "noac":
return ACMode.NO_AC;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.retrodaredevil.solarthing.config;

import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down Expand Up @@ -81,7 +83,26 @@ public static ConfigException createExceptionFromJackson(String fileRepresentati
if (jacksonIOException.getMessage().contains("end-of-input")) {
throw new ConfigException("Invalid JSON in file: " + fileRepresentation + " (check formatting)", jacksonIOException);
}
throw new ConfigException("Couldn't parse data from file: " + fileRepresentation + " Please make sure your JSON is correct.", jacksonIOException);
if (jacksonIOException instanceof DatabindException) {
if (jacksonIOException instanceof UnrecognizedPropertyException) {
String internalMessage = jacksonIOException.getMessage();
final String property;
if (internalMessage != null && internalMessage.startsWith("Unrecognized field \"")) {
String[] splitMessage = internalMessage.split("\"", 3);
if (splitMessage.length == 3) {
property = splitMessage[1];
} else {
property = null;
}
} else {
property = null;
}
String extraMessage = property != null ? " Unrecognized property: " + property : "";
throw new ConfigException("Unrecognized config property in file: " + fileRepresentation + " Please make sure you don't have any typos." + extraMessage, jacksonIOException);
}
throw new ConfigException("Couldn't parse data from file: " + fileRepresentation + " Please make sure all your properties are correct", jacksonIOException);
}
throw new ConfigException("Couldn't parse data from file: " + fileRepresentation + " Please make sure your JSON is formatted correctly.", jacksonIOException);
}

public static <T> T readConfig(Path file, Class<T> clazz, ObjectMapper objectMapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface ErrorReporter extends Identifiable {
*/
int getErrorModeValue();
@GraphQLInclude("errorModes")
@NotNull Collection<@NotNull ? extends BitmaskMode> getErrorModes();
@NotNull Collection<? extends @NotNull BitmaskMode> getErrorModes();
@GraphQLInclude("hasError")
default boolean hasError() {
return getErrorModeValue() != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ default int getStreetLightStatusValue(){
int getErrorModeValue();
@ValidSinceVersion(version = RoverStatusPacket.Version.CORRECT_TWO_REGISTER)
@Override
default @NotNull Collection<@NotNull ? extends SimpleRoverErrorMode> getErrorModes(){
default @NotNull Collection<? extends @NotNull SimpleRoverErrorMode> getErrorModes(){
if (isDcdc()) {
return getDcdcErrorModes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.Locale;

@UtilityClass
public class TimeUtil {
Expand Down Expand Up @@ -35,7 +36,7 @@ public static String millisToPrettyString(long millis) {
}

public static String informalDurationToFormal(String informalDuration) {
String formalDuration = informalDuration.toUpperCase()
String formalDuration = informalDuration.toUpperCase(Locale.ENGLISH)
.replaceAll("SECONDS|SECOND", "S")
.replaceAll("MINUTES|MINUTE", "M")
.replaceAll("HOURS|HOUR", "H")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public int getErrorModeValue() {
}

@Override
public @NotNull Collection<@NotNull ? extends BitmaskMode> getErrorModes() {
public @NotNull Collection<? extends @NotNull BitmaskMode> getErrorModes() {
throw new UnsupportedOperationException();
}
};
Expand Down

0 comments on commit b09fccc

Please sign in to comment.