From 857c62ce375f593f67529db4971f24b8a91a10a4 Mon Sep 17 00:00:00 2001 From: Igor Panteleyev Date: Mon, 29 Jul 2024 17:57:09 +0300 Subject: [PATCH] Separate data builder and QRcode generator --- src/generator.ts | 34 ++++++++++++ src/models/data-builder.ts | 0 src/models/generator.ts | 108 ------------------------------------- 3 files changed, 34 insertions(+), 108 deletions(-) create mode 100644 src/generator.ts create mode 100644 src/models/data-builder.ts delete mode 100644 src/models/generator.ts diff --git a/src/generator.ts b/src/generator.ts new file mode 100644 index 0000000..638837a --- /dev/null +++ b/src/generator.ts @@ -0,0 +1,34 @@ +import QRCode from "qrcode"; + +import { DataUrl } from "../types/types"; +import { TranslatableError } from "./error"; + + +class QRCodeGenerator { + + protected readonly inputString: string; + + // TODO: make it configurable + protected readonly quality = { + margin: 1, + width: 500 + } + + public constructor(inputString: string) { + this.inputString = inputString; + } + + public async generate(): Promise { + try { + return QRCode.toDataURL(this.inputString, this.quality); + } + catch (e: unknown) { + throw new TranslatableError("generation.unknown_error") + } + } + +} + +export async function generateQR(inputString: string): Promise { + return await (new QRCodeGenerator(inputString)).generate(); +} diff --git a/src/models/data-builder.ts b/src/models/data-builder.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/models/generator.ts b/src/models/generator.ts deleted file mode 100644 index dd15adc..0000000 --- a/src/models/generator.ts +++ /dev/null @@ -1,108 +0,0 @@ -import QRCode from "qrcode"; - -import { - DataUrl, EntitySourceConfig, - QRCodeCardConfig, - QRCodeGeneratorClass, - TextSourceConfig, - WiFiSourceConfig, -} from "../types/types"; -import { isPasswordProtected } from "./authentication-type"; -import { SourceType } from "./source-type"; -import { getValueFromConfig } from "../utils" -import { HomeAssistant } from "custom-card-helpers"; -import { TranslatableError } from "./error"; - - -abstract class QRCodeGenerator { - - protected readonly config: T; - protected readonly hass: HomeAssistant; - - // TODO: make it configurable - protected readonly quality = { - margin: 1, - width: 500 - } - - public constructor(hass: HomeAssistant, config: T) { - this.hass = hass; - this.config = config; - } - - public async generate(): Promise { - try { - return QRCode.toDataURL(this.input, this.quality); - } - catch (e: unknown) { - if (e instanceof TranslatableError) { - throw e - } else if (e instanceof Error) { - throw new TranslatableError(["generation.error", "{message}", e.message]) - } - throw new TranslatableError("generation.unknown_error") - } - } - - protected abstract get input(): string - -} - - -class TextQRCodeGenerator extends QRCodeGenerator { - - protected get input(): string { - return this.config.text || ""; - } -} - - -class WiFiQRCodeGenerator extends QRCodeGenerator { - protected readonly special_chars = ['\\', ';', ',', '"', ':'] - - protected _escape(plain: string): string { - return this.special_chars.reduce( - (previousValue, currentValue) => { - return previousValue.replace(currentValue, '\\'+currentValue) - }, - plain - ); - } - - protected get input(): string { - const ssid = getValueFromConfig(this.hass, this.config, "ssid"); - let text = `WIFI:T:${this.config.auth_type || ""};S:${this._escape(ssid)};`; - - if (isPasswordProtected(this.config.auth_type)) { - const password = getValueFromConfig(this.hass, this.config, "password"); - text += `P:${this._escape(password)};` - } - - if (!this.config.is_hidden) { - text += "H:true" - } - - return text; - } -} - -class EntityQRCodeGenerator extends QRCodeGenerator { - protected get input(): string { - return getValueFromConfig(this.hass, this.config, "entity") - } -} - -const generatorMap = new Map>>([ - [SourceType.TEXT, TextQRCodeGenerator], - [SourceType.WIFI, WiFiQRCodeGenerator], - [SourceType.ENTITY, EntityQRCodeGenerator] -]); - - -export async function generateQR(hass: HomeAssistant, config: QRCodeCardConfig): Promise { - const generatorCls = generatorMap.get(config.source); - - if (!generatorCls) throw new TranslatableError("validation.source.invalid"); - - return await (new generatorCls(hass, config)).generate(); -}