deviceManager.ts 9.76 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
import { defHttp } from '/@/utils/http/axios';
import {
  DeviceModel,
  DeviceModelOfMatterAttrs,
  DeviceProfileModel,
  DeviceProfileQueryParam,
  DeviceQueryParam,
  DeviceRecord,
  DeviceTypeEnum,
  DeviceStateLogModel,
} from '/@/api/device/model/deviceModel';
import { ChildDeviceParams } from './model/deviceModel';
import { PaginationResult } from '/#/axios';
import { AlarmLogItem } from './model/deviceConfigModel';
import { omit } from 'lodash-es';
import { CommandDeliveryWayEnum } from '/@/enums/deviceEnum';
import { isShareMode } from '/@/views/sys/share/hook';
enum DeviceManagerApi {
  /**
   * 设备URL
   */
  DEVICE_URL = '/device',
  /**
   * 设备配置URL
   */
  DEVICE_PROFILE_URL = '/device_profile',

  DEVICE_PROFILE_URL_ME = '/device_profile/me/list',

  DEVICE_ALARM_URL = '/alarm',

  ALARM_BATCH_ACK = '/alarm/batch/ack',

  ALARM_BATCH_CLEAR = '/alarm/batch/clear',

  DEVICE_CREDENTIALS = '/device/credentials',

  COMMAND_ISSUANCE = '/rpc',

  DEVICE_ATTR = '/device/attributes',

  GATEWAY_DEVICE = '/device/gateway/list',

  DEVICE_STATE_LOG_URL = '/device/state/log',

  DEVICE_PUBLIC = '/customer/public/device',

  DEVICE_PRIVATE = '/customer/device',
  DEVICE_COLLECT = '/collect/DEVICE_COLLECT', //收藏

  /**
   * @description 通过设备列表获取设备信息
   */
  QUERY_DEVICES = '/device/get/devices',

  /**
   * @description 批量变更产品
   */
  BATCH_UPDATE_PRODUCT = '/device/batch/update',

  /**
   * @description 批量公开设备
   */
  BATCH_PUBLIC_DEVICE = '/batch/customer/public/device/',

  /**
   * @description 批量私有设备
   */
  BATCH_PRIVATE_DEVICE = '/batch/customer/device/',

  /**
   * @description 批量修改设备
   */
  BATCH_UPDATE_DEVICE = '/device/batch/update',
}

export const devicePage = (params: DeviceQueryParam) => {
  const { page, pageSize } = params;
  const otherParams = omit(params, ['page', 'pageSize']);
  return defHttp.post<PaginationResult<DeviceModel>>({
    url: `${DeviceManagerApi.DEVICE_URL}?page=${page}&pageSize=${pageSize}`,
    params: otherParams,
  });
};

export const deviceCollect = (params: string[]) => {
  return defHttp.post({
    url: `${DeviceManagerApi.DEVICE_COLLECT}`,
    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<DeviceRecord[]>({
    url: DeviceManagerApi.DEVICE_PROFILE_URL_ME,
  });
};

export const queryDeviceProfileBy = (params: Recordable) => {
  return defHttp.get<DeviceRecord[]>({
    url: DeviceManagerApi.DEVICE_PROFILE_URL_ME,
    params,
  });
};

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

// 查询设备详情
export const getDeviceDetail = (id: string) => {
  return defHttp.get<DeviceRecord>(
    {
      url: `${DeviceManagerApi.DEVICE_URL}${isShareMode() ? '/public' : ''}/${id}`,
    },
    { withShareToken: true }
  );
};

// 查询设备详情中的告警
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: {
  organizationId: string;
  deviceType: DeviceTypeEnum.GATEWAY;
}) => {
  const { organizationId, deviceType } = params;
  const res = await defHttp.get({
    url: `/device/list`,
    params: {
      organizationId,
      deviceType,
    },
  });
  return Promise.resolve<{ label: string; value: string }[]>(
    res.map((item) => ({ label: item.name, value: item.tbDeviceId }))
  );
};

export const getGatewayDevice = (params: Record<'organizationId' | 'transportType', string>) => {
  const { organizationId, transportType } = params;
  return defHttp.get<DeviceRecord[]>({
    url: DeviceManagerApi.GATEWAY_DEVICE,
    params: {
      organizationId,
      transportType,
    },
  });
};

// 获取网关设备
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: CommandDeliveryWayEnum,
  tbDeviceId: string,
  data: Recordable
) => {
  return defHttp.post(
    {
      url: `${DeviceManagerApi.COMMAND_ISSUANCE}/${type}/${tbDeviceId}`,
      data,
    },
    {
      joinPrefix: false,
      withShareToken: true,
    }
  );
};

export const getDeviceAttrs = (params: { deviceProfileId: string; dataType?: string }) => {
  const { deviceProfileId, dataType } = params;
  return defHttp.get<DeviceModelOfMatterAttrs[]>({
    url: `${DeviceManagerApi.DEVICE_ATTR}/${deviceProfileId}`,
    params: { dataType },
  });
};

/**
 * 设备上下线记录相关api
 */
export const deviceStateLogPage = (params: DeviceQueryParam) => {
  return defHttp.get<DeviceStateLogModel>({
    url: DeviceManagerApi.DEVICE_STATE_LOG_URL,
    params,
  });
};

export const deleteStateLogDevice = (ids: string[]) => {
  return defHttp.delete({
    url: DeviceManagerApi.DEVICE_STATE_LOG_URL,
    data: {
      ids: ids,
    },
  });
};

export const deviceStateLogDetail = (id) => {
  return defHttp.get<DeviceStateLogModel>({
    url: DeviceManagerApi.DEVICE_STATE_LOG_URL + '/' + id,
  });
};

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

export const publicDevice = (tbDeviceId: string) => {
  return defHttp.post(
    {
      url: `${DeviceManagerApi.DEVICE_PUBLIC}/${tbDeviceId}`,
    },
    { joinPrefix: false }
  );
};

export const privateDevice = (tbDeviceId: string) => {
  return defHttp.delete(
    {
      url: `${DeviceManagerApi.DEVICE_PRIVATE}/${tbDeviceId}`,
    },
    { joinPrefix: false }
  );
};

export const getDevicesByDeviceIds = (ids: string[]) => {
  return defHttp.post<Record<'data', DeviceModel[]>>({
    url: DeviceManagerApi.QUERY_DEVICES,
    data: ids,
  });
};

export const doBatchAckAlarm = (ids: string[]) => {
  return defHttp.post(
    {
      url: DeviceManagerApi.ALARM_BATCH_ACK,
      data: { alarmIds: ids },
    },
    { joinPrefix: false }
  );
};

export const doBatchClearAlarm = (ids: string[]) => {
  return defHttp.post(
    {
      url: DeviceManagerApi.ALARM_BATCH_CLEAR,
      data: { alarmIds: ids },
    },
    { joinPrefix: false }
  );
};

export const doBatchUpdateProduct = (params: { deviceIds: string[]; deviceProfileId: string }) => {
  return defHttp.post(
    {
      url: DeviceManagerApi.BATCH_UPDATE_PRODUCT,
      data: params,
    },
    { joinPrefix: false }
  );
};

/**
 *
 * @param deviceIds tbDeviceId分割的字符串
 * @returns
 */
export const doBatchPublicDevice = (deviceIds: string) => {
  return defHttp.post(
    {
      url: `${DeviceManagerApi.BATCH_PUBLIC_DEVICE}?deviceIds=${deviceIds}`,
    },
    { joinPrefix: false }
  );
};

/**
 *
 * @param deviceIds tbDeviceId分割的字符串
 * @returns
 */
export const doBatchPrivateDevice = (deviceIds: string) => {
  return defHttp.post(
    {
      url: `${DeviceManagerApi.BATCH_PRIVATE_DEVICE}?deviceIds=${deviceIds}`,
    },
    { joinPrefix: false }
  );
};

/**
 *
 * @param orgId
 * @returns
 */
export const doBatchUpdateDevice = (orgId: string, data: Recordable[]) => {
  return defHttp.post({
    url: `${DeviceManagerApi.BATCH_UPDATE_DEVICE}?orgId=${orgId}`,
    data,
  });
};