feat: integration with API Vega Cloud
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { httpRequest } from '~/helpers/http/axios'
|
||||
import logger from '~/plugins/logger'
|
||||
|
||||
export async function queryDeviceStatus(topic: string): Promise<{
|
||||
floor: string
|
||||
unit: string
|
||||
device: string
|
||||
room: string
|
||||
devType: string
|
||||
code: string
|
||||
tower: string
|
||||
payload: string
|
||||
} | null> {
|
||||
try {
|
||||
const response = await httpRequest({
|
||||
method: 'GET',
|
||||
url: '/state-reply',
|
||||
body: { topic },
|
||||
})
|
||||
|
||||
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 })
|
||||
|
||||
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')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { verify } from '~/helpers/signature'
|
||||
import { DeviceQueryParams } from '../schema'
|
||||
import { queryDeviceStatus } from './query'
|
||||
|
||||
export default abstract class QueryService {
|
||||
static async getDeviceStatus(query: DeviceQueryParams, headers: Record<string, string | undefined>): Promise<{
|
||||
status: 'success'
|
||||
message: 'command accepted'
|
||||
data: {
|
||||
floor: string
|
||||
unit: string
|
||||
device: string
|
||||
room: string
|
||||
devType: string
|
||||
code: string
|
||||
tower: string
|
||||
payload: string
|
||||
}
|
||||
} | null> {
|
||||
const apiKey = headers['x-api-key']
|
||||
|
||||
// For now, use apiKey as secretKey since DB validation is skipped
|
||||
const secretKey = apiKey
|
||||
|
||||
// Remove signature from query for verification
|
||||
const { signature, ...queryWithoutSignature } = query
|
||||
|
||||
const isValid = verify(queryWithoutSignature, signature, secretKey)
|
||||
if (!isValid) {
|
||||
throw new Error('invalid_signature')
|
||||
}
|
||||
|
||||
// TODO: Check nonce for replay attack when DB is implemented
|
||||
|
||||
const topic = `${query.merchantName}/${query.floorName}_${query.unitNumber}-${query.deviceName}-${query.roomName}-${query.deviceType}-${query.commandType}-${query.towerNumber}`
|
||||
|
||||
const deviceData = await queryDeviceStatus(topic)
|
||||
|
||||
if (deviceData) {
|
||||
return {
|
||||
status: 'success',
|
||||
message: 'command accepted',
|
||||
data: deviceData,
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user