deviceManager.ts 4.68 KB
import { defHttp } from '/@/utils/http/axios';
import {
  DeviceModel,
  DeviceProfileModel,
  DeviceProfileQueryParam,
  DeviceQueryParam,
} from '/@/api/device/model/deviceModel';
import { ChildDeviceParams } from './model/deviceModel';
import { PaginationResult } from '/#/axios';
import { AlarmLogItem } from './model/deviceConfigModel';
enum DeviceManagerApi {
  /**
   * 设备URL
   */
  DEVICE_URL = '/device',
  /**
   * 设备配置URL
   */
  DEVICE_PROFILE_URL = '/device_profile',

  DEVICE_PROFILE_URL_ME = '/device_profile/me/list',

  DEVICE_ALARM_URL = '/alarm',

  DEVICE_CREDENTIALS = '/device/credentials',
  COMMAND_ISSUANCE = '/rpc',
}

export const devicePage = (params: DeviceQueryParam) => {
  return defHttp.get<DeviceModel>({
    url: DeviceManagerApi.DEVICE_URL,
    params,
  });
};

/**
 * 分页查询设备配置页面
 * @param params pageSize page name
 */
export const deviceProfilePage = (params: DeviceProfileQueryParam) => {
  return defHttp.get<DeviceProfileModel>({
    url: DeviceManagerApi.DEVICE_PROFILE_URL,
    params,
  });
};
/**
 * 删除设备配置
 * @param ids 删除的ids
 */
export const deleteDeviceProfile = (ids: string[]) => {
  return defHttp.delete({
    url: DeviceManagerApi.DEVICE_PROFILE_URL,
    data: {
      ids: ids,
    },
  });
};

/**
 * 删除设备
 * @param ids 删除的ids
 */
export const deleteDevice = (ids: string[]) => {
  return defHttp.delete({
    url: DeviceManagerApi.DEVICE_URL,
    data: {
      ids: ids,
    },
  });
};

/**
 * 查询设备配置
 * @param params pageSize page name
 */
export const deviceProfile = () => {
  return defHttp.get({
    url: DeviceManagerApi.DEVICE_PROFILE_URL_ME,
  });
};

// 创建或编辑设备
export const createOrEditDevice = (data) => {
  return defHttp.post({
    url: DeviceManagerApi.DEVICE_URL,
    data,
  });
};

// 查询设备详情
export const getDeviceDetail = (id: string) => {
  return defHttp.get({
    url: DeviceManagerApi.DEVICE_URL + `/${id}`,
  });
};

// 查询设备详情中的告警
export const getDeviceAlarm = (params?: DeviceProfileQueryParam) => {
  return defHttp.get<PaginationResult<AlarmLogItem>>({
    url: DeviceManagerApi.DEVICE_ALARM_URL,
    params,
  });
};

// 如果是flag为true 就是清除报警,否则是处理报警
export const clearOrAckAlarm = (id: string, flag: boolean) => {
  return defHttp.post(
    {
      url: `${DeviceManagerApi.DEVICE_ALARM_URL}/${id}/${flag ? 'clear' : 'ack'}`,
    },
    {
      joinPrefix: false,
    }
  );
};

// 获取设备凭证详情
export const getDeviceToken = (id: string) => {
  return defHttp.get(
    {
      url: `${DeviceManagerApi.DEVICE_URL}/${id}/credentials`,
    },
    {
      joinPrefix: false,
    }
  );
};

// 保存设备凭证详情

export const saveDeviceToken = (data) => {
  return defHttp.post(
    {
      url: DeviceManagerApi.DEVICE_CREDENTIALS,
      data,
    },
    {
      joinPrefix: false,
    }
  );
};

// 获取当前租户下的客户列表
export const getCustomerList = (params) => {
  return defHttp.get({
    url: '/user/customers/' + params.organizationId,
  });
};

// 分配客户
export const dispatchCustomer = (data) => {
  const { customerId, tbDeviceId } = data;
  return defHttp.post(
    {
      url: `/customer/${customerId}/device/${tbDeviceId}`,
    },
    {
      joinPrefix: false,
    }
  );
};
// 检查设备是否被占用
export const checkDeviceOccupied = (id) => {
  return defHttp.get({
    url: `/device/used/${id}`,
  });
};

// 取消分配客户
export const cancelDispatchCustomer = (data) => {
  const { tbDeviceId } = data;
  return defHttp.delete(
    {
      url: `/customer/device/${tbDeviceId}`,
    },
    {
      joinPrefix: false,
    }
  );
};

// 获取组织下的的所有网关设备
export const getGATEWAYdevice = async (params: { organization: string }) => {
  const res = await defHttp.get({
    url: `/device/list/GATEWAY`,
    params,
  });
  return Promise.resolve<{ label: string; value: string }[]>(
    res.map((item) => ({ label: item.name, value: item.id }))
  );
};

// 获取网关设备
export const getGATEWAY = (tbDeviceId: string) => {
  return defHttp.get({
    url: '/device/gateway/' + tbDeviceId,
  });
};

// 获取子设备的分页
export const getChildDevicePage = (params: ChildDeviceParams) => {
  return defHttp.get({
    url: '/device/relation',
    params,
  });
};

export const generateSNCode = () => {
  return defHttp.get({
    url: '/device/sn',
  });
};

//命令下发
export const commandIssuanceApi = (type, tbDeviceId, data) => {
  const T = type === 'OneWay' ? 'oneway' : 'twoway';
  return defHttp.post(
    {
      url: DeviceManagerApi.COMMAND_ISSUANCE + '/' + T + '/' + tbDeviceId,
      data,
    },
    {
      joinPrefix: false,
    }
  );
};