CamelDataUtil.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package org.dromara.util;
  2. import cn.hutool.json.JSON;
  3. import cn.hutool.json.JSONArray;
  4. import cn.hutool.json.JSONUtil;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.camel.CamelContext;
  7. import org.apache.camel.Exchange;
  8. import org.apache.camel.ProducerTemplate;
  9. import org.apache.commons.lang3.time.DateFormatUtils;
  10. import org.dromara.common.core.constant.Constants;
  11. import org.dromara.common.core.utils.DateUtils;
  12. import org.dromara.common.core.utils.SpringUtils;
  13. import org.dromara.common.core.utils.StringUtils;
  14. import org.dromara.common.redis.utils.RedisUtils;
  15. import org.dromara.sms4j.api.universal.RedisUtil;
  16. import org.dromara.system.domain.GpsData;
  17. import org.dromara.system.domain.SysDictData;
  18. import org.dromara.system.domain.TblCarInfo;
  19. import org.dromara.system.domain.bo.SysDictDataBo;
  20. import org.dromara.system.domain.bo.TblCarInfoBo;
  21. import org.dromara.system.domain.bo.TblCarScheduleBo;
  22. import org.dromara.system.domain.vo.SysDictDataVo;
  23. import org.dromara.system.domain.vo.TblCarInfoVo;
  24. import org.dromara.system.domain.vo.TblCarScheduleVo;
  25. import org.dromara.system.service.*;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import cn.hutool.json.JSONObject;
  28. import org.springframework.stereotype.Component;
  29. import java.io.UnsupportedEncodingException;
  30. import java.nio.charset.Charset;
  31. import java.time.Duration;
  32. import java.util.Calendar;
  33. import java.util.Date;
  34. import java.util.List;
  35. import java.util.Map;
  36. @Slf4j
  37. @Component("camelDataUtil")
  38. public class CamelDataUtil {
  39. @Autowired
  40. private CamelContext camelContext;
  41. @Autowired
  42. ITblGpsDataService gpsDataService;
  43. @Autowired
  44. ITblCarInfoService tblCarInfoService;
  45. @Autowired
  46. ITblCarScheduleService tblCarScheduleService;
  47. @Autowired
  48. ISysDictDataService sysDictDataService;
  49. private StandardThreadExecutor threadPoolExecutor=new StandardThreadExecutor();
  50. public void SendData(String endrouter,Object payload,String headerkey,Object headervalue){
  51. ProducerTemplate template = camelContext.createProducerTemplate();
  52. template.sendBodyAndHeader(endrouter,payload,headerkey,headervalue);
  53. }
  54. public void processdata(String body, Exchange exchange) throws UnsupportedEncodingException {
  55. Map<String,String> o = exchange.getMessage().getHeader("mqheader", Map.class);
  56. exchange.getMessage().setHeader("CamelPahoOverrideTopic","/dev/"+o.get("devtype")+"/"+o.get("devcode")+"/");
  57. exchange.getMessage().setBody(body.getBytes(Charset.forName("utf8")));
  58. // exchange.getMessage().setBody(body);
  59. }
  60. public void processGpsData(GpsData gpsData){
  61. log.info("body:{}",JSONUtil.toJsonStr(gpsData));
  62. gpsDataService.insertData(gpsData);
  63. //缓存最新数据到redis
  64. RedisUtils.setCacheObject(StringUtils.format("gps_{}", gpsData.getDevicecode()), gpsData);
  65. threadPoolExecutor.execute(new Runnable() {
  66. @Override
  67. public void run() {
  68. TblCarInfoBo carInfoBo = new TblCarInfoBo();
  69. carInfoBo.setDeviceInfos(gpsData.getDevicecode());
  70. List<TblCarInfoVo> carInfoVos = tblCarInfoService.queryList(carInfoBo);
  71. if(carInfoVos.size()>0){
  72. TblCarInfoVo carInfoVo = carInfoVos.get(0);
  73. if(StringUtils.isNotEmpty(carInfoVo.getCarNum())){
  74. //查询车辆信息
  75. TblCarInfoBo carInfoBo1 = new TblCarInfoBo();
  76. carInfoBo1.setCarNum(carInfoVo.getCarNum());
  77. List<TblCarInfoVo> carInfoVos1 = tblCarInfoService.queryList(carInfoBo1);
  78. if(carInfoVos1.size()>0){
  79. // 查询车辆信息
  80. TblCarInfoVo carInfoVo1 = carInfoVos1.get(0);
  81. RedisUtils.setCacheObject(StringUtils.format("gps_{}", carInfoVo1.getId()), gpsData);
  82. TblCarScheduleBo tblCarScheduleBo = new TblCarScheduleBo();
  83. tblCarScheduleBo.setCarNum(carInfoVo1.getCarNum());
  84. Map<String, Object> map = Map.of("scheduleDate", DateUtils.getDate() );
  85. tblCarScheduleBo.setParams(map);
  86. List<TblCarScheduleVo> list = tblCarScheduleService.queryList(tblCarScheduleBo);
  87. if(list.size()>0){
  88. long t0 = 10;
  89. TblCarScheduleVo scheduleVo = null;
  90. for (TblCarScheduleVo vo : list) {
  91. if(vo.getScheduleDate().getTime()-DateUtils.getNowDate().getTime()<0){
  92. if(t0>0){
  93. t0 = vo.getScheduleDate().getTime()-DateUtils.getNowDate().getTime();
  94. scheduleVo = vo;
  95. }
  96. if(t0<vo.getScheduleDate().getTime()-DateUtils.getNowDate().getTime()){
  97. t0 = vo.getScheduleDate().getTime()-DateUtils.getNowDate().getTime();
  98. scheduleVo = vo;
  99. }
  100. }
  101. }
  102. // 查询路线信息
  103. if(scheduleVo!=null){
  104. try{
  105. JSONArray lines = JSONUtil.parseObj(scheduleVo.getPathInfo()).getJSONArray("pathLine");
  106. for (JSONObject jsonObject : lines.toList(JSONObject.class)) {
  107. double distance = DistanceCalculator.calculateDistance(jsonObject.getStr("location"),StringUtils.format("{},{}", gpsData.getLng(),gpsData.getLat()));
  108. String configdistance = SpringUtils.getBean(ISysConfigService.class).selectConfigByKey("sys.car.distance");
  109. if(Double.parseDouble(configdistance)>distance){
  110. //靠近站点
  111. String stationName = RedisUtils.getCacheObject(StringUtils.format("car_{}_{}_tip",carInfoVo1.getCarNum(),DateFormatUtils.format( scheduleVo.getScheduleDate(),DateUtils.YYYYMMDDHHMMSS) ));
  112. if(stationName!=null && stationName.contains(jsonObject.getStr("stationName"))){
  113. //提报过的不再提报
  114. }else{
  115. stationName=stationName==null?"":stationName;
  116. RedisUtils.setCacheObject(StringUtils.format("car_{}_{}_tip",carInfoVo1.getCarNum(), DateFormatUtils.format( scheduleVo.getScheduleDate(),DateUtils.YYYYMMDDHHMMSS) ), stationName+","+jsonObject.getStr("stationName"),Duration.ofSeconds(3600*24));
  117. //开始播报
  118. //获取站点设备信息
  119. SysDictDataBo sysDictData = new SysDictDataBo();
  120. sysDictData.setDictLabel(jsonObject.getStr("stationName"));
  121. sysDictData.setDictType("tbl_sites");
  122. List<SysDictDataVo> stations = sysDictDataService.selectDictDataList(sysDictData);
  123. if(stations.size()>0){
  124. //满足条件播报(led or 广播)
  125. String message = StringUtils.format("{}即将到达,请准备接车,请等待。。。", carInfoVo1.getCarNum());
  126. List<JSONObject> devicedatas = JSONUtil.parseArray( stations.get(0).getRemark()).toList(JSONObject.class);
  127. for (JSONObject i :devicedatas) {
  128. if(i.getStr("type").equals("led")){
  129. MessageUtils.sendLed(i.getStr("code"), message);
  130. }
  131. if(i.getStr("type").equals("tts")){
  132. MessageUtils.sendTTs(i.getStr("code"), message);
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }catch (Exception e){
  140. log.error("---------------------");
  141. log.error(e.getMessage());
  142. log.error("---------------------");
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. });
  151. }
  152. public void ReciveData(String body, Exchange exchange){
  153. if(exchange.getMessage().getHeaders().get("CamelMqttTopic").toString().contains("gps")){
  154. String devicecode = exchange.getMessage().getHeaders().get("CamelMqttTopic").toString().split("/")[3];
  155. GpsData gpsData = new GpsData();
  156. gpsData.setDevicecode(devicecode);
  157. gpsData.setLat(JSONUtil.parseObj(body).getDouble("LAT"));
  158. gpsData.setLng(JSONUtil.parseObj(body).getDouble("LNG"));
  159. gpsData.setAlt(JSONUtil.parseObj(body).getDouble("ALT"));
  160. gpsData.setSpeed(JSONUtil.parseObj(body).getDouble("SPD"));
  161. if(JSONUtil.parseObj(body).getDouble("SPD")>0){
  162. processGpsData(gpsData);
  163. }
  164. }
  165. log.info("body:{}",body);
  166. }
  167. }