无人机管理后台前端(已迁走)
张含笑
2025-09-01 2ca94de8ede18ac07ccfd8dec7b6f6a707adde9b
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
import { ElMessage } from 'element-plus';
 
import { putDeviceProps } from '@/api/device-setting';
import {
  DeviceSettingKeyEnum,
  ObstacleAvoidanceStatusEnum,
  NightLightsStateEnum,
  DistanceLimitStatusEnum,
} from '@/types/device-setting';
import { messages } from '@/lang';
 
export function useDeviceSetting() {
  // 生成参数
  function genDevicePropsBySettingKey(key, fromModel) {
    const body = {};
    if (key === DeviceSettingKeyEnum.NIGHT_LIGHTS_MODE_SET) {
      body.night_lights_state = fromModel.nightLightsState
        ? NightLightsStateEnum.OPEN
        : NightLightsStateEnum.CLOSE;
    } else if (key === DeviceSettingKeyEnum.HEIGHT_LIMIT_SET) {
      body.height_limit = fromModel.heightLimit;
    } else if (key === DeviceSettingKeyEnum.DISTANCE_LIMIT_SET) {
      body.distance_limit_status = {};
      if (fromModel.distanceLimitStatus.state) {
        body.distance_limit_status.state = DistanceLimitStatusEnum.SET;
        body.distance_limit_status.distance_limit = fromModel.distanceLimitStatus.distanceLimit;
      } else {
        body.distance_limit_status.state = DistanceLimitStatusEnum.UNSET;
      }
    } else if (key === DeviceSettingKeyEnum.OBSTACLE_AVOIDANCE_HORIZON) {
      body.obstacle_avoidance = {
        horizon: fromModel.obstacleAvoidanceHorizon
          ? ObstacleAvoidanceStatusEnum.OPEN
          : ObstacleAvoidanceStatusEnum.CLOSE,
      };
    } else if (key === DeviceSettingKeyEnum.OBSTACLE_AVOIDANCE_UPSIDE) {
      body.obstacle_avoidance = {
        upside: fromModel.obstacleAvoidanceUpside
          ? ObstacleAvoidanceStatusEnum.OPEN
          : ObstacleAvoidanceStatusEnum.CLOSE,
      };
    } else if (key === DeviceSettingKeyEnum.OBSTACLE_AVOIDANCE_DOWNSIDE) {
      body.obstacle_avoidance = {
        downside: fromModel.obstacleAvoidanceDownside
          ? ObstacleAvoidanceStatusEnum.OPEN
          : ObstacleAvoidanceStatusEnum.CLOSE,
      };
    }
    return body;
  }
 
  // 设置设备属性
  async function setDeviceProps(sn, body) {
    try {
      const { code, message: msg } = await putDeviceProps(sn, body);
      console.log(code,messages)
      if (code === 0) {
        ElMessage({
          message: '设备属性设置成功!',
          type: 'success',
        });
      }
      throw msg;
    } catch (e) {
      // ElMessage({
      //   message: '设备属性设置失败',
      //   type: 'error',
      // });
      return false;
    }
  }
 
  return {
    genDevicePropsBySettingKey,
    setDeviceProps,
  };
}