package org.dromara.util; import cn.hutool.json.JSON; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONUtil; import lombok.extern.slf4j.Slf4j; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; import org.apache.camel.ProducerTemplate; import org.apache.commons.lang3.time.DateFormatUtils; import org.dromara.common.core.constant.Constants; import org.dromara.common.core.utils.DateUtils; import org.dromara.common.core.utils.SpringUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.redis.utils.RedisUtils; import org.dromara.sms4j.api.universal.RedisUtil; import org.dromara.system.domain.GpsData; import org.dromara.system.domain.SysDictData; import org.dromara.system.domain.TblCarInfo; import org.dromara.system.domain.bo.SysDictDataBo; import org.dromara.system.domain.bo.TblCarInfoBo; import org.dromara.system.domain.bo.TblCarScheduleBo; import org.dromara.system.domain.vo.SysDictDataVo; import org.dromara.system.domain.vo.TblCarInfoVo; import org.dromara.system.domain.vo.TblCarScheduleVo; import org.dromara.system.service.*; import org.springframework.beans.factory.annotation.Autowired; import cn.hutool.json.JSONObject; import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; import java.time.Duration; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; @Slf4j @Component("camelDataUtil") public class CamelDataUtil { @Autowired private CamelContext camelContext; @Autowired ITblGpsDataService gpsDataService; @Autowired ITblCarInfoService tblCarInfoService; @Autowired ITblCarScheduleService tblCarScheduleService; @Autowired ISysDictDataService sysDictDataService; private StandardThreadExecutor threadPoolExecutor=new StandardThreadExecutor(); public void SendData(String endrouter,Object payload,String headerkey,Object headervalue){ ProducerTemplate template = camelContext.createProducerTemplate(); template.sendBodyAndHeader(endrouter,payload,headerkey,headervalue); } public void processdata(String body, Exchange exchange) throws UnsupportedEncodingException { Map o = exchange.getMessage().getHeader("mqheader", Map.class); exchange.getMessage().setHeader("CamelPahoOverrideTopic","/dev/"+o.get("devtype")+"/"+o.get("devcode")); exchange.getMessage().setBody(body.getBytes("gb2312")); } public void ReciveData(String body, Exchange exchange){ if(exchange.getMessage().getHeaders().get("CamelMqttTopic").toString().contains("gps")){ String devicecode = exchange.getMessage().getHeaders().get("CamelMqttTopic").toString().split("/")[3]; GpsData gpsData = new GpsData(); gpsData.setDevicecode(devicecode); gpsData.setLat(JSONUtil.parseObj(body).getDouble("LAT")); gpsData.setLng(JSONUtil.parseObj(body).getDouble("LNG")); gpsData.setAlt(JSONUtil.parseObj(body).getDouble("ALT")); gpsData.setSpeed(JSONUtil.parseObj(body).getDouble("SPD")); log.info("body:{}",JSONUtil.toJsonStr(gpsData)); gpsDataService.insertData(gpsData); //缓存最新数据到redis RedisUtils.setCacheObject(StringUtils.format("gps_{}", devicecode), gpsData); threadPoolExecutor.execute(new Runnable() { @Override public void run() { TblCarInfoBo carInfoBo = new TblCarInfoBo(); carInfoBo.setDeviceInfos(devicecode); List carInfoVos = tblCarInfoService.queryList(carInfoBo); if(carInfoVos.size()>0){ TblCarInfoVo carInfoVo = carInfoVos.get(0); if(StringUtils.isNotEmpty(carInfoVo.getCarNum())){ //查询车辆信息 TblCarInfoBo carInfoBo1 = new TblCarInfoBo(); carInfoBo1.setCarNum(carInfoVo.getCarNum()); List carInfoVos1 = tblCarInfoService.queryList(carInfoBo1); if(carInfoVos1.size()>0){ // 查询车辆信息 TblCarInfoVo carInfoVo1 = carInfoVos1.get(0); RedisUtils.setCacheObject(StringUtils.format("gps_{}", carInfoVo1.getId()), gpsData); TblCarScheduleBo tblCarScheduleBo = new TblCarScheduleBo(); tblCarScheduleBo.setCarNum(carInfoVo1.getCarNum()); Map map = Map.of("scheduleDate", DateUtils.parseDate(DateUtils.getDate()+" 00:00:00") ); tblCarScheduleBo.setParams(map); List list = tblCarScheduleService.queryList(tblCarScheduleBo); if(list.size()>0){ long t0 = 10; TblCarScheduleVo scheduleVo = null; for (TblCarScheduleVo vo : list) { if(vo.getScheduleDate().getTime()-DateUtils.getNowDate().getTime()<0){ if(t0>0){ t0 = vo.getScheduleDate().getTime()-DateUtils.getNowDate().getTime(); scheduleVo = vo; } if(t0distance){ //靠近站点 String stationName = RedisUtils.getCacheObject(StringUtils.format("car_{}_{}_tip",carInfoVo1.getCarNum(),DateFormatUtils.format( scheduleVo.getScheduleDate(),DateUtils.YYYYMMDDHHMMSS) )); if(stationName!=null && stationName.contains(jsonObject.getStr("stationName"))){ //提报过的不再提报 }else{ stationName=stationName==null?"":stationName; RedisUtils.setCacheObject(StringUtils.format("car_{}_{}_tip",carInfoVo1.getCarNum(), DateFormatUtils.format( scheduleVo.getScheduleDate(),DateUtils.YYYYMMDDHHMMSS) ), stationName+","+jsonObject.getStr("stationName"),Duration.ofSeconds(3600*24)); //开始播报 //获取站点设备信息 SysDictDataBo sysDictData = new SysDictDataBo(); sysDictData.setDictLabel(jsonObject.getStr("stationName")); sysDictData.setDictType("tbl_sites"); List stations = sysDictDataService.selectDictDataList(sysDictData); if(stations.size()>0){ //满足条件播报(led or 广播) String message = StringUtils.format("{}即将到达,请准备接车,请等待。。。", carInfoVo1.getCarNum()); List devicedatas = JSONUtil.parseArray( stations.get(0).getRemark()).toList(JSONObject.class); for (JSONObject i :devicedatas) { if(i.getStr("type").equals("led")){ MessageUtils.sendLed(i.getStr("code"), message); } if(i.getStr("type").equals("tts")){ MessageUtils.sendTTs(i.getStr("code"), message); } } } } } } }catch (Exception e){ log.error("---------------------"); log.error(e.getMessage()); log.error("---------------------"); } } } } } } } }); } log.info("body:{}",body); } }