deploy: change method to get status from device
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
import { httpRequest } from '~/helpers/http/axios'
|
||||
import { httpConfig } from '~/config'
|
||||
import logger from '~/plugins/logger'
|
||||
|
||||
export async function sendCommandToThirdParty(topicData: string, payload: string): Promise<void> {
|
||||
try {
|
||||
const response = await httpRequest({
|
||||
const response = await fetch(`${httpConfig.baseUrl}/command`, {
|
||||
method: 'POST',
|
||||
url: '/command',
|
||||
body: {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
topic: topicData,
|
||||
payload,
|
||||
},
|
||||
}),
|
||||
})
|
||||
|
||||
logger.info({ msg: 'Third-party command sent successfully', topic: topicData, status: response.status })
|
||||
} catch (error) {
|
||||
logger.error({ msg: 'Failed to send command to third-party service', topic: topicData, error: (error as Error).message })
|
||||
|
||||
@@ -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'])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user