Skip to content

Commit

Permalink
update: refactored logging to simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Bello authored and Josh Bello committed Nov 1, 2024
1 parent f79909f commit 725bf9e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "homebridge-hue-daylight-sync",
"displayName": "Hue Daylight Sync",
"version": "1.3.9",
"version": "1.4.0",
"description": "A Homebridge plugin for syncing Hue lights with daylight",
"main": "dist/index.js",
"configSchema": "config.schema.json",
Expand Down
16 changes: 10 additions & 6 deletions src/auto-mode-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class AutoModeService {
}

private async initializeAutoMode() {
this.platform.log.info(`Initializing Auto Mode: ${this.isAutoMode ? 'ON' : 'OFF'}`);
this.platform.log.info(`Auto Mode: ${this.isAutoMode ? 'ON' : 'OFF'}`);
this.service.updateCharacteristic(this.platform.Characteristic.On, this.isAutoMode);
if (this.isAutoMode) {
await this.updateTemperature();
Expand All @@ -39,7 +39,7 @@ export class AutoModeService {

async setAutoMode(value: CharacteristicValue) {
this.isAutoMode = value as boolean;
this.platform.log.info('Set Auto Mode ->', value);
this.platform.log.info('Auto Mode:', value);
if (this.isAutoMode) {
await this.updateTemperature();
this.startAutoUpdate();
Expand All @@ -55,7 +55,7 @@ export class AutoModeService {

public disableAutoModeDueToManualChange() {
if (this.isAutoMode) {
this.platform.log.info('Manual change detected. Disabling Auto Mode.');
this.platform.log.info('Manual Change Detected: Auto Mode Disabled');
this.isAutoMode = false;
this.service.updateCharacteristic(this.platform.Characteristic.On, this.isAutoMode);
this.stopAutoUpdate();
Expand All @@ -75,7 +75,7 @@ export class AutoModeService {
if (this.autoUpdateInterval) {
clearInterval(this.autoUpdateInterval);
this.autoUpdateInterval = null;
this.platform.log.debug('Auto update stopped');
this.platform.log.debug('Auto Update Stopped');
}
}

Expand All @@ -85,9 +85,13 @@ export class AutoModeService {
}
const currentTemp = this.lightService.getCurrentTemp();
const targetTemp = await this.temperatureCalculator.calculateIdealTemp();
this.platform.log.info(`Auto Mode Update - Current Temp: ${currentTemp}K, Target Temp: ${targetTemp}K`);

this.platform.log.info('Auto Mode Update');
this.platform.log.info(`Current Temp: ${currentTemp}K`);
this.platform.log.info(`Target Temp: ${targetTemp}K`);
this.platform.log.info('----------------------------------------');

if (currentTemp !== targetTemp) {
this.platform.log.info(`Updating temperature from ${currentTemp}K to ${targetTemp}K`);
await this.lightService.updateTemperature(targetTemp);
} else {
this.platform.log.debug(`Temperature remains unchanged at ${currentTemp}K`);
Expand Down
23 changes: 16 additions & 7 deletions src/light-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class LightService {

async setOn(value: CharacteristicValue) {
this.isOn = value as boolean;
this.platform.log.info('Set Characteristic On ->', value);
this.platform.log.info('Set Characteristic On:', value);
this.updateHomeKitCharacteristics();

if (this.isOn) {
Expand All @@ -64,7 +64,13 @@ export class LightService {
async setBrightness(value: CharacteristicValue) {
const sliderPosition = value as number;
const newTemp = this.getKelvinFromSliderPosition(sliderPosition);
this.platform.log.info(`Brightness slider set to ${sliderPosition}%, corresponding to ${newTemp}K`);

this.platform.log.info('----------------------------------------');
this.platform.log.info('Manual Update');
this.platform.log.info(`Brightness Slider: ${sliderPosition}%`);
this.platform.log.info(`Target Temp: ${newTemp}K`);
this.platform.log.info('----------------------------------------');

this.onManualChange();
this.debounceUpdate(() => this.updateLights(newTemp));
}
Expand All @@ -73,7 +79,13 @@ export class LightService {
const mired = value as number;
const kelvin = miredToKelvin(mired);
const sliderPosition = this.getSliderPosition(kelvin);
this.platform.log.info(`Color temperature set to ${kelvin}K, corresponding to ${sliderPosition}% on brightness slider`);

this.platform.log.info('----------------------------------------');
this.platform.log.info('Manual Update');
this.platform.log.info(`Color Slider: ${kelvin}K`);
this.platform.log.info(`Slider Position: ${sliderPosition}%`);
this.platform.log.info('----------------------------------------');

this.onManualChange();
this.debounceUpdate(() => this.updateLights(kelvin));
}
Expand All @@ -84,7 +96,7 @@ export class LightService {

async updateTemperature(newTemp: number) {
const sliderPosition = this.getSliderPosition(newTemp);
this.platform.log.info(`Updating temperature from ${this.currentTemp}K to ${newTemp}K (${sliderPosition}% on brightness slider)`);
this.platform.log.info(`Brightness Slider: ${sliderPosition}%`);
this.currentTemp = newTemp;
this.updateHomeKitCharacteristics();
this.updateBrightnessBasedOnTemperature();
Expand All @@ -93,7 +105,6 @@ export class LightService {

async updateBrightnessBasedOnTemperature() {
const sliderPosition = this.getSliderPosition();
this.platform.log.info(`Updating brightness slider to ${sliderPosition}% based on current temperature ${this.currentTemp}K`);
this.service.updateCharacteristic(this.platform.Characteristic.Brightness, sliderPosition);
this.platform.log.debug(`Brightness characteristic value after update: ${this.service.getCharacteristic(this.platform.Characteristic.Brightness).value}`);
}
Expand Down Expand Up @@ -144,9 +155,7 @@ export class LightService {
this.isUpdating = true;

try {
this.platform.log.info(`Updating lights to ${this.currentTemp}K`);
await this.queueProcessor.updateLightsColor(this.currentTemp);
this.platform.log.info(`Lights updated to ${this.currentTemp}K`);
this.updateHomeKitCharacteristics();
} catch (error) {
this.platform.log.error('Error updating lights:', error);
Expand Down
8 changes: 4 additions & 4 deletions src/queue-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ export class QueueProcessor {
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
});
if (response.status === 200) {
this.log.info(`${lightName} has been updated to ${kelvin}K`);
this.log.info(`${lightName}: ${kelvin}K`);
} else {
this.log.error(`Failed to change ${lightName}: ${response.status}`);
this.log.error(`Failed to Change ${lightName}: ${response.status}`);
}
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 429) {
this.log.warn(`Rate limit hit for ${lightName}, re-queueing`);
this.log.warn(`Rate Limit Hit for ${lightName}, Re-Queueing`);
this.queueLightUpdate(lightId, lightName, kelvin);
} else if (error instanceof Error) {
this.log.error(`Error updating ${lightName}: ${error.message}`);
this.log.error(`Error Updating ${lightName}: ${error.message}`);
} else {
this.log.error(`Unknown error updating ${lightName}`);
}
Expand Down
16 changes: 11 additions & 5 deletions src/temperature-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@ export class TemperatureCalculator {
const solarNoon = times.solarNoon;
const maxSunPos = SunCalc.getPosition(solarNoon, this.latitude, this.longitude);
const maxAltitude = maxSunPos.altitude;
const maxAltitudeDegrees = (maxAltitude * 180) / Math.PI;

// Current altitude in radians
const currentAltitude = sunPos.altitude;
const currentAltitudeDegrees = (currentAltitude * 180) / Math.PI;

// Before sunrise or after sunset
if (currentAltitude <= 0) {
this.log.info(`Night time, altitude: ${currentAltitude.toFixed(4)}, factor: 0`);
this.log.info('----------------------------------------');
this.log.info(`Night Time: ${now.toLocaleTimeString()}`);
this.log.info(`Solar Noon: ${solarNoon.toLocaleTimeString()}`);
this.log.info(`Current Altitude: ${currentAltitudeDegrees.toFixed(2)}°`);
this.log.info(`Max Altitude: ${maxAltitudeDegrees.toFixed(2)}°`);
this.log.info('Transition Factor: 0');
this.log.info('----------------------------------------');

return 0;
}

Expand All @@ -60,11 +69,8 @@ export class TemperatureCalculator {
// Ensure we don't exceed 1.0
transitionFactor = Math.min(1, transitionFactor);

const currentAltitudeDegrees = (currentAltitude * 180) / Math.PI;
const maxAltitudeDegrees = (maxAltitude * 180) / Math.PI;

this.log.info('----------------------------------------');
this.log.info(`Time: ${now.toLocaleTimeString()}`);
this.log.info(`Day Time: ${now.toLocaleTimeString()}`);
this.log.info(`Solar Noon: ${solarNoon.toLocaleTimeString()}`);
this.log.info(`Current Altitude: ${currentAltitudeDegrees.toFixed(2)}°`);
this.log.info(`Max Altitude: ${maxAltitudeDegrees.toFixed(2)}°`);
Expand Down

0 comments on commit 725bf9e

Please sign in to comment.