index.ts 4.99 KB
import type { DeviceActiveType, DeviceAttributeItemType, DeviceItemType, DeviceProfileItemType, OrganizationItemType, ProductsDetailWithThingsModelType, RpcCommandType, SendValue, ThingsModel, ThingsModelItemType } from './model'
import { CommandWayEnum } from '@/enums/commandEnum'
import type { DeviceTypeEnum } from '@/enums/deviceEnum'
import { FunctionTypeEnum } from '@/enums/objectModelEnum'
import { isShareMode } from '@/utils/env'
import { defHttp } from '@/utils/http'

enum Api {
  GET_DEVICE_PROFILE = '/device_profile/me/list',
  GET_ORG = '/organization/me/list',
  GET_DEVICE_LIST = '/device/list',
  GET_DEVICE_ATTRIBUTES = '/device/attributes/',
  GET_DEVICE_BY_DEVICE_PROFILED_IDS = '/device/getListByDeviceProfileIds',
  GET_THINGS_MODEL_SERVICES = '/things_model/get_services/',

  RPC_COMMAND = '/rpc',
  GET_DEVICE_ACTIVE = '/plugins/telemetry/DEVICE/',
  RPC_ONEWAY = '/rpc/oneway',
  RPC_TWOWAY = '/rpc/twoway',

  GEN_MODBUS_COMMAND = '/js/modbus',
  GET_DEVICE_DETAIL = '/device/', // 获取设备详情

  GET_LIST_BY_CONFIGURATION_ID = '/configuration/center/getListByConfigurationId',

  GET_PRODUCTS_DETAIL_WITH_THINGS_MODEL = '/things_model/batch/get_tsl',

}

export interface GenModbusCommandType {
  crc: string
  deviceCode: string
  method: string
  registerAddress: number
  registerNumber?: number
  registerValues?: number[]
  hexByteOrderEnum?: string
}

export const getDeviceProfile = (deviceType?: DeviceTypeEnum) => {
  return defHttp.get<DeviceProfileItemType[]>({
    url: Api.GET_DEVICE_PROFILE,
    params: { deviceType },
  })
}

export const getOrganization = (organizationId?: string) => {
  return defHttp.get<OrganizationItemType[]>({
    url: Api.GET_ORG,
    params: { organizationId },
  })
}

export const getDevice = ({ deviceProfileId, deviceType, organizationId }: { deviceType?: DeviceTypeEnum; deviceProfileId?: string; organizationId?: string }) => {
  return defHttp.get<DeviceItemType[]>({
    url: Api.GET_DEVICE_LIST,
    params: { deviceProfileId, deviceType, organizationId },
  })
}

export const getDeviceAttributes = (deviceProfileId: string) => {
  return defHttp.get<ThingsModelItemType[]>({
    url: `${Api.GET_DEVICE_ATTRIBUTES}${deviceProfileId}`,

  })
}

export const getListByDeviceProfileIds = ({ deviceProfileIds, organizationId }: Recordable) => {
  return defHttp.post<DeviceItemType[]>({
    url: `${Api.GET_DEVICE_BY_DEVICE_PROFILED_IDS}?organizationId=${organizationId}`,
    data: deviceProfileIds,
  })
}

export const getThingsModelServices = (deviceProfileId: string) => {
  return defHttp.get<ThingsModel>({
    url: `${Api.GET_THINGS_MODEL_SERVICES}${deviceProfileId}`,
  })
}

export const doCommandDelivery = ({ way, deviceId, command }: { way: CommandWayEnum; deviceId: string; command: RpcCommandType }) => {
  return defHttp.post<Record<'rpcId', string>>({
    url: `${Api.RPC_COMMAND}/${way}/${deviceId}`,
    data: command,
  },
  {
    joinPrefix: false,
  },
  )
}

export const getDeviceActive = (deviceId: string) => {
  return defHttp.get<DeviceAttributeItemType[]>({
    url: `${Api.GET_DEVICE_ACTIVE}/${deviceId}/values/attributes?keys=active`,
  }, { joinPrefix: false })
}

// 命令下发
export const sendRpcOneway = (param: SendValue | any, deviceId?: string, way?: string) => {
  return defHttp.post({
    url: `${way !== CommandWayEnum.TWO_WAY ? Api.RPC_ONEWAY : Api.RPC_TWOWAY}/${deviceId}`,
    data: param,
  },
  {
    joinPrefix: false,
  })
}

/**
 * @description 生成modbus指令
 * @param data
 * @returns {string}
 */
export const genModbusCommand = (data: GenModbusCommandType) => {
  return defHttp.post<string>({
    url: Api.GEN_MODBUS_COMMAND,
    data,
  })
}

// 获取设备状态,在线 or 离线时间  entityId:设备id
export const getDeviceActiveTime = (entityId: string) => {
  return defHttp.get<DeviceActiveType[]>(
    {
      url: `/plugins/telemetry/DEVICE/${entityId}/values/attributes?keys=active`,
    },
    {
      joinPrefix: false,
    },
  )
}

export const getDeviceTelemetryValue = (params: { entityId: string; keys: string }) => {
  return defHttp.get({
    url: `${Api.GET_DEVICE_ACTIVE}${params.entityId}/values/timeseries`,
    params: {
      keys: params.keys,
    },
  }, {
    joinPrefix: false,
  })
}

// 获取设备详情
export const getDeviceInfo = (deviceId: string) => {
  return defHttp.get<DeviceItemType>(
    {
      url: isShareMode() ? `${Api.GET_DEVICE_DETAIL}public/${deviceId}` : `${Api.GET_DEVICE_DETAIL}${deviceId}`,
    },
  )
}

export const getListByConfigurationId = (configurationId: string) => {
  return defHttp.post({
    url: `${Api.GET_LIST_BY_CONFIGURATION_ID}?configurationId=${configurationId}`,
  })
}

export const getProductsDetailWithThingsModel = ({ deviceProfileIds, functionTypeEnum }: { deviceProfileIds: string[]; functionTypeEnum?: FunctionTypeEnum | 'all' }) => {
  return defHttp.post<ProductsDetailWithThingsModelType[]>({
    url: Api.GET_PRODUCTS_DETAIL_WITH_THINGS_MODEL,
    data: {
      deviceProfileIds,
      functionTypeEnum: functionTypeEnum || FunctionTypeEnum.PROPERTIES,
    },
  })
}