Skip to content

Commit

Permalink
fix(effects): prevent 4xx errors for lights with wrong handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoronex committed Feb 3, 2025
1 parent 159bb12 commit 84ba71b
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/stores/effects-controller.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
updateLightsEffectColorColors,
updatePredefinedLightsEffect,
} from '@/api';
import { useHandlersStore } from '@/stores/handlers.store';

interface PushedEffect {
timestamp: Date;
Expand Down Expand Up @@ -77,8 +78,16 @@ export const useEffectsControllerStore = defineStore('effectsController', {
resetLightsGroupSelection() {
this.selectedLightsGroupIds = [];
},
lightsGroupHasSetEffectsHandler(id: number) {
const store = useHandlersStore();
const registeredLights = store.getRegisteredLights('SetEffectsHandler');
const ids = registeredLights.map((l) => l.id);
return ids.includes(id);
},
setColorEffect(effect: LightsEffectsColorCreateParams, lightGroupIds?: number[]) {
const ids = lightGroupIds ?? this.selectedLightsGroupIds;
let ids = lightGroupIds ?? this.selectedLightsGroupIds;
// Only send request for lights groups that have the correct handler (prevents 4xx errors)
ids = ids.filter(this.lightsGroupHasSetEffectsHandler);
ids.map(async (id) => {
await applyLightsEffectColor({
body: [effect],
Expand All @@ -93,7 +102,9 @@ export const useEffectsControllerStore = defineStore('effectsController', {
this.pastPushedEffects = [pastEffect, ...this.pastPushedEffects.slice(0, 9)];
},
setMovementEffect(effect: LightsEffectsMovementCreateParams, lightGroupIds?: number[]) {
const ids = lightGroupIds ?? this.selectedLightsGroupIds;
let ids = lightGroupIds ?? this.selectedLightsGroupIds;
// Only send request for lights groups that have the correct handler (prevents 4xx errors)
ids = ids.filter(this.lightsGroupHasSetEffectsHandler);
ids.map(async (id) => {
await applyLightsEffectMovement({
body: [effect],
Expand All @@ -108,7 +119,9 @@ export const useEffectsControllerStore = defineStore('effectsController', {
this.pastPushedEffects = [pastEffect, ...this.pastPushedEffects.slice(0, 9)];
},
async enableStrobe(lightGroupIds?: number[]) {
const ids = lightGroupIds ?? this.selectedLightsGroupIds;
let ids = lightGroupIds ?? this.selectedLightsGroupIds;
// Only send request for lights groups that have the correct handler (prevents 4xx errors)
ids = ids.filter(this.lightsGroupHasSetEffectsHandler);
await Promise.all(
ids.map((id) => {
return enableStrobeOnLightsGroup({
Expand All @@ -118,7 +131,9 @@ export const useEffectsControllerStore = defineStore('effectsController', {
);
},
async disableStrobe(lightGroupIds?: number[]) {
const ids = lightGroupIds ?? this.selectedLightsGroupIds;
let ids = lightGroupIds ?? this.selectedLightsGroupIds;
// Only send request for lights groups that have the correct handler (prevents 4xx errors)
ids = ids.filter(this.lightsGroupHasSetEffectsHandler);
await Promise.all(
ids.map((id) => {
return disableStrobeOnLightsGroup({
Expand All @@ -132,7 +147,9 @@ export const useEffectsControllerStore = defineStore('effectsController', {
* Uses "selectedLightsGroupIds if none given"
*/
async disableLightsColors(lightGroupIds?: number[]) {
const ids = lightGroupIds ?? this.selectedLightsGroupIds;
let ids = lightGroupIds ?? this.selectedLightsGroupIds;
// Only send request for lights groups that have the correct handler (prevents 4xx errors)
ids = ids.filter(this.lightsGroupHasSetEffectsHandler);
await Promise.all(
ids.map((id) => {
return applyLightsEffectColor({
Expand All @@ -147,7 +164,9 @@ export const useEffectsControllerStore = defineStore('effectsController', {
* Uses "selectedLightsGroupIds if none given"
*/
async disableLightsMovement(lightGroupIds?: number[]) {
const ids = lightGroupIds ?? this.selectedLightsGroupIds;
let ids = lightGroupIds ?? this.selectedLightsGroupIds;
// Only send request for lights groups that have the correct handler (prevents 4xx errors)
ids = ids.filter(this.lightsGroupHasSetEffectsHandler);
await Promise.all(
ids.map((id) => {
return applyLightsEffectMovement({
Expand Down Expand Up @@ -248,10 +267,16 @@ export const useEffectsControllerStore = defineStore('effectsController', {
case 'LightsButtonColors': {
const properties = button.properties as LightsButtonColors;
this.currentColors = properties.colors;
const promises = button.properties.lightsGroupIds?.map(async (id) => {
await updateLightsEffectColorColors({ path: { id }, body: { colors: properties.colors } });
});
if (promises) await Promise.all(promises);

let ids = button.properties.lightsGroupIds || [];
// Only send request for lights groups that have the correct handler (prevents 4xx errors)
ids = ids.filter(this.lightsGroupHasSetEffectsHandler);
await Promise.all(
ids.map(async (id) => {
await updateLightsEffectColorColors({ path: { id }, body: { colors: properties.colors } });
}),
);

return;
}
case 'LightsButtonEffectColor': {
Expand Down

0 comments on commit 84ba71b

Please sign in to comment.