物联网项目-通讯协议之 Java版
0、背景
物联网项目温湿度管理后台,使用Spring boot 开发,下面主要介绍代码实现部分的辅助类。
1、代码-辅助类
iems
task
IEMSTask.java-------计划任务实现类
web
controller
iems
IemsAPIController.java -------- API接口
IemsTerminalController.java ------终端管理控制器
IemsTerminalLogController.java ------终端日志控制器
IemsTerminalRealtimeController.java ------终端实时状态控制器
IemsTerminalReportController.java -------报表展示控制器
iems-system
iems
terminal
domain----实体类
mapper----数据库映射
service ----服务实现
计划任务-检查设备是否在线
IEMSTask.java
package com.iems.task;
import com.iems.terminal.domain.IemsTerminal;
import com.iems.terminal.domain.IemsTerminalRealtime;
import com.iems.terminal.service.IIemsTerminalRealtimeService;
import com.iems.terminal.service.IIemsTerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
@Component("IEMSTask")
public class IEMSTask {
@Autowired
private IIemsTerminalService iemsTerminalService;
@Autowired
private IIemsTerminalRealtimeService iemsTerminalRealtimeService;
/**
* RealTime 在线的挑出来检查是否离线
* 如果离线那么Terminal也改成离线
* 2020-09-10
* 改成查所有的终端更新他们的状态
* */
public void CheckTerminalState(){
IemsTerminalRealtime realtime = new IemsTerminalRealtime();
realtime.setStatus(3L);
List<IemsTerminalRealtime> list = iemsTerminalRealtimeService.selectIemsTerminalRealtimeList(realtime);
//List<IemsTerminalRealtime> list = iemsTerminalRealtimeService.selectIemsTerminalRealtimeListExcludeNotUse();
if(list != null){
Date now = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(now);
calendar.add(calendar.MINUTE, -30);//倒退30分钟
now = calendar.getTime();
List<IemsTerminalRealtime> tempList = new ArrayList<>();
for(IemsTerminalRealtime rt : list){
boolean before = rt.getRefreshTime().before(now);
if(before){
tempList.add(rt);
}
}
if(tempList.size() > 0){
for(int i = 0; i < tempList.size(); i++) {
IemsTerminalRealtime itr = tempList.get(i);
itr.setStatus(2L);
iemsTerminalRealtimeService.updateIemsTerminalRealtime(itr);
IemsTerminal terminal = iemsTerminalService.selectIemsTerminalById(itr.getTerminalId());
if (terminal != null) {
terminal.setStatus(2L);
iemsTerminalService.updateIemsTerminal(terminal);
}
}
}
}
}
}
Spring boot 链接RabbitMq 配置
RabbitMqConfig.java
package com.iems.framework.config;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Scope;
@Configuration
public class RabbitMqConfig {
@Value("${rabbitmq.host}")
private String host;
@Value("${rabbitmq..port}")
private String port;
@Value("${rabbitmq.username}")
private String username;
@Value("${rabbitmq.password}")
private String password;
@Value("${rabbitmq.virtual-host}")
private String virtual_host;
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
//connectionFactory.setAddresses("10.1.31.31:5672");
//connectionFactory.setUsername("Skywell");
//connectionFactory.setPassword("Skywell@123");
//connectionFactory.setVirtualHost("IEMSVHOST");
connectionFactory.setAddresses(host + ":" + port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtual_host);
connectionFactory.setPublisherConfirms(true);//消息确认
connectionFactory.setPublisherReturns(true);
System.out.println(host + ":" + port + "," + username + "," + password + "," + virtual_host);
return connectionFactory;
}
@Autowired
private ConnectionFactory connectionFactory;
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
//必须是prototype类型
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
//RabbitTemplate template = new RabbitTemplate(connectionFactory);
return template;
}
//注意此下两个bean 有用 有坑时用到
/*@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(new Jackson2JsonMessageConverter());
return template;
}
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
return factory;
}*/
//队列
public static final String QUEUE = "topic.upload";//"IEMS_QUEUE";
public static final String QUEUES = "topic.login";//"IEMS_QUEUE";
//交换机
public static final String EXCHANGE = "IEMS";//"IEMS_EXCHANGE";
//交换机与队列绑定的RoutingKey
public static final String ROUTINGKEY_UPLOAD = "upload";//"upload2";//"upload";
public static final String ROUTINGKEY_LOGIN = "login";//"upload2";//"upload";
/**
* 针对消费者配置
* 1. 设置交换机类型
* 2. 将队列绑定到交换机
FanoutExchange: 将消息分发到所有的绑定队列,无routingkey的概念
HeadersExchange :通过添加属性key-value匹配
DirectExchange:按照routingkey分发到指定队列
TopicExchange:多关键字匹配
*/
@Bean
public TopicExchange defaultExchange(){
return new TopicExchange(EXCHANGE, true, false);
}
@Bean
public Queue queue(){
return new Queue(QUEUE, true);
}
@Bean
public Queue queues(){
return new Queue(QUEUES, true);
}
@Bean
public Binding binding(){
return BindingBuilder.bind(queue()).to(defaultExchange()).with(RabbitMqConfig.ROUTINGKEY_UPLOAD);
}
@Bean
public Binding bindings(){
return BindingBuilder.bind(queues()).to(defaultExchange()).with(RabbitMqConfig.ROUTINGKEY_LOGIN);
}
}