diff --git a/src/helpers/alphabet_conversion.ts b/src/helpers/alphabet_conversion.ts index 9ed0a1c..00ed9a1 100644 --- a/src/helpers/alphabet_conversion.ts +++ b/src/helpers/alphabet_conversion.ts @@ -32,3 +32,36 @@ export const convertAlphabetString = (input: string): string => { .filter((s) => s !== '') .join('') } + +/** + * Converts a two-digit string number (01-26) into its corresponding alphabet character (A-Z). + * @param numStr The two-digit numeric string to convert. + * @returns The alphabet character, or an empty string if invalid. + */ +export const numberStringToAlphabet = (numStr: string): string => { + if (!numStr || numStr.length !== 2) return '' + + const position = parseInt(numStr, 10) + if (isNaN(position) || position < 1 || position > 26) return '' + + // A is 65 (1 + 64) + return String.fromCharCode(position + 64) +} + +/** + * Converts a string of numbers into a string of alphabet characters. + * Example: "010203" -> "ABC" + * @param input The string of numbers (must be even length). + * @returns The converted alphabet string. + */ +export const convertNumberStringToAlphabet = (input: string): string => { + if (!input || input.length % 2 !== 0) return '' + + const result: string[] = [] + for (let i = 0; i < input.length; i += 2) { + const part = input.substring(i, i + 2) + result.push(numberStringToAlphabet(part)) + } + + return result.join('') +} diff --git a/src/modules/device/queries/service.ts b/src/modules/device/queries/service.ts index 8dd2201..72b3889 100644 --- a/src/modules/device/queries/service.ts +++ b/src/modules/device/queries/service.ts @@ -1,4 +1,4 @@ -import { alphabetToNumberString } from '~/helpers/alphabet_conversion' +import { alphabetToNumberString, convertNumberStringToAlphabet } from '~/helpers/alphabet_conversion' import { sendQueryToThirdParty } from '../commands/command' import { DeviceQueryParams } from '../schema' @@ -39,7 +39,7 @@ export default abstract class QueryService { message: 'command accepted', data: { floorName: request.data.data.floor, - unitNumber: request.data.data.unit, + unitNumber: convertNumberStringToAlphabet(request.data.data.unit), deviceName: request.data.data.device, roomName: request.data.data.room, deviceType: request.data.data.devType,