18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
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
package com.ruoyi.jt.service.impl;
 
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import com.ruoyi.jt.common.DateUtils;
import com.ruoyi.jt.domain.JtDevice;
import com.ruoyi.jt.mapper.JtDeviceMapper;
import com.ruoyi.jt.uuid.IdUtils;
import io.github.yezhihao.netmc.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.jt.mapper.JtDeviceLocationMapper;
import com.ruoyi.jt.domain.JtDeviceLocation;
import com.ruoyi.jt.service.IJtDeviceLocationService;
import org.yzh.protocol.commons.transform.AttributeKey;
import org.yzh.protocol.t808.T0200;
import org.yzh.web.model.entity.DeviceDO;
import org.yzh.web.model.enums.SessionKey;
 
/**
 * 设备位置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-26
 */
@Service
public class JtDeviceLocationServiceImpl implements IJtDeviceLocationService {
    private static DateTimeFormatter dataFormater = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private JtDeviceLocationMapper jtDeviceLocationMapper;
    @Autowired
    private JtDeviceMapper jtDeviceMapper;
 
 
    /**
     * 查询设备位置
     *
     * @param id 设备位置主键
     * @return 设备位置
     */
    @Override
    public JtDeviceLocation selectJtDeviceLocationById(String id) {
        return jtDeviceLocationMapper.selectJtDeviceLocationById(id);
    }
 
    /**
     * 查询设备位置列表
     *
     * @param jtDeviceLocation 设备位置
     * @return 设备位置
     */
    @Override
    public List<JtDeviceLocation> selectJtDeviceLocationList(JtDeviceLocation jtDeviceLocation) {
        return jtDeviceLocationMapper.selectJtDeviceLocationList(jtDeviceLocation);
    }
 
    /**
     * 新增设备位置
     *
     * @param jtDeviceLocation 设备位置
     * @return 结果
     */
    @Override
    public int insertJtDeviceLocation(JtDeviceLocation jtDeviceLocation) {
        jtDeviceLocation.setId(IdUtils.simpleUUID());
        jtDeviceLocation.setCreateTime(DateUtils.getNowDate());
        return jtDeviceLocationMapper.insertJtDeviceLocation(jtDeviceLocation);
    }
 
    /**
     * 修改设备位置
     *
     * @param jtDeviceLocation 设备位置
     * @return 结果
     */
    @Override
    public int updateJtDeviceLocation(JtDeviceLocation jtDeviceLocation) {
        return jtDeviceLocationMapper.updateJtDeviceLocation(jtDeviceLocation);
    }
 
    /**
     * 批量删除设备位置
     *
     * @param ids 需要删除的设备位置主键
     * @return 结果
     */
    @Override
    public int deleteJtDeviceLocationByIds(String[] ids) {
        return jtDeviceLocationMapper.deleteJtDeviceLocationByIds(ids);
    }
 
    /**
     * 删除设备位置信息
     *
     * @param id 设备位置主键
     * @return 结果
     */
    @Override
    public int deleteJtDeviceLocationById(String id) {
        return jtDeviceLocationMapper.deleteJtDeviceLocationById(id);
    }
 
    @Override
    public int saveT0200s(List<T0200> list, boolean isUpdateDevicePosition) {
        T0200 latestT0200 = null;
        for (T0200 t0200 : list) {
            if (t0200.getWarnBit() != 0) {
                //TODO 处理报警
                System.out.println("!!!T0200报警:" + t0200);
 
            }
 
            this.saveJtDeviceLocation(t0200);
            //获得最新的设备时间
            if (latestT0200 == null || t0200.getDeviceTime().isAfter(latestT0200.getDeviceTime())) {
                latestT0200 = t0200;
            }
            //   }
        }
        if (isUpdateDevicePosition && latestT0200 != null) {
 
            this.updateLatestDeviceLocation(latestT0200);
        }
        return 0;
    }
 
    private void updateLatestDeviceLocation(T0200 t0200) {
        //按clientId 获取设备
        JtDevice jtDevice = jtDeviceMapper.selectJtDeviceByClientId(t0200.getClientId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        //如果设备时间大于更新时间,则更新位置和更新时间
        if (jtDevice != null) {
            LocalDateTime jtDeviceLocalDateTime = null;
            // Date 转 LocalDateTime
            if (jtDevice.getUpdateTime() != null) {
                jtDeviceLocalDateTime = jtDevice.getUpdateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            }
            if (jtDeviceLocalDateTime == null || t0200.getDeviceTime().isAfter(jtDeviceLocalDateTime)) {
                jtDevice.setLongitude(t0200.getLng() + "");
                jtDevice.setLatitude(t0200.getLat() + "");
                jtDevice.setAltitude(t0200.getAltitude() + "");
                jtDevice.setSpeed(Double.valueOf(t0200.getSpeed())/10+"");
                jtDevice.setDirection(t0200.getDirection() + "");
                jtDevice.setUpdateTime(Date.from(t0200.getDeviceTime().atZone(ZoneId.systemDefault()).toInstant()));
 
                //处理附加信息
                Map<Integer, Object> attributes = t0200.getAttributes();
 
                if (attributes!=null) {
                    System.out.println("!!!!附加信息!!!! = " + attributes);
                    Object mileage = attributes.get(AttributeKey.Mileage);
                    if (mileage != null) {
                        double mileageInt = Double.parseDouble(mileage.toString()) / 10;
                        jtDevice.setMileage(mileageInt + "");
                    }
                    jtDevice.setFuel(attributes.get(AttributeKey.Fuel) + "");
                    jtDevice.setTirePressure(attributes.get(AttributeKey.TirePressure) + "");
                    jtDevice.setCarriageTemperature(attributes.get(AttributeKey.CarriageTemperature) + "");
                    jtDevice.setExtendSignalStatus(attributes.get(AttributeKey.Signal)+"");
                    jtDevice.setSignalStrength(attributes.get(AttributeKey.SignalStrength)+"");
                    jtDevice.setGnssCount(attributes.get(AttributeKey.GnssCount)+"");
                }
 
 
                jtDeviceMapper.updateJtDevice(jtDevice);
            }
 
        }
 
    }
 
    public int saveJtDeviceLocation(T0200 t0200) {
        Session session = t0200.getSession();
        if (session != null) {
            JtDeviceLocation location = new JtDeviceLocation();
            location.setId(IdUtils.simpleUUID());
            location.setAltitude(t0200.getAltitude() + "");
            location.setLatitude(t0200.getLat() + "");
            location.setLongitude(t0200.getLng() + "");
            location.setClientId(t0200.getClientId());
            location.setCreateTime(DateUtils.getNowDate());
            location.setDeviceTime(t0200.getDeviceTime().format(dataFormater));
            location.setDirection(t0200.getDirection() + "");
            location.setSpeed(t0200.getSpeed() * 0.1 + "");
            location.setProtocolVersion(t0200.getProtocolVersion() + "");
            location.setStatusBit(t0200.getStatusBit() + "");
            location.setWarnBit(t0200.getWarnBit() + "");
 
            //处理附加信息
            Map<Integer, Object> attributes = t0200.getAttributes();
 
            if (attributes!=null) {
                System.out.println(">>>>>>>附加信息:" + attributes);
                Object mileage = attributes.get(AttributeKey.Mileage);
                if (mileage != null) {
                    double mileageInt = Double.parseDouble(mileage.toString()) / 10;
                    location.setMileage(mileageInt + "");
                }
                location.setFuel(attributes.get(AttributeKey.Fuel) + "");
                location.setTirePressure(attributes.get(AttributeKey.TirePressure) + "");
                location.setCarriageTemperature(attributes.get(AttributeKey.CarriageTemperature) + "");
                location.setExtendSignalStatus(attributes.get(AttributeKey.Signal)+"");
                location.setSignalStrength(attributes.get(AttributeKey.SignalStrength)+"");
                location.setGnssCount(attributes.get(AttributeKey.GnssCount)+"");
            }
 
 
 
            DeviceDO deviceDO = session.getAttribute(SessionKey.Device);
            if (deviceDO != null) {
                location.setPlateNo(deviceDO.getPlateNo());
                ;
            }
            return jtDeviceLocationMapper.insertJtDeviceLocation(location);
        }
        return 0;
    }
 
 
}