From f56f87aa18bd56f9b4014b3cb8e74c08ca0b2981 Mon Sep 17 00:00:00 2001 From: Ian Mustafa Date: Mon, 19 Jan 2026 22:35:10 +0700 Subject: [PATCH] Add handling to differentiate between switching AC power and set its temperature --- src/helpers/utils.ts | 18 ++++++++++++++---- src/modules/device/commands/service.ts | 6 +++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 9546b32..34fb8bb 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -5,22 +5,32 @@ export const passwordVerify = (password: string, hash: string) => Bun.password.v export const dataMapping = [ { deviceName: 'AC', + deviceType: 'S', command: 'AC=', }, + { + deviceName: 'AC', + deviceType: 'A', + command: 'Temp=', + }, { deviceName: 'L', + deviceType: 'S', command: 'Light=', }, { deviceName: 'DL', + deviceType: 'S', command: 'Doorlock=', }, { deviceName: 'BL', - command: 'Blind=', - }, - { - deviceName: 'BL', + deviceType: 'S', command: 'Blind=', }, ] + +export const getDeviceCommand = (deviceName: string, deviceType: string): string => { + const device = dataMapping.find((item) => item.deviceName === deviceName && item.deviceType === deviceType) + return device?.command || '' +} diff --git a/src/modules/device/commands/service.ts b/src/modules/device/commands/service.ts index 871b864..e0a8516 100644 --- a/src/modules/device/commands/service.ts +++ b/src/modules/device/commands/service.ts @@ -1,4 +1,4 @@ -import { dataMapping } from '~/helpers/utils' +import { getDeviceCommand } from '~/helpers/utils' import { DeviceCommandBody } from '../schema' import { sendCommandToThirdParty } from './command' import { alphabetToNumberString } from '~/helpers/alphabet_conversion' @@ -11,8 +11,8 @@ export default abstract class CommandService { }> { const unitNumber = alphabetToNumberString(body.unitNumber) const topicData = `${body.merchantName}/${body.floorName}_${unitNumber}-${body.deviceName}-${body.roomName}-${body.deviceType}-${body.commandType}-${body.towerNumber}` - const cmd = dataMapping.find((device) => device.deviceName == body.deviceName) - const request = await sendCommandToThirdParty(topicData, `${cmd?.command}${body.payload.action}`) + const cmd = getDeviceCommand(body.deviceName, body.deviceType) + const request = await sendCommandToThirdParty(topicData, `${cmd}${body.payload.action}`) if (request.status !== 200 || request.data.status == 400) { return { status: 'error' as const,