ZhuDongming
2019-11-08 b284d078f196af80a105dc3bcb610d8ed37d9251
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
package com.moral.service.impl;
 
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;
 
import com.moral.entity.Device;
import com.moral.entity.Point;
import com.moral.entity.Sensor;
import com.moral.mapper.HistoryHourlyMapper;
import com.moral.mapper.SensorMapper;
import com.moral.service.DeviceService;
import com.moral.service.HistoryHourlyService;
import com.moral.service.SensorService;
 
import javax.annotation.Resource;
 
@Service
public class HistoryHourlyServiceImpl implements HistoryHourlyService {
    @Resource
    private HistoryHourlyMapper historyHourlyMapper;
 
    @Resource
    private DeviceService deviceService;
 
    @Resource
    private SensorService sensorService;
 
    @Resource
    private SensorMapper sensorMapper;
 
    @Override
    public Map<String, Object> getPollutionSourceData(Map<String, Object> parameters) throws Exception {
        List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
        List<String> sensorKeys = new ArrayList<>();
        for (Sensor sensor : sensors) {
            sensorKeys.add(sensor.getSensorKey());
        }
        parameters.put("sensorKeys", sensorKeys);
        Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceData(parameters);
        if (MapUtils.isNotEmpty(pollutionSourceData)) {
            Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
            String selectSensorKey = parameters.get("sensorKey").toString();
            for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
                if (selectSensorKey.equals(s.getKey())) {
                    selectSensorKey = s.getValue();
                }
            }
            pollutionSourceData.put("selectSensorKey", selectSensorKey);
        }
        return pollutionSourceData;
    }
 
    @Override
    public Map<String, Object> getPollutionSourceDataByHour(Map<String, Object> parameters) throws Exception {
        List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
        List<String> sensorKeys = new ArrayList<>();
        for (Sensor sensor : sensors) {
            sensorKeys.add(sensor.getSensorKey());
        }
        parameters.put("sensorKeys", sensorKeys);
        Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceDataByHour(parameters);
        if (MapUtils.isNotEmpty(pollutionSourceData)) {
            Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
            String selectSensorKey = parameters.get("sensorKey").toString();
            for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
                if (selectSensorKey.equals(s.getKey())) {
                    selectSensorKey = s.getValue();
                }
            }
            pollutionSourceData.put("selectSensorKey", selectSensorKey);
        }
        return pollutionSourceData;
    }
 
    @Override
    public Map<String, Object> getPollutionSourceDataAll(Map<String, Object> parameters) throws Exception {
        List<Sensor> sensors = sensorMapper.getSensorsByMac(parameters);
        List<String> sensorKeys = new ArrayList<>();
        for (Sensor sensor : sensors) {
            sensorKeys.add(sensor.getSensorKey());
        }
        parameters.put("sensorKeys", sensorKeys);
        Map<String, Object> pollutionSourceData = historyHourlyMapper.getPollutionSourceDataAll(parameters);
        if (MapUtils.isNotEmpty(pollutionSourceData)) {
            Map<String, String> sensorsMap = sensorService.getSensorsMap(parameters);
            String selectSensorKey = parameters.get("sensorKey").toString();
            for (Map.Entry<String, String> s : sensorsMap.entrySet()) {
                if (selectSensorKey.equals(s.getKey())) {
                    selectSensorKey = s.getValue();
                }
            }
            pollutionSourceData.put("selectSensorKey", selectSensorKey);
        }
        return pollutionSourceData;
    }
 
    @Override
    public Point getDirPoint(Map<String, Object> parameters) throws Exception {
        Map<String, Object> pollutionSourceData = getPollutionSourceDataByHour(parameters);
        if (MapUtils.isEmpty(pollutionSourceData)) {
            pollutionSourceData = getPollutionSourceDataAll(parameters);
        }
        String mac = parameters.get("mac").toString();
        Device device = deviceService.getDeviceByMac(mac, false);
        Point pointEnd = new Point();
        if (MapUtils.isNotEmpty(pollutionSourceData)) {
            if (pollutionSourceData.get("e18") != null && pollutionSourceData.get("e23") != null && pollutionSourceData.get("e6") != null) {
                double windSpeed = Double.valueOf(pollutionSourceData.get("e18").toString());
                double winDir = Double.valueOf(pollutionSourceData.get("e23").toString());
                double distance = windSpeed * 3600;
                double long1 = device.getLongitude();
                double lat1 = device.getLatitude();
                String[] result = calLocationByDistanceAndLocationAndDirection(winDir, long1, lat1, distance);
                pointEnd.setLng(Double.valueOf(result[0]));
                pointEnd.setLat(Double.valueOf(result[1]));
            }
        }
        return pointEnd;
    }
 
 
    /**
     * 根据一点的坐标与距离,以及方向,计算另外一点的位置
     *
     * @param angle     角度,从正北顺时针方向开始计算
     * @param startLong 起始点经度
     * @param startLat  起始点纬度
     * @param distance  距离,单位m
     * @return
     */
    private String[] calLocationByDistanceAndLocationAndDirection(double angle, double startLong, double startLat, double distance) {
        /** 地球半径 **/
        final double R = 6371e3;
        /** 180° **/
        final DecimalFormat df = new DecimalFormat("0.000000");
        String[] result = new String[2];
        //将距离转换成经度的计算公式
        double δ = distance / R;
        // 转换为radian,否则结果会不正确
        angle = Math.toRadians(angle);
        startLong = Math.toRadians(startLong);
        startLat = Math.toRadians(startLat);
        double lat = Math.asin(Math.sin(startLat) * Math.cos(δ) + Math.cos(startLat) * Math.sin(δ) * Math.cos(angle));
        double lng = startLong + Math.atan2(Math.sin(angle) * Math.sin(δ) * Math.cos(startLat), Math.cos(δ) - Math.sin(startLat) * Math.sin(lat));
        // 转为正常的10进制经纬度
        lng = Math.toDegrees(lng);
        lat = Math.toDegrees(lat);
        result[0] = df.format(lng);
        result[1] = df.format(lat);
        return result;
    }
 
}