import request from '@/utils/request';
import { uniqueId } from 'lodash';
export const getSiteList = () => {
  return request({
    url: '/map/devices?projectId=' + sessionStorage.getItem('projectId'),
    method: 'get',
  }).then(res => {
    return (res?.data || []).map(mo => {
      return {
        ...mo,
        label: mo.townName,
        id: uniqueId('mo'),
        iconType: 'folder',
        type: 'town',
        children: (mo.communities || []).map(co => {
          return {
            ...mo,
            ...co,
            label: co.communityName,
            id: uniqueId('co'),
            ids: co.id,
            iconType: 'folder',
            type: 'community',
            children: (co.devices || []).map(de => {
              return {
                ...mo,
                ...co,
                ...de,
                iconType: 'device',
                type: 'device',
                label: de.name,
                id: uniqueId('de'),
                ids: de.id,
              };
            }),
          };
        }),
      };
    });
  });
};
//站点详情
export function getDeviceInfo(data) {
  return request({
    url: '/map/deviceInfo?deviceCode=' + data.deviceCode,
    method: 'get',
    data,
  });
}
//项目下站点列表
export function getDeviceList(data) {
  return request({
    url: '/device/list?projectId=' + data.projectId,
    method: 'get',
    data,
  });
}
//站点信息
export function getDeviceInfos(data) {
  return request({
    url: '/device/deviceInfo?deviceCode=' + data.deviceCode,
    method: 'get',
    data,
  });
}
// 获取监控播放地址
export function getLiveAddr(data) {
  return request({
    url: '/device/liveAddr?deviceCode=' + data.deviceCode,
    method: 'get',
    data,
  });
}
// 水质达标率
export function getWaterRate(data) {
  return request({
    url: '/device/waterRate?deviceCode=' + data.deviceCode,
    method: 'get',
    data,
  });
}
// 抓拍照片
export function getCaptureImg(data) {
  return request({
    url:
      '/device/captureImg?deviceCode=' + data.deviceCode + '&page=' + data.page,
    method: 'get',
    data,
  });
}
//传感器数据 可读
export function deviceCode(data) {
  return request({
    url:
      '/device/sensors?deviceCode=' +
      data.deviceCode +
      '&page=' +
      data.page +
      '&size=' +
      data.size,
    method: 'get',
    data,
  });
}
//传感器数据 可读
export function sensorsOfWrite(data) {
  return request({
    url:
      '/device/sensorsOfWrite?deviceCode=' +
      data.deviceCode +
      '&page=' +
      data.page +
      '&size=' +
      data.size,
    method: 'get',
    data,
  });
}
//下发数据
export function writeData(data) {
  return request({
    url:
      '/deviceSensor/write?deviceCode=' +
      data.deviceCode +
      '&dataKey=' +
      data.dataKey +
      '&dataValue=' +
      data.dataValue,
    method: 'post',
    data,
  });
}
