deploy: change method to get status from device

This commit is contained in:
2025-10-15 10:54:05 +07:00
parent c5545ddde2
commit c167f4ec43
6 changed files with 32 additions and 260 deletions
+24 -30
View File
@@ -1,7 +1,7 @@
import { httpRequest } from '~/helpers/http/axios'
import logger from '~/plugins/logger'
import { omit } from 'lodash-es'
import { httpConfig } from '~/config'
export async function queryDeviceStatus(topic: string): Promise<{
interface DeviceStatus {
floor: string
unit: string
device: string
@@ -10,34 +10,28 @@ export async function queryDeviceStatus(topic: string): Promise<{
code: string
tower: string
payload: string
} | null> {
try {
const response = await httpRequest({
method: 'GET',
url: '/state-reply',
body: { topic },
})
topics?: unknown
createAt?: unknown
}
if ((response.data as Record<string, unknown>) && (response.data as Record<string, unknown>).data) {
const thirdPartyData = (response.data as Record<string, unknown>).data as Record<string, unknown>
logger.info({ msg: 'Device status queried successfully', topic, status: response.status })
export async function queryDeviceStatus(topic: string): Promise<Omit<DeviceStatus, 'topics' | 'createAt'> | null> {
const response = await fetch(`${httpConfig.baseUrl}/state-reply`, {
method: 'POST',
body: JSON.stringify({ topic }),
headers: {
'Content-Type': 'application/json',
},
})
return {
floor: thirdPartyData.floor as string,
unit: thirdPartyData.unit as string,
device: thirdPartyData.device as string,
room: thirdPartyData.room as string,
devType: thirdPartyData.devType as string,
code: thirdPartyData.code as string,
tower: thirdPartyData.tower as string,
payload: thirdPartyData.payload as string,
}
} else {
logger.info({ msg: 'Device status not found', topic, status: response.status })
return null
}
} catch (error) {
logger.error({ msg: 'Failed to query device status', topic, error: (error as Error).message })
throw new Error('third_party_query_failed')
if (!response.ok) {
throw new Error('internal_server_error')
}
const json = await response.json()
if (!json || !json.data) {
return null
}
return omit(json.data as DeviceStatus, ['topics', 'createAt'])
}