| | |
| | | package com.ruoyi.sy.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | |
| | | import com.ruoyi.sy.mapper.ArdSyCarRtuMapper; |
| | | import com.ruoyi.sy.param.AddRtuParam; |
| | | import com.ruoyi.sy.param.LockTypeParam; |
| | | import com.ruoyi.sy.param.RtuTypeParam; |
| | | import com.ruoyi.sy.service.ArdSyCarLockService; |
| | | import com.ruoyi.sy.vo.CarVo; |
| | | import com.ruoyi.sy.vo.RtuVo; |
| | | import com.ruoyi.system.mapper.SysConfigMapper; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.utils.mqtt.MqttOnce; |
| | | import com.ruoyi.utils.qymqtt.newM.EmqClient; |
| | | import com.ruoyi.utils.qymqtt.newM.QosEnum; |
| | | import com.ruoyi.utils.qymqtt.oldM.MqttCustomerClient; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.eclipse.paho.client.mqttv3.MqttException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.text.DecimalFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | private ArdSyCarRtuMapper rtuMapper; |
| | | @Autowired |
| | | private ArdSyCarLockMapper lockMapper; |
| | | @Autowired |
| | | private SysConfigMapper configMapper; |
| | | |
| | | |
| | | @Override |
| | |
| | | ArdSyCarRtu ardSyCarRtu = new ArdSyCarRtu(); |
| | | ardSyCarRtu.setId(String.valueOf(UUID.randomUUID())); |
| | | ardSyCarRtu.setCarId(addRtuParam.getCarId()); |
| | | ardSyCarRtu.setTopicPublish("/p"+addRtuParam.getCarId()+"/"); |
| | | ardSyCarRtu.setTopicSubscribe("/p"+addRtuParam.getCarId()+"/set/"); |
| | | ardSyCarRtu.setTopicPublish("/p"+addRtuParam.getCarId()+"/set/"); |
| | | ardSyCarRtu.setTopicSubscribe("/p"+addRtuParam.getCarId()+"/"); |
| | | ardSyCarRtu.setInstallType(addRtuParam.getInstallType()); |
| | | ardSyCarRtu.setUnloadType(addRtuParam.getUnloadType()); |
| | | int num = rtuMapper.insert(ardSyCarRtu); |
| | |
| | | ArdSyCar ardSyCar = ardSyCarMapper.selectOne(queryWrapper); |
| | | ardSyCar.setReserved1(ardSyCarRtu.getId()); |
| | | ardSyCarMapper.updateById(ardSyCar); |
| | | MqttOnce mqttOnce = new MqttOnce(); |
| | | mqttOnce.subscribeCar(ardSyCarRtu); |
| | | MqttCustomerClient mqttCustomerClient = new MqttCustomerClient(); |
| | | mqttCustomerClient.connect(addRtuParam.getCarId()+"mqtt"); |
| | | mqttCustomerClient.subscribe(ardSyCarRtu.getTopicSubscribe()); |
| | | return num; |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public int delCar(String id) { |
| | | ArdSyCarRtu ardSyCarRtu = rtuMapper.selectById(id); |
| | | String subscribe = ardSyCarRtu.getTopicSubscribe(); |
| | | MqttCustomerClient mqttCustomerClient = new MqttCustomerClient(); |
| | | mqttCustomerClient.connect(ardSyCarRtu.getCarId()+"del"); |
| | | mqttCustomerClient.unSubscribe(subscribe); |
| | | rtuMapper.deleteById(id); |
| | | QueryWrapper<ArdSyCarLock> queryWrapper1 = new QueryWrapper<>(); |
| | | queryWrapper1.eq("rtu_id",id); |
| | |
| | | @Override |
| | | public List<CarVo> queryCar(List<String> list) { |
| | | QueryWrapper<ArdSyCar> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.ne("reserved_1","").notIn("car_id",list); |
| | | if(list.size()>0){ |
| | | queryWrapper.ne("reserved_1","").notIn("car_id",list); |
| | | }else { |
| | | queryWrapper.ne("reserved_1",""); |
| | | } |
| | | List<ArdSyCar> ardSyCars = ardSyCarMapper.selectList(queryWrapper); |
| | | return carAnalysis(ardSyCars); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public List<ArdSyCarLock> data(String msg) { |
| | | String pattern = "0.00"; // 指定保留两位小数的格式 |
| | | DecimalFormat decimalFormat = new DecimalFormat(pattern); // |
| | | // String pattern = "0.00"; // 指定保留两位小数的格式 |
| | | // DecimalFormat decimalFormat = new DecimalFormat(pattern); // |
| | | JSONObject jsonObject = JSONObject.parseObject(msg); |
| | | List<ArdSyCarLock> lockList = new ArrayList<>(); |
| | | ArdSyCarLock ardSyCarLock1 = new ArdSyCarLock(); |
| | | ardSyCarLock1.setRelay("A01"); |
| | | ardSyCarLock1.setRelayInfo(String.valueOf(jsonObject.getInteger("A01"))); |
| | | // ardSyCarLock1.setRelayInfo(String.valueOf(jsonObject.getInteger("A01"))); |
| | | ardSyCarLock1.setRelayInfo(jsonObject.getInteger("A01")); |
| | | ardSyCarLock1.setCurrents("C01"); |
| | | ardSyCarLock1.setCurrentInfo(decimalFormat.format(jsonObject.getDouble("C01"))); |
| | | // ardSyCarLock1.setCurrentInfo(decimalFormat.format(jsonObject.getDouble("C01"))); |
| | | ardSyCarLock1.setCurrentInfo(jsonObject.getDouble("C01")); |
| | | lockList.add(ardSyCarLock1); |
| | | ArdSyCarLock ardSyCarLock2 = new ArdSyCarLock(); |
| | | ardSyCarLock2.setRelay("A02"); |
| | | ardSyCarLock2.setRelayInfo(String.valueOf(jsonObject.getInteger("A02"))); |
| | | ardSyCarLock2.setRelayInfo(jsonObject.getInteger("A02")); |
| | | ardSyCarLock2.setCurrents("C02"); |
| | | ardSyCarLock2.setCurrentInfo(decimalFormat.format(jsonObject.getDouble("C02"))); |
| | | ardSyCarLock2.setCurrentInfo(jsonObject.getDouble("C02")); |
| | | lockList.add(ardSyCarLock2); |
| | | return lockList; |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public AjaxResult seal(LockTypeParam lockTypeParam) { |
| | | public AjaxResult seal(RtuTypeParam rtuTypeParam) { |
| | | QueryWrapper<ArdSyCarRtu> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("carId",lockTypeParam.getCarId()); |
| | | queryWrapper.eq("carId",rtuTypeParam.getCarId()); |
| | | ArdSyCarRtu ardSyCarRtu = rtuMapper.selectOne(queryWrapper); |
| | | MqttOnce mqttOnce = new MqttOnce(); |
| | | //1解封 |
| | | if(lockTypeParam.getType()==1){ |
| | | if(rtuTypeParam.getType()==1){ |
| | | Map<String,Object> map = new HashMap<>(); |
| | | map.put("A01", 110000);//导通第一路继电器 |
| | | map.put("A02", 110000);//导通第二路继电器 |
| | | map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | try { |
| | | return AjaxResult.success(mqttOnce.publishCar(lockTypeParam.getCarId(),ardSyCarRtu.getTopicPublish(),String.valueOf(new com.alibaba.fastjson2.JSONObject(map)),"解封")); |
| | | } catch (MqttException e) { |
| | | log.debug("解封指令发生失败"+e); |
| | | } |
| | | String mapString = JSON.toJSONString(map); |
| | | EmqClient emqClient = new EmqClient(rtuTypeParam.getCarId()+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | return AjaxResult.success("解封命令发送成功!"); |
| | | // return AjaxResult.success(mqttOnce.publishCar(rtuTypeParam.getCarId(),ardSyCarRtu.getTopicPublish(),String.valueOf(new com.alibaba.fastjson2.JSONObject(map)),"解封")); |
| | | } |
| | | //0施封 |
| | | if(lockTypeParam.getType()==0){ |
| | | if(rtuTypeParam.getType()==0){ |
| | | //锁开关状态 |
| | | int lockCloseNum = 0; |
| | | //查询锁信息 |
| | |
| | | for (int j = 0; j < ardSyCarLocks.size(); j++) { |
| | | ArdSyCarLock ardSyCarLock = ardSyCarLocks.get(j); |
| | | String currents = ardSyCarLock.getCurrents(); |
| | | String currentInfo = ardSyCarLock.getCurrentInfo(); |
| | | Double currentInfo = ardSyCarLock.getCurrentInfo(); |
| | | //电流值为4.0是关锁状态 |
| | | if(currentInfo.equals(4.00)){ |
| | | if(currentInfo.equals(4.00) || currentInfo.equals(4)){ |
| | | lockCloseNum += 1; |
| | | }else { |
| | | String key = ""; |
| | |
| | | Map<String,Object> map = new HashMap(); |
| | | map.put(key, 0.01); |
| | | map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | try { |
| | | mqttOnce.publishCar("carId",ardSyCarRtu.getTopicPublish(),String.valueOf(new com.alibaba.fastjson2.JSONObject(map)),"开锁:"+currents); |
| | | } catch (MqttException e) { |
| | | log.debug("开锁指令发生失败"+e); |
| | | } |
| | | String mapString = JSON.toJSONString(map); |
| | | EmqClient emqClient = new EmqClient(rtuTypeParam.getCarId()+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | // mqttOnce.publishCar("carId",ardSyCarRtu.getTopicPublish(),String.valueOf(new com.alibaba.fastjson2.JSONObject(map)),"开锁:"+currents); |
| | | while(true){ |
| | | ArdSyCarLock ardSyCarLock1 = getCurrentInfo(ardSyCarRtu.getId(),currents); |
| | | String currentInfo1 = ardSyCarLock1.getCurrentInfo(); |
| | | if(currentInfo1.equals(4.00)){ |
| | | Double currentInfo1 = ardSyCarLock1.getCurrentInfo(); |
| | | if(currentInfo1.equals(4.00) || currentInfo1.equals(4)){ |
| | | lockCloseNum +=1; |
| | | break; |
| | | } |
| | |
| | | map.put("A01", 100000);//断开第一路继电器 |
| | | map.put("A02", 100000);//断开第二路继电器 |
| | | map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | try { |
| | | return AjaxResult.success(mqttOnce.publishCar(lockTypeParam.getCarId(),ardSyCarRtu.getTopicPublish(),String.valueOf(new com.alibaba.fastjson2.JSONObject(map)),"施封")); |
| | | } catch (MqttException e) { |
| | | log.debug("施封指令发生失败"+e); |
| | | } |
| | | String mapString = JSON.toJSONString(map); |
| | | EmqClient emqClient = new EmqClient(rtuTypeParam.getCarId()+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | return AjaxResult.success("施封命令发生成功!"); |
| | | // return AjaxResult.success(mqttOnce.publishCar(rtuTypeParam.getCarId(),ardSyCarRtu.getTopicPublish(),String.valueOf(new com.alibaba.fastjson2.JSONObject(map)),"施封")); |
| | | } |
| | | } |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | @Override |
| | | public AjaxResult onOff(LockTypeParam lockTypeParam) { |
| | | public AjaxResult onOff(LockTypeParam lockTypeParam) throws MqttException { |
| | | QueryWrapper<ArdSyCarRtu> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("car_id",lockTypeParam.getCarId()); |
| | | ArdSyCarRtu ardSyCarRtu = rtuMapper.selectOne(queryWrapper); |
| | | ArdSyCarLock ardSyCarLock = lockMapper.selectById(lockTypeParam.getLockId()); |
| | | String relay = ardSyCarLock.getRelayInfo(); |
| | | if(relay.equals("1")){ |
| | | AjaxResult.error(0,"该锁处于解封状态,远程无法操控,请先执行施封操作!"); |
| | | Integer relay = ardSyCarLock.getRelayInfo(); |
| | | if(relay!=null){ |
| | | if(relay.equals("1")){ |
| | | return AjaxResult.error(0,"该锁处于解封状态,远程无法操控,请先执行施封操作!"); |
| | | } |
| | | } |
| | | String key = ""; |
| | | String currentInfo = ardSyCarLock.getCurrentInfo(); |
| | | if(currentInfo.equals("C01")){ |
| | | String currents = ardSyCarLock.getCurrents(); |
| | | if(currents.equals("C01")){ |
| | | key = "D03"; |
| | | }else if(currentInfo.equals("C02")){ |
| | | }else if(currents.equals("C02")){ |
| | | key = "D04"; |
| | | } |
| | | Number lockState = (Number) lockTypeParam.getNum(); |
| | | if(lockState.equals(100)){ |
| | | double lockState = lockTypeParam.getNum(); |
| | | if(lockState==100){ |
| | | lockState = 99.99; |
| | | }else if(lockState.equals(0)){ |
| | | }else if(lockState==0){ |
| | | lockState = 0.01; |
| | | }else{ |
| | | lockState = lockState.doubleValue() + 0.01; |
| | | lockState = lockState + 0.01; |
| | | } |
| | | Map<String,Object> map = new HashMap(); |
| | | map.put(key, lockState);//范围是0.01到99.99 |
| | | map.put("res", String.valueOf(System.currentTimeMillis())); |
| | | MqttOnce mqttOnce = new MqttOnce(); |
| | | try { |
| | | return AjaxResult.success(mqttOnce.publishCar("carId",ardSyCarRtu.getTopicPublish(),String.valueOf(new com.alibaba.fastjson2.JSONObject(map)),"开关锁指令"+lockState)); |
| | | } catch (MqttException e) { |
| | | log.debug("开关锁指令发送失败"+e); |
| | | String mapString = JSON.toJSONString(map); |
| | | System.out.println(mapString); |
| | | EmqClient emqClient = new EmqClient(lockTypeParam.getCarId()+"mqttPublish"+UUID.randomUUID()); |
| | | emqClient.connect(); |
| | | emqClient.publish(ardSyCarRtu.getTopicPublish(),mapString, QosEnum.QoS2,false); |
| | | // MqttCustomerClient mqttCustomerClient = new MqttCustomerClient(); |
| | | // mqttCustomerClient.connect(lockTypeParam.getCarId()+"change"+UUID.randomUUID()); |
| | | // mqttCustomerClient.pushlish(ardSyCarRtu.getTopicPublish(),mapString); |
| | | return AjaxResult.success("开关锁命令发送成功,发送进度为"+lockTypeParam.getNum()); |
| | | } |
| | | |
| | | @Override |
| | | public List<ArdSyCarLock> lockIng(String carId) { |
| | | QueryWrapper<ArdSyCarRtu> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("car_id",carId); |
| | | ArdSyCarRtu ardSyCarRtu = rtuMapper.selectOne(queryWrapper); |
| | | List<ArdSyCarLock> locks = lockMapper.lockList(ardSyCarRtu.getId()); |
| | | for (int i = 0; i < locks.size(); i++) { |
| | | ArdSyCarLock ardSyCarLock = locks.get(i); |
| | | double info = ardSyCarLock.getCurrentInfo(); |
| | | int d = (int) ((int)(info*150/138.3 - 4)/0.16); |
| | | ardSyCarLock.setCurrentInfo((double) d); |
| | | } |
| | | return AjaxResult.success(); |
| | | return locks; |
| | | } |
| | | |
| | | |