| | |
| | | import com.gps31.push.netty.PushMsg; |
| | | import com.gps31.push.netty.client.TcpClient; |
| | | import com.gps31.push.util.MapUtil; |
| | | import com.ruoyi.common.core.domain.entity.SysConfig; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.common.utils.uuid.IdUtils; |
| | | import com.ruoyi.sy.domain.ArdSyCarLock; |
| | | import com.ruoyi.sy.domain.ArdSyCarRtu; |
| | | import com.ruoyi.sy.domain.ArdTankLock; |
| | | import com.ruoyi.sy.domain.ArdTankLockState; |
| | | import com.ruoyi.sy.service.ArdSyCarLockService; |
| | | import com.ruoyi.sy.service.ArdSyCarRtuService; |
| | | import com.ruoyi.sy.service.impl.ArdSyCarDayServiceImpl; |
| | | import com.ruoyi.sy.service.impl.ArdSyCarServiceImpl; |
| | | import com.ruoyi.sy.service.impl.ArdTankAbnormalParkAlarmServiceImpl; |
| | | import com.ruoyi.sy.service.impl.*; |
| | | import com.ruoyi.system.service.impl.SysConfigServiceImpl; |
| | | import com.ruoyi.utils.forest.SYClient; |
| | | import com.ruoyi.utils.qymqtt.newM.EmqClient; |
| | | import com.ruoyi.utils.qymqtt.newM.QosEnum; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | import okhttp3.Response; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.eclipse.paho.client.mqttv3.MqttException; |
| | | |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * <p>Description: </p> |
| | |
| | | * @author chencq |
| | | * @version 1.0 |
| | | */ |
| | | @Slf4j(topic = "mqttCar") |
| | | public class PushClientImplSerialPort extends PushClient implements Runnable { |
| | | |
| | | // private static final Log log = LogFactory.getLog(PushClientImplPosition.class); |
| | | private static final Log log = LogFactory.getLog(PushClientImplSerialPort.class); |
| | | |
| | | private String ip; |
| | | private static String ip; |
| | | |
| | | private String userId; |
| | | private static String userId; |
| | | |
| | | private String password; |
| | | private static String password; |
| | | |
| | | private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | public PushClientImplSerialPort(String ip, String userId, String password) { |
| | | this.ip = ip; |
| | |
| | | */ |
| | | }else if("0401".equals(pushMsg.getCmd())) {//透传消息 |
| | | Map<String,Object> dataMap = pushMsg.getJsonMap(); |
| | | String carName = MapUtil.getStrVal(dataMap, "carName","");//获取车牌号 |
| | | //String carName = MapUtil.getStrVal(dataMap, "carName","");//获取车牌号 |
| | | //log.error(String.format(" ---->收到透传数据:%s",JSON.toJSONString(dataMap))); |
| | | Thread thread = new Thread(){ |
| | | @Override |
| | | public void run() { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date now = new Date(); |
| | | try { |
| | | String data = (String) dataMap.get("data"); |
| | | if(data.length() %2 != 0){//长度不是偶数,就返回 |
| | | return; |
| | | } |
| | | |
| | | Integer sum = 0; |
| | | for(int i = 0;i <= data.length() - 4;i = i + 2){ |
| | | String str = data.substring(i, i + 2); |
| | | sum = sum + Integer.parseInt(str, 16); |
| | | }//求和 |
| | | String checkNum = data.substring(data.length() - 2, data.length()); |
| | | String checkNumComputed = Integer.toHexString(sum).toUpperCase();//计算校验 |
| | | //当校验数据大于2个字符,截取最后两位 |
| | | if(checkNumComputed.length() != 2){ |
| | | checkNumComputed = checkNumComputed.substring(checkNumComputed.length() - 2,checkNumComputed.length()); |
| | | } |
| | | if(!checkNum.equals(checkNumComputed)){//校验不通过,就返回 |
| | | return; |
| | | } |
| | | |
| | | String lockNum = data.substring(0, 4); |
| | | String code = data.substring(4, 6); |
| | | if(code.equals("01")){//功能码01为锁实时状态 |
| | | String lockState = data.substring(6, 8); |
| | | String lockPositionState = data.substring(8, 10); |
| | | String lockShellState = data.substring(10, 12); |
| | | String restartState = data.substring(12, 14); |
| | | String batterVoltage = data.substring(14, 18); |
| | | String powerVoltage = data.substring(18, 22); |
| | | //String time = data.substring(22, 34); |
| | | String time = sdf.format(new Date()); |
| | | |
| | | ArdTankLock ardTankLock = new ArdTankLock(); |
| | | ardTankLock.setLockNum(lockNum); |
| | | List<ArdTankLock> ardTankLockResult = ((ArdTankLockServiceImpl)SpringUtils.getBean("ardTankLockServiceImpl")).selectArdTankLockList(ardTankLock); |
| | | |
| | | if(ardTankLockResult.isEmpty()){//锁没有录入则返回 |
| | | return; |
| | | } |
| | | |
| | | ArdTankLockState ardTankLockState = new ArdTankLockState(); |
| | | ardTankLockState.setId(IdUtils.simpleUUID()); |
| | | ardTankLockState.setLockId(ardTankLockResult.get(0).getId()); |
| | | ardTankLockState.setLockNum(lockNum); |
| | | switch (lockState) {//锁芯状态 |
| | | case "01": ardTankLockState.setLockState("开锁状态");break; |
| | | case "02": ardTankLockState.setLockState("关锁状态");break; |
| | | case "03": ardTankLockState.setLockState("异常状态");break; |
| | | case "04": ardTankLockState.setLockState("操控中...");break; |
| | | default: |
| | | break; |
| | | } |
| | | switch (lockPositionState) {//位置状态 |
| | | case "01": ardTankLockState.setLockPositionState("正常");break; |
| | | case "02": ardTankLockState.setLockPositionState("异常");break; |
| | | default: |
| | | break; |
| | | } |
| | | switch (lockShellState) {//外壳状态 |
| | | case "01": ardTankLockState.setLockShellState("正常");break; |
| | | case "02": ardTankLockState.setLockShellState("异常");break; |
| | | default: |
| | | break; |
| | | } |
| | | switch (restartState) {//复位状态 |
| | | case "01": ardTankLockState.setRestartState("开锁状态");break; |
| | | case "02": ardTankLockState.setRestartState("关锁状态");break; |
| | | default: |
| | | break; |
| | | } |
| | | Number batterVoltageNum = Integer.parseInt(batterVoltage,16) * 0.1; |
| | | String batterVoltageStr = String.valueOf(batterVoltageNum); |
| | | if(batterVoltageStr.length() > 4){ |
| | | batterVoltageStr = batterVoltageStr.substring(0,4); |
| | | } |
| | | if(batterVoltageStr.contains(".")){//四舍五入 |
| | | String decimalPart = batterVoltageStr.split("\\.")[1]; |
| | | if(Integer.parseInt(decimalPart) > 5){ |
| | | batterVoltageStr = batterVoltageStr.split("\\.")[0] + ".5"; |
| | | }else if(Integer.parseInt(decimalPart) <= 5){ |
| | | batterVoltageStr = batterVoltageStr.split("\\.")[0] + ".0"; |
| | | } |
| | | }else{ |
| | | batterVoltageStr = batterVoltageStr + ".0"; |
| | | } |
| | | ardTankLockState.setBatterVoltage(batterVoltageStr); |
| | | |
| | | |
| | | Number powerVoltageNum = Integer.parseInt(powerVoltage,16) * 0.1; |
| | | String powerVoltageStr = String.valueOf(powerVoltageNum); |
| | | if(powerVoltageStr.length() > 4){ |
| | | powerVoltageStr = powerVoltageStr.substring(0,4); |
| | | } |
| | | if(powerVoltageStr.contains(".")){//四舍五入 |
| | | String decimalPart = powerVoltageStr.split("\\.")[1]; |
| | | if(Integer.parseInt(decimalPart) > 5){ |
| | | powerVoltageStr = powerVoltageStr.split("\\.")[0] + ".5"; |
| | | }else if(Integer.parseInt(decimalPart) <= 5){ |
| | | powerVoltageStr = powerVoltageStr.split("\\.")[0] + ".0"; |
| | | } |
| | | }else{ |
| | | powerVoltageStr = powerVoltageStr + ".0"; |
| | | } |
| | | ardTankLockState.setPowerVoltage(powerVoltageStr); |
| | | |
| | | String uniqueMark = lockNum + "," + lockState + "," + lockPositionState + "," + lockShellState |
| | | + "," + restartState + "," + batterVoltageStr + "," + powerVoltageStr; |
| | | |
| | | ardTankLockState.setUploadTime(time); |
| | | ardTankLockState.setUniqueMark(uniqueMark); |
| | | ((ArdTankLockStateServiceImpl)SpringUtils.getBean("ardTankLockStateServiceImpl")).insertOrUpdateArdTankLockState(ardTankLockState); |
| | | }else if(code.equals("05")){//功能码05为读取锁密码 |
| | | Set<String> passwordAndTimeSet = new HashSet(); |
| | | for(int i = 1;i <= 10;i++){ |
| | | String passwordAndTime = data.substring(6 + 24*(i - 1),6 + 24 * i); |
| | | passwordAndTimeSet.add(passwordAndTime);//拆分出密码和使用时间 |
| | | } |
| | | if(passwordAndTimeSet.size() == 1){//全部为初始密码,则从新设置 |
| | | Set<String> passwordSet = new HashSet(); |
| | | while(passwordSet.size() != 10){ |
| | | String password = ""; |
| | | for(int i = 0;i <= 5;i++){ |
| | | password = password + String.valueOf(10 * Math.random()).split("\\.")[0]; |
| | | } |
| | | passwordSet.add(password); |
| | | } |
| | | if(PositionContainer.getCarLockPasswordMap().get(lockNum) == null){ |
| | | PositionContainer.getCarLockPasswordMap().put(lockNum,passwordSet); |
| | | } |
| | | }else{//查看哪些密码使用过 |
| | | for(String passwordAndTime : passwordAndTimeSet){ |
| | | String time = passwordAndTime.substring(12,24); |
| | | if(!time.equals("FFFFFFFFFFFF")){ |
| | | String passwordHex = passwordAndTime.substring(0,12);//16进制密码 |
| | | String password = "";//实际密码 |
| | | for(int i = 0;i <= 10;i = i + 2){ |
| | | String passwordHexByte = passwordHex.substring(i, i+2); |
| | | password = password + passwordHexByte.split("")[1]; |
| | | } |
| | | |
| | | String useTimeHex = passwordAndTime.substring(12,24);//16进制时间 |
| | | String yearHex = useTimeHex.substring(0, 2); |
| | | String monthHex = useTimeHex.substring(2, 4); |
| | | String dayHex = useTimeHex.substring(4, 6); |
| | | String hourHex = useTimeHex.substring(6, 8); |
| | | String minuteHex = useTimeHex.substring(8, 10); |
| | | String secondHex = useTimeHex.substring(10, 12); |
| | | |
| | | String useTime = ""; |
| | | Integer yearNum = Integer.parseInt(yearHex, 16); |
| | | if(yearNum <= 9){ |
| | | useTime = "200" + yearNum + "-"; |
| | | }else{ |
| | | useTime = "20" + yearNum + "-"; |
| | | } |
| | | |
| | | Integer monthNum = Integer.parseInt(monthHex, 16); |
| | | if(monthNum <= 9){ |
| | | useTime = useTime + "0" + monthNum + "-"; |
| | | }else{ |
| | | useTime = useTime + monthNum + "-"; |
| | | } |
| | | |
| | | Integer dayNum = Integer.parseInt(dayHex, 16); |
| | | if(dayNum <= 9){ |
| | | useTime = useTime + "0" + dayNum + " "; |
| | | }else{ |
| | | useTime = useTime + dayNum + " "; |
| | | } |
| | | |
| | | Integer hourNum = Integer.parseInt(hourHex, 16); |
| | | if(hourNum <= 9){ |
| | | useTime = useTime + "0" + hourNum + ":"; |
| | | }else{ |
| | | useTime = useTime + hourNum + ":"; |
| | | } |
| | | |
| | | Integer minuteNum = Integer.parseInt(minuteHex, 16); |
| | | if(minuteNum <= 9){ |
| | | useTime = useTime + "0" + minuteNum + ":"; |
| | | }else{ |
| | | useTime = useTime + minuteNum + ":"; |
| | | } |
| | | |
| | | Integer secondNum = Integer.parseInt(secondHex, 16); |
| | | if(secondNum <= 9){ |
| | | useTime = useTime + "0" + secondNum; |
| | | }else{ |
| | | useTime = useTime + secondNum; |
| | | } |
| | | ((ArdTankLockPasswordServiceImpl)SpringUtils.getBean("ardTankLockPasswordServiceImpl")).updateArdTankLockPasswordByLockNum(lockNum,password,useTime); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return;//强转失败返回 |
| | | } |
| | | } |
| | | }; |
| | | thread.start(); |
| | | /** |
| | | * |
| | | * 请加入三方集成方的业务逻辑 |
| | |
| | | sendMassage(); |
| | | } |
| | | |
| | | private void feng(String carId,String type,ArdSyCarRtuService carRtuService,ArdSyCarLockService lockService) throws MqttException { |
| | | //查询装油点既定Rtu状态 |
| | | ArdSyCarRtu ardSyCarRtu = carRtuService.one(carId); |
| | | if(ardSyCarRtu!=null){ |
| | | //查询锁信息 |
| | | List<ArdSyCarLock> ardSyCarLocks = lockService.carLock(ardSyCarRtu.getId()); |
| | | if(ardSyCarLocks.size()>0){ |
| | | Integer fengType = null; |
| | | if("装油点".equals(type)){ |
| | | fengType = ardSyCarRtu.getInstallType(); |
| | | }else if("卸油点".equals(type)){ |
| | | fengType = ardSyCarRtu.getUnloadType(); |
| | | }else if("施封".equals(type)){ |
| | | fengType = 0; |
| | | } |
| | | //1解封 |
| | | if(fengType==1){ |
| | | //如果不是解封封状态,则发送施封命令 |
| | | if(ardSyCarLocks.get(0).getRelayInfo()==0 || ardSyCarLocks.get(0).getRelayInfo()==null){ |
| | | // Map<String,Object> map = new HashMap<>(); |
| | | // map.put("A01", 110000);//导通第一路继电器 |
| | | // map.put("A02", 110000);//导通第二路继电器 |
| | | // map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | // String mapString = com.alibaba.fastjson.JSON.toJSONString(map); |
| | | String mapString = "{\"A01\":110000,\"A02\":110000,\"res\":\""+String.valueOf(System.currentTimeMillis())+"\"}"; |
| | | log.debug("自动解封:"+mapString); |
| | | EmqClient emqClient = new EmqClient(carId+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | log.debug("车辆"+carId+"解封命令发送成功!"); |
| | | // mqttOnce.publishCar(carId,ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"解封"); |
| | | } |
| | | } |
| | | public static Map<String,Object> loginIn(String syURL,String userId,String password){//登录 |
| | | OkHttpClient okHttpClient = new OkHttpClient(); |
| | | |
| | | //0施封 |
| | | if(fengType==0){ |
| | | if(ardSyCarLocks.get(0).getRelayInfo()==1 || ardSyCarLocks.get(0).getRelayInfo()==null){ |
| | | //锁开关状态 |
| | | int lockCloseNum = 0; |
| | | String mapString1 = "{\"A01\":100000,\"A02\":100000,\"res\":\""+String.valueOf(System.currentTimeMillis())+"\"}"; |
| | | System.out.println("自动施封:"+mapString1); |
| | | EmqClient emqClient1 = new EmqClient(carId+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient1.connect(); |
| | | emqClient1.publish(ardSyCarRtu.getTopicPublish(),mapString1, QosEnum.QoS2,false); |
| | | //如果不是施封状态,则发送施封命令 |
| | | if(ardSyCarLocks.get(0).getRelayInfo()!=null){ |
| | | if(ardSyCarLocks.get(0).getRelayInfo()==1){ |
| | | for (int j = 0; j < ardSyCarLocks.size(); j++) { |
| | | ArdSyCarLock ardSyCarLock = ardSyCarLocks.get(j); |
| | | String currents = ardSyCarLock.getCurrents(); |
| | | Double currentInfo = ardSyCarLock.getCurrentInfo(); |
| | | //电流值为4.0是关锁状态 |
| | | if(currentInfo.equals(4.00) || currentInfo.equals(4)){ |
| | | lockCloseNum += 1; |
| | | }else { |
| | | String key = ""; |
| | | if(currents.equals("C01")){ |
| | | key = "D03"; |
| | | }else if(currents.equals("C02")){ |
| | | key = "D04"; |
| | | } |
| | | // Map<String,Object> map = new HashMap(); |
| | | // map.put(key, 0.01); |
| | | // map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | // String mapString = com.alibaba.fastjson.JSON.toJSONString(map); |
| | | String mapString = "{\""+key+"\":0.01,\"res\":\""+String.valueOf(System.currentTimeMillis())+"\"}"; |
| | | log.debug("自动关锁:"+mapString); |
| | | EmqClient emqClient = new EmqClient(carId+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | // mqttOnce.publishCar("carId",ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"开锁:"+currents); |
| | | while(true){ |
| | | ArdSyCarLock ardSyCarLock1 = lockService.getCurrentInfo(ardSyCarRtu.getId(),currents); |
| | | Double currentInfo1 = ardSyCarLock1.getCurrentInfo(); |
| | | if(currentInfo1.equals(4.00) || currentInfo1.equals(4)){ |
| | | lockCloseNum +=1; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if(lockCloseNum == 2){ |
| | | // Map<String,Object> map = new HashMap<>(); |
| | | // map.put("A01", 100000);//断开第一路继电器 |
| | | // map.put("A02", 100000);//断开第二路继电器 |
| | | // map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | // String mapString = com.alibaba.fastjson.JSON.toJSONString(map); |
| | | log.debug("车辆"+carId+"施封命令发送成功!"); |
| | | // mqttOnce.publishCar(carId,ardSyCarRtu.getTopicPublish(),String.valueOf(new JSONObject(map)),"施封"); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | String passwordMd5 = DigestUtils.md5Hex(password); |
| | | |
| | | public Double[] azimuth_offset(double origin_lon, double origin_lat, Integer direction,double distance){ |
| | | Double[] lonlat = new Double[2]; |
| | | if(direction != null && distance > 0){ |
| | | lonlat[0] = origin_lon + distance * Math.sin(direction* Math.PI / 180) * 180 / ( Math.PI * 6371229 * Math.cos(origin_lat * Math.PI / 180)); |
| | | lonlat[1] = origin_lat + distance * Math.cos(direction* Math.PI / 180) / ( Math.PI * 6371229 / 180); |
| | | }else{ |
| | | lonlat[0] = origin_lon; |
| | | lonlat[1] = origin_lat; |
| | | } |
| | | return lonlat; |
| | | } |
| | | |
| | | public Double getDistance(Double velocity ,Double acceleration ,String time){ |
| | | Double distance = velocity*Double.parseDouble(time) + 0.5*acceleration*Double.parseDouble(time)*Double.parseDouble(time); |
| | | return distance; |
| | | } |
| | | |
| | | /*public Double getVelocity(Double velocity ,Double acceleration ,String time){ |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Long t = (long) 0; |
| | | Request request = new Request.Builder() |
| | | .url(syURL + "/gps-web/api/login.jsp?"+"password="+passwordMd5+"&userId="+userId+"&loginType=user&loginWay=interface&loginLang=zh_CN") |
| | | .build(); |
| | | Response response = null; |
| | | try { |
| | | t = (new Date().getTime() - simpleDateFormat.parse(time).getTime())/1000; |
| | | } catch (Exception e) { |
| | | response = okHttpClient.newCall(request).execute(); |
| | | } catch (IOException e1) { |
| | | // TODO Auto-generated catch block |
| | | e1.printStackTrace(); |
| | | } |
| | | |
| | | response.headers(); //响应头 |
| | | |
| | | okhttp3.ResponseBody responseBody = response.body(); |
| | | |
| | | try { |
| | | String message = responseBody.string();// 响应体 |
| | | Map<String,Object> map = (Map<String, Object>) JSON.parse(message); |
| | | map.put("passwordMd5", passwordMd5); |
| | | //System.out.println(message); |
| | | return map; |
| | | } catch (IOException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | return new HashMap(); |
| | | } |
| | | } |
| | | |
| | | public Map<String,Object> sendCmd(String syURL,String userId,String carId,Integer cmdId,String cmd,String paramsStr,String sessionId){ |
| | | OkHttpClient okHttpClient = new OkHttpClient(); |
| | | |
| | | Request request = new Request.Builder() |
| | | .url(syURL + "/gps-web/api/send_cmd.jsp?params="+paramsStr+"&userId="+userId+"&carId="+carId+"&cmdId="+cmdId+"&cmd="+cmd+"&sessionId="+sessionId) |
| | | .build(); |
| | | //System.out.println("url = " + syURL + "/gps-web/api/send_cmd.jsp?params="+paramsStr+"&userId="+userId+"&carId="+carId+"&cmdId="+cmdId+"&cmd="+cmd+"&sessionId="+sessionId); |
| | | Response response = null; |
| | | try { |
| | | response = okHttpClient.newCall(request).execute(); |
| | | } catch (IOException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | Double velocityt = velocity + acceleration*t; |
| | | return velocityt; |
| | | }*/ |
| | | |
| | | okhttp3.ResponseBody responseBody = response.body(); |
| | | |
| | | try { |
| | | String message = responseBody.string();// 响应体 |
| | | Map<String,Object> map = (Map<String, Object>) JSON.parse(message); |
| | | return map; |
| | | } catch (IOException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | return new HashMap(); |
| | | } |
| | | } |
| | | |
| | | } |