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 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 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 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 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; } }