| | |
| | | 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.ArdTankLockServiceImpl; |
| | | 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.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | * @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 Timer lockStateTimer; |
| | | private static String password; |
| | | |
| | | public PushClientImplSerialPort(String ip, String userId, String password) { |
| | | this.ip = ip; |
| | | this.userId = userId; |
| | | this.password = password; |
| | | |
| | | /*Date date = new Date(); |
| | | this.lockStateTimer = new Timer();//定时获取各个锁的状态 |
| | | TimerTask lockStateTask =new TimerTask(){ |
| | | @Override |
| | | public void run(){ |
| | | SysConfig config = new SysConfig(); |
| | | config.setConfigKey("syCarPT"); |
| | | List<SysConfig> sysConfigResult = ((SysConfigServiceImpl)SpringUtils.getBean("sysConfigServiceImpl")).selectConfigList(config); |
| | | String syURL = ""; |
| | | |
| | | if(sysConfigResult.size() == 0){//三一车辆加入开关 |
| | | return; |
| | | }else{ |
| | | syURL = sysConfigResult.get(0).getConfigValue(); |
| | | } |
| | | String passwordMd5 = DigestUtils.md5Hex(password); |
| | | Map<String, Object> LogInResult = ((SYClient)SpringUtils.getBean("sYClient")).logIn(syURL, passwordMd5, userId); |
| | | String sessionId = (String) LogInResult.get("sessionId"); |
| | | |
| | | String finalSyURL = syURL; |
| | | Thread thread = new Thread(finalSyURL){ |
| | | @Override |
| | | public void run() { |
| | | List<ArdTankLock> result = ((ArdTankLockServiceImpl)SpringUtils.getBean("ardTankLockServiceImpl")).getArdTankLockAll();//查询全部电磁锁 |
| | | for(ArdTankLock ardTankLock : result){ |
| | | //Thread LockThread = new Thread(finalSyURL){ |
| | | String lockNumHead = ardTankLock.getLockNum().substring(0,2); |
| | | String lockNumTail = ardTankLock.getLockNum().substring(2,4); |
| | | //计算校验 |
| | | String checkNum = Integer.toHexString(Integer.parseInt(lockNumHead, 16) + Integer.parseInt(lockNumTail, 16) + Integer.parseInt("01", 16)); |
| | | |
| | | if(checkNum.length() == 1){ |
| | | checkNum = "0" + checkNum; |
| | | }else if(checkNum.length() > 2){ |
| | | checkNum = checkNum.substring(checkNum.length() - 2,checkNum.length()); |
| | | } |
| | | |
| | | String paramsStr = "%7B%22type%22%3A%22ff%22%2C%22dataCnt%22%3A%22" + ardTankLock.getLockNum() + "01" + checkNum + "%22%7D"; |
| | | |
| | | //Map<String,Object> result = ((SYClient)SpringUtils.getBean("sYClient")).sendCmd(finalSyURL,userId,ardTankLock.getCarId(),199,"DataDownTransfer", paramsStr,sessionId); |
| | | //}; |
| | | //LockThread.start(); |
| | | } |
| | | } |
| | | }; |
| | | thread.start(); |
| | | } |
| | | }; |
| | | lockStateTimer.scheduleAtFixedRate(lockStateTask,date,1000);*/ |
| | | } |
| | | |
| | | public PushClientImplSerialPort() { |
| | |
| | | */ |
| | | }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 year = time.substring(0, 2); |
| | | String month = time.substring(2, 4); |
| | | String day = time.substring(4, 6); |
| | | String hour = time.substring(6, 8); |
| | | String minute = time.substring(8, 10); |
| | | String second = time.substring(10, 12); |
| | | |
| | | Integer yearNum = Integer.parseInt(year, 16); |
| | | if(yearNum <= 9){ |
| | | time = "200" + yearNum + "-"; |
| | | }else{ |
| | | time = "20" + yearNum + "-"; |
| | | } |
| | | |
| | | Integer monthNum = Integer.parseInt(month, 16); |
| | | if(monthNum <= 9){ |
| | | time = time + "0" + monthNum + "-"; |
| | | }else{ |
| | | time = time + monthNum + "-"; |
| | | } |
| | | |
| | | Integer dayNum = Integer.parseInt(day, 16); |
| | | if(dayNum <= 9){ |
| | | time = time + "0" + dayNum + " "; |
| | | }else{ |
| | | time = time + dayNum + " "; |
| | | } |
| | | |
| | | Integer hourNum = Integer.parseInt(hour, 16); |
| | | if(hourNum <= 9){ |
| | | time = time + "0" + hourNum + ":"; |
| | | }else{ |
| | | time = time + hourNum + ":"; |
| | | } |
| | | |
| | | Integer minuteNum = Integer.parseInt(minute, 16); |
| | | if(minuteNum <= 9){ |
| | | time = time + "0" + minuteNum + ":"; |
| | | }else{ |
| | | time = time + minuteNum + ":"; |
| | | } |
| | | |
| | | Integer secondNum = Integer.parseInt(second, 16); |
| | | if(secondNum <= 9){ |
| | | time = time + "0" + secondNum; |
| | | }else{ |
| | | time = time + secondNum; |
| | | } |
| | | |
| | | ArdTankLock ardTankLock = new ArdTankLock(); |
| | | ardTankLock.setLockNum(lockNum); |
| | | List<ArdTankLock> ardTankLockResult = ((ArdTankLockServiceImpl)SpringUtils.getBean("ardTankLockServiceImpl")).selectArdTankLockList(ardTankLock); |
| | | |
| | | if(ardTankLockResult.isEmpty()){//锁没有录入则返回 |
| | | return; |
| | | } |
| | | |
| | | long cha = now.getTime() - sdf.parse(time).getTime(); |
| | | if(Math.abs(cha) >= 5 * 1000){ |
| | | //时间校准 |
| | | /*String url = "http://" + ip + ":9999"; |
| | | String passwordMd5 = DigestUtils.md5Hex(password); |
| | | Map<String, Object> LogInResult = loginIn(url, userId, passwordMd5); |
| | | String sessionId = (String) LogInResult.get("sessionId"); |
| | | //获取时间并拆分年月日时分秒 |
| | | String nowTime = sdf.format(now); |
| | | String nowYear = nowTime.substring(2,4); |
| | | String nowMonth = nowTime.substring(5,7); |
| | | String nowDay = nowTime.substring(8,10); |
| | | String nowHour = nowTime.substring(11,13); |
| | | String nowMinute = nowTime.substring(14,16); |
| | | String nowSecond = nowTime.substring(17,19); |
| | | |
| | | String nowYearHex = Integer.toHexString(Integer.parseInt(nowYear)); |
| | | if(nowYearHex.length() == 1){ |
| | | nowYearHex = "0" + nowYearHex; |
| | | } |
| | | String nowMonthHex = Integer.toHexString(Integer.parseInt(nowMonth)); |
| | | if(nowMonthHex.length() == 1){ |
| | | nowMonthHex = "0" + nowMonthHex; |
| | | } |
| | | String nowDayHex = Integer.toHexString(Integer.parseInt(nowDay)); |
| | | if(nowDayHex.length() == 1){ |
| | | nowDayHex = "0" + nowDayHex; |
| | | } |
| | | String nowHourHex = Integer.toHexString(Integer.parseInt(nowHour)); |
| | | if(nowHourHex.length() == 1){ |
| | | nowHourHex = "0" + nowHourHex; |
| | | } |
| | | String nowMinuteHex = Integer.toHexString(Integer.parseInt(nowMinute)); |
| | | if(nowMinuteHex.length() == 1){ |
| | | nowMinuteHex = "0" + nowMinuteHex; |
| | | } |
| | | String nowSecondHex = Integer.toHexString(Integer.parseInt(nowSecond)); |
| | | if(nowSecondHex.length() == 1){ |
| | | nowSecondHex = "0" + nowSecondHex; |
| | | } |
| | | //16进制时间 |
| | | String timeHex = nowYearHex + nowMonthHex + nowDayHex + nowHourHex + nowMinuteHex + nowSecondHex; |
| | | String lockNumHead = ardTankLockResult.get(0).getLockNum().substring(0,2); |
| | | String lockNumTail = ardTankLockResult.get(0).getLockNum().substring(2,4); |
| | | //计算校验 |
| | | String checkNum = Integer.toHexString(Integer.parseInt(lockNumHead, 16) + Integer.parseInt(lockNumTail, 16) + Integer.parseInt("08", 16) + |
| | | Integer.parseInt(nowYearHex, 16) + Integer.parseInt(nowMonthHex, 16) + Integer.parseInt(nowDayHex, 16) + |
| | | Integer.parseInt(nowHourHex, 16) + Integer.parseInt(nowMinuteHex, 16) + Integer.parseInt(nowSecondHex, 16)); |
| | | if(checkNum.length() == 1){ |
| | | checkNum = "0" + checkNum; |
| | | }else if(checkNum.length() > 2){ |
| | | checkNum = checkNum.substring(checkNum.length() - 2,checkNum.length()); |
| | | } |
| | | String paramsStr = "%7B%22type%22%3A%22ff%22%2C%22dataCnt%22%3A%22" + ardTankLock.getLockNum() + "08" + timeHex + checkNum + "%22%7D"; |
| | | System.out.println(ardTankLock.getLockNum() + "08" + timeHex + checkNum); |
| | | sendCmd(url,userId,ardTankLockResult.get(0).getCarId(),199,"DataDownTransfer", paramsStr,sessionId);*/ |
| | | return; |
| | | }else{ |
| | | 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("06")){//功能码06为锁历史报警 |
| | | |
| | | }*/ |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return;//强转失败返回 |
| | | } |
| | | } |
| | | }; |
| | | thread.start(); |
| | | /** |
| | | * |
| | | * 请加入三方集成方的业务逻辑 |
| | |
| | | public void sendMassage() { |
| | | try { |
| | | PushClientImplSerialPort client = new PushClientImplSerialPort(); |
| | | client.setLog(true);//是否打印明文 |
| | | client.setLog(false);//是否打印明文 |
| | | client.setHost(this.ip);//服务器IP |
| | | client.setPort(10100);//服务器端口 |
| | | client.setUserName(this.userId);//系统用户名 |
| | |
| | | sendMassage(); |
| | | } |
| | | |
| | | public static Map<String,Object> loginIn(String syURL,String userId,String password){//登录 |
| | | OkHttpClient okHttpClient = new OkHttpClient(); |
| | | |
| | | String passwordMd5 = DigestUtils.md5Hex(password); |
| | | |
| | | 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 { |
| | | 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(); |
| | | } |
| | | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | } |