service.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import BaseService from '@/utils/BaseService';
  2. import { request } from 'umi';
  3. import type { DeviceInstance, PropertyData } from '@/pages/device/Instance/typings';
  4. import SystemConst from '@/utils/const';
  5. import { defer, from, mergeMap, toArray } from 'rxjs';
  6. import { filter, groupBy, map } from 'rxjs/operators';
  7. class Service extends BaseService<DeviceInstance> {
  8. public detail = (id: string) => request(`${this.uri}/${id}/detail`, { method: 'GET' });
  9. public getConfigMetadata = (id: string) =>
  10. request(`${this.uri}/${id}/config-metadata`, { method: 'GET' });
  11. public getUnits = () => request(`/${SystemConst.API_BASE}/protocol/units`, { method: 'GET' });
  12. public propertyRealTime = (data: Record<string, unknown>[]) =>
  13. defer(() =>
  14. from(
  15. request(`/${SystemConst.API_BASE}/dashboard/_multi`, {
  16. method: 'POST',
  17. data,
  18. }),
  19. ).pipe(
  20. filter((resp) => resp.status === 200),
  21. map((resp) => resp.result),
  22. mergeMap((temp: PropertyData[]) => from(temp)),
  23. map((item) => ({
  24. timeString: item.data.timeString,
  25. timestamp: item.data.timestamp,
  26. ...item.data.value,
  27. })),
  28. groupBy((group$) => group$.property),
  29. mergeMap((group) => group.pipe(toArray())),
  30. map((arr) => ({
  31. list: arr.sort((a, b) => a.timestamp - b.timestamp),
  32. property: arr[0].property,
  33. })),
  34. // toArray()
  35. ),
  36. );
  37. public getProperty = (id: string, type: string) =>
  38. request(`/${SystemConst.API_BASE}/device/standard/${id}/property/${type}`, {
  39. method: 'GET',
  40. });
  41. public setProperty = (deviceId: string, data: Record<string, unknown>) =>
  42. request(`/${SystemConst.API_BASE}/device/instance/${deviceId}/property`, {
  43. method: 'PUT',
  44. data,
  45. });
  46. public getPropertyData = (deviceId: string, params: Record<string, unknown>) =>
  47. request(`/${SystemConst.API_BASE}/device-instance/${deviceId}/properties/_query`, {
  48. method: 'GET',
  49. params,
  50. });
  51. public getEventCount = (deviceId: string, eventId: string, params: Record<string, unknown>) =>
  52. request(`/${SystemConst.API_BASE}/device/instance/${deviceId}/event/${eventId}`, {
  53. method: 'GET',
  54. params,
  55. });
  56. public deleteMetadata = (deviceId: string) =>
  57. request(`/${SystemConst.API_BASE}/device/instance/${deviceId}/metadata`, {
  58. method: 'DELETE',
  59. });
  60. public invokeFunction = (deviceId: string, functionId: string, data: Record<string, unknown>) =>
  61. request(`/${SystemConst.API_BASE}/device/invoked/${deviceId}/function/${functionId}`, {
  62. method: 'POST',
  63. data,
  64. });
  65. public queryLog = (deviceId: string, params: Record<string, unknown>) =>
  66. request(`/${SystemConst.API_BASE}/device-instance/${deviceId}/logs`, {
  67. method: 'GET',
  68. params,
  69. });
  70. public getLogType = () =>
  71. request(`/${SystemConst.API_BASE}/dictionary/device-log-type/items`, {
  72. method: 'GET',
  73. });
  74. }
  75. export default Service;