├── .idea ├── $CACHE_FILE$ ├── .gitignore ├── DAQ-IoT-SpringBoot.iml ├── IoT-SpringBoot.iml ├── compiler.xml ├── encodings.xml ├── jarRepositories.xml ├── misc.xml ├── sqldialects.xml ├── uiDesigner.xml └── vcs.xml ├── IoT-SpringBoot.iml ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── my │ │ └── iot │ │ ├── IoTSpringBootApplication.java │ │ ├── config │ │ ├── ServerConfig.java │ │ ├── StringToDateConverter.java │ │ └── WebAppConfig.java │ │ ├── controller │ │ ├── DataController.java │ │ ├── FileController.java │ │ ├── GatewayController.java │ │ ├── GatewayExceptionController.java │ │ ├── HomeController.java │ │ ├── SensorClassifyController.java │ │ ├── SensorController.java │ │ ├── SensorExceptionController.java │ │ └── UserController.java │ │ ├── domain │ │ ├── Data.java │ │ ├── Gateway.java │ │ ├── GatewayException.java │ │ ├── PageBean.java │ │ ├── Result.java │ │ ├── Sensor.java │ │ ├── SensorClassify.java │ │ ├── SensorException.java │ │ └── User.java │ │ ├── interceptor │ │ └── LoginInterceptor.java │ │ ├── mapper │ │ ├── DataMapper.java │ │ ├── GatewayExceptionMapper.java │ │ ├── GatewayMapper.java │ │ ├── SensorClassifyMapper.java │ │ ├── SensorExceptionMapper.java │ │ ├── SensorMapper.java │ │ └── UserMapper.java │ │ ├── schedule │ │ └── AsyncTaskService.java │ │ ├── service │ │ ├── DataService.java │ │ ├── GatewayExceptionService.java │ │ ├── GatewayService.java │ │ ├── SensorClassifyService.java │ │ ├── SensorExceptionService.java │ │ ├── SensorService.java │ │ ├── UserService.java │ │ └── impl │ │ │ ├── DataServiceImpl.java │ │ │ ├── GatewayExceptionServiceImpl.java │ │ │ ├── GatewayServiceImpl.java │ │ │ ├── SensorClassifyServiceImpl.java │ │ │ ├── SensorExceptionServiceImpl.java │ │ │ ├── SensorServiceImpl.java │ │ │ └── UserServiceImpl.java │ │ └── util │ │ └── Md5Util.java └── resources │ ├── application.yml │ ├── iot.sql │ ├── static-old │ ├── html │ │ ├── changeBasicInfomation.html │ │ ├── changePassword.html │ │ ├── data_post.html │ │ ├── data_sensor.html │ │ ├── download.html │ │ ├── exception.html │ │ ├── gateway.html │ │ ├── login.html │ │ ├── network.html │ │ ├── regist.html │ │ ├── sensor.html │ │ └── sensor_classify.html │ ├── index.html │ └── js │ │ ├── dateformat.js │ │ ├── echarts.min.js │ │ └── jquery-3.4.1.min.js │ └── static │ ├── html │ ├── basicInformation.html │ ├── changePassword.html │ ├── data-post.html │ ├── data-sensor.html │ ├── download.html │ ├── exception.html │ ├── gateway.html │ ├── login.html │ ├── network.html │ ├── regist.html │ └── sensor.html │ ├── index.html │ └── js │ ├── dateformat.js │ └── echarts.min.js └── test └── java └── com └── my └── iot ├── BaseTest.java └── log └── LoggerTest.java /.idea/$CACHE_FILE$: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Android 10 | 11 | 12 | CorrectnessLintAndroid 13 | 14 | 15 | Gradle 16 | 17 | 18 | Kotlin 19 | 20 | 21 | LintAndroid 22 | 23 | 24 | Maven 25 | 26 | 27 | MavenMigrationKotlin 28 | 29 | 30 | MigrationKotlin 31 | 32 | 33 | OSGi 34 | 35 | 36 | 可能的错误Gradle 37 | 38 | 39 | 40 | 41 | Android 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/DAQ-IoT-SpringBoot.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.idea/IoT-SpringBoot.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/sqldialects.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /IoT-SpringBoot.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 基于SpringBoot框架搭建的物联网数据采集系统服务器端 2 | DAQ-IoT-SSM的升级版 3 | 4 | #### 2020-7-15 更新内容 5 | * 1.前端页面完全重构 6 | * 使用elements-ui重新编写所有前端页面,优化视觉感受。 7 | * 完全抛弃JQuery,使用vue.js + axios实现前后端交互,优化交互逻辑和用户体验。 8 | * 2.API优化 9 | * 取消虚拟路径 10 | * 添加部分API,如根据时间段查询数据、条件查询传感器。 11 | * 过时原有按时间段查询异常的API。 12 | 13 | #### 2020-05-14 更新内容 14 | * 1.框架迁移到SpringBoot+MyBatis,相比于SSM版的项目大大减少了xml配置,仅在application.yml文件中配置了少量信息 15 | * 2.添加Redis缓存,在以下部分提供缓存支持: 16 | * 当查询单个Gateway、Sensor、SensorClassify时使用查询缓存,从数据库查询过的数据会存入缓存,提高查询效率 17 | * 传感器提交Data数据时使用添加缓存,不直接操作数据库,而是将Data添加到Redis中形成缓存队列,提高并发效率 18 | * 将用户登录信息不直接存入session,而是存入Redis缓存,以实现分布式session共享 19 | * 3.提交Data数据的异步任务支持。通过线程池实现异步地将Redis中缓存队列添加到数据库,减少数据库的写入压力。 20 | * 4.nginx与tomcat集群支持: 21 | * 通过SpringBoot的内置Tomcat方便了Tomcat集群的部署 22 | * 提供查看IP和端口API方便进行nginx反向代理和负载均衡的部署和测试 23 | * 分布式session共享避免了集群环境下用户登录信息失效的问题 24 | * 5.测试页面优化 25 | * 模拟传感器数据提交页面支持批量数据提交 26 | * 按时间段查询传感器异常页面不再需要输入时间戳而是通过控件输入日期 27 | * 修复了前端页面显示时间与数据库存储时间不一致的bug 28 | 29 | 30 | ### 注意: 31 | * 前端页面仅供测试,本系统主要是为底层传感网络提供数据提交和管理的平台。 32 | * 默认请求路径 http://localhost:8080/ 8080为SpringBoot内置Tomcat端口,可在application.yml文件中修改。 33 | * 以下所有API除测试、用户相关的/login、/info、/exit之外,都会被登录拦截器所拦截,调用其他API需要先登录一个用户。 34 | * 本系统除下载部分外,所有响应数据均为同样的JSON格式。 35 | * 格式:{"status" : true/false, "message" : "description...", "data": data } 36 | * status: 表示请求是否成功。 37 | * message: 对请求的描述,如果响应失败,描述失败原因。 38 | * data: 请求成功时响应的数据,为Object类型,可以是任何类型的数据。data的具体json的格式可参考domain包中的实体类结构。 39 | 40 | ### 提供API 41 | #### 0. 测试 42 | |功能|请求uri|请求方式|请求参数| 43 | |----|------|--------|-------| 44 | |测试|/api/home|GET|无| 45 | |查看IP和端口|/api/address|GET|无| 46 | 47 | #### 1. 用户相关 48 | |功能|请求uri|请求方式|请求参数| 49 | |----|------|--------|-------| 50 | |用户登录|/api/user/login|POST|User对象| 51 | |查看登录用户|/api/user/info|GET|无| 52 | |退出登录|/api/user/exit|GET|无| 53 | |用户注册|/api/user/regist|POST|User对象| 54 | |修改密码|/api/user/password|POST|User对象、新密码| 55 | |修改基本信息|/api/user/modify|POST|User对象| 56 | 57 | #### 2. 网关相关(restful风格) 58 | |功能|请求uri|请求方式|请求参数| 59 | |----|------|--------|-------| 60 | |添加网关|/api/gateway|POST|Gateway对象| 61 | |更新网关|/api/gateway|PUT|Gateway对象| 62 | |删除网关|/api/gateway/{id}|DELETE|id值| 63 | |查询网关|/api/gateway/{id}|GET|id值| 64 | |查询所有网关|/api/gateway|GET|无| 65 | |关联查询网关下的传感器|/api/gateway?withSensors=true|GET|布尔值| 66 | 67 | #### 3. 传感器分类相关(restful风格) 68 | |功能|请求uri|请求方式|请求参数| 69 | |----|------|--------|-------| 70 | |添加传感器分类|/api/classify|POST|SensorClassify对象| 71 | |查询传感器分类|/api/classify/{id}|GET|id值| 72 | |查询所有传感器分类|/api/classify|GET|无| 73 | |关联查询传感器分类下的传感器|/api/classify?withSensors=true|GET|布尔值| 74 | |查询某一网关下的所有传感器分类|/api/classify/gateway/{id}|GET|网关id值| 75 | 76 | #### 4. 传感器相关(restful风格) 77 | |功能|请求uri|请求方式|请求参数| 78 | |----|------|--------|-------| 79 | |添加传感器|/api/sensor|POST|Sensor对象| 80 | |更新传感器|/api/sensor|PUT|Sensor对象| 81 | |删除传感器|/api/sensor/{id}|DELETE|id值| 82 | |查询传感器|/api/sensor/{id}|GET|id值| 83 | |查询所有传感器|/api/sensor|GET|无| 84 | |查询某一分类所有传感器|/api/sensor/classify/{id}|GET|分类id| 85 | |查询某一网关所有传感器|/api/sensor/gateway/{id}|GET|网关id| 86 | |根据网关和分类条件查询传感器|/api/sensor/gateway-classify|GET|网关id、分类id| 87 | 88 | #### 5. 传感器数据相关 89 | |功能|请求uri|请求方式|请求参数| 90 | |----|------|--------|-------| 91 | |提交一个数据|/api/data/receive|POST|Data对象的json格式字符串| 92 | |提交多个数据|/api/data/receiveAll|POST|Data对象数组的json格式字符串| 93 | |查询一个传感器的所有数据|/api/data/sensor/{id}|GET|传感器id| 94 | |根据时间范围查询一个传感器的数据|/api/data/sensor|GET|传感器id、起始时间、结束时间| 95 | 96 | #### 6. 异常相关 97 | |功能|请求uri|请求方式|请求参数| 98 | |----|------|--------|-------| 99 | |查询网关异常|/api/gatewayException|GET|无| 100 | |查询网关异常(分页)|/api/gatewayException/page/{page}|GET|页码| 101 | |查询一段时间内的网关异常(过时)|/api/gatewayException/{timetamp}|GET|字符串格式:"时间戳1@时间戳2"| 102 | |查询一段时间内的网关异常|/api/gatewayException/time|GET|起始时间、结束时间| 103 | |查询传感器异常|/api/sensorException|GET|无| 104 | |查询传感器异常(分页)|/api/sensorException/page/{page}|GET|页码| 105 | |查询一段时间内的传感器异常(过时)|/api/sensorException/{timetamp}|GET|字符串格式:"时间戳1@时间戳2"| 106 | |查询一段时间内的网关异常|/api/sensorException/time|GET|起始时间、结束时间| 107 | 108 | #### 7. 下载相关 109 | |功能|请求uri|请求方式|请求参数| 110 | |----|------|--------|-------| 111 | |下载测试文件|/api/file/test|GET|无| 112 | |下载所有网关的xls表格|/api/file/gateway|GET|无| 113 | |下载所有传感器的xls表格|/api/file/sensor|GET|无| 114 | |下载一个网关及其所有传感器的xls表格|/api/file/gateway/{id}|GET|网关id| 115 | |下载一个传感器及其所有数据的xls表格|/api/file/sensor/{id}|GET|传感器id| 116 | 117 | #### 其他 118 | 由于目前所有传感器提交的数据都是存在同一个表中,项目运行时间长了之后单表数据量会非常大,影响数据库效率,以后考虑加入分库分表功能。 119 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.my.iot 8 | IoT-SpringBoot 9 | 1.0-SNAPSHOT 10 | jar 11 | 12 | IoT-SpringBoot 13 | http://www.example.com 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 2.1.6.RELEASE 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-devtools 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-test 44 | test 45 | 46 | 47 | 48 | 49 | org.mybatis.spring.boot 50 | mybatis-spring-boot-starter 51 | 2.0.1 52 | 53 | 54 | 55 | 56 | mysql 57 | mysql-connector-java 58 | 5.1.36 59 | 60 | 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-data-redis 65 | 66 | 67 | 68 | 69 | org.apache.poi 70 | poi 71 | 4.1.0 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | org.springframework.boot 80 | spring-boot-maven-plugin 81 | 82 | IoT-SpringBoot 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/IoTSpringBootApplication.java: -------------------------------------------------------------------------------- 1 | package com.my.iot; 2 | 3 | 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | public class IoTSpringBootApplication { 9 | public static void main(String[] args) { 10 | SpringApplication.run(IoTSpringBootApplication.class); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/config/ServerConfig.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.config; 2 | 3 | import org.springframework.boot.web.context.WebServerInitializedEvent; 4 | import org.springframework.context.ApplicationListener; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.net.Inet4Address; 8 | import java.net.InetAddress; 9 | import java.net.UnknownHostException; 10 | 11 | //用于获取本机IP和端口号的组件,用于测试nginx反向代理和负载均衡 12 | @Component 13 | public class ServerConfig implements ApplicationListener { 14 | 15 | private int serverPort; 16 | 17 | public int getPort() { 18 | return this.serverPort; 19 | } 20 | 21 | public String getIp() { 22 | try { 23 | InetAddress localHost = Inet4Address.getLocalHost(); 24 | return localHost.getHostAddress(); 25 | } catch (UnknownHostException e) { 26 | e.printStackTrace(); 27 | } 28 | return null; 29 | } 30 | 31 | @Override 32 | public void onApplicationEvent(WebServerInitializedEvent event) { 33 | this.serverPort = event.getWebServer().getPort(); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/com/my/iot/config/StringToDateConverter.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.core.convert.converter.Converter; 5 | 6 | import java.text.DateFormat; 7 | import java.text.ParseException; 8 | import java.text.SimpleDateFormat; 9 | import java.util.Date; 10 | 11 | /** 12 | * 自定义的字符串转日期的转换器 13 | * SpringMVC需要在springmvc.xml中配置 14 | * SpringBoot直接加@Configuration注解即可 15 | */ 16 | 17 | @Configuration 18 | public class StringToDateConverter implements Converter { 19 | private static final SimpleDateFormat DATE = new SimpleDateFormat("yyyy-MM-dd"); 20 | private static final SimpleDateFormat DATE_TIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 21 | 22 | @Override 23 | public Date convert(String source) { 24 | if (source == null) { 25 | throw new RuntimeException("需要传入数据!"); 26 | } 27 | 28 | try { 29 | if (source.length() > 10) { 30 | return DATE_TIME.parse(source); 31 | } else { 32 | return DATE.parse(source); 33 | } 34 | } catch (ParseException e) { 35 | throw new RuntimeException("数据类型转换出错!"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/config/WebAppConfig.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.config; 2 | 3 | import com.my.iot.interceptor.LoginInterceptor; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 8 | 9 | //添加登录拦截器的配置 10 | @Configuration 11 | public class WebAppConfig implements WebMvcConfigurer { 12 | 13 | @Autowired 14 | private LoginInterceptor loginInterceptor; 15 | 16 | @Override 17 | public void addInterceptors(InterceptorRegistry registry) { 18 | registry.addInterceptor(loginInterceptor) 19 | .addPathPatterns("/html/**","/index.html") 20 | .addPathPatterns("/api/**")//拦截所有路径 21 | .excludePathPatterns( 22 | "/api/address", 23 | "/api/home", 24 | "/api/user/login", 25 | "/api/user/info", 26 | "/api/user/exit") 27 | .excludePathPatterns("/html/login.html");//放行路径 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/DataController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.domain.*; 4 | import com.my.iot.service.*; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | import java.util.*; 9 | 10 | @RestController 11 | @RequestMapping("/api/data") 12 | public class DataController { 13 | 14 | @Autowired 15 | private DataService dataService; 16 | 17 | @Autowired 18 | private SensorService sensorService; 19 | 20 | @Autowired 21 | private GatewayExceptionService gatewayExceptionService; 22 | 23 | @Autowired 24 | private SensorExceptionService sensorExceptionService; 25 | 26 | //@GetMapping("/{id}") 27 | public Result addData(@PathVariable("id") int sensor_id) {//该方法仅供测试,给一个传感器添加一些随机数据,不经缓存直接添加到数据库 28 | Sensor sensor = sensorService.findById(sensor_id); 29 | if (sensor == null) { 30 | return new Result(false, "sensor not exist", null); 31 | } 32 | List datas = new ArrayList<>(); 33 | long now = System.currentTimeMillis(); 34 | Random random = new Random(); 35 | for (int i = 0; i < 10; i++) { 36 | now += 1000; 37 | Data tmp = new Data(null, (float) (i * 0.2), new Date(now), sensor_id); 38 | System.out.print(tmp.getData()); 39 | datas.add(tmp); 40 | } 41 | dataService.add(datas); 42 | 43 | return new Result(true, "add success", datas); 44 | } 45 | 46 | 47 | //只接受json格式的传感器提交数据,数据格式:{"id":null,"data":1.23,"time":"2020-01-01 13:13:13","sensor_id":1} 48 | @PostMapping(path = "/receive", consumes = "application/json") 49 | public Result receive(@RequestBody Data data) {//接受一个传感器数据,存入redis缓存,只返回提交状态,不响应数据 50 | //System.out.println(data); 51 | Result result = checkData(data); 52 | if (!result.getStatus()) {//检查数据是否合法 53 | return result; 54 | } 55 | //将数据存入缓存,异步任务会将其添加到数据库 56 | dataService.add(data); 57 | return result; 58 | } 59 | 60 | //数据格式:[{"id":null,"data":1.23,"time":"2020-01-01 13:13:13","sensor_id":1}, ...] 61 | @PostMapping(path = "/receiveAll", consumes = "application/json") 62 | public Result receiveAll(@RequestBody List datas) {//接受多个个传感器数据,存入redis缓存,只返回提交状态,不响应数据 63 | //System.out.println(datas); 64 | Result result = checkData(datas); 65 | if (!result.getStatus()) {//检查数据是否合法 66 | return result; 67 | } 68 | //将数据存入缓存,异步任务会将其添加到数据库 69 | dataService.add(datas); 70 | return result; 71 | } 72 | 73 | private Result checkData(Data data) {//保证提交数据除id外均为非空,且保证sensor_id存在 74 | if (data.getData() == null || data.getTime() == null || data.getSensor_id() == null) { 75 | return new Result(false, "data deficient", null); 76 | } 77 | if (data.getTime().after(new Date())) {//如果日期比现在还超前则不接受 78 | return new Result(false, "date illegal", null); 79 | } 80 | Integer sensor_id = data.getSensor_id(); 81 | Sensor sensor = sensorService.findById(sensor_id); 82 | if (sensor == null) { 83 | return new Result(false, "sensor not exist", null); 84 | } 85 | //模拟产生传感器异常 86 | if (data.getData() < 0.01) { 87 | this.createSensorException(sensor_id); 88 | System.out.println("出现传感器异常..."); 89 | } 90 | //模拟产生网关异常 91 | if (data.getData() < 0.001) { 92 | Integer gate_id = sensor.getGate_id(); 93 | this.createGatewayException(gate_id); 94 | System.out.println("出现网关异常..."); 95 | } 96 | 97 | return new Result(true, "receive finished", null); 98 | } 99 | 100 | private Result checkData(List datas) {//保证提交所有数据除id外均为非空,且保证所有sensor_id存在 101 | for (Data data : datas) { 102 | Result result = checkData(data); 103 | if (!result.getStatus()) { 104 | return result; 105 | } 106 | } 107 | return new Result(true, "receive finished", null); 108 | } 109 | 110 | public void createGatewayException(Integer gate_id) {//模拟创建网关异常 111 | String mesage[] = {"未接收到数据", "连接异常", "网络异常", "数据异常"}; 112 | int index = new Random().nextInt(4); 113 | GatewayException gatewayException = new GatewayException(null, mesage[index], new Date(), gate_id); 114 | gatewayExceptionService.add(gatewayException); 115 | } 116 | 117 | private void createSensorException(Integer sensor_id) {//模拟创建传感器异常 118 | String mesage[] = {"未接收到数据", "连接异常", "网络异常", "数据异常"}; 119 | int index = new Random().nextInt(4); 120 | SensorException sensorException = new SensorException(null, mesage[index], new Date(), sensor_id); 121 | sensorExceptionService.add(sensorException); 122 | } 123 | 124 | @GetMapping(path = "/sensor/{id}") 125 | public Result getDataBySensorId(@PathVariable("id") int sensor_id) {//查询一个传感器下的所有数据 126 | Sensor sensor = sensorService.findById(sensor_id); 127 | if (sensor == null) { 128 | return new Result(false, "sensor not exist", null); 129 | } 130 | List datas = dataService.findBySensorId(sensor_id); 131 | if (datas == null || datas.isEmpty()) { 132 | return new Result(false, "the sensor has no data", null); 133 | } 134 | return new Result(true, "get success", datas); 135 | } 136 | 137 | @GetMapping(path = "/sensor", params = "sensorId") 138 | public Result getDataBySensorIdInDatetime(Integer sensorId, Date datetime1, Date datetime2) {//查询一个传感器下的一段时间内的所有数据 139 | Sensor sensor = sensorService.findById(sensorId); 140 | if (sensor == null) { 141 | return new Result(false, "sensor not exist", null); 142 | } 143 | List datas = dataService.findBySensorIdInDatetime(sensorId,datetime1,datetime2); 144 | if (datas == null || datas.isEmpty()) { 145 | return new Result(false, "the sensor has no data", null); 146 | } 147 | return new Result(true, "get success", datas); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/GatewayController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.domain.Gateway; 4 | import com.my.iot.domain.Result; 5 | import com.my.iot.service.GatewayService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("/api/gateway") 13 | public class GatewayController {//该控制器采用restful风格,不同控制器方法访问url相同,通过请求方式和请求参数区分 14 | 15 | @Autowired 16 | private GatewayService gatewayService; 17 | 18 | @PostMapping(path = "") 19 | public Result addGateway(Gateway gateway) {//添加 20 | //保证网关有ip和port 21 | if (gateway.getIp() == null || gateway.getIp().isEmpty() 22 | || gateway.getPort() == null || gateway.getPort().isEmpty()) { 23 | return new Result(false, "some info null", null); 24 | } 25 | gatewayService.add(gateway); 26 | return new Result(true, "add success", gateway); 27 | } 28 | 29 | @PutMapping(path = "") 30 | public Result updateGateway(Gateway gateway) {//更改 31 | //System.out.println(gateway); 32 | //保证网关有ip和port 33 | if (gateway.getIp() == null || gateway.getIp().isEmpty() 34 | || gateway.getPort() == null || gateway.getPort().isEmpty()) { 35 | return new Result(false, "some info null", null); 36 | } 37 | Gateway g = gatewayService.findById(gateway.getId()); 38 | if (g != null) { 39 | gatewayService.update(gateway); 40 | return new Result(true, "update success", gateway); 41 | } else { 42 | return new Result(false, "update failed, gateway not exist", null); 43 | } 44 | } 45 | 46 | @GetMapping(path = "/{id}") 47 | public Result getGatewayById(@PathVariable("id") int id) {//根据id查询 48 | Gateway gateway = gatewayService.findById(id); 49 | if (gateway != null) { 50 | return new Result(true, "get success", gateway); 51 | } else { 52 | return new Result(false, "get failed, gateway not exist", null); 53 | } 54 | } 55 | 56 | @DeleteMapping(path = "/{id}") 57 | public Result deleteGatewayById(@PathVariable("id") int id) {//根据id删除,注意!该操作会删除该网关下的所有传感器及其所有数据! 58 | Gateway gateway = gatewayService.findById(id); 59 | if (gateway != null) { 60 | gatewayService.deleteById(id); 61 | return new Result(true, "delete success", null); 62 | } else { 63 | return new Result(false, "delete failed , gateway not exist", null); 64 | } 65 | } 66 | 67 | @GetMapping(path = "") 68 | public Result getAllGateway(Boolean withSensors) {//请求不带这个布尔参数就不关联查询传感器 69 | List gateways = gatewayService.findAll(withSensors);//获取所有网关,关联?查询所有传感器 70 | if (gateways == null || gateways.size() == 0) { 71 | return new Result(false, "get failed , no gateway exist", null); 72 | } 73 | return new Result(true, "get success", gateways); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/GatewayExceptionController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.domain.GatewayException; 4 | import com.my.iot.domain.PageBean; 5 | import com.my.iot.domain.Result; 6 | import com.my.iot.service.GatewayExceptionService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | import java.util.Date; 11 | import java.util.List; 12 | 13 | 14 | @RestController 15 | @RequestMapping("/api/gatewayException") 16 | public class GatewayExceptionController { 17 | 18 | @Autowired 19 | private GatewayExceptionService gatewayExceptionService; 20 | 21 | @GetMapping("") 22 | public Result getAllGatewayException() { 23 | List gatewayExceptions = gatewayExceptionService.findAll(); 24 | return new Result(true, "get success", gatewayExceptions); 25 | } 26 | 27 | @GetMapping("/page/{page}") 28 | public Result getAllGatewayExceptionByPage(@PathVariable("page") int page) { 29 | PageBean pageBean = gatewayExceptionService.findByPage(page); 30 | return new Result(true, "get success", pageBean); 31 | } 32 | 33 | @GetMapping("/{timetamp}") 34 | @Deprecated 35 | public Result getGatewayExceptionByTime(@PathVariable("timetamp") String timetamp) {//查询一段时间的异常,参数格式:时间戳1@时间戳2 36 | String[] s = timetamp.split("@"); 37 | long dateFromTamp = Long.parseLong(s[0]); 38 | long dateToTamp = Long.parseLong(s[1]); 39 | List gatewayExceptions = gatewayExceptionService.findByTime(new Date(dateFromTamp), new Date(dateToTamp)); 40 | return new Result(true, "get success", gatewayExceptions); 41 | } 42 | 43 | @GetMapping("/time") 44 | public Result getGatewayExceptionByDateTime(Date datetime1, Date datetime2) {//查询一段时间的异常 45 | List gatewayExceptions = gatewayExceptionService.findByTime(datetime1, datetime2); 46 | return new Result(true, "get success", gatewayExceptions); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.config.ServerConfig; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.util.Random; 10 | 11 | @RestController 12 | @RequestMapping("/api") 13 | public class HomeController { 14 | private final String[] s = { 15 | "Talk is cheap. Show me the code.", 16 | "Stay hungry Stay foolish.", 17 | "Go big or go home.", 18 | "Done is better than perfect." 19 | }; 20 | private final Random r = new Random(); 21 | 22 | @Autowired 23 | private ServerConfig serverConfig; 24 | 25 | @GetMapping("/home") 26 | public String home() { 27 | return s[r.nextInt(4)]; 28 | } 29 | 30 | @GetMapping("/address")//显示部署服务器的IP和端口号,用于测试nginx反向代理和负载均衡功能 31 | public String getIPandPort() { 32 | return "IP: " + serverConfig.getIp() + "\tPort: " + serverConfig.getPort(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/SensorClassifyController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.domain.Gateway; 4 | import com.my.iot.domain.Result; 5 | import com.my.iot.domain.SensorClassify; 6 | import com.my.iot.service.GatewayService; 7 | import com.my.iot.service.SensorClassifyService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import javax.annotation.Resource; 12 | import java.util.List; 13 | 14 | @RestController 15 | @RequestMapping("/api/classify") 16 | public class SensorClassifyController {//该控制器依然采用restful风格,对传感器分类只提供增加和查询api 17 | 18 | @Autowired 19 | private SensorClassifyService sensorClassifyService; 20 | 21 | @Autowired 22 | private GatewayService gatewayService; 23 | 24 | @PostMapping("") 25 | public Result addClassify(SensorClassify classify) { 26 | if (classify.getName() == null || classify.getName().isEmpty()) { 27 | return new Result(false, "name can not be empty", null); 28 | } 29 | sensorClassifyService.add(classify); 30 | return new Result(true, "add success", classify); 31 | } 32 | 33 | @GetMapping("") 34 | public Result getAllClassify(Boolean withSensors) {//请求不带这个布尔参数就不关联查询传感器 35 | List list = sensorClassifyService.findAll(withSensors); 36 | if (list == null || list.size() == 0) { 37 | return new Result(false, "no classify exist", null); 38 | } 39 | return new Result(true, "get success", list); 40 | } 41 | 42 | @GetMapping("/{id}") 43 | public Result getClassifyById(@PathVariable("id") int id) { 44 | SensorClassify classify = sensorClassifyService.findById(id); 45 | if (classify == null) { 46 | return new Result(false, "classify not exist", null); 47 | } 48 | return new Result(true, "get success", classify); 49 | } 50 | 51 | @GetMapping("/gateway/{id}") 52 | public Result getClassifyByGatewayId(@PathVariable("id") int gateway_id) {//获取某一网关下的所有传感器分类(需要使用多对多查询) 53 | Gateway gateway = gatewayService.findById(gateway_id); 54 | if (gateway == null) {// 网关存在 55 | return new Result(false, "gateway not exist", null); 56 | } 57 | List classifys = sensorClassifyService.findByGatewayId(gateway_id); 58 | if (classifys == null || classifys.isEmpty()) { 59 | return new Result(false, "classify not exist", null); 60 | } 61 | return new Result(true, "get success", classifys); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/SensorController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.domain.Gateway; 4 | import com.my.iot.domain.Result; 5 | import com.my.iot.domain.Sensor; 6 | import com.my.iot.domain.SensorClassify; 7 | import com.my.iot.service.GatewayService; 8 | import com.my.iot.service.SensorClassifyService; 9 | import com.my.iot.service.SensorService; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | import java.util.List; 14 | 15 | @RestController 16 | @RequestMapping("/api/sensor") 17 | public class SensorController { 18 | 19 | @Autowired 20 | private GatewayService gatewayService; 21 | 22 | @Autowired 23 | private SensorClassifyService sensorClassifyService; 24 | 25 | @Autowired 26 | private SensorService sensorService; 27 | 28 | @PostMapping(path = "") 29 | public Result addSensor(Sensor sensor) {//此处需要配置自定义类型转换器将日期默认yyyy/MM/dd绑定格式改为yyyy-MM-dd 30 | Integer gate_id = sensor.getGate_id(); 31 | Integer classify_id = sensor.getClassify_id(); 32 | //gate_id 和 classify_id 保证非空 33 | if (gate_id == null || classify_id == null) { 34 | return new Result(false, "gate_id and classify_id is nessesary", null); 35 | } 36 | //查询关联网关 37 | Gateway gateway = gatewayService.findById(gate_id); 38 | if (gateway == null) { 39 | return new Result(false, "gate_id not exist", null); 40 | } 41 | //查询关联种类 42 | SensorClassify sensorClassify = sensorClassifyService.findById(classify_id); 43 | if (sensorClassify == null) { 44 | return new Result(false, "classify_id not exist", null); 45 | } 46 | //设置关联数据 47 | //sensor.setGateway(gateway); 48 | // sensor.setSensorClassify(sensorClassify); 49 | sensorService.add(sensor); 50 | return new Result(true, "add success", sensor); 51 | } 52 | 53 | @GetMapping(path = "/{id}") 54 | public Result getSensorById(@PathVariable("id") int id) { 55 | Sensor sensor = sensorService.findById(id); 56 | if (sensor == null) { 57 | return new Result(false, "sensor not exist", null); 58 | } 59 | return new Result(true, "get success", sensor); 60 | } 61 | 62 | @GetMapping(path = "") 63 | public Result getAllSensors() { 64 | List sensors = sensorService.findAll(); 65 | if (sensors == null || sensors.size() == 0) { 66 | return new Result(false, "no sensor exist", null); 67 | } 68 | return new Result(true, "get success", sensors); 69 | } 70 | 71 | @PutMapping(path = "") 72 | public Result updateSensor(Sensor sensor) { 73 | Integer id = sensor.getId(); 74 | if (id == null) {//id 非空 75 | return new Result(false, "id is nessesary", null); 76 | } 77 | Sensor s = sensorService.findById(id); 78 | if (s == null) {// 传感器存在 79 | return new Result(false, "sensor not exist", null); 80 | } 81 | 82 | Integer gate_id = sensor.getGate_id(); 83 | Integer classify_id = sensor.getClassify_id(); 84 | if (gate_id == null || classify_id == null) {//gate_id 和 classify_id 保证非空 85 | return new Result(false, "gate_id and classify_id is nessesary", null); 86 | } 87 | //查询关联网关 88 | Gateway gateway = gatewayService.findById(gate_id); 89 | if (gateway == null) { 90 | return new Result(false, "gate_id not exist", null); 91 | } 92 | //查询关联种类 93 | SensorClassify sensorClassify = sensorClassifyService.findById(classify_id); 94 | if (sensorClassify == null) { 95 | return new Result(false, "classify_id not exist", null); 96 | } 97 | 98 | sensorService.update(sensor); 99 | return new Result(true, "update success", sensor); 100 | } 101 | 102 | @DeleteMapping(path = "/{id}") 103 | public Result deteleSensorById(@PathVariable("id") int id) {//注意!删除传感器会将其所有数据一并删除! 104 | Sensor s = sensorService.findById(id); 105 | if (s == null) {// 传感器存在 106 | return new Result(false, "delete failed, sensor not exist", null); 107 | } 108 | sensorService.deleteById(id); 109 | return new Result(true, "detele success", null); 110 | } 111 | 112 | //----------// 113 | @GetMapping(path = "/classify/{id}") 114 | public Result getSensorByClassifyId(@PathVariable("id") int classify_id) {//获取某一分类下的所有传感器 115 | SensorClassify sensorClassify = sensorClassifyService.findById(classify_id); 116 | if (sensorClassify == null) {// 传感器分类存在 117 | return new Result(false, "sensor classify not exist", null); 118 | } 119 | List sensors = sensorService.findByClassifyId(classify_id); 120 | if (sensors == null || sensors.isEmpty()) { 121 | return new Result(false, "sensors not exist", null); 122 | } 123 | return new Result(true, "get success", sensors); 124 | } 125 | 126 | @GetMapping(path = "/gateway/{id}") 127 | public Result getSensorByGatewayId(@PathVariable("id") int gateway_id) {//获取某一网关下的所有传感器 128 | Gateway gateway = gatewayService.findById(gateway_id); 129 | if (gateway == null) {// 网关存在 130 | return new Result(false, "gateway not exist", null); 131 | } 132 | List sensors = sensorService.findByGatewayId(gateway_id); 133 | if (sensors == null || sensors.isEmpty()) { 134 | return new Result(false, "sensors not exist", null); 135 | } 136 | return new Result(true, "get success", sensors); 137 | } 138 | 139 | @GetMapping(path = "/gateway-classify") 140 | public Result getSensorByGatewayIdAndClassifyId(Integer gateway_id, Integer classify_id) {//获取某一网关下的某一类型传感器 141 | List sensors = sensorService.findByGatewayIdAndClassifyId(gateway_id, classify_id); 142 | if (sensors == null || sensors.isEmpty()) { 143 | return new Result(false, "sensors not exist", null); 144 | } 145 | return new Result(true, "get success", sensors); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/SensorExceptionController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.domain.GatewayException; 4 | import com.my.iot.domain.PageBean; 5 | import com.my.iot.domain.Result; 6 | import com.my.iot.domain.SensorException; 7 | import com.my.iot.service.SensorExceptionService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.Date; 12 | import java.util.List; 13 | 14 | @RestController 15 | @RequestMapping("/api/sensorException") 16 | public class SensorExceptionController { 17 | @Autowired 18 | private SensorExceptionService sensorExceptionService; 19 | 20 | @GetMapping("") 21 | public Result getAllSensorException() { 22 | List sensorExceptions = sensorExceptionService.findAll(); 23 | return new Result(true, "get success", sensorExceptions); 24 | } 25 | 26 | @GetMapping("/page/{page}") 27 | public Result getAllSensorExceptionByPage(@PathVariable("page") int page) { 28 | PageBean pageBean = sensorExceptionService.findByPage(page); 29 | return new Result(true, "get success", pageBean); 30 | } 31 | 32 | @GetMapping("/{timetamp}") 33 | public Result getAllSensorExceptionByTime(@PathVariable("timetamp") String timetamp) {//查询一段时间的异常,参数格式:时间戳1@时间戳2 34 | String[] s = timetamp.split("@"); 35 | long dateFromTamp = Long.parseLong(s[0]); 36 | long dateToTamp = Long.parseLong(s[1]); 37 | List sensorExceptions = sensorExceptionService.findByTime(new Date(dateFromTamp), new Date(dateToTamp)); 38 | return new Result(true, "get success", sensorExceptions); 39 | } 40 | @GetMapping("/time") 41 | public Result getGatewayExceptionByDateTime(Date datetime1, Date datetime2) {//查询一段时间的异常 42 | List sensorExceptions = sensorExceptionService.findByTime(datetime1, datetime2); 43 | return new Result(true, "get success", sensorExceptions); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.controller; 2 | 3 | import com.my.iot.domain.Result; 4 | import com.my.iot.domain.User; 5 | import com.my.iot.service.UserService; 6 | import com.my.iot.util.Md5Util; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.data.redis.core.RedisTemplate; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import javax.annotation.Resource; 12 | import javax.servlet.http.HttpSession; 13 | import java.util.concurrent.TimeUnit; 14 | 15 | @RestController 16 | @RequestMapping("/api/user") 17 | public class UserController { 18 | 19 | public static final String LOGIN_USER = "login_user"; 20 | 21 | @Autowired 22 | private UserService userService; 23 | 24 | @Resource 25 | private RedisTemplate redisTemplate;//利用Redis实现分布式Session 26 | 27 | @PostMapping("/regist") 28 | public Result regist(User user) throws Exception {//注册 29 | //调用service完成注册 30 | user.setPassword(Md5Util.encodeByMd5(user.getPassword()));//对密码加密 31 | boolean flag = userService.regist(user); 32 | return new Result(flag, flag ? "regist success" : "regist failed, username existed", null); 33 | } 34 | 35 | @PostMapping("/login") 36 | public Result login(User user, HttpSession session) throws Exception {//登录 37 | user.setPassword(Md5Util.encodeByMd5(user.getPassword()));//对密码加密 38 | //调用service完成登录 39 | User u = userService.login(user); 40 | if (u == null) { 41 | return new Result(false, "login failed", null); 42 | } else { 43 | String sessionId = session.getId();//登录成功创建session,获取sessionId 44 | redisTemplate.opsForHash().put(sessionId, LOGIN_USER, u);//key sessionId, hkey login_user, value 登录用户 45 | redisTemplate.expire(sessionId, 7, TimeUnit.DAYS);//有效时间7天 46 | return new Result(true, "login success", u); 47 | } 48 | } 49 | 50 | @GetMapping("/info") 51 | public Result loginUserInfo(@CookieValue("JSESSIONID") String sessionId) {//查看登录用户信息 52 | if (sessionId == null || sessionId.isEmpty()) { 53 | return new Result(false, "no login, without JSESSIONID", null); 54 | } 55 | User login_user = (User) redisTemplate.opsForHash().get(sessionId, LOGIN_USER); 56 | if (login_user == null) {//未登录 57 | return new Result(false, "no login", null); 58 | } 59 | return new Result(true, "success", login_user);//已登录 60 | } 61 | 62 | @GetMapping("/exit") 63 | public Result exit(@CookieValue("JSESSIONID") String sessionId) {//退出登录 64 | User login_user = (User) redisTemplate.opsForHash().get(sessionId, LOGIN_USER); 65 | if (login_user == null) { 66 | return new Result(false, "exit failed, no login", null); 67 | } 68 | redisTemplate.delete(sessionId); 69 | return new Result(true, "exit success", null); 70 | } 71 | 72 | //以下方法要经过拦截器的登录拦截 73 | @PostMapping("/password") 74 | public Result changePassword(User user, String newPassword, @CookieValue("JSESSIONID") String sessionId) throws Exception {//修改密码 75 | //1.拿到登录用户 76 | User login_user = (User) redisTemplate.opsForHash().get(sessionId, LOGIN_USER); 77 | //2.验证用户旧密码是否正确 78 | user.setPassword(Md5Util.encodeByMd5(user.getPassword()));//对密码加密 79 | if (userService.login(user) == null) { 80 | return new Result(false, "failed, old password error", null); 81 | } 82 | //3.更改新密码 83 | newPassword = Md5Util.encodeByMd5(newPassword); 84 | login_user.setPassword(newPassword); 85 | //调用service完成修改密码 86 | userService.update(login_user); 87 | //更新redis 88 | redisTemplate.opsForHash().put(sessionId, LOGIN_USER, login_user); 89 | return new Result(true, "change password success", null); 90 | } 91 | 92 | //以下方法要经过拦截器的登录拦截 93 | @PostMapping("/modify") 94 | public Result changeBasicInformation(User user, @CookieValue("JSESSIONID") String sessionId) {//修改用户基本信息(用户名不能修改) 95 | //1.拿到登录用户 96 | User login_user = (User) redisTemplate.opsForHash().get(sessionId, LOGIN_USER); 97 | //2.更新基本信息 98 | login_user.setEmail(user.getEmail()); 99 | login_user.setNickname(user.getNickname()); 100 | login_user.setTel(user.getTel()); 101 | //调用service完成修改基本信息 102 | userService.update(login_user); 103 | //更新redis 104 | redisTemplate.opsForHash().put(sessionId, LOGIN_USER, login_user); 105 | return new Result(true, "change basic information success", login_user); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/Data.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | 5 | import java.io.Serializable; 6 | import java.util.Date; 7 | 8 | //传感器的数据 9 | public class Data implements Serializable { 10 | private Integer id;//主键 11 | private Float data;//数据值 12 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 13 | private Date time;//数据采集时间 14 | private Integer sensor_id;//外键,关联传感器表的主键 15 | 16 | //一(多)对一关联传感器 17 | private Sensor sensor; 18 | 19 | public Data() { 20 | } 21 | 22 | public Data(Integer id, Float data, Date time, Integer sensor_id) { 23 | this.id = id; 24 | this.data = data; 25 | this.time = time; 26 | this.sensor_id = sensor_id; 27 | } 28 | 29 | public Integer getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Integer id) { 34 | this.id = id; 35 | } 36 | 37 | public Float getData() { 38 | return data; 39 | } 40 | 41 | public void setData(Float data) { 42 | this.data = data; 43 | } 44 | 45 | public Date getTime() { 46 | return time; 47 | } 48 | 49 | public void setTime(Date time) { 50 | this.time = time; 51 | } 52 | 53 | public Integer getSensor_id() { 54 | return sensor_id; 55 | } 56 | 57 | public void setSensor_id(Integer sensor_id) { 58 | this.sensor_id = sensor_id; 59 | } 60 | 61 | public Sensor getSensor() { 62 | return sensor; 63 | } 64 | 65 | public void setSensor(Sensor sensor) { 66 | this.sensor = sensor; 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "Data{" + 72 | "id=" + id + 73 | ", data=" + data + 74 | ", time=" + time + 75 | ", sensor_id=" + sensor_id + 76 | '}'; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/Gateway.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | public class Gateway implements Serializable { 7 | //固有属性 8 | private Integer id;//主键 9 | private String ip; 10 | private String port; 11 | private String description; 12 | private String location; 13 | 14 | //关联属性 15 | private List sensors;//一对多关联的传感器 16 | 17 | public Gateway() { 18 | 19 | } 20 | 21 | public Gateway(Integer id, String ip, String port, String description, String location) { 22 | this.id = id; 23 | this.ip = ip; 24 | this.port = port; 25 | this.description = description; 26 | this.location = location; 27 | } 28 | 29 | public Integer getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Integer id) { 34 | this.id = id; 35 | } 36 | 37 | public String getIp() { 38 | return ip; 39 | } 40 | 41 | public void setIp(String ip) { 42 | this.ip = ip; 43 | } 44 | 45 | public String getPort() { 46 | return port; 47 | } 48 | 49 | public void setPort(String port) { 50 | this.port = port; 51 | } 52 | 53 | public String getDescription() { 54 | return description; 55 | } 56 | 57 | public void setDescription(String description) { 58 | this.description = description; 59 | } 60 | 61 | public String getLocation() { 62 | return location; 63 | } 64 | 65 | public void setLocation(String location) { 66 | this.location = location; 67 | } 68 | 69 | public List getSensors() { 70 | return sensors; 71 | } 72 | 73 | public void setSensors(List sensors) { 74 | this.sensors = sensors; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "Gateway{" + 80 | "id=" + id + 81 | ", ip='" + ip + '\'' + 82 | ", port='" + port + '\'' + 83 | ", description='" + description + '\'' + 84 | ", location='" + location + '\'' + 85 | '}'; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/GatewayException.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | 5 | import java.io.Serializable; 6 | import java.util.Date; 7 | 8 | public class GatewayException implements Serializable {//网关异常 9 | private Integer id;//主键 10 | private String description; 11 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 12 | private Date time; 13 | private Integer gate_id; //关联网关主键 14 | 15 | private Gateway gateway;//一对一关联网关对象 16 | 17 | public GatewayException() { 18 | } 19 | 20 | public GatewayException(Integer id, String description, Date time, Integer gate_id) { 21 | this.id = id; 22 | this.description = description; 23 | this.time = time; 24 | this.gate_id = gate_id; 25 | } 26 | 27 | public Integer getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public String getDescription() { 36 | return description; 37 | } 38 | 39 | public void setDescription(String description) { 40 | this.description = description; 41 | } 42 | 43 | public Date getTime() { 44 | return time; 45 | } 46 | 47 | public void setTime(Date time) { 48 | this.time = time; 49 | } 50 | 51 | public Integer getGate_id() { 52 | return gate_id; 53 | } 54 | 55 | public void setGate_id(Integer gate_id) { 56 | this.gate_id = gate_id; 57 | } 58 | 59 | public Gateway getGateway() { 60 | return gateway; 61 | } 62 | 63 | public void setGateway(Gateway gateway) { 64 | this.gateway = gateway; 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return "GatewayException{" + 70 | "id=" + id + 71 | ", description='" + description + '\'' + 72 | ", time=" + time + 73 | ", gate_id=" + gate_id + 74 | '}'; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/PageBean.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import java.util.List; 4 | 5 | public class PageBean {//分页对象 6 | private int count;//总记录数 7 | private int pageCount;//总页数 8 | private int page;//当前页码 9 | private int pageSize;//每页记录数,默认5 10 | private List list;//当前页数据 11 | 12 | private int startIndex;//开始记录的索引 13 | 14 | public PageBean() { 15 | } 16 | 17 | public PageBean(int count, int pageCount, int page, int pageSize, List list) { 18 | this.count = count; 19 | this.pageCount = pageCount; 20 | this.page = page; 21 | this.pageSize = pageSize; 22 | this.list = list; 23 | } 24 | 25 | public int getCount() { 26 | return count; 27 | } 28 | 29 | public void setCount(int count) { 30 | this.count = count; 31 | } 32 | 33 | public int getPageCount() { 34 | return pageCount; 35 | } 36 | 37 | public void setPageCount(int pageCount) { 38 | this.pageCount = pageCount; 39 | } 40 | 41 | public int getPage() { 42 | return page; 43 | } 44 | 45 | public void setPage(int page) { 46 | this.page = page; 47 | } 48 | 49 | public int getPageSize() { 50 | return pageSize; 51 | } 52 | 53 | public void setPageSize(int pageSize) { 54 | this.pageSize = pageSize; 55 | } 56 | 57 | public List getList() { 58 | return list; 59 | } 60 | 61 | public void setList(List list) { 62 | this.list = list; 63 | } 64 | 65 | public int getStartIndex() { 66 | return startIndex; 67 | } 68 | 69 | public void setStartIndex(int startIndex) { 70 | this.startIndex = startIndex; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return "PageBean{" + 76 | "count=" + count + 77 | ", pageCount=" + pageCount + 78 | ", page=" + page + 79 | ", pageSize=" + pageSize + 80 | ", list=" + list + 81 | '}'; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/Result.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | //统一的json响应信息 3 | public class Result { 4 | private Boolean status;//布尔量,表示本次响应是否成功 5 | private String message;//本次响应的基本描述信息 6 | private Object data;//本次响应的数据,可以是任何类型数据,如果没有数据,为null 7 | 8 | public Result() { 9 | } 10 | 11 | public Result(Boolean status, String message, Object data) { 12 | this.status = status; 13 | this.message = message; 14 | this.data = data; 15 | } 16 | 17 | public Boolean getStatus() { 18 | return status; 19 | } 20 | 21 | public void setStatus(Boolean status) { 22 | this.status = status; 23 | } 24 | 25 | public String getMessage() { 26 | return message; 27 | } 28 | 29 | public void setMessage(String message) { 30 | this.message = message; 31 | } 32 | 33 | public Object getData() { 34 | return data; 35 | } 36 | 37 | public void setData(Object data) { 38 | this.data = data; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/Sensor.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | 5 | import java.io.Serializable; 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | public class Sensor implements Serializable { 10 | private Integer id;//主键 11 | private String description; 12 | private String location; 13 | private String factory; 14 | 15 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 16 | private Date install_time;//安装日期 17 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 18 | private Date produce_date;//生产日期 19 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 20 | private Date maintenance_time;//维修日期 21 | 22 | private Integer gate_id;//外键,关联网关主键 23 | private Integer classify_id;//外键,关联分类主键 24 | 25 | //以下为关联属性 26 | private Gateway gateway;//关联的网关 一对一 27 | private SensorClassify sensorClassify;//关联的分类 一对一 28 | private List datas;//关联的传感器数据 一对多 29 | 30 | public Sensor() { 31 | } 32 | 33 | public Sensor(Integer id, String description, String location, String factory, Date install_time, Date produce_date, Date maintenance_time, Integer gate_id, Integer classify_id) { 34 | this.id = id; 35 | this.description = description; 36 | this.location = location; 37 | this.factory = factory; 38 | this.install_time = install_time; 39 | this.produce_date = produce_date; 40 | this.maintenance_time = maintenance_time; 41 | this.gate_id = gate_id; 42 | this.classify_id = classify_id; 43 | } 44 | 45 | public Integer getId() { 46 | return id; 47 | } 48 | 49 | public void setId(Integer id) { 50 | this.id = id; 51 | } 52 | 53 | public String getDescription() { 54 | return description; 55 | } 56 | 57 | public void setDescription(String description) { 58 | this.description = description; 59 | } 60 | 61 | public String getLocation() { 62 | return location; 63 | } 64 | 65 | public void setLocation(String location) { 66 | this.location = location; 67 | } 68 | 69 | public String getFactory() { 70 | return factory; 71 | } 72 | 73 | public void setFactory(String factory) { 74 | this.factory = factory; 75 | } 76 | 77 | public Date getInstall_time() { 78 | return install_time; 79 | } 80 | 81 | public void setInstall_time(Date install_time) { 82 | this.install_time = install_time; 83 | } 84 | 85 | public Date getProduce_date() { 86 | return produce_date; 87 | } 88 | 89 | public void setProduce_date(Date produce_date) { 90 | this.produce_date = produce_date; 91 | } 92 | 93 | public Date getMaintenance_time() { 94 | return maintenance_time; 95 | } 96 | 97 | public void setMaintenance_time(Date maintenance_time) { 98 | this.maintenance_time = maintenance_time; 99 | } 100 | 101 | public Integer getGate_id() { 102 | return gate_id; 103 | } 104 | 105 | public void setGate_id(Integer gate_id) { 106 | this.gate_id = gate_id; 107 | } 108 | 109 | public Integer getClassify_id() { 110 | return classify_id; 111 | } 112 | 113 | public void setClassify_id(Integer classify_id) { 114 | this.classify_id = classify_id; 115 | } 116 | 117 | public Gateway getGateway() { 118 | return gateway; 119 | } 120 | 121 | public void setGateway(Gateway gateway) { 122 | this.gateway = gateway; 123 | } 124 | 125 | public SensorClassify getSensorClassify() { 126 | return sensorClassify; 127 | } 128 | 129 | public void setSensorClassify(SensorClassify sensorClassify) { 130 | this.sensorClassify = sensorClassify; 131 | } 132 | 133 | public List getDatas() { 134 | return datas; 135 | } 136 | 137 | public void setDatas(List datas) { 138 | this.datas = datas; 139 | } 140 | 141 | @Override 142 | public String toString() { 143 | return "Sensor{" + 144 | "id=" + id + 145 | ", description='" + description + '\'' + 146 | ", location='" + location + '\'' + 147 | ", factory='" + factory + '\'' + 148 | ", install_time=" + install_time + 149 | ", produce_date=" + produce_date + 150 | ", maintenance_time=" + maintenance_time + 151 | ", gate_id=" + gate_id + 152 | ", classify_id=" + classify_id + 153 | '}'; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/SensorClassify.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | //传感器分类 7 | public class SensorClassify implements Serializable { 8 | 9 | private Integer id;//主键 10 | private String name;//分类名称 11 | 12 | //该分类下关联的多个传感器(一对多) 13 | private List sensors; 14 | 15 | public SensorClassify() { 16 | } 17 | 18 | public SensorClassify(Integer id, String name) { 19 | this.id = id; 20 | this.name = name; 21 | } 22 | 23 | public Integer getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Integer id) { 28 | this.id = id; 29 | } 30 | 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public void setName(String name) { 36 | this.name = name; 37 | } 38 | 39 | public List getSensors() { 40 | return sensors; 41 | } 42 | 43 | public void setSensors(List sensors) { 44 | this.sensors = sensors; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "SensorClassify{" + 50 | "id=" + id + 51 | ", name='" + name + '\'' + 52 | '}'; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/SensorException.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import com.fasterxml.jackson.annotation.JsonFormat; 4 | 5 | import java.io.Serializable; 6 | import java.util.Date; 7 | 8 | public class SensorException implements Serializable { 9 | private Integer id;//主键 10 | private String description; 11 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 12 | private Date time; 13 | private Integer sensor_id; //关联传感器主键 14 | 15 | private Sensor sensor;//关联传感器对象 16 | 17 | public SensorException() { 18 | } 19 | 20 | public SensorException(Integer id, String description, Date time, Integer sensor_id) { 21 | this.id = id; 22 | this.description = description; 23 | this.time = time; 24 | this.sensor_id = sensor_id; 25 | } 26 | 27 | public Integer getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | 35 | public String getDescription() { 36 | return description; 37 | } 38 | 39 | public void setDescription(String description) { 40 | this.description = description; 41 | } 42 | 43 | public Date getTime() { 44 | return time; 45 | } 46 | 47 | public void setTime(Date time) { 48 | this.time = time; 49 | } 50 | 51 | public Integer getSensor_id() { 52 | return sensor_id; 53 | } 54 | 55 | public void setSensor_id(Integer sensor_id) { 56 | this.sensor_id = sensor_id; 57 | } 58 | 59 | public Sensor getSensor() { 60 | return sensor; 61 | } 62 | 63 | public void setSensor(Sensor sensor) { 64 | this.sensor = sensor; 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return "SensorException{" + 70 | "id=" + id + 71 | ", description='" + description + '\'' + 72 | ", time=" + time + 73 | ", sensor_id=" + sensor_id + 74 | '}'; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/domain/User.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.domain; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | 5 | import java.io.Serializable; 6 | 7 | public class User implements Serializable { 8 | private Integer id; 9 | private String username; 10 | private String nickname; 11 | 12 | @JsonIgnore 13 | private String password;//响应json时不包含密码 14 | private String tel; 15 | private String email; 16 | 17 | public User() { 18 | } 19 | 20 | public User(Integer id, String username, String nickname, String password, String tel, String email) { 21 | this.id = id; 22 | this.username = username; 23 | this.nickname = nickname; 24 | this.password = password; 25 | this.tel = tel; 26 | this.email = email; 27 | } 28 | 29 | public Integer getId() { 30 | return id; 31 | } 32 | 33 | public void setId(Integer id) { 34 | this.id = id; 35 | } 36 | 37 | public String getUsername() { 38 | return username; 39 | } 40 | 41 | public void setUsername(String username) { 42 | this.username = username; 43 | } 44 | 45 | public String getNickname() { 46 | return nickname; 47 | } 48 | 49 | public void setNickname(String nickname) { 50 | this.nickname = nickname; 51 | } 52 | 53 | public String getPassword() { 54 | return password; 55 | } 56 | 57 | public void setPassword(String password) { 58 | this.password = password; 59 | } 60 | 61 | public String getTel() { 62 | return tel; 63 | } 64 | 65 | public void setTel(String tel) { 66 | this.tel = tel; 67 | } 68 | 69 | public String getEmail() { 70 | return email; 71 | } 72 | 73 | public void setEmail(String email) { 74 | this.email = email; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "User{" + 80 | "id=" + id + 81 | ", username='" + username + '\'' + 82 | ", nickname='" + nickname + '\'' + 83 | ", password='" + password + '\'' + 84 | ", tel='" + tel + '\'' + 85 | ", email='" + email + '\'' + 86 | '}'; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/interceptor/LoginInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.interceptor; 2 | 3 | import com.my.iot.controller.UserController; 4 | import com.my.iot.domain.User; 5 | import org.springframework.data.redis.core.RedisTemplate; 6 | import org.springframework.stereotype.Component; 7 | import org.springframework.web.servlet.HandlerInterceptor; 8 | 9 | import javax.annotation.Resource; 10 | import javax.servlet.http.Cookie; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | 14 | //拦截所有未登录的请求 15 | @Component 16 | public class LoginInterceptor implements HandlerInterceptor { 17 | 18 | private static final String json = "{\"status\":false, \"message\":\"please login\", \"data\":null}";//未登录被拦截时的响应 19 | private static final String JSESSIONID = "JSESSIONID"; 20 | 21 | @Resource 22 | private RedisTemplate redisTemplate; 23 | 24 | @Override 25 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 26 | //System.out.println("拦截器...preHandle..."); 27 | String sessionId = getJSESSIONID(request); 28 | response.setContentType("text/json;charset=UTF-8"); 29 | if (sessionId == null) {//请求中没有JSESSIONID,不放行 30 | //response.getWriter().write(json); 31 | response.sendRedirect("/html/login.html"); 32 | return false; 33 | } 34 | //从redis中查询登录用户 35 | User login_user = (User) redisTemplate.opsForHash().get(sessionId, UserController.LOGIN_USER); 36 | if (login_user == null) { //未登录,不放行 37 | //response.getWriter().write(json); 38 | response.sendRedirect("/html/login.html"); 39 | return false; 40 | } else { 41 | return true;//登陆后放行 42 | } 43 | } 44 | 45 | //从请求中获取名称为JSESSIONID的Cookie值 46 | private String getJSESSIONID(HttpServletRequest request) { 47 | Cookie[] cookies = request.getCookies(); 48 | for (Cookie cookie : cookies) { 49 | if (JSESSIONID.equals(cookie.getName())) { 50 | return cookie.getValue(); 51 | } 52 | } 53 | return null; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/mapper/DataMapper.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.mapper; 2 | 3 | import com.my.iot.domain.Data; 4 | import org.apache.ibatis.annotations.*; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.Date; 8 | import java.util.List; 9 | 10 | @Repository 11 | @Mapper 12 | public interface DataMapper { 13 | 14 | @Insert("insert into tb_data values(null, #{data}, #{time}, #{sensor_id})") 15 | @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")//返回自增主键 16 | public void add(Data data); 17 | 18 | @Select("select * from tb_data") 19 | public List findAll(); 20 | 21 | @Select("select * from tb_data where sensor_id = #{value}") 22 | public List findBySensorId(int sensor_id); 23 | 24 | @Select("delete from tb_data where sensor_id = #{value}") 25 | public void deleteBySensorId(int sensor_id); 26 | 27 | @Select("select * from tb_data where sensor_id = #{sensor_id} and time between #{datetime1} and #{datetime2}") 28 | List findBySensorIdInDatetime(@Param("sensor_id") Integer sensor_id, Date datetime1, Date datetime2); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/mapper/GatewayExceptionMapper.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.mapper; 2 | 3 | import com.my.iot.domain.GatewayException; 4 | import com.my.iot.domain.PageBean; 5 | import org.apache.ibatis.annotations.Insert; 6 | import org.apache.ibatis.annotations.Mapper; 7 | import org.apache.ibatis.annotations.Options; 8 | import org.apache.ibatis.annotations.Select; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import java.util.Date; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | 15 | @Repository 16 | @Mapper 17 | public interface GatewayExceptionMapper { 18 | 19 | @Insert("insert into tb_gateway_exception values(null, #{description}, #{time}, #{gate_id})") 20 | @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")//返回自增主键 21 | public void add(GatewayException gatewayException); 22 | 23 | @Select("select * from tb_gateway_exception") 24 | public List findAll(); 25 | 26 | @Select("select count(id) from tb_gateway_exception") 27 | public int findAllCount(); 28 | 29 | @Select("select * from tb_gateway_exception limit #{startIndex}, #{pageSize}") 30 | public List findByPage(PageBean pageBean); 31 | 32 | @Select("select * from tb_gateway_exception where time between #{from} and #{to}") 33 | public List findByTime(HashMap conditions); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/mapper/GatewayMapper.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.mapper; 2 | 3 | import com.my.iot.domain.Gateway; 4 | import org.apache.ibatis.annotations.*; 5 | import org.apache.ibatis.mapping.FetchType; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | 10 | @Repository 11 | @Mapper 12 | public interface GatewayMapper { 13 | 14 | @Insert("insert into tb_gateway values(null, #{ip}, #{port}, #{description}, #{location})") 15 | @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")//返回自增主键,封装到gateway对象中 16 | public void insert(Gateway gateway); 17 | 18 | @Select("select * from tb_gateway where id = #{value}") 19 | public Gateway findById(int id); 20 | 21 | @Update("update tb_gateway set ip = #{ip}, port = #{port}, description = #{description}, location = #{location} where id = #{id}") 22 | public void update(Gateway gateway); 23 | 24 | @Delete("delete from tb_gateway where id = #{value}") 25 | public void deleteById(int id); 26 | 27 | @Select("select * from tb_gateway") 28 | public List findAll(); 29 | 30 | 31 | @Select("select * from tb_gateway where id = #{value}") 32 | @Results(id = "resultMap2", value = { 33 | @Result(property = "id", column = "id", id = true),//主键 //固有属性property与column相同可以自动封装 34 | @Result(property = "sensors", column = "id", many = @Many(select = "com.my.iot.mapper.SensorMapper.findByGatewayId", fetchType = FetchType.DEFAULT))//一对多封装sensors属性 35 | }) 36 | public Gateway findByIdWithSensors(int id); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/mapper/SensorClassifyMapper.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.mapper; 2 | 3 | import com.my.iot.domain.SensorClassify; 4 | import org.apache.ibatis.annotations.*; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | @Mapper 11 | public interface SensorClassifyMapper { 12 | @Insert("insert into tb_sensor_classify values(null, #{name})") 13 | @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")//返回自增主键 14 | public void add(SensorClassify classify); 15 | 16 | @Select("select * from tb_sensor_classify") 17 | public List findAll(); 18 | 19 | @Select("select * from tb_sensor_classify where id = #{value}") 20 | public SensorClassify findById(int id); 21 | 22 | @Select("select t1.id cid, t1.name cname " + 23 | "from tb_sensor_classify t1, tb_sensor t2, tb_gateway t3 " + 24 | "where t2.gate_id = #{value} and t1.id = t2.classify_id and t3.id = t2.gate_id " + 25 | "group by t1.id;") 26 | @Results(id = "resultMap1",value = { 27 | @Result(id = true,column = "cid",property = "id"), 28 | @Result(column = "cname",property = "name") 29 | }) 30 | public List findByGatewayId(int gate_id);//使用传感器表作为中间表多表查询(多对多) 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/mapper/SensorExceptionMapper.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.mapper; 2 | 3 | import com.my.iot.domain.GatewayException; 4 | import com.my.iot.domain.PageBean; 5 | import com.my.iot.domain.SensorException; 6 | import org.apache.ibatis.annotations.Insert; 7 | import org.apache.ibatis.annotations.Mapper; 8 | import org.apache.ibatis.annotations.Options; 9 | import org.apache.ibatis.annotations.Select; 10 | import org.springframework.stereotype.Repository; 11 | 12 | import java.util.Date; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | 16 | @Repository 17 | @Mapper 18 | public interface SensorExceptionMapper { 19 | 20 | @Insert("insert into tb_sensor_exception values(null, #{description}, #{time}, #{sensor_id})") 21 | @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")//返回自增主键 22 | public void add(SensorException sensorException); 23 | 24 | @Select("select * from tb_sensor_exception") 25 | public List findAll(); 26 | 27 | @Select("select count(id) from tb_sensor_exception") 28 | public int findAllCount(); 29 | 30 | @Select("select * from tb_sensor_exception limit #{startIndex}, #{pageSize}") 31 | public List findByPage(PageBean pageBean); 32 | 33 | @Select("select * from tb_sensor_exception where time >= #{from} and time <= #{to}") 34 | public List findByTime(HashMap conditions); 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/mapper/SensorMapper.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.mapper; 2 | 3 | import com.my.iot.domain.Sensor; 4 | import org.apache.ibatis.annotations.*; 5 | import org.apache.ibatis.mapping.FetchType; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | 10 | @Repository 11 | @Mapper 12 | public interface SensorMapper { 13 | @Insert("insert into tb_sensor values(null, #{description}, #{location}, #{factory}, #{install_time}, #{produce_date}, #{maintenance_time}, #{gate_id}, #{classify_id})") 14 | @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")//返回自增主键 15 | public void add(Sensor sensor); 16 | 17 | @Select("select * from tb_sensor where id = #{value}") 18 | public Sensor findById(int id); 19 | 20 | @Select("select * from tb_sensor") 21 | public List findAll(); 22 | 23 | @Update("update tb_sensor set description = #{description}, location = #{location}, " + 24 | "factory = #{factory}, install_time = #{install_time}, produce_date = #{produce_date}, " + 25 | "maintenance_time = #{maintenance_time}, gate_id = #{gate_id}, classify_id = #{classify_id} " + 26 | "where id = #{id}") 27 | public void update(Sensor sensor); 28 | 29 | @Delete("delete from tb_sensor where id = #{value}") 30 | public void deleteById(int id); 31 | 32 | @Select("select * from tb_sensor where classify_id = #{value}") 33 | public List findByClassifyId(int classify_id); 34 | 35 | @Select("select * from tb_sensor where gate_id = #{value}") 36 | public List findByGatewayId(int gateway_id); 37 | 38 | @Select("select * from tb_sensor where id = #{value}") 39 | @Results(id = "resultMap3", value = { 40 | @Result(property = "id", column = "id", id = true),//主键 //固有属性property与column相同可以自动封装 41 | @Result(property = "datas", column = "id", many = @Many(select = "com.my.iot.mapper.DataMapper.findBySensorId", fetchType = FetchType.DEFAULT))//一对多封装datas属性 42 | }) 43 | public Sensor findByIdWithDatas(int id); 44 | 45 | @Select("")//使用动态sql,如果参数为空,就不拼接条件 50 | public List findByGatewayIdAndClassifyId(@Param("gateway_id") Integer gateway_id, @Param("classify_id") Integer classify_id); 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.mapper; 2 | 3 | import com.my.iot.domain.User; 4 | import org.apache.ibatis.annotations.Insert; 5 | import org.apache.ibatis.annotations.Mapper; 6 | import org.apache.ibatis.annotations.Select; 7 | import org.apache.ibatis.annotations.Update; 8 | import org.springframework.stereotype.Repository; 9 | 10 | @Repository 11 | @Mapper 12 | public interface UserMapper { 13 | @Select("select * from tb_user where username = #{value}") 14 | public User findByUsername(String username); 15 | 16 | @Insert("insert into tb_user values(null, #{username}, #{nickname}, #{password}, #{tel}, #{email})") 17 | public void insert(User user); 18 | 19 | @Select("select * from tb_user where username = #{username} and password = #{password}") 20 | public User findByUsernameAndPassword(User user); 21 | 22 | @Update("update tb_user set nickname = #{nickname}, password = #{password}, tel = #{tel}, email = #{email} where id = #{id}") 23 | void update(User user); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/schedule/AsyncTaskService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.schedule; 2 | 3 | import com.my.iot.domain.Data; 4 | import com.my.iot.mapper.DataMapper; 5 | import com.my.iot.service.impl.DataServiceImpl; 6 | import org.springframework.beans.factory.DisposableBean; 7 | import org.springframework.beans.factory.InitializingBean; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.data.redis.core.RedisTemplate; 10 | import org.springframework.stereotype.Component; 11 | 12 | import javax.annotation.Resource; 13 | import java.util.concurrent.ExecutorService; 14 | import java.util.concurrent.Executors; 15 | import java.util.concurrent.TimeUnit; 16 | //异步任务服务,用于将Redis的Data缓存队列添加到数据库 17 | @Component 18 | public class AsyncTaskService implements InitializingBean, DisposableBean { 19 | 20 | @Autowired 21 | private DataMapper dataMapper; 22 | 23 | @Resource 24 | private RedisTemplate redisTemplate; 25 | 26 | private ExecutorService executorService; 27 | 28 | @Override 29 | public void afterPropertiesSet() {//Bean的初始化方法 30 | System.out.println("线程池初始化..."); 31 | executorService = Executors.newSingleThreadExecutor(); 32 | start(DataServiceImpl.DATA_BUFFER); 33 | } 34 | 35 | @Override 36 | public void destroy() { 37 | System.out.println("线程池初始化..."); 38 | shutdown(); 39 | } 40 | 41 | private volatile boolean run = false; 42 | 43 | public synchronized void start(String key) {//启动线程池,启动异步任务 44 | run = true; 45 | executorService.submit(() -> { 46 | while (run) { 47 | Data data = redisTemplate.opsForList().leftPop(key, 10, TimeUnit.SECONDS);//阻塞式地不断从缓存队列里拿出数据 48 | if (data != null) { 49 | //System.out.println("向数据库保存数据" + data); 50 | dataMapper.add(data);//存入数据库 51 | } 52 | } 53 | }); 54 | } 55 | 56 | public synchronized void shutdown() {//关闭线程池 57 | run = false; 58 | executorService.shutdown(); 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/DataService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service; 2 | 3 | import com.my.iot.domain.Data; 4 | 5 | import java.util.Date; 6 | import java.util.List; 7 | 8 | public interface DataService { 9 | public void add(List datas); 10 | 11 | public void add(Data data); 12 | 13 | public List findAll(); 14 | 15 | public List findBySensorId(int sensor_id); 16 | 17 | public List findBySensorIdInDatetime(Integer sensor_id, Date datetime1, Date datetime2); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/GatewayExceptionService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service; 2 | 3 | import com.my.iot.domain.GatewayException; 4 | import com.my.iot.domain.PageBean; 5 | 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | public interface GatewayExceptionService { 10 | public void add(GatewayException gatewayException); 11 | 12 | public List findAll(); 13 | 14 | public PageBean findByPage(int page); 15 | 16 | public List findByTime(Date date1, Date date2); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/GatewayService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service; 2 | 3 | import com.my.iot.domain.Gateway; 4 | 5 | import java.util.List; 6 | 7 | public interface GatewayService { 8 | public void add(Gateway gateway); 9 | 10 | public Gateway findById(int id); 11 | 12 | public void update(Gateway gateway); 13 | 14 | public void deleteById(int id); 15 | 16 | public List findAll(Boolean withSensors); 17 | 18 | public Gateway findByIdWithSensors(int id); 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/SensorClassifyService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service; 2 | 3 | import com.my.iot.domain.SensorClassify; 4 | 5 | import java.util.List; 6 | 7 | public interface SensorClassifyService { 8 | 9 | public void add(SensorClassify classify); 10 | 11 | public List findAll(Boolean withSensors); 12 | 13 | public SensorClassify findById(int id); 14 | 15 | public List findByGatewayId(int gateway_id); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/SensorExceptionService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service; 2 | 3 | import com.my.iot.domain.PageBean; 4 | import com.my.iot.domain.SensorException; 5 | 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | public interface SensorExceptionService { 10 | 11 | public void add(SensorException sensorException); 12 | 13 | public List findAll(); 14 | 15 | public PageBean findByPage(int page); 16 | 17 | public List findByTime(Date date1, Date date2); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/SensorService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service; 2 | 3 | import com.my.iot.domain.Sensor; 4 | 5 | import java.util.List; 6 | 7 | public interface SensorService { 8 | 9 | public void add(Sensor sensor); 10 | 11 | public Sensor findById(int id); 12 | 13 | public List findAll(); 14 | 15 | public void update(Sensor sensor); 16 | 17 | public void deleteById(int id); 18 | 19 | public List findByClassifyId(int classify_id); 20 | 21 | public List findByGatewayId(int gateway_id); 22 | 23 | public List findByGatewayIdAndClassifyId(Integer gateway_id,Integer classify_id); 24 | 25 | public Sensor findByIdWithDatas(int id); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service; 2 | 3 | import com.my.iot.domain.User; 4 | 5 | public interface UserService { 6 | public boolean regist(User user); 7 | 8 | public User login(User user); 9 | 10 | public void update(User user); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/impl/DataServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service.impl; 2 | 3 | import com.my.iot.domain.Data; 4 | import com.my.iot.mapper.DataMapper; 5 | import com.my.iot.schedule.AsyncTaskService; 6 | import com.my.iot.service.DataService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.data.redis.core.RedisTemplate; 9 | import org.springframework.stereotype.Service; 10 | 11 | import javax.annotation.Resource; 12 | import java.util.Date; 13 | import java.util.List; 14 | 15 | @Service("dataService") 16 | public class DataServiceImpl implements DataService { 17 | 18 | public static final String DATA_BUFFER = "data_buffer"; 19 | 20 | @Resource 21 | private RedisTemplate redisTemplate; 22 | 23 | @Autowired 24 | private DataMapper dataMapper; 25 | 26 | @Override 27 | public void add(Data data) { 28 | //将数据存入缓存(程序一开始就会启动异步任务,不断从缓存队列取出元素存入数据库) 29 | redisTemplate.opsForList().rightPush(DATA_BUFFER, data); 30 | } 31 | 32 | @Override 33 | public List findAll() { 34 | return dataMapper.findAll(); 35 | } 36 | 37 | @Override 38 | public List findBySensorId(int sensor_id) { 39 | return dataMapper.findBySensorId(sensor_id); 40 | } 41 | 42 | @Override 43 | public List findBySensorIdInDatetime(Integer sensor_id, Date datetime1, Date datetime2) { 44 | return dataMapper.findBySensorIdInDatetime(sensor_id, datetime1, datetime2); 45 | } 46 | 47 | @Override 48 | public void add(List datas) { 49 | //将数据存入缓存(程序一开始就会启动异步任务,不断从缓存队列取出元素存入数据库) 50 | redisTemplate.opsForList().rightPushAll(DATA_BUFFER, datas); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/impl/GatewayExceptionServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service.impl; 2 | 3 | import com.my.iot.domain.GatewayException; 4 | import com.my.iot.domain.PageBean; 5 | import com.my.iot.mapper.GatewayExceptionMapper; 6 | import com.my.iot.service.GatewayExceptionService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.Date; 11 | import java.util.HashMap; 12 | import java.util.List; 13 | 14 | @Service("gatewayExceptionServiceImpl") 15 | public class GatewayExceptionServiceImpl implements GatewayExceptionService { 16 | 17 | @Autowired 18 | private GatewayExceptionMapper gatewayExceptionMapper; 19 | 20 | @Override 21 | public void add(GatewayException gatewayException) { 22 | gatewayExceptionMapper.add(gatewayException); 23 | } 24 | 25 | @Override 26 | public List findAll() { 27 | return gatewayExceptionMapper.findAll(); 28 | } 29 | 30 | @Override 31 | public PageBean findByPage(int page) { 32 | int count = gatewayExceptionMapper.findAllCount(); 33 | int pageSize = 2;//默认每页5个 34 | int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;//计算总页数 35 | page = Math.max(1, page);//限制页数 36 | page = Math.min(page, pageCount);//限制页数 37 | PageBean pageBean = new PageBean<>(count, pageCount, page, pageSize, null);//封装pageBean 38 | 39 | //调用持久层分页查询 40 | int startIndex = (page - 1) * pageSize;//计算开始索引 41 | pageBean.setStartIndex(startIndex); 42 | List list = gatewayExceptionMapper.findByPage(pageBean); 43 | pageBean.setList(list); 44 | return pageBean; 45 | } 46 | 47 | @Override 48 | public List findByTime(Date date1, Date date2) { 49 | HashMap hashMap = new HashMap<>(); 50 | hashMap.put("from", date1); 51 | hashMap.put("to",date2); 52 | return gatewayExceptionMapper.findByTime(hashMap); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/impl/GatewayServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service.impl; 2 | 3 | import com.my.iot.domain.Gateway; 4 | import com.my.iot.domain.Sensor; 5 | import com.my.iot.mapper.DataMapper; 6 | import com.my.iot.mapper.GatewayMapper; 7 | import com.my.iot.mapper.SensorMapper; 8 | import com.my.iot.service.GatewayService; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.data.redis.core.RedisTemplate; 11 | import org.springframework.stereotype.Service; 12 | import org.springframework.transaction.annotation.Transactional; 13 | 14 | import javax.annotation.Resource; 15 | import java.util.List; 16 | 17 | @Service("gatewayService") 18 | public class GatewayServiceImpl implements GatewayService { 19 | 20 | public static final String GATEWAY = "gateway_"; 21 | 22 | @Autowired 23 | private GatewayMapper gatewayMapper; 24 | 25 | @Autowired 26 | private SensorMapper sensorMapper; 27 | 28 | @Autowired 29 | private DataMapper dataMapper; 30 | 31 | @Resource 32 | private RedisTemplate redisTemplate; 33 | 34 | @Override 35 | public void add(Gateway gateway) { 36 | gatewayMapper.insert(gateway); 37 | } 38 | 39 | @Override 40 | public Gateway findById(int id) { 41 | Gateway gateway = (Gateway) redisTemplate.opsForValue().get(GATEWAY + id);//先从缓存查询 42 | if (gateway != null) { 43 | //System.out.println("从缓存查询"); 44 | return gateway; 45 | } else { 46 | //System.out.println("从数据库查询"); 47 | gateway = gatewayMapper.findById(id); 48 | redisTemplate.opsForValue().set(GATEWAY + id, gateway);//存入缓存 49 | } 50 | return gateway; 51 | } 52 | 53 | @Override 54 | public void update(Gateway gateway) { 55 | redisTemplate.delete(GATEWAY + gateway.getId());//清除对应缓存 56 | gatewayMapper.update(gateway); 57 | } 58 | 59 | @Override 60 | @Transactional 61 | public void deleteById(int id) { 62 | //先删除网关下的传感器及其所有数据 63 | List sensors = sensorMapper.findByGatewayId(id); 64 | for (Sensor sensor : sensors) { 65 | Integer sensorId = sensor.getId(); 66 | dataMapper.deleteBySensorId(sensorId); 67 | sensorMapper.deleteById(sensorId); 68 | } 69 | //再删除网关 70 | redisTemplate.delete(GATEWAY + id);//清除对应缓存 71 | gatewayMapper.deleteById(id); 72 | } 73 | 74 | @Override 75 | public List findAll(Boolean withSensors) {//所有信息可能经常变化,不使用缓存 76 | List gateways = gatewayMapper.findAll();//查询所有网关信息 77 | if (new Boolean(true).equals(withSensors)) { 78 | for (Gateway gateway : gateways) {//查询并封装网关的传感器信息 79 | Integer gatewayId = gateway.getId(); 80 | List sensors = sensorMapper.findByGatewayId(gatewayId); 81 | gateway.setSensors(sensors); 82 | } 83 | } 84 | return gateways; 85 | } 86 | 87 | @Override 88 | public Gateway findByIdWithSensors(int id) {//传感器信息可能经常变化,不使用缓存 89 | return gatewayMapper.findByIdWithSensors(id); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/impl/SensorClassifyServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service.impl; 2 | 3 | import com.my.iot.domain.Gateway; 4 | import com.my.iot.domain.Sensor; 5 | import com.my.iot.domain.SensorClassify; 6 | import com.my.iot.mapper.SensorClassifyMapper; 7 | import com.my.iot.mapper.SensorMapper; 8 | import com.my.iot.service.SensorClassifyService; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.data.redis.core.RedisTemplate; 11 | import org.springframework.stereotype.Service; 12 | 13 | import javax.annotation.Resource; 14 | import java.util.List; 15 | 16 | @Service("sensorClassifyService") 17 | public class SensorClassifyServiceImpl implements SensorClassifyService { 18 | 19 | public static final String SENSOR_CLASSIFY = "sensor_classify_"; 20 | 21 | @Resource 22 | private RedisTemplate redisTemplate; 23 | 24 | @Autowired 25 | private SensorClassifyMapper sensorClassifyMapper; 26 | 27 | @Autowired 28 | private SensorMapper sensorMapper; 29 | 30 | @Override 31 | public void add(SensorClassify classify) { 32 | sensorClassifyMapper.add(classify); 33 | } 34 | 35 | @Override 36 | public List findAll(Boolean withSensors) { 37 | List classifies = sensorClassifyMapper.findAll();//查询所有分类 38 | if (new Boolean(true).equals(withSensors)) { 39 | for (SensorClassify classify : classifies) {//查询并封装每个分类下的传感器 40 | Integer classifyId = classify.getId(); 41 | List sensors = sensorMapper.findByClassifyId(classifyId); 42 | classify.setSensors(sensors); 43 | } 44 | } 45 | return classifies; 46 | } 47 | 48 | @Override 49 | public SensorClassify findById(int id) { 50 | SensorClassify sensorClassify = (SensorClassify) redisTemplate.opsForValue().get(SENSOR_CLASSIFY + id);//先从缓存查询 51 | if (sensorClassify == null) { 52 | sensorClassify = sensorClassifyMapper.findById(id); 53 | redisTemplate.opsForValue().set(SENSOR_CLASSIFY + id, sensorClassify); 54 | } 55 | return sensorClassify; 56 | } 57 | 58 | @Override 59 | public List findByGatewayId(int gateway_id) { 60 | return sensorClassifyMapper.findByGatewayId(gateway_id); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/impl/SensorExceptionServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service.impl; 2 | 3 | import com.my.iot.domain.GatewayException; 4 | import com.my.iot.domain.PageBean; 5 | import com.my.iot.domain.SensorException; 6 | import com.my.iot.mapper.SensorExceptionMapper; 7 | import com.my.iot.service.SensorExceptionService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import java.util.Date; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | 15 | @Service("sensorExceptionService") 16 | public class SensorExceptionServiceImpl implements SensorExceptionService { 17 | 18 | @Autowired 19 | private SensorExceptionMapper sensorExceptionMapper; 20 | 21 | @Override 22 | public void add(SensorException sensorException) { 23 | sensorExceptionMapper.add(sensorException); 24 | } 25 | 26 | @Override 27 | public List findAll() { 28 | return sensorExceptionMapper.findAll(); 29 | } 30 | 31 | @Override 32 | public PageBean findByPage(int page) { 33 | int count = sensorExceptionMapper.findAllCount(); 34 | int pageSize = 2;//默认每页5个 35 | int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;//计算总页数 36 | page = Math.max(1, page);//限制页数 37 | page = Math.min(page, pageCount);//限制页数 38 | PageBean pageBean = new PageBean<>(count, pageCount, page, pageSize, null);//封装pageBean 39 | 40 | //调用持久层分页查询 41 | int startIndex = (page - 1) * pageSize;//计算开始索引 42 | pageBean.setStartIndex(startIndex); 43 | List list = sensorExceptionMapper.findByPage(pageBean); 44 | pageBean.setList(list); 45 | return pageBean; 46 | } 47 | 48 | @Override 49 | public List findByTime(Date date1, Date date2) { 50 | HashMap hashMap = new HashMap<>(); 51 | hashMap.put("from", date1); 52 | hashMap.put("to",date2); 53 | return sensorExceptionMapper.findByTime(hashMap); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/impl/SensorServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service.impl; 2 | 3 | import com.my.iot.domain.Sensor; 4 | import com.my.iot.domain.SensorClassify; 5 | import com.my.iot.mapper.DataMapper; 6 | import com.my.iot.mapper.SensorMapper; 7 | import com.my.iot.service.SensorService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.data.redis.core.RedisTemplate; 10 | import org.springframework.stereotype.Service; 11 | import org.springframework.transaction.annotation.Transactional; 12 | 13 | import javax.annotation.Resource; 14 | import java.util.List; 15 | 16 | @Service("SensorService") 17 | public class SensorServiceImpl implements SensorService { 18 | 19 | public static final String SENSOR = "sensor_"; 20 | 21 | @Resource 22 | private RedisTemplate redisTemplate; 23 | 24 | @Autowired 25 | private SensorMapper sensorMapper; 26 | 27 | @Autowired 28 | private DataMapper dataMapper; 29 | 30 | @Override 31 | public void add(Sensor sensor) { 32 | sensorMapper.add(sensor); 33 | } 34 | 35 | @Override 36 | public Sensor findById(int id) { 37 | Sensor sensor = (Sensor) redisTemplate.opsForValue().get(SENSOR + id);//先从缓存查询 38 | if (sensor == null) { 39 | sensor = sensorMapper.findById(id); 40 | redisTemplate.opsForValue().set(SENSOR + id, sensor); 41 | } 42 | return sensor; 43 | } 44 | 45 | @Override 46 | public List findAll() { 47 | return sensorMapper.findAll(); 48 | } 49 | 50 | @Override 51 | public void update(Sensor sensor) { 52 | redisTemplate.delete(SENSOR + sensor.getId());//清除对应缓存 53 | sensorMapper.update(sensor); 54 | } 55 | 56 | @Override 57 | @Transactional 58 | public void deleteById(int id) { 59 | //先删除传感器下的所有数据 60 | Sensor sensor = sensorMapper.findById(id); 61 | dataMapper.deleteBySensorId(sensor.getId()); 62 | //再删除传感器 63 | redisTemplate.delete(SENSOR + sensor.getId());//清除对应缓存 64 | sensorMapper.deleteById(id); 65 | } 66 | 67 | @Override 68 | public List findByClassifyId(int classify_id) { 69 | return sensorMapper.findByClassifyId(classify_id); 70 | } 71 | 72 | @Override 73 | public List findByGatewayId(int gateway_id) { 74 | return sensorMapper.findByGatewayId(gateway_id); 75 | } 76 | 77 | @Override 78 | public List findByGatewayIdAndClassifyId(Integer gateway_id,Integer classify_id) { 79 | return sensorMapper.findByGatewayIdAndClassifyId(gateway_id,classify_id); 80 | } 81 | 82 | @Override 83 | public Sensor findByIdWithDatas(int id) { 84 | return sensorMapper.findByIdWithDatas(id); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.service.impl; 2 | 3 | import com.my.iot.domain.User; 4 | import com.my.iot.mapper.UserMapper; 5 | import com.my.iot.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | @Service("userService") 10 | public class UserServiceImpl implements UserService { 11 | @Autowired 12 | private UserMapper userMapper; 13 | 14 | @Override 15 | public boolean regist(User user) { 16 | User u = userMapper.findByUsername(user.getUsername()); 17 | if (u != null) {//用户名已存在,注册失败 18 | return false; 19 | } 20 | userMapper.insert(user);//不存在,注册 21 | return true; 22 | } 23 | 24 | @Override 25 | public User login(User user) {//登录,成功返回完整用户信息,失败返回null 26 | return userMapper.findByUsernameAndPassword(user); 27 | } 28 | 29 | @Override 30 | public void update(User user) { 31 | userMapper.update(user); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/my/iot/util/Md5Util.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.util; 2 | 3 | import java.security.MessageDigest; 4 | 5 | /** 6 | * 写一个MD5算法,运行结果与MySQL的md5()函数相同 7 | * 将明文密码转成MD5密码 8 | * 123456->e10adc3949ba59abbe56e057f20f883e 9 | */ 10 | public final class Md5Util { 11 | private Md5Util(){} 12 | /** 13 | * 将明文密码转成MD5密码 14 | */ 15 | public static String encodeByMd5(String password) throws Exception{ 16 | //Java中MessageDigest类封装了MD5和SHA算法,今天我们只要MD5算法 17 | MessageDigest md5 = MessageDigest.getInstance("MD5"); 18 | //调用MD5算法,即返回16个byte类型的值 19 | byte[] byteArray = md5.digest(password.getBytes()); 20 | //注意:MessageDigest只能将String转成byte[],接下来的事情,由我们程序员来完成 21 | return byteArrayToHexString(byteArray); 22 | } 23 | /** 24 | * 将byte[]转在16进制字符串 25 | */ 26 | private static String byteArrayToHexString(byte[] byteArray) { 27 | StringBuffer sb = new StringBuffer(); 28 | //遍历 29 | for(byte b : byteArray){//16次 30 | //取出每一个byte类型,进行转换 31 | String hex = byteToHexString(b); 32 | //将转换后的值放入StringBuffer中 33 | sb.append(hex); 34 | } 35 | return sb.toString(); 36 | } 37 | /** 38 | * 将byte转在16进制字符串 39 | */ 40 | private static String byteToHexString(byte b) {//-31转成e1,10转成0a,。。。 41 | //将byte类型赋给int类型 42 | int n = b; 43 | //如果n是负数 44 | if(n < 0){ 45 | //转正数 46 | //-31的16进制数,等价于求225的16进制数 47 | n = 256 + n; 48 | } 49 | //商(14),数组的下标 50 | int d1 = n / 16; 51 | //余(1),数组的下标 52 | int d2 = n % 16; 53 | //通过下标取值 54 | return hex[d1] + hex[d2]; 55 | } 56 | private static String[] hex = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; 57 | /** 58 | * 测试 59 | */ 60 | public static void main(String[] args) throws Exception{ 61 | String password = "123456"; 62 | String passwordMD5 = Md5Util.encodeByMd5(password); 63 | System.out.println(password); 64 | System.out.println(passwordMD5); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 # 内置tomcat端口 3 | servlet: 4 | context-path: / # 项目部署虚拟路径 5 | 6 | spring: 7 | datasource: # hikari pool 数据源 8 | driver-class-name: com.mysql.jdbc.Driver 9 | url: jdbc:mysql://localhost:3306/iot 10 | username: root 11 | password: root 12 | redis: 13 | host: 127.0.0.1 14 | port: 6379 15 | 16 | mybatis: 17 | type-aliases-package: com.my.domain # pojo别名扫描 -------------------------------------------------------------------------------- /src/main/resources/iot.sql: -------------------------------------------------------------------------------- 1 | -- MySQL dump 10.13 Distrib 5.7.26, for Win64 (x86_64) 2 | -- 3 | -- Host: localhost Database: iot 4 | -- ------------------------------------------------------ 5 | -- Server version 5.7.26-log 6 | 7 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 8 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 9 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 10 | /*!40101 SET NAMES utf8 */; 11 | /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; 12 | /*!40103 SET TIME_ZONE='+00:00' */; 13 | /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; 14 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 15 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 16 | /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; 17 | 18 | -- 19 | -- Table structure for table `tb_data` 20 | -- 21 | 22 | DROP TABLE IF EXISTS `tb_data`; 23 | /*!40101 SET @saved_cs_client = @@character_set_client */; 24 | /*!40101 SET character_set_client = utf8 */; 25 | CREATE TABLE `tb_data` ( 26 | `id` int(11) NOT NULL AUTO_INCREMENT, 27 | `data` float DEFAULT NULL, 28 | `time` datetime DEFAULT NULL, 29 | `sensor_id` int(11) NOT NULL, 30 | PRIMARY KEY (`id`), 31 | KEY `fk_data_sensor_1` (`sensor_id`), 32 | CONSTRAINT `fk_data_sensor_1` FOREIGN KEY (`sensor_id`) REFERENCES `tb_sensor` (`id`) 33 | ) ENGINE=InnoDB AUTO_INCREMENT=64 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 34 | /*!40101 SET character_set_client = @saved_cs_client */; 35 | 36 | -- 37 | -- Dumping data for table `tb_data` 38 | -- 39 | 40 | LOCK TABLES `tb_data` WRITE; 41 | /*!40000 ALTER TABLE `tb_data` DISABLE KEYS */; 42 | INSERT INTO `tb_data` VALUES (1,0,'2020-02-17 14:16:32',1),(2,0.1,'2020-02-17 14:16:33',1),(3,0.2,'2020-02-17 14:16:34',1),(4,0.3,'2020-02-17 14:16:35',1),(5,0.4,'2020-02-17 14:16:46',1),(6,0.5,'2020-02-17 14:16:47',1),(7,0.6,'2020-02-17 14:16:48',1),(8,0.7,'2020-02-17 14:16:49',1),(9,0.8,'2020-02-17 14:16:50',1),(10,0.9,'2020-02-17 14:16:51',1),(11,0,'2020-02-17 18:05:24',2),(12,0.2,'2020-02-17 18:05:25',2),(13,0.4,'2020-02-17 18:05:26',2),(14,0.6,'2020-02-17 18:05:27',2),(15,0.8,'2020-02-17 18:05:28',2),(16,1,'2020-02-17 18:05:29',2),(17,1.2,'2020-02-17 18:05:30',2),(18,1.4,'2020-02-17 18:05:31',2),(19,1.6,'2020-02-17 18:05:32',2),(20,1.8,'2020-02-17 18:05:33',2),(21,11.78,'2020-02-20 00:23:48',2),(22,61.97,'2020-05-13 16:26:07',9),(23,76.67,'2020-05-13 16:26:17',9),(24,11.96,'2020-05-13 16:26:19',9),(25,91.32,'2020-05-13 16:26:20',9),(26,71.42,'2020-05-13 16:26:22',9),(27,48.56,'2020-05-13 16:29:48',9),(28,31.27,'2020-05-13 16:29:49',9),(29,69.18,'2020-05-13 16:29:52',9),(30,39.23,'2020-05-13 16:29:54',9),(31,73.88,'2020-05-13 16:29:56',9),(32,65.97,'2020-05-13 16:57:12',9),(33,65.52,'2020-05-13 16:58:50',9),(40,-2.3,'2020-05-13 17:05:57',9),(41,-1.3,'2020-05-13 17:05:58',9),(42,-0.3,'2020-05-13 17:05:59',9),(43,0.699,'2020-05-13 17:06:00',9),(44,1.699,'2020-05-13 17:06:01',9),(45,64.29,'2020-05-13 21:45:38',9),(46,92.22,'2020-05-13 21:47:58',9),(47,42.12,'2020-05-13 21:48:06',9),(48,43.12,'2020-05-13 21:48:07',9),(49,44.12,'2020-05-13 21:48:08',9),(50,45.12,'2020-05-13 21:48:09',9),(51,46.12,'2020-05-13 21:48:10',9),(52,50.03,'2020-05-13 21:49:07',9),(53,51.03,'2020-05-13 21:49:08',9),(54,52.03,'2020-05-13 21:49:09',9),(55,53.03,'2020-05-13 21:49:10',9),(56,54.03,'2020-05-13 21:49:11',9),(58,13.74,'2020-05-13 21:52:54',9),(59,14.74,'2020-05-13 21:52:55',9),(60,15.74,'2020-05-13 21:52:56',9),(61,2,'2020-05-13 21:52:57',9),(62,1,'2020-05-13 21:52:58',9); 43 | /*!40000 ALTER TABLE `tb_data` ENABLE KEYS */; 44 | UNLOCK TABLES; 45 | 46 | -- 47 | -- Table structure for table `tb_gateway` 48 | -- 49 | 50 | DROP TABLE IF EXISTS `tb_gateway`; 51 | /*!40101 SET @saved_cs_client = @@character_set_client */; 52 | /*!40101 SET character_set_client = utf8 */; 53 | CREATE TABLE `tb_gateway` ( 54 | `id` int(11) NOT NULL AUTO_INCREMENT, 55 | `ip` varchar(32) COLLATE utf8_unicode_ci NOT NULL, 56 | `port` varchar(10) COLLATE utf8_unicode_ci NOT NULL, 57 | `description` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 58 | `location` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 59 | PRIMARY KEY (`id`) 60 | ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 61 | /*!40101 SET character_set_client = @saved_cs_client */; 62 | 63 | -- 64 | -- Dumping data for table `tb_gateway` 65 | -- 66 | 67 | LOCK TABLES `tb_gateway` WRITE; 68 | /*!40000 ALTER TABLE `tb_gateway` DISABLE KEYS */; 69 | INSERT INTO `tb_gateway` VALUES (1,'127.0.0.1','8080','我是网关1号','天马-1-1-101'),(4,'127.0.0.4','8484','haha,我是网关4号','天马-1-1-104'),(5,'127.0.0.5','80','haha,我是网关5号','天马-1-1-105'),(6,'127.0.0.6','666','haha,我是网关6号','天马-1-1-106'),(7,'127.0.0.7','8787','我是网关7号','天马-1-1-107'); 70 | /*!40000 ALTER TABLE `tb_gateway` ENABLE KEYS */; 71 | UNLOCK TABLES; 72 | 73 | -- 74 | -- Table structure for table `tb_gateway_exception` 75 | -- 76 | 77 | DROP TABLE IF EXISTS `tb_gateway_exception`; 78 | /*!40101 SET @saved_cs_client = @@character_set_client */; 79 | /*!40101 SET character_set_client = utf8 */; 80 | CREATE TABLE `tb_gateway_exception` ( 81 | `id` int(11) NOT NULL AUTO_INCREMENT, 82 | `description` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 83 | `time` datetime DEFAULT NULL, 84 | `gate_id` int(11) NOT NULL, 85 | PRIMARY KEY (`id`), 86 | KEY `fk_exception_gateway_1` (`gate_id`), 87 | CONSTRAINT `fk_exception_gateway_1` FOREIGN KEY (`gate_id`) REFERENCES `tb_gateway` (`id`) 88 | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 89 | /*!40101 SET character_set_client = @saved_cs_client */; 90 | 91 | -- 92 | -- Dumping data for table `tb_gateway_exception` 93 | -- 94 | 95 | LOCK TABLES `tb_gateway_exception` WRITE; 96 | /*!40000 ALTER TABLE `tb_gateway_exception` DISABLE KEYS */; 97 | INSERT INTO `tb_gateway_exception` VALUES (1,'数据异常','2020-02-19 15:05:30',7),(2,'网络异常','2020-02-19 15:06:30',7),(3,'连接异常','2020-02-19 15:07:30',7); 98 | /*!40000 ALTER TABLE `tb_gateway_exception` ENABLE KEYS */; 99 | UNLOCK TABLES; 100 | 101 | -- 102 | -- Table structure for table `tb_sensor` 103 | -- 104 | 105 | DROP TABLE IF EXISTS `tb_sensor`; 106 | /*!40101 SET @saved_cs_client = @@character_set_client */; 107 | /*!40101 SET character_set_client = utf8 */; 108 | CREATE TABLE `tb_sensor` ( 109 | `id` int(11) NOT NULL AUTO_INCREMENT, 110 | `description` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 111 | `location` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 112 | `factory` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL, 113 | `install_time` date DEFAULT NULL, 114 | `produce_date` date DEFAULT NULL, 115 | `maintenance_time` date DEFAULT NULL, 116 | `gate_id` int(11) NOT NULL, 117 | `classify_id` int(11) NOT NULL, 118 | PRIMARY KEY (`id`), 119 | KEY `fk_sensor_gate_1` (`gate_id`), 120 | KEY `fk_sensor_class_1` (`classify_id`), 121 | CONSTRAINT `fk_sensor_class_1` FOREIGN KEY (`classify_id`) REFERENCES `tb_sensor_classify` (`id`), 122 | CONSTRAINT `fk_sensor_gate_1` FOREIGN KEY (`gate_id`) REFERENCES `tb_gateway` (`id`) 123 | ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 124 | /*!40101 SET character_set_client = @saved_cs_client */; 125 | 126 | -- 127 | -- Dumping data for table `tb_sensor` 128 | -- 129 | 130 | LOCK TABLES `tb_sensor` WRITE; 131 | /*!40000 ALTER TABLE `tb_sensor` DISABLE KEYS */; 132 | INSERT INTO `tb_sensor` VALUES (1,'haha,我是温度传感器1号','天马-1-3-131','华美集团','2020-02-01','2020-01-01','2021-01-31',1,1),(2,'haha,我是温度传感器2号','天马-1-3-132','华美集团','2020-01-31','2019-12-31','2021-01-30',1,1),(3,'haha,我是湿度传感器1号','天马-1-3-133','xx集团','2020-01-28','2019-12-28','2021-01-27',5,2),(4,'haha,我是湿度传感器2号','天马-1-3-134','华美集团','2020-01-26','2019-12-26','2021-01-25',4,2),(6,'haha,我是温度传感器3号','天马-1-3-135','yy集团','2020-01-30','2019-12-30','2021-01-29',1,1),(7,'我是光照传感器1号','天马-1-1-107','mm集团','2020-01-01','2020-01-01','2020-02-02',1,3),(8,'haha,我是光照传感器2号','天马-2-1-201','东马集团','2020-02-01','2020-01-01','2020-08-01',5,3),(9,'红外传感器1号','天马-3-3-103','zz集团','2020-05-01','2020-05-03','2020-05-07',7,4); 133 | /*!40000 ALTER TABLE `tb_sensor` ENABLE KEYS */; 134 | UNLOCK TABLES; 135 | 136 | -- 137 | -- Table structure for table `tb_sensor_classify` 138 | -- 139 | 140 | DROP TABLE IF EXISTS `tb_sensor_classify`; 141 | /*!40101 SET @saved_cs_client = @@character_set_client */; 142 | /*!40101 SET character_set_client = utf8 */; 143 | CREATE TABLE `tb_sensor_classify` ( 144 | `id` int(11) NOT NULL AUTO_INCREMENT, 145 | `name` varchar(32) COLLATE utf8_unicode_ci NOT NULL, 146 | PRIMARY KEY (`id`) 147 | ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 148 | /*!40101 SET character_set_client = @saved_cs_client */; 149 | 150 | -- 151 | -- Dumping data for table `tb_sensor_classify` 152 | -- 153 | 154 | LOCK TABLES `tb_sensor_classify` WRITE; 155 | /*!40000 ALTER TABLE `tb_sensor_classify` DISABLE KEYS */; 156 | INSERT INTO `tb_sensor_classify` VALUES (1,'温度传感器'),(2,'湿度传感器'),(3,'光照传感器'),(4,'红外传感器'); 157 | /*!40000 ALTER TABLE `tb_sensor_classify` ENABLE KEYS */; 158 | UNLOCK TABLES; 159 | 160 | -- 161 | -- Table structure for table `tb_sensor_exception` 162 | -- 163 | 164 | DROP TABLE IF EXISTS `tb_sensor_exception`; 165 | /*!40101 SET @saved_cs_client = @@character_set_client */; 166 | /*!40101 SET character_set_client = utf8 */; 167 | CREATE TABLE `tb_sensor_exception` ( 168 | `id` int(11) NOT NULL AUTO_INCREMENT, 169 | `description` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL, 170 | `time` datetime DEFAULT NULL, 171 | `sensor_id` int(11) NOT NULL, 172 | PRIMARY KEY (`id`), 173 | KEY `fk_exception_sensor_1` (`sensor_id`), 174 | CONSTRAINT `fk_exception_sensor_1` FOREIGN KEY (`sensor_id`) REFERENCES `tb_sensor` (`id`) 175 | ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 176 | /*!40101 SET character_set_client = @saved_cs_client */; 177 | 178 | -- 179 | -- Dumping data for table `tb_sensor_exception` 180 | -- 181 | 182 | LOCK TABLES `tb_sensor_exception` WRITE; 183 | /*!40000 ALTER TABLE `tb_sensor_exception` DISABLE KEYS */; 184 | INSERT INTO `tb_sensor_exception` VALUES (1,'网络异常','2020-02-19 15:03:15',7); 185 | /*!40000 ALTER TABLE `tb_sensor_exception` ENABLE KEYS */; 186 | UNLOCK TABLES; 187 | 188 | -- 189 | -- Table structure for table `tb_user` 190 | -- 191 | 192 | DROP TABLE IF EXISTS `tb_user`; 193 | /*!40101 SET @saved_cs_client = @@character_set_client */; 194 | /*!40101 SET character_set_client = utf8 */; 195 | CREATE TABLE `tb_user` ( 196 | `id` int(11) NOT NULL AUTO_INCREMENT, 197 | `username` varchar(20) COLLATE utf8_unicode_ci NOT NULL, 198 | `nickname` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL, 199 | `password` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 200 | `tel` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, 201 | `email` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, 202 | PRIMARY KEY (`id`), 203 | UNIQUE KEY `username` (`username`) 204 | ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 205 | /*!40101 SET character_set_client = @saved_cs_client */; 206 | 207 | -- 208 | -- Dumping data for table `tb_user` 209 | -- 210 | 211 | LOCK TABLES `tb_user` WRITE; 212 | /*!40000 ALTER TABLE `tb_user` DISABLE KEYS */; 213 | INSERT INTO `tb_user` VALUES (1,'zhangsan','张三','01d7f40760960e7bd9443513f22ab9af','17788888877','xxxx@126.com'); 214 | /*!40000 ALTER TABLE `tb_user` ENABLE KEYS */; 215 | UNLOCK TABLES; 216 | /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; 217 | 218 | /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; 219 | /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; 220 | /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; 221 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 222 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 223 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 224 | /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; 225 | 226 | -- Dump completed on 2020-05-14 18:29:48 227 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/changeBasicInfomation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 修改基本信息 6 | 7 | 8 |

修改基本信息

9 |
10 | 昵称:
11 | 电话:
12 | 邮箱:
13 | 14 |
15 | 16 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/changePassword.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 修改密码 6 | 7 | 8 |

修改密码

9 |
10 | 用户名:
11 | 旧密码:
12 | 新密码:
13 | 14 |
15 | 16 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/data_post.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 模拟传感器数据提交 6 | 7 | 8 | 56 | 57 | 58 |
59 | 传感器id=
60 | 64 |
65 |
    66 |
  • 实时数值data=0(提交单个数据,时间为当前时间)
  • 67 |
  • 实时数值data=0 0 0 0 0(提交5个批量数据,时间为当前时间前5秒)
  • 68 |
69 |
70 |

data表单

71 |
72 |
73 | 返回消息: 74 |
75 | 76 | | status: | message: | 78 | 79 |
80 | 该api不提供返回数据 81 |
82 | 提交传感器data数据: 83 | 84 | 85 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/data_sensor.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 获取传感器数据 6 | 7 | 8 | 9 | 61 | 62 | 63 | 传感器id: 64 | 65 |
66 | 返回消息: 67 |
68 | | status: | message: | 70 |
71 | 72 |
73 |
74 | 75 |
76 | 77 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/download.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 下载页面 6 | 7 | 19 | 20 | 21 |

下载相关

22 |
23 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/exception.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 获取网关和传感器异常 6 | 7 | 79 | 80 | 81 |

Exception

82 |
86 | 页码:(不填写为不分页,默认一页两项) 87 | 88 |
89 | 根据时间戳查询: 90 | 从 91 | 92 |
93 |
94 | 返回消息: 95 |
96 | 97 | | status: | message: | 98 | 99 |
100 | 返回数据: 101 | 102 |
103 |
104 | 105 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/gateway.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 网关 6 | 7 | 108 | 109 | 110 |

gateway表单

111 |
112 | id:(添加时不必填写)
113 | ip:
114 | port:
115 | 描述:
116 | 位置:
117 |
118 |
119 | 返回消息: 120 |
121 | 122 | | status: | message: | 123 | 124 |
125 | 返回数据: 126 | 127 |
128 |
129 | 根据id查询网关: 130 | 131 | (不填写id表示查询所有)
132 | 新添加一个网关: 133 | 134 | (成功后回显信息)
135 | 根据id更新网关: 136 | 137 | (成功后回显信息)
138 | 根据id删除网关: 139 | 140 | (成功后清空输入框)
141 | 142 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 登录 6 | 7 | 8 |

用户登录

9 |
10 | 用户名:
11 | 密码:
12 | 13 |
14 | 15 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/network.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 传感网络(网关分类) 6 | 7 | 8 | 93 | 94 | 95 |

传感网络

96 |
97 |
98 | 99 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/regist.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 注册 6 | 7 | 8 |

用户注册

9 |
10 | 用户名:
11 | 昵称:
12 | 密码:
13 | 电话:
14 | 邮箱:
15 | 16 |
17 | 18 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/sensor.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 传感器 6 | 7 | 120 | 121 | 122 |

sensor表单

123 |
124 | id:(添加时不必填写)
125 | description:
126 | location:
127 | factory:
128 | install_time:
129 | produce_date:
130 | maintenance_time:
131 | gate_id:
132 | classify_id:
133 |
134 |
135 | 返回消息: 136 |
137 | 138 | | status: | message: | 139 | 140 |
141 | 返回数据: 142 | 143 |
144 |
145 | 根据id查询传感器: 146 | 147 | (不填写id表示查询所有)
148 | 新添加一个传感器: 149 | 150 | (成功后回显信息)
151 | 根据id更新传感器: 152 | 153 | (成功后回显信息)
154 | 根据id删除传感器: 155 | 156 | (成功后清空输入框)
157 | 158 | -------------------------------------------------------------------------------- /src/main/resources/static-old/html/sensor_classify.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 传感器分类 6 | 7 | 65 | 66 | 67 |

SensorClassify表单

68 |
69 | id:(添加时不必填写)
70 | name:
71 |
72 |
73 | 返回消息: 74 |
75 | 76 | | status: | message: | 77 | 78 |
79 | 返回数据: 80 | 81 |
82 |
83 | 根据id查询分类: 84 | 85 | (不填写id表示查询所有)
86 | 新添加一个分类: 87 | 88 | (成功后回显信息)
89 | 90 | -------------------------------------------------------------------------------- /src/main/resources/static-old/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 首页 6 | 7 | 8 |
9 |

IOT 数据采集和管理系统 SpringBoot版

10 |
11 |

测试API

12 | 16 | 17 |
18 |

user相关

19 | 27 |
28 |

Gateway相关

29 | 32 |
33 |

SensorClassify相关

34 | 37 |
38 |

Sensor相关

39 | 42 |
43 |

Data相关

44 | 48 |
49 |

Exception相关

50 | 53 |
54 |

传感网络

55 | 58 |
59 |

下载相关

60 | 63 |
64 | 65 | -------------------------------------------------------------------------------- /src/main/resources/static-old/js/dateformat.js: -------------------------------------------------------------------------------- 1 | // 对Date的扩展,将 Date 转化为指定格式的String 2 | // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 3 | // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 4 | // 例子: 5 | // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 6 | // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 7 | Date.prototype.Format = function (fmt) { //author: meizz 8 | var o = { 9 | "M+": this.getMonth() + 1, //月份 10 | "d+": this.getDate(), //日 11 | "h+": this.getHours(), //小时 12 | "m+": this.getMinutes(), //分 13 | "s+": this.getSeconds(), //秒 14 | "q+": Math.floor((this.getMonth() + 3) / 3), //季度 15 | "S": this.getMilliseconds() //毫秒 16 | }; 17 | if (/(y+)/.test(fmt)) 18 | fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 19 | for (var k in o) 20 | if (new RegExp("(" + k + ")").test(fmt)) 21 | fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); 22 | return fmt; 23 | } -------------------------------------------------------------------------------- /src/main/resources/static/html/basicInformation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 个人信息 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 105 | 106 | 107 |
108 | 109 | 110 | 120 | 首页 121 | 自定义 122 | 消息中心 123 | 订单管理 124 | 125 | 126 | 个人信息 127 | 128 | 修改密码 129 | 130 | 退出登录 131 | 132 | 133 | 134 | 135 | 136 | 137 | 141 | 142 | 143 |
144 | 146 |
147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 修改个人信息 162 | 提交 163 | 164 | 165 |
166 | 167 |
168 | © 2019-2020 . Powered by CUG | 169 | 服务器状态 170 | | 171 | 测试API 172 | | 173 | 注册新用户 174 | 175 |
176 |
177 | 265 | 266 | -------------------------------------------------------------------------------- /src/main/resources/static/html/changePassword.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 修改密码 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 105 | 106 | 107 |
108 | 109 | 110 | 120 | 首页 121 | 自定义 122 | 消息中心 123 | 订单管理 124 | 125 | 126 | 个人信息 127 | 128 | 修改密码 129 | 130 | 退出登录 131 | 132 | 133 | 134 | 135 | 136 | 137 | 141 | 142 | 143 |
144 | 146 |
147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 提交 166 | 重置 167 | 168 | 169 |
170 | 171 |
172 | © 2019-2020 . Powered by CUG | 173 | 服务器状态 174 | | 175 | 测试API 176 | 177 |
178 |
179 | 277 | 278 | -------------------------------------------------------------------------------- /src/main/resources/static/html/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 登录 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 95 | 96 | 97 |
98 | 99 | 100 | 110 | 首页 111 | 自定义 112 | 消息中心 113 | 订单管理 114 | 115 | 116 | 个人信息 117 | 修改密码 118 | 退出登录 119 | 120 | 121 | 122 | 123 | 124 | 125 |
126 | 127 |
128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 登录 138 | 重置 139 | 140 | 141 |
142 | 143 |
144 | © 2019-2020 . Powered by CUG | 145 | 服务器状态 146 | | 147 | 测试API 148 | 149 |
150 |
151 | 237 | 238 | -------------------------------------------------------------------------------- /src/main/resources/static/html/network.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 传感网络 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 106 | 107 | 108 |
109 | 110 | 111 | 121 | 首页 122 | 自定义 123 | 消息中心 124 | 订单管理 125 | 126 | 127 | 个人信息 128 | 129 | 修改密码 130 | 131 | 退出登录 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 |
141 |
142 | 143 |
144 |
145 |
146 |
147 | 148 |
149 | © 2019-2020 . Powered by CUG | 150 | 服务器状态 151 | | 152 | 测试API 153 | 154 |
155 |
156 | 294 | 295 | -------------------------------------------------------------------------------- /src/main/resources/static/html/regist.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 注册 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 105 | 106 | 107 |
108 | 109 | 110 | 120 | 首页 121 | 自定义 122 | 消息中心 123 | 订单管理 124 | 125 | 126 | 个人信息 127 | 修改密码 128 | 退出登录 129 | 130 | 131 | 132 | 133 | 134 | 135 | 139 | 140 | 141 |
142 | 144 |
145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 注册 167 | 重置 168 | 169 | 170 |
171 | 172 |
173 | © 2019-2020 . Powered by CUG | 174 | 服务器状态 175 | | 176 | 测试API 177 | 178 |
179 |
180 | 283 | 284 | -------------------------------------------------------------------------------- /src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 首页 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 98 | 99 | 100 |
101 | 102 | 103 | 112 | 首页 113 | 自定义 114 | 消息中心 115 | 订单管理 116 | 117 | 118 | 个人信息 119 | 修改密码 120 | 退出登录 121 | 122 | 123 | 124 | 125 | 126 |
127 | 128 | 129 | 130 | 131 | 132 |
133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 |
142 | 144 | 传感网络 145 | 146 |
147 |
148 | 149 |
151 | 153 | 网关 154 | 155 |
156 |
157 |
158 | 159 | 160 |
162 | 164 | 传感器 165 | 166 |
167 |
168 | 169 |
171 | 173 | 数据提交 174 | 175 |
176 |
177 | 178 |
180 | 182 | 数据 183 | 184 |
185 |
186 |
187 | 188 | 189 |
191 | 193 | 异常 194 | 195 |
196 |
197 | 198 |
200 | 202 | 下载 203 | 204 |
205 |
206 |
207 |
208 | 209 |
210 |
211 |
212 | © 2019-2020 . Powered by CUG | 213 | 服务器状态 214 | | 215 | 测试API 216 | 217 |
218 |
219 | 258 | 259 | -------------------------------------------------------------------------------- /src/main/resources/static/js/dateformat.js: -------------------------------------------------------------------------------- 1 | // 对Date的扩展,将 Date 转化为指定格式的String 2 | // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 3 | // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 4 | // 例子: 5 | // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 6 | // (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 7 | Date.prototype.Format = function (fmt) { //author: meizz 8 | var o = { 9 | "M+": this.getMonth() + 1, //月份 10 | "d+": this.getDate(), //日 11 | "h+": this.getHours(), //小时 12 | "m+": this.getMinutes(), //分 13 | "s+": this.getSeconds(), //秒 14 | "q+": Math.floor((this.getMonth() + 3) / 3), //季度 15 | "S": this.getMilliseconds() //毫秒 16 | }; 17 | if (/(y+)/.test(fmt)) 18 | fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 19 | for (var k in o) 20 | if (new RegExp("(" + k + ")").test(fmt)) 21 | fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); 22 | return fmt; 23 | } -------------------------------------------------------------------------------- /src/test/java/com/my/iot/BaseTest.java: -------------------------------------------------------------------------------- 1 | package com.my.iot; 2 | 3 | import org.junit.runner.RunWith; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | import org.springframework.test.context.junit4.SpringRunner; 6 | 7 | @RunWith(SpringRunner.class) 8 | @SpringBootTest(classes = IoTSpringBootApplication.class) 9 | public class BaseTest { 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/com/my/iot/log/LoggerTest.java: -------------------------------------------------------------------------------- 1 | package com.my.iot.log; 2 | 3 | import com.my.iot.BaseTest; 4 | import com.my.iot.IoTSpringBootApplication; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | public class LoggerTest extends BaseTest { 12 | 13 | Logger logger = LoggerFactory.getLogger(LoggerTest.class); 14 | @Test 15 | public void test1(){ 16 | logger.debug("调试信息");//这句不会显示 17 | System.out.println("日志信息"); 18 | logger.warn("警告信息"); 19 | } 20 | } 21 | --------------------------------------------------------------------------------