/**
*
Description:
* Copyright: Copyright (c) 2020
* Company: www.31gps.net
* @author chencq
* @version 1.0
*/
package com.ruoyi.sy.gps31;
import com.alibaba.fastjson2.JSON;
import com.gps31.push.netty.PushClient;
import com.gps31.push.netty.PushMsg;
import com.gps31.push.netty.client.TcpClient;
import com.gps31.push.util.MapUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.sy.domain.ArdSyCarLock;
import com.ruoyi.sy.domain.ArdSyCarRtu;
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.utils.qymqtt.newM.EmqClient;
import com.ruoyi.utils.qymqtt.newM.QosEnum;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Description:
* Copyright: Copyright (c) 2020
* Company: www.31gps.net
* @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 String ip;
private String userId;
private String password;
public PushClientImplSerialPort(String ip, String userId, String password) {
this.ip = ip;
this.userId = userId;
this.password = password;
}
public PushClientImplSerialPort() {
}
@Override
public void messageReceived(TcpClient tcpClient, PushMsg pushMsg)
throws Exception {
if("8001".equals(pushMsg.getCmd())) {//登录应答
String rspResult = MapUtil.getStrVal(pushMsg.getJsonMap(),"rspResult","1");
if("0".equals(rspResult)) {//登录成功,定阅动态消息
Map map = new HashMap();
map.put("seq", "1");
map.put("action", "add");
map.put("msgIds", JSON.toJSONString(getSubCmdSet()));
PushMsg subMsg = getInstance("0003",map);
sendMsg(subMsg);
}
}else if("8002".equals(pushMsg.getCmd())){//心跳应答
}else if("8003".equals(pushMsg.getCmd())){//订阅动态消息应答
String rspResult = MapUtil.getStrVal(pushMsg.getJsonMap(),"rspResult","1");
//log.error(String.format(" 订阅应答:%s", "0".equals(rspResult)?"成功":"失败"));
}else if("0200".equals(pushMsg.getCmd())) {//定位信息
Map gpsMap = pushMsg.getJsonMap();
log.debug(String.format(" ---->----收到定位数据如下:%s",JSON.toJSONString(gpsMap)));
/**
*
* 请加入三方集成方的业务逻辑
*
*/
}else if("0300".equals(pushMsg.getCmd())) {//报警消息
Map alarmMap = pushMsg.getJsonMap();
String carName = MapUtil.getStrVal(alarmMap, "carName","");//获取车牌号
//log.error(String.format(" ---->收到报警数据:%s",JSON.toJSONString(alarmMap)));
/**
*
* 请加入三方集成方的业务逻辑
*
*/
}else if("0401".equals(pushMsg.getCmd())) {//透传消息
Map dataMap = pushMsg.getJsonMap();
String carName = MapUtil.getStrVal(dataMap, "carName","");//获取车牌号
//log.error(String.format(" ---->收到透传数据:%s",JSON.toJSONString(dataMap)));
/**
*
* 请加入三方集成方的业务逻辑
*
*/
}
}
public void sendMassage() {
try {
PushClientImplSerialPort client = new PushClientImplSerialPort();
client.setLog(false);//是否打印明文
client.setHost(this.ip);//服务器IP
client.setPort(10100);//服务器端口
client.setUserName(this.userId);//系统用户名
client.setPwd(this.password);//系统用户密码
client.setSubMsgIds("0401");//订阅的动态消息,多个动态消息使用|辟分,当前示例是订阅 定位消息(0x0200)和报警消息(0x0300)
client.setDesc("测试客户端");//客户端的描述,
client.start();
while(true) {
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
sendMassage();
}
private void feng(String carId,String type,ArdSyCarRtuService carRtuService,ArdSyCarLockService lockService) throws MqttException {
//查询装油点既定Rtu状态
ArdSyCarRtu ardSyCarRtu = carRtuService.one(carId);
if(ardSyCarRtu!=null){
//查询锁信息
List 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 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)),"解封");
}
}
//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 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 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)),"施封");
}
}
}
}
}
}
}
}
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;
try {
t = (new Date().getTime() - simpleDateFormat.parse(time).getTime())/1000;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Double velocityt = velocity + acceleration*t;
return velocityt;
}*/
}