CamelDataUtil.java 10 KB

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