Skip to content

Commit

Permalink
chore: remove a few more manual log statements (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboxer23 authored Feb 3, 2025
1 parent 0efa68c commit dd259ab
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
import com.bigboxer23.lights.controllers.govee.GoveeHumidifierController;
import com.bigboxer23.utils.command.RetryingCommand;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;

/**
* Govee humidifier sometimes "sticks" on when in auto mode. This action resets the humidity control
* if detected its stuck open
*/
@Slf4j
public class HumidifierResetAction implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(HumidifierResetAction.class);

private final GoveeHumidifierController goveeController;

private final String humidifierModel;
Expand Down Expand Up @@ -44,7 +42,7 @@ public void run() {
},
"Set 70% " + goveeController.getIdentifier(humidifierId));
} catch (InterruptedException | IOException e) {
logger.error("HumidifierResetAction", e);
log.error("HumidifierResetAction", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
Expand All @@ -30,9 +29,9 @@
* Controller for managing a system of devices from different ecosystems to do a refill of a
* humidifier on command
*/
@Slf4j
@RestController
public class HumiditySystemController implements InitializingBean, IHumidityEventHandler {
private static final Logger logger = LoggerFactory.getLogger(HumiditySystemController.class);

private final SwitchBotController switchbotController;

Expand Down Expand Up @@ -75,13 +74,13 @@ public void triggerHumidifierRefillAndRestart(
@Parameter(description = "deviceName (for logging only)") @PathVariable(value = "deviceName")
String deviceName,
@Parameter(description = "deviceModel") @PathVariable(value = "deviceModel") String deviceModel) {
logger.info(deviceId + " requested via web api");
log.info(deviceId + " requested via web api");
outOfWaterEvent(deviceId, deviceName, deviceModel);
}

@Scheduled(fixedDelay = 600000) // 10min
public void checkAfterInterval() {
logger.info("checking humidity");
log.info("checking humidity");
humidifierMap.values().stream()
.filter(cluster -> !StringUtils.isEmpty(cluster.getHumiditySensor()))
.forEach(cluster -> {
Expand All @@ -93,11 +92,11 @@ public void checkAfterInterval() {
.getDeviceStatus(cluster.getHumiditySensor())
.getHumidity(),
"Get humidity " + switchbotController.getIdentifier(cluster.getHumiditySensor()));
logger.info(switchbotController.getIdentifier(cluster.getHumiditySensor())
log.info(switchbotController.getIdentifier(cluster.getHumiditySensor())
+ " humidity "
+ humidity);
if (humidity < cluster.getLowHumidityPoint()) {
logger.info("low humidity detected " + humidity + ":" + cluster.getLowHumidityPoint());
log.info("low humidity detected " + humidity + ":" + cluster.getLowHumidityPoint());
float watts = RetryingCommand.execute(
() -> switchbotController
.getSwitchbotAPI()
Expand All @@ -106,7 +105,7 @@ public void checkAfterInterval() {
.getWatts(),
"Get watts " + switchbotController.getIdentifier(cluster.getOutlet()));
if (watts > HUMIDIFIER_RUNNING_WATTAGE) {
logger.info("humidifier is running, detected wattage: " + watts);
log.info("humidifier is running, detected wattage: " + watts);
return;
}
outOfWaterEvent(
Expand All @@ -123,7 +122,7 @@ public void checkAfterInterval() {
.getWatts(),
"Get watts " + switchbotController.getIdentifier(cluster.getOutlet()));
if (watts > HUMIDIFIER_RUNNING_WATTAGE) {
logger.info("humidifier should not be running, humidify is too"
log.info("humidifier should not be running, humidify is too"
+ " high "
+ watts
+ " "
Expand All @@ -134,7 +133,7 @@ public void checkAfterInterval() {
}
}
} catch (IOException e) {
logger.error("manualCheck: ", e);
log.error("manualCheck: ", e);
}
});
}
Expand All @@ -143,10 +142,10 @@ public void checkAfterInterval() {
public void outOfWaterEvent(String deviceId, String deviceName, String deviceModel) {
HumidifierCluster cluster = humidifierMap.get(deviceId);
if (cluster == null) {
logger.warn("No cluster for " + deviceId);
log.warn("No cluster for " + deviceId);
return;
}
logger.info("Out of water event triggered " + deviceName + " : " + deviceId);
log.info("Out of water event triggered " + deviceName + " : " + deviceId);
if (!goveeController.isLastEventRecent(deviceId, deviceName)) {
new Thread(new RefillAction(
switchbotController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import com.bigboxer23.switch_bot.IDeviceCommands;
import com.bigboxer23.utils.command.RetryingCommand;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;

/** */
@Slf4j
public class RefillAction implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(RefillAction.class);

private final SwitchBotController switchbotController;

private final GoveeHumidifierController goveeController;
Expand Down Expand Up @@ -90,7 +88,7 @@ public void run() {
},
"Off " + switchbotController.getIdentifier(pumpId));
} catch (IOException | InterruptedException e) {
logger.error("error refilling humidifier, attempting to turn off pump " + pumpId, e);
log.error("error refilling humidifier, attempting to turn off pump " + pumpId, e);
try {
Thread.sleep(5 * 1000); // 5 sec
RetryingCommand.execute(
Expand All @@ -103,7 +101,7 @@ public void run() {
},
"Off " + switchbotController.getIdentifier(pumpId));
} catch (IOException | InterruptedException e2) {
logger.error("error turning off pump " + pumpId, e2);
log.error("error turning off pump " + pumpId, e2);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand All @@ -42,11 +41,10 @@
* <p>Based off the quickstart google cal api example. credentials.json file needs to be placed into
* src/main/resources
*/
@Slf4j
@Component
@EnableAutoConfiguration
public class GCalendarController extends HubContext {
private static final Logger logger = LoggerFactory.getLogger(GCalendarController.class);

private static final JsonFactory kJSON_FACTORY = GsonFactory.getDefaultInstance();

private static List<String> kVacationKeywords = new ArrayList<String>() {
Expand Down Expand Up @@ -85,7 +83,7 @@ protected GCalendarController(
}

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
logger.info("Getting gCal creds");
log.info("Getting gCal creds");
// Load client secrets.
InputStream aCredStream = GCalendarController.class.getResourceAsStream("/credentials.json");
GoogleClientSecrets aClientSecrets =
Expand All @@ -100,15 +98,15 @@ private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File("tokens")))
.setAccessType("offline")
.build();
logger.info("Starting local server receiver");
log.info("Starting local server receiver");
return new AuthorizationCodeInstalledApp(
aFlow, new LocalServerReceiver.Builder().setPort(8890).build())
.authorize("user");
}

@Scheduled(cron = "0 0 0 ? * *") // Run every day at 12am
private void fetchCalendarStatus() {
logger.info("Fetching calendar information");
log.info("Fetching calendar information");
try {
NetHttpTransport aTransport = GoogleNetHttpTransport.newTrustedTransport();
Calendar aCalendar = new Calendar.Builder(aTransport, kJSON_FACTORY, getCredentials(aTransport))
Expand All @@ -133,22 +131,22 @@ private void fetchCalendarStatus() {
myOpenHABController.setVacationMode(findMatchingEvents("Vacation", events, kVacationKeywords));
myOpenHABController.setPTOMode(findMatchingEvents("PTO", events, kPTOKeywords));
myOpenHABController.setExtendedEveningMode(findLateEvents(events));
logger.info("Calendar information fetched and parsed");
log.info("Calendar information fetched and parsed");
} catch (GeneralSecurityException | IOException e) {
logger.error("fetchCalendarStatus:", e);
log.error("fetchCalendarStatus:", e);
}
}

private boolean findMatchingEvents(String theType, Events theEvents, List<String> theKeywords) {
return theEvents.getItems().stream()
.anyMatch(theEvent -> theKeywords.stream().anyMatch(theWord -> {
logger.debug(theEvent.getSummary());
log.debug(theEvent.getSummary());
boolean aFound = (theEvent.getSummary() != null
&& theEvent.getSummary().toLowerCase().contains(theWord))
|| (theEvent.getDescription() != null
&& theEvent.getDescription().toLowerCase().contains(theWord));
if (aFound) {
logger.warn(theType + " enabled: " + theEvent.getSummary());
log.warn(theType + " enabled: " + theEvent.getSummary());
}
return aFound;
}));
Expand All @@ -168,7 +166,7 @@ private boolean findLateEvents(Events events) {
&& event.getEnd().getDateTime().getValue()
>= tenThirty.toInstant().toEpochMilli();
if (found) {
logger.warn("findLateEvents enabled: " + event.getSummary());
log.warn("findLateEvents enabled: " + event.getSummary());
}
return found;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import com.bigboxer23.lights.controllers.vera.VeraDeviceVO;
import com.bigboxer23.lights.controllers.vera.VeraHouseVO;
import java.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

/** */
@Slf4j
@Controller
public class EconetController {
private static final Logger logger = LoggerFactory.getLogger(EconetController.class);

private EcoNetAPI ecoNetAPI;

@Value("${econet_email}")
Expand All @@ -38,7 +36,7 @@ private EcoNetAPI getEcoNetAPI() {
@Scheduled(fixedDelay = 300000) // 5min
private void fetchWaterHeaterStatus() {
try {
logger.debug("Fetching water heater status...");
log.debug("Fetching water heater status...");
getEcoNetAPI().fetchUserData().ifPresent(data -> {
Equipment equipment =
data.getResults().getLocations().get(0).getEquipments().get(0);
Expand All @@ -60,9 +58,9 @@ private void fetchWaterHeaterStatus() {
waterHeaterData.setLevel(kwh + "");
});
});
logger.debug("Fetched water heater status...");
log.debug("Fetched water heater status...");
} catch (Exception e) {
logger.error("fetchWaterHeaterStatus", e);
log.error("fetchWaterHeaterStatus", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
Expand All @@ -23,6 +22,7 @@
* Controller listens for empty events from humidifier and triggers a pump to add water to the tank
* and turn the humidifier back on
*/
@Slf4j
@Controller
public class GoveeHumidifierController implements InitializingBean {
@Value("${govee_api_key}")
Expand All @@ -40,8 +40,6 @@ public class GoveeHumidifierController implements InitializingBean {
@Value("${goveePushEnabled}")
private boolean isEnabled;

private static final Logger logger = LoggerFactory.getLogger(GoveeHumidifierController.class);

private final Map<String, Long> goveeEvents = new HashMap<>();
private final Map<String, Long> mailSent = new HashMap<>();

Expand All @@ -58,16 +56,15 @@ public GoveeDeviceCommandResponse sendDeviceCommand(GoveeDeviceStatusRequest com
@Override
public void afterPropertiesSet() {
if (!isEnabled) {
logger.warn("govee event listener not enabled");
log.warn("govee event listener not enabled");
return;
}
logger.warn("starting govee event listener");
log.warn("starting govee event listener");
GoveeApi.getInstance(API_KEY).subscribeToGoveeEvents(new GoveeEventSubscriber() {
@Override
public void messageReceived(GoveeEvent event) {
if (event.isLackWaterEvent()) {
logger.warn(
"no water: " + event.getModel() + " " + event.getDeviceId() + " " + event.getDeviceName());
log.warn("no water: " + event.getModel() + " " + event.getDeviceId() + " " + event.getDeviceName());
if (!isLastEventRecent(event.getDeviceId(), event.getDeviceName())) {
handlers.forEach(handler ->
handler.outOfWaterEvent(event.getDeviceId(), event.getDeviceName(), event.getModel()));
Expand All @@ -82,7 +79,7 @@ public boolean isLastEventRecent(String deviceId, String deviceName) {
goveeEvents.put(deviceId, System.currentTimeMillis() + ITimeConstants.FIFTEEN_MINUTES);
boolean isRecent = System.currentTimeMillis() <= lastEvent;
if (isRecent) {
logger.error("govee event recent " + lastEvent + ":" + System.currentTimeMillis());
log.error("govee event recent " + lastEvent + ":" + System.currentTimeMillis());
Long lastMailEvent = mailSent.getOrDefault(deviceId, Long.MIN_VALUE);
mailSent.put(deviceId, System.currentTimeMillis() + ITimeConstants.HOUR);
if (System.currentTimeMillis() > lastMailEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** */
@Slf4j
public class HueCompatibleClient {
private static final Logger logger = LoggerFactory.getLogger(HueCompatibleClient.class);
private static OkHttpClient instance;

public static OkHttpClient getClient() {
Expand All @@ -39,7 +38,7 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
.build();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
logger.warn("getInstance", e);
log.warn("getInstance", e);
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/** */
@Slf4j
@Tag(
name = "Hue V2 Controller",
description = "Controller to control hue scenes or other constructs without going through the" + " native app.")
@RestController
public class HueV2Controller {

private static final Logger logger = LoggerFactory.getLogger(HueV2Controller.class);

private static final String BASE_URL = "https://{0}/clip/v2/{1}";

private static final String AUTH_HEADER = "hue-application-key";
Expand Down Expand Up @@ -108,7 +105,7 @@ private List<HueResource> getResource(String url) {
.map(Arrays::asList)
.orElse(Collections.emptyList());
} catch (IOException e) {
logger.error("getResource " + url, e);
log.error("getResource " + url, e);
return Collections.emptyList();
}
}
Expand Down
Loading

0 comments on commit dd259ab

Please sign in to comment.