Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdaptiveLightingController fix & improvement #1038

Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 44 additions & 22 deletions src/lib/controller/AdaptiveLightingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@ export const enum AdaptiveLightingControllerEvents {
DISABLED = "disable",
}

/**
* see {@link ActiveAdaptiveLightingTransition}.
*/
export interface AdaptiveLightingControllerUpdate {
transitionStartMillis: number;
timeMillisOffset: number;
transitionCurve: AdaptiveLightingTransitionCurveEntry[];
brightnessAdjustmentRange: BrightnessAdjustmentMultiplierRange;
updateInterval: number,
notifyIntervalThreshold: number;
}

/**
* @group Adaptive Lighting
*/
Expand All @@ -325,7 +337,7 @@ export declare interface AdaptiveLightingController {
* @param event
* @param listener
*/
on(event: "update", listener: () => void): this;
on(event: "update", listener: (update: AdaptiveLightingControllerUpdate) => void): this;
/**
* See {@link AdaptiveLightingControllerEvents.DISABLED}
*
Expand All @@ -334,7 +346,7 @@ export declare interface AdaptiveLightingController {
*/
on(event: "disable", listener: () => void): this;

emit(event: "update"): boolean;
emit(event: "update", update: AdaptiveLightingControllerUpdate): boolean;
emit(event: "disable"): boolean;
}

Expand Down Expand Up @@ -526,15 +538,10 @@ export class AdaptiveLightingController
}

if (this.activeTransition) {
this.colorTemperatureCharacteristic!.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener);
this.brightnessCharacteristic!.removeListener(CharacteristicEventTypes.CHANGE, this.adjustmentFactorChangedListener);

if (this.hueCharacteristic) {
this.hueCharacteristic.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener);
}
if (this.saturationCharacteristic) {
this.saturationCharacteristic.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener);
}
this.colorTemperatureCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener);
this.brightnessCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.adjustmentFactorChangedListener);
this.hueCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener);
this.saturationCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener);

this.activeTransition = undefined;

Expand All @@ -554,7 +561,7 @@ export class AdaptiveLightingController

this.didRunFirstInitializationStep = false;

this.activeTransitionCount!.sendEventNotification(0);
this.activeTransitionCount?.sendEventNotification(0);

debug("[%s] Disabling adaptive lighting", this.lightbulb.displayName);
}
Expand Down Expand Up @@ -631,16 +638,31 @@ export class AdaptiveLightingController
// ----------- PUBLIC API END -----------

private handleActiveTransitionUpdated(calledFromDeserializer = false): void {
if (!calledFromDeserializer) {
this.activeTransitionCount!.sendEventNotification(1);
} else {
this.activeTransitionCount!.value = 1;
if (this.activeTransitionCount) {
if (!calledFromDeserializer) {
this.activeTransitionCount.sendEventNotification(1);
} else {
this.activeTransitionCount.value = 1;
}
}

if (this.mode === AdaptiveLightingControllerMode.AUTOMATIC) {
this.scheduleNextUpdate();
} else if (this.mode === AdaptiveLightingControllerMode.MANUAL) {
this.emit(AdaptiveLightingControllerEvents.UPDATE);
if (!this.activeTransition) {
throw new Error("There is no active transition!");
}

const update: AdaptiveLightingControllerUpdate = {
transitionStartMillis: this.activeTransition.transitionStartMillis,
timeMillisOffset: this.activeTransition.timeMillisOffset,
transitionCurve: this.activeTransition.transitionCurve,
brightnessAdjustmentRange: this.activeTransition.brightnessAdjustmentRange,
updateInterval: this.activeTransition.updateInterval,
notifyIntervalThreshold: this.activeTransition.notifyIntervalThreshold,
}

this.emit(AdaptiveLightingControllerEvents.UPDATE, update);
} else {
throw new Error("Unsupported adaptive lighting controller mode: " + this.mode);
}
Expand Down Expand Up @@ -833,7 +855,7 @@ export class AdaptiveLightingController
this.activeTransition.brightnessAdjustmentRange.minBrightnessValue,
Math.min(
this.activeTransition.brightnessAdjustmentRange.maxBrightnessValue,
this.brightnessCharacteristic!.value as number, // get handler is not called for optimal performance
this.brightnessCharacteristic?.value as number, // get handler is not called for optimal performance
),
);

Expand Down Expand Up @@ -877,7 +899,7 @@ export class AdaptiveLightingController
this.hueCharacteristic.value = color.hue;
}

this.colorTemperatureCharacteristic!.handleSetRequest(temperature, undefined, context).catch(reason => { // reason is HAPStatus code
this.colorTemperatureCharacteristic?.handleSetRequest(temperature, undefined, context).catch(reason => { // reason is HAPStatus code
debug("[%s] Failed to next adaptive lighting transition point: %d", this.lightbulb.displayName, reason);
});

Expand All @@ -898,7 +920,7 @@ export class AdaptiveLightingController
};

if (this.lastNotifiedTemperatureValue !== temperature) {
this.colorTemperatureCharacteristic!.sendEventNotification(temperature, eventContext);
this.colorTemperatureCharacteristic?.sendEventNotification(temperature, eventContext);
this.lastNotifiedTemperatureValue = temperature;
}
if (this.saturationCharacteristic && this.lastNotifiedSaturationValue !== color.saturation) {
Expand Down Expand Up @@ -1023,8 +1045,8 @@ export class AdaptiveLightingController
}

private handleSupportedTransitionConfigurationRead(): string {
const brightnessIID = this.lightbulb!.getCharacteristic(Characteristic.Brightness).iid;
const temperatureIID = this.lightbulb!.getCharacteristic(Characteristic.ColorTemperature).iid;
const brightnessIID = this.lightbulb?.getCharacteristic(Characteristic.Brightness).iid;
const temperatureIID = this.lightbulb?.getCharacteristic(Characteristic.ColorTemperature).iid;
assert(brightnessIID, "iid for brightness characteristic is undefined");
assert(temperatureIID, "iid for temperature characteristic is undefined");

Expand Down
Loading