-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(effects-controller): add support for simple effects controller w…
…ith preconfigured buttons (#40) * chore(lights): make pattern enum string-based * chore(lights): rename predefined effect to track effect * feat(effects-controller): add endpoints for effect controller buttons * fix(effects-controller): small issues and mistakes * feat(lights-controller): add reset button * feat(lights-effects): allow changing colors mid-effect * fix(lights-effects): deadlock when passing undefined as color
- Loading branch information
Showing
25 changed files
with
427 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { Repository } from 'typeorm'; | ||
import LightsPredefinedEffect, { | ||
LightsPredefinedEffectProperties, | ||
} from '../../lights/entities/scenes/lights-predefined-effect'; | ||
import dataSource from '../../../database'; | ||
import { HttpApiException } from '../../../helpers/custom-error'; | ||
import { HttpStatusCode } from 'axios'; | ||
|
||
export interface LightsPredefinedEffectResponse { | ||
id: number; | ||
createdAt: string; | ||
updatedAt: string; | ||
buttonId: number; | ||
icon?: string | null; | ||
name?: string | null; | ||
properties: LightsPredefinedEffectProperties; | ||
} | ||
|
||
export interface LightsPredefinedEffectCreateParams | ||
extends Pick<LightsPredefinedEffect, 'buttonId' | 'properties' | 'icon' | 'name'> {} | ||
|
||
export interface LightsPredefinedEffectUpdateParams | ||
extends Partial<LightsPredefinedEffectCreateParams> {} | ||
|
||
export default class SetEffectsService { | ||
private repo: Repository<LightsPredefinedEffect>; | ||
|
||
constructor(repo?: Repository<LightsPredefinedEffect>) { | ||
this.repo = repo ?? dataSource.getRepository(LightsPredefinedEffect); | ||
} | ||
|
||
public static toLightsEffectPredefinedEffectResponse( | ||
e: LightsPredefinedEffect, | ||
): LightsPredefinedEffectResponse { | ||
return { | ||
id: e.id, | ||
createdAt: e.createdAt.toISOString(), | ||
updatedAt: e.updatedAt.toISOString(), | ||
buttonId: e.buttonId, | ||
name: e.name, | ||
icon: e.icon, | ||
properties: e.properties, | ||
}; | ||
} | ||
|
||
public async getAllPredefinedEffects(): Promise<LightsPredefinedEffect[]> { | ||
return this.repo.find(); | ||
} | ||
|
||
public async getSinglePredefinedEffect({ | ||
id, | ||
buttonId, | ||
}: { | ||
id?: number; | ||
buttonId?: number; | ||
}): Promise<LightsPredefinedEffect | null> { | ||
return this.repo.findOne({ where: { id, buttonId } }); | ||
} | ||
|
||
public async createPredefinedEffect( | ||
params: LightsPredefinedEffectCreateParams, | ||
): Promise<LightsPredefinedEffect> { | ||
const existing = await this.getSinglePredefinedEffect({ buttonId: params.buttonId }); | ||
if (existing) { | ||
throw new HttpApiException( | ||
HttpStatusCode.BadRequest, | ||
`Effect with button ID "${params.buttonId}" already exists.`, | ||
); | ||
} | ||
|
||
return this.repo.save(params); | ||
} | ||
|
||
public async updatePredefinedEffect( | ||
id: number, | ||
params: LightsPredefinedEffectUpdateParams, | ||
): Promise<LightsPredefinedEffect> { | ||
const existing = await this.getSinglePredefinedEffect({ id }); | ||
if (!existing) { | ||
throw new HttpApiException(HttpStatusCode.NotFound, `Effect with ID "${id}" not found.`); | ||
} | ||
|
||
// New button ID | ||
if (params.buttonId !== undefined && existing.buttonId !== params.buttonId) { | ||
const buttonMatch = await this.getSinglePredefinedEffect({ buttonId: params.buttonId }); | ||
if (buttonMatch) { | ||
throw new HttpApiException( | ||
HttpStatusCode.BadRequest, | ||
`Effect with button ID "${params.buttonId}" already exists."`, | ||
); | ||
} | ||
existing.buttonId = params.buttonId; | ||
} | ||
|
||
if (params.properties) existing.properties = params.properties; | ||
if (params.name) existing.name = params.name; | ||
if (params.icon) existing.icon = params.icon; | ||
|
||
return this.repo.save(existing); | ||
} | ||
|
||
public async deletePredefinedEffect(id: number): Promise<void> { | ||
await this.repo.delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.