| | |
| | | import com.ruoyi.sy.domain.ArdSyCarRtu; |
| | | import com.ruoyi.sy.mapper.ArdSyCarRtuMapper; |
| | | import com.ruoyi.sy.service.ArdSyCarRtuService; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.eclipse.paho.client.mqttv3.*; |
| | | import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.CommandLineRunner; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class MqttOnce implements CommandLineRunner { |
| | | public class MqttOnce{ |
| | | |
| | | @Autowired |
| | | private ArdSyCarRtuService ardSyCarRtuService; |
| | | |
| | | @Override |
| | | public void run(String... args) throws Exception { |
| | | String[] topic = ardSyCarRtuService.allCarTopic(); |
| | | MqttConsumer mqttConsumer = new MqttConsumer(); |
| | | for (int i = 0; i < topic.length; i++) { |
| | | mqttConsumer.subscribe(topic[i],2); |
| | | @Autowired |
| | | private ISysConfigService sysConfigService; |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | //获取电磁锁地址 |
| | | String mqttUrl = sysConfigService.selectConfigByKey("ElectromagneticLockUrl"); |
| | | List<ArdSyCarRtu> rtus = ardSyCarRtuService.allCarTopicList(); |
| | | for (int i = 0; i < rtus.size(); i++) { |
| | | ArdSyCarRtu rtu = rtus.get(i); |
| | | String carId = rtu.getCarId(); |
| | | String subscribe = rtu.getTopicSubscribe(); |
| | | try { |
| | | MqttClient client = new MqttClient(mqttUrl,carId); |
| | | // log.debug("--创建"+carId+"号车辆电子锁mqtt客户端"); |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | //设置用户名密码 |
| | | options.setUserName("admin"); |
| | | options.setPassword("xzx12345".toCharArray()); |
| | | // 设置超时时间 |
| | | options.setConnectionTimeout(100); |
| | | // 设置会话心跳时间 |
| | | options.setKeepAliveInterval(60); |
| | | // 是否清除session |
| | | options.setCleanSession(true); |
| | | // log.debug("--生成"+carId+"号车辆电子锁mqtt配置对象"); |
| | | client.setCallback(new MqttOnceCallback(client,options,subscribe,2)); |
| | | // log.debug("--添加车辆电磁锁回调处理类"); |
| | | client.connect(options); |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | log.info("mqtt订阅清淤车辆锁信息:"+Arrays.toString(topic)); |
| | | // MqttConsumer mqttConsumer = new MqttConsumer(); |
| | | // for (int i = 0; i < topic.length; i++) { |
| | | // mqttConsumer.subscribe(topic[i],2); |
| | | // } |
| | | // log.info("mqtt订阅清淤车辆锁信息:"+Arrays.toString(topic)); |
| | | } |
| | | } |