| | |
| | | package com.ruoyi.sy.service.impl; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | import java.util.function.Predicate; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.uuid.IdUtils; |
| | | import com.ruoyi.common.websocket.util.WebSocketUtils; |
| | | import com.ruoyi.sy.domain.ArdSyCarDay; |
| | | import com.ruoyi.sy.mapper.ArdSyCarDayMapper; |
| | | import com.ruoyi.utils.websocket.util.WebSocketUtils; |
| | | import com.ruoyi.device.hiksdk.config.MinioClientSingleton; |
| | | import com.ruoyi.device.hiksdk.util.minio.MinioUtils; |
| | | import com.ruoyi.storage.minio.utils.MinioUtils; |
| | | import com.ruoyi.sy.domain.ArdSyUser; |
| | | import com.ruoyi.sy.gps31.PositionContainer; |
| | | import com.ruoyi.sy.mapper.ArdSyUserMapper; |
| | |
| | | import io.minio.Result; |
| | | import io.minio.http.Method; |
| | | import io.minio.messages.Item; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | import okhttp3.Response; |
| | | import okhttp3.ResponseBody; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ruoyi.sy.mapper.ArdSyCarMapper; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import static com.ruoyi.common.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS; |
| | | import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS; |
| | | |
| | | /** |
| | | * 三一车辆Service业务层处理 |
| | |
| | | @Resource |
| | | private SYClient sYClient; |
| | | |
| | | @Resource |
| | | private ArdSyCarDayMapper ardSyCarDayMapper; |
| | | |
| | | private Map<Integer,Map<String,String>> logInMap = new HashMap(); |
| | | |
| | | private Map<String, Timer> userCarTimerMap = new HashMap(); |
| | | |
| | | /** |
| | | * 查询三一车辆 |
| | |
| | | @Override |
| | | public Map<String, Object> getArdSyCarAndDeptByCarId(String carId) { |
| | | Map<String, Object> result = ardSyCarMapper.getArdSyCarAndDeptByCarId(carId); |
| | | if(result != null){ |
| | | if(result.get("carPicture") != null){ |
| | | if(!((String)result.get("carPicture")).equals("")){ |
| | | String url = ((String)result.get("carPicture")).split("sycar")[0]; |
| | | String carModel = url + (String)result.get("carModel"); |
| | | result.put("carModel",carModel); |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public void sendArdSyCarPosition() { |
| | | for(String key : ONLINE_USER_SESSIONS.keySet()){ |
| | | SysUser sysUser = userMapper.selectUserById(key); |
| | | if(sysUser == null){ |
| | | return; |
| | | if(PositionContainer.getUserPositionMap().keySet().contains(key)){//当追踪的车辆容器包含此用户Id,则实时位置推送需要剔除该车辆 |
| | | SysUser sysUser = userMapper.selectUserById(key); |
| | | if(sysUser == null){ |
| | | //return; |
| | | continue; |
| | | } |
| | | String deptId = String.valueOf(sysUser.getDeptId()); |
| | | Map<String,Map<String,Object>> map = PositionContainer.getDeptPositionMap().get(deptId); |
| | | List<String> carIdList = new ArrayList(); |
| | | carIdList.addAll(PositionContainer.getUserPositionMap().get(key).keySet()); |
| | | String carId = carIdList.get(0); |
| | | map.remove(carId); |
| | | if(map != null){ |
| | | Map<String,Object> data = new HashMap(); |
| | | data.put("30000", map.values()); |
| | | WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(key), JSON.toJSONString(data)); |
| | | } |
| | | }else{ |
| | | SysUser sysUser = userMapper.selectUserById(key); |
| | | if(sysUser == null){ |
| | | //return; |
| | | continue; |
| | | } |
| | | String deptId = String.valueOf(sysUser.getDeptId()); |
| | | Map<String,Map<String,Object>> map = PositionContainer.getDeptPositionMap().get(deptId); |
| | | if(map != null){ |
| | | Map<String,Object> data = new HashMap(); |
| | | data.put("30000", map.values()); |
| | | WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(key), JSON.toJSONString(data)); |
| | | } |
| | | } |
| | | String deptId = String.valueOf(sysUser.getDeptId()); |
| | | Map<String,Map<String,Object>> map = PositionContainer.getDeptPositionMap().get(deptId); |
| | | if(map != null){ |
| | | Map<String,Object> data = new HashMap(); |
| | | data.put("30000", map.values()); |
| | | WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(key), JSON.toJSONString(data)); |
| | | } |
| | | for(String userId : userCarTimerMap.keySet()){//用户离线后将车辆追踪资源释放 |
| | | if(!ONLINE_USER_SESSIONS.keySet().contains(userId)){ |
| | | userCarTimerMap.get(userId).cancel(); |
| | | userCarTimerMap.remove(userId); |
| | | PositionContainer.getUserPositionMap().remove(userId); |
| | | } |
| | | } |
| | | } |
| | |
| | | for (int i = 0; i < carList.size(); i++) { |
| | | ArdSyCar ardSyCar = carList.get(i); |
| | | if(carId.equals(ardSyCar.getCarId())){ |
| | | m.put("carModel",ardSyCar.getCarModel()); |
| | | String url = ""; |
| | | if(ardSyCar.getCarPicture() != null){ |
| | | if(!ardSyCar.getCarPicture().equals("")){ |
| | | url = ardSyCar.getCarPicture().split("sycar")[0]; |
| | | } |
| | | } |
| | | m.put("carModel",url + ardSyCar.getCarModel());//zns |
| | | //m.put("carModel",ardSyCar.getCarModel()); |
| | | m.put("carTypeArd",ardSyCar.getCarType()); |
| | | m.put("carBrand",ardSyCar.getCarBrand()); |
| | | m.put("carPicture",ardSyCar.getCarPicture()); |
| | |
| | | result0 = sYClient.getCarNearPositionByCarId(syURL,carId,result.get("userId"),result.get("sessionId")); |
| | | return Results.succeed(result0); |
| | | } |
| | | |
| | | @Override |
| | | public Results chaseCarByCarId(String usersId, String carId, String cycle) { |
| | | try{ |
| | | /*Map<String,Map<String,Object>> map = new HashMap(); |
| | | map.put(carId,new HashMap()); |
| | | PositionContainer.getUserPositionMap().put(usersId,map);*/ |
| | | if(cycle.equals("0")){ |
| | | if(userCarTimerMap.get(usersId) != null){ |
| | | userCarTimerMap.get(usersId).cancel(); |
| | | userCarTimerMap.remove(usersId); |
| | | PositionContainer.getUserPositionMap().remove(usersId); |
| | | } |
| | | return Results.succeed("停止追踪"); |
| | | }else{ |
| | | if(userCarTimerMap.get(usersId) != null){ |
| | | userCarTimerMap.get(usersId).cancel(); |
| | | userCarTimerMap.remove(usersId); |
| | | PositionContainer.getUserPositionMap().remove(usersId); |
| | | Map<String,Map<String,Object>> map = new HashMap(); |
| | | map.put(carId,new HashMap()); |
| | | PositionContainer.getUserPositionMap().put(usersId,map); |
| | | Date date = new Date(); |
| | | Timer carPositionTimer = new Timer();//定时推送实时位置 |
| | | TimerTask carPositionTask =new TimerTask(){ |
| | | @Override |
| | | public void run(){ |
| | | Map<String,Object> position = PositionContainer.getUserPositionMap().get(usersId).get(carId); |
| | | if(position != null){ |
| | | if(position.size() != 0){ |
| | | Map<String,Map<String,Object>> data = new HashMap(); |
| | | data.put("31000",position); |
| | | WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(usersId), JSON.toJSONString(data)); |
| | | } |
| | | } |
| | | } |
| | | }; |
| | | carPositionTimer.scheduleAtFixedRate(carPositionTask,date,Integer.parseInt(cycle));//启用 |
| | | this.userCarTimerMap.put(usersId,carPositionTimer); |
| | | return Results.succeed("开始追踪"); |
| | | }else{ |
| | | Map<String,Map<String,Object>> map = new HashMap(); |
| | | map.put(carId,new HashMap()); |
| | | PositionContainer.getUserPositionMap().put(usersId,map); |
| | | Date date = new Date(); |
| | | Timer carPositionTimer = new Timer();//定时推送实时位置 |
| | | TimerTask carPositionTask =new TimerTask(){ |
| | | @Override |
| | | public void run(){ |
| | | Map<String,Object> position = PositionContainer.getUserPositionMap().get(usersId).get(carId); |
| | | if(position != null){ |
| | | if(position.size() != 0){ |
| | | Map<String,Map<String,Object>> data = new HashMap(); |
| | | data.put("31000",position); |
| | | WebSocketUtils.sendMessage(ONLINE_USER_SESSIONS.get(usersId), JSON.toJSONString(data)); |
| | | } |
| | | } |
| | | } |
| | | }; |
| | | carPositionTimer.scheduleAtFixedRate(carPositionTask,date,Integer.parseInt(cycle));//启用 |
| | | this.userCarTimerMap.put(usersId,carPositionTimer); |
| | | return Results.succeed("开始追踪"); |
| | | } |
| | | } |
| | | }catch(Exception e){ |
| | | e.printStackTrace(); |
| | | return Results.error("追踪失败"); |
| | | } |
| | | } |
| | | |
| | | /*@Override |
| | | public Results getExistTraceByCarIdAndMonth(String userId, String carId, String time) { |
| | | SysConfig config = new SysConfig(); |
| | | config.setConfigKey("syCarPT"); |
| | | List<SysConfig> sysConfigResult = sysConfigMapper.selectConfigList(config); |
| | | String syURL = ""; |
| | | if(sysConfigResult.size() == 0){ |
| | | return Results.succeed("三一车辆url没有录入"); |
| | | }else{ |
| | | syURL = sysConfigResult.get(0).getConfigValue(); |
| | | ArdSyUser ardSyUserPara = new ArdSyUser(); |
| | | ardSyUserPara.setSysUserId(userId); |
| | | List<ArdSyUser> ardSyUserList = ardSyUserMapper.selectArdSyUserList(ardSyUserPara); |
| | | if(ardSyUserList.size() == 0){ |
| | | return Results.succeed("用户未挂接三一车辆"); |
| | | }else{ |
| | | ArdSyUser ardSyUser = ardSyUserList.get(0); |
| | | String passwordMd5 = DigestUtils.md5Hex(ardSyUser.getPassword()); |
| | | Map<String,Integer> existTraceMap = getExistTraceByCarIdAndMonth(syURL,ardSyUser.getUserId(),passwordMd5,carId,time); |
| | | return Results.succeed(existTraceMap); |
| | | } |
| | | } |
| | | }*/ |
| | | |
| | | @Override |
| | | public Results getExistTraceByCarIdAndMonth(String userId, String carId, String time) { |
| | | List<ArdSyCarDay> result = ardSyCarDayMapper.selectArdSyCarDayByCarIdAndDay(carId,time + "%"); |
| | | List<String> list = new ArrayList(); |
| | | for(ArdSyCarDay ardSyCarDay : result){ |
| | | // list.add(ardSyCarDay.getDay().split("-")[2]); |
| | | list.add(ardSyCarDay.getDay()); |
| | | } |
| | | return Results.succeed(list); |
| | | } |
| | | |
| | | public Map<String,Integer> getExistTraceByCarIdAndMonth(String url,String usersId,String password,String carId,String time){ |
| | | Map<String,Object> LogInResult = sYClient.logIn(url,password,usersId); |
| | | String sessionId = (String) LogInResult.get("sessionId"); |
| | | |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); |
| | | Date date = null; |
| | | try { |
| | | date = sdf.parse(time); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//获取该月天数 |
| | | Map<String,Integer> existTraceMap = new HashMap(); |
| | | ExecutorService newCachedThreadPoll = Executors.newCachedThreadPool(); |
| | | for (int i = 1; i <= maxDay; i = i + 7) { |
| | | int temp = i; |
| | | String finalTime = time.replace("-",""); |
| | | String finalsyURL = url; |
| | | newCachedThreadPoll.execute(new Runnable(){ |
| | | @Override |
| | | public void run() { |
| | | int temp0 = temp + 6; |
| | | if(temp0 > maxDay){ |
| | | temp0 = maxDay; |
| | | } |
| | | for (int j = temp; j <= temp0; j++) { |
| | | if(j <= 9){ |
| | | existTraceMap.put("0" + String.valueOf(j), -1); |
| | | }else{ |
| | | existTraceMap.put(String.valueOf(j), -1); |
| | | } |
| | | } |
| | | if(temp <= 9){ |
| | | if(temp0 <= 9){ |
| | | String startTime = finalTime + "0" + temp + "000000"; |
| | | String endTime = finalTime + "0" + temp0 + "235959"; |
| | | Map<String,Object> result = getCarGPSTrack(finalsyURL,carId,startTime,endTime,sessionId); |
| | | String rspCode = (String)result.get("rspCode"); |
| | | if(rspCode.equals("1")){ |
| | | List<Map<String,Object>> list = (List<Map<String, Object>>) result.get("list"); |
| | | for(Map<String,Object> m : list){ |
| | | String time = (String) m.get("time"); |
| | | time = time.split(" ")[0]; |
| | | time = time.split("-")[2]; |
| | | existTraceMap.put(time, 1); |
| | | } |
| | | } |
| | | }else{ |
| | | String startTime = finalTime + "0" + temp + "000000"; |
| | | String endTime = finalTime + temp0 + "235959"; |
| | | Map<String,Object> result = getCarGPSTrack(finalsyURL,carId,startTime,endTime,sessionId); |
| | | String rspCode = (String)result.get("rspCode"); |
| | | if(rspCode.equals("1")){ |
| | | List<Map<String,Object>> list = (List<Map<String, Object>>) result.get("list"); |
| | | for(Map<String,Object> m : list){ |
| | | String time = (String) m.get("time"); |
| | | time = time.split(" ")[0]; |
| | | time = time.split("-")[2]; |
| | | existTraceMap.put(time, 1); |
| | | } |
| | | } |
| | | } |
| | | }else{ |
| | | String startTime = finalTime + temp + "000000"; |
| | | String endTime = finalTime + temp0 + "235959"; |
| | | Map<String,Object> result = getCarGPSTrack(finalsyURL,carId,startTime,endTime,sessionId); |
| | | String rspCode = (String)result.get("rspCode"); |
| | | if(rspCode.equals("1")){ |
| | | List<Map<String,Object>> list = (List<Map<String, Object>>) result.get("list"); |
| | | for(Map<String,Object> m : list){ |
| | | String time = (String) m.get("time"); |
| | | time = time.split(" ")[0]; |
| | | time = time.split("-")[2]; |
| | | existTraceMap.put(time, 1); |
| | | } |
| | | } |
| | | } |
| | | for(int j = temp; j <= temp0; j++){ |
| | | String key = ""; |
| | | if(j <= 9){ |
| | | key = "0" + j; |
| | | }else{ |
| | | key = String.valueOf(j); |
| | | } |
| | | if(existTraceMap.get(key) == -1){ |
| | | existTraceMap.put(key, 0); |
| | | } |
| | | |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | while(true){ |
| | | if(!existTraceMap.values().contains(-1)){ |
| | | if(existTraceMap.size() == maxDay){ |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | try { |
| | | return existTraceMap; |
| | | } finally { |
| | | newCachedThreadPoll.shutdown(); |
| | | } |
| | | } |
| | | |
| | | public static Map<String,Object> getCarGPSTrack(String syURL,String carId,String startTime,String endTime,String sessionId){ |
| | | OkHttpClient okHttpClient = new OkHttpClient(); |
| | | |
| | | Request request = new Request.Builder() |
| | | .url(syURL + "/gps-web/api/get_gps_h.jsp?carId="+carId+"&startTime="+startTime+"&endTime="+endTime+"&filter0=true&maxSpeed=&stopLong=5&queryAlarm=false&userId=yanshi&loginType=user&loginWay=android&loginLang=zh_CN&appDevId=&sessionId="+sessionId) |
| | | .build(); |
| | | |
| | | Response response = null; |
| | | try { |
| | | response = okHttpClient.newCall(request).execute(); |
| | | } catch (IOException e1) { |
| | | // TODO Auto-generated catch block |
| | | e1.printStackTrace(); |
| | | } |
| | | |
| | | response.headers(); //响应头 |
| | | |
| | | ResponseBody responseBody = response.body(); |
| | | |
| | | try { |
| | | String message = responseBody.string();// 响应体 |
| | | Map<String,Object> map = (Map<String, Object>) JSON.parse(message); |
| | | //System.out.println(message); |
| | | return map; |
| | | } catch (IOException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | return new HashMap(); |
| | | } |
| | | } |
| | | } |