commit 0058180cc3dd463e4cb2e377b69103fd7668ab64
Author: mengff <1198151809@qq.com>
Date: Fri Jan 5 09:37:38 2024 +0800
----定时设备扫描状态
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/active-tab-highlighter.xml b/.idea/active-tab-highlighter.xml
new file mode 100644
index 0000000..409aba9
--- /dev/null
+++ b/.idea/active-tab-highlighter.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..9e89714
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..63e9001
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..712ab9d
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..ea0b3a9
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f39e73d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# device-status
+
+## **用于济菏高速设备定时获取其状态信息**
diff --git a/device-status.iml b/device-status.iml
new file mode 100644
index 0000000..2f3f33e
--- /dev/null
+++ b/device-status.iml
@@ -0,0 +1,103 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/device.sql b/device.sql
new file mode 100644
index 0000000..17ba5fb
--- /dev/null
+++ b/device.sql
@@ -0,0 +1,61 @@
+/*
+ Navicat Premium Data Transfer
+
+ Source Server : mysql8
+ Source Server Type : MySQL
+ Source Server Version : 80027
+ Source Host : 39.106.31.193:3307
+ Source Schema : device
+
+ Target Server Type : MySQL
+ Target Server Version : 80027
+ File Encoding : 65001
+
+ Date: 05/01/2024 09:34:02
+*/
+
+SET NAMES utf8mb4;
+SET FOREIGN_KEY_CHECKS = 0;
+
+-- ----------------------------
+-- Table structure for device
+-- ----------------------------
+DROP TABLE IF EXISTS `device`;
+CREATE TABLE `device` (
+ `id` bigint NOT NULL AUTO_INCREMENT COMMENT '设备ID',
+ `device_no` varchar(30) COLLATE utf8_bin DEFAULT NULL COMMENT '设备编号',
+ `device_name` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '设备名称',
+ `device_ip` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '设备IP',
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;
+
+-- ----------------------------
+-- Records of device
+-- ----------------------------
+BEGIN;
+INSERT INTO `device` VALUES (1, '1001', '千里眼', '192.168.1');
+INSERT INTO `device` VALUES (2, '1002', '智慧眼', '192.168.2');
+INSERT INTO `device` VALUES (3, '1003', '摄像机', '192.168.3');
+INSERT INTO `device` VALUES (4, '1004', '随身行', '10.168.77.88');
+COMMIT;
+
+-- ----------------------------
+-- Table structure for status
+-- ----------------------------
+DROP TABLE IF EXISTS `status`;
+CREATE TABLE `status` (
+ `id` bigint NOT NULL AUTO_INCREMENT COMMENT '设备状态ID',
+ `device_no` varchar(30) COLLATE utf8_bin DEFAULT NULL COMMENT '设备编号',
+ `device_name` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '设备名称',
+ `device_status` int DEFAULT NULL COMMENT '设备状态',
+ `time` datetime DEFAULT NULL COMMENT '插入时间',
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;
+
+-- ----------------------------
+-- Records of status
+-- ----------------------------
+BEGIN;
+COMMIT;
+
+SET FOREIGN_KEY_CHECKS = 1;
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..09193c0
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,63 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.5.RELEASE
+
+
+ com.example
+ device-status
+ 0.0.1-SNAPSHOT
+ demo
+ Demo project for Spring Boot
+
+
+ 1.8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-jdbc
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.mybatis.spring.boot
+ mybatis-spring-boot-starter
+ 2.1.1
+
+
+
+ mysql
+ mysql-connector-java
+ runtime
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/src/main/java/com/example/device/DeviceApplication.java b/src/main/java/com/example/device/DeviceApplication.java
new file mode 100644
index 0000000..2bd64a4
--- /dev/null
+++ b/src/main/java/com/example/device/DeviceApplication.java
@@ -0,0 +1,22 @@
+package com.example.device;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
+/**
+ * @author mengff
+ * @Date 2024/01/04
+ */
+
+@EnableScheduling
+@MapperScan("com.example.device.mapper") //扫描的mapper
+@SpringBootApplication
+public class DeviceApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DeviceApplication.class, args);
+ }
+
+}
diff --git a/src/main/java/com/example/device/controller/DeviceStatus.java b/src/main/java/com/example/device/controller/DeviceStatus.java
new file mode 100644
index 0000000..855f3df
--- /dev/null
+++ b/src/main/java/com/example/device/controller/DeviceStatus.java
@@ -0,0 +1,55 @@
+package com.example.device.controller;
+import com.example.device.entity.Device;
+import com.example.device.entity.Status;
+import com.example.device.service.DeviceService;
+import com.example.device.service.StatusService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.time.LocalDateTime;
+import java.util.List;
+
+@Component
+@EnableScheduling
+public class DeviceStatus {
+ @Autowired
+ private DeviceService deviceService;
+
+ @Autowired
+ private StatusService statusService;
+
+
+ //每天凌晨开始执行,每4小时执行一次
+ @Scheduled(cron = "0 0 4/0 * * ?")
+ public void generateDeviceStatus(){
+ deviceStatus();
+ }
+ public void deviceStatus() {
+ Status status=new Status();
+ List deviceList = deviceService.SelectList();
+ for (Device device : deviceList) {
+ try {
+ InetAddress address = InetAddress.getByName(device.getDeviceIp());
+ boolean reachable = address.isReachable(5000); // Timeout: 5 seconds
+ status.setDeviceNo(device.getDeviceNo());
+ status.setDeviceName(device.getDeviceName());
+ LocalDateTime localDateTime = LocalDateTime.now();
+ status.setTime(localDateTime);
+ //1-在线 0-离线
+ if (reachable) {
+ status.setDeviceStatus(1);
+ } else {
+ status.setDeviceStatus(0);
+ }
+ statusService.Add(status);
+ } catch (IOException e) {
+ System.out.println("Error pinging " + device.getDeviceIp() + ": " + e.getMessage());
+ }
+ }
+ }
+
+}
+
diff --git a/src/main/java/com/example/device/entity/Device.java b/src/main/java/com/example/device/entity/Device.java
new file mode 100644
index 0000000..f07cd01
--- /dev/null
+++ b/src/main/java/com/example/device/entity/Device.java
@@ -0,0 +1,55 @@
+package com.example.device.entity;
+
+/**
+ * @author mengff
+ * @Date 2020/03/03
+ */
+public class Device {
+ private long id;
+
+ public String getDeviceNo() {
+ return deviceNo;
+ }
+
+ public void setDeviceNo(String deviceNo) {
+ this.deviceNo = deviceNo;
+ }
+
+ public String getDeviceName() {
+ return deviceName;
+ }
+
+ public void setDeviceName(String deviceName) {
+ this.deviceName = deviceName;
+ }
+
+ public String getDeviceIp() {
+ return deviceIp;
+ }
+
+ @Override
+ public String toString() {
+ return "Device{" +
+ "id=" + id +
+ ", deviceNo='" + deviceNo + '\'' +
+ ", deviceName='" + deviceName + '\'' +
+ ", deviceIp='" + deviceIp + '\'' +
+ '}';
+ }
+
+ public Device(long id, String deviceNo, String deviceName, String deviceIp) {
+ this.id = id;
+ this.deviceNo = deviceNo;
+ this.deviceName = deviceName;
+ this.deviceIp = deviceIp;
+ }
+
+ public void setDeviceIp(String deviceIp) {
+ this.deviceIp = deviceIp;
+ }
+
+ private String deviceNo;
+ private String deviceName;
+ private String deviceIp;
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/device/entity/Status.java b/src/main/java/com/example/device/entity/Status.java
new file mode 100644
index 0000000..93e275d
--- /dev/null
+++ b/src/main/java/com/example/device/entity/Status.java
@@ -0,0 +1,74 @@
+package com.example.device.entity;
+
+import java.time.LocalDateTime;
+
+/**
+ * @author mengff
+ * @Date 2024/01/04
+ */
+public class Status {
+ public LocalDateTime getTime() {
+ return time;
+ }
+
+ public void setTime(LocalDateTime time) {
+ this.time = time;
+ }
+
+ public String getDeviceNo() {
+ return deviceNo;
+ }
+
+ public Status(long id, String deviceNo, String deviceName, int deviceStatus) {
+ this.id = id;
+ this.deviceNo = deviceNo;
+ this.deviceName = deviceName;
+ this.deviceStatus = deviceStatus;
+ }
+
+ public Status() {
+ }
+
+ public void setDeviceNo(String deviceNo) {
+ this.deviceNo = deviceNo;
+ }
+
+ public String getDeviceName() {
+ return deviceName;
+ }
+
+ public void setDeviceName(String deviceName) {
+ this.deviceName = deviceName;
+ }
+
+ public int getDeviceStatus() {
+ return deviceStatus;
+ }
+
+ public void setDeviceStatus(int deviceStatus) {
+ this.deviceStatus = deviceStatus;
+ }
+
+ private long id;
+
+ private String deviceNo;
+
+ @Override
+ public String toString() {
+ return "Status{" +
+ "id=" + id +
+ ", deviceNo='" + deviceNo + '\'' +
+ ", deviceName='" + deviceName + '\'' +
+ ", deviceStatus=" + deviceStatus +
+ ", time=" + time +
+ '}';
+ }
+
+
+
+ private String deviceName;
+ private int deviceStatus;
+
+ private LocalDateTime time;
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/device/mapper/DeviceMapper.java b/src/main/java/com/example/device/mapper/DeviceMapper.java
new file mode 100644
index 0000000..5acb6a3
--- /dev/null
+++ b/src/main/java/com/example/device/mapper/DeviceMapper.java
@@ -0,0 +1,17 @@
+package com.example.device.mapper;
+
+import com.example.device.entity.Device;
+import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+/**
+ * @author mengff
+ * @Date 2024/01/04
+ */
+@Repository
+public interface DeviceMapper {
+
+ List SelectList();
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/device/mapper/StatusMapper.java b/src/main/java/com/example/device/mapper/StatusMapper.java
new file mode 100644
index 0000000..c81c662
--- /dev/null
+++ b/src/main/java/com/example/device/mapper/StatusMapper.java
@@ -0,0 +1,18 @@
+package com.example.device.mapper;
+
+import com.example.device.entity.Status;
+import org.apache.ibatis.annotations.Param;
+import org.springframework.stereotype.Repository;
+
+/**
+ * @author mengff
+ * @Date 2024/01/04
+ */
+@Repository
+public interface StatusMapper {
+
+
+
+ int Add(@Param("status")Status status);
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/device/service/DeviceService.java b/src/main/java/com/example/device/service/DeviceService.java
new file mode 100644
index 0000000..11b4650
--- /dev/null
+++ b/src/main/java/com/example/device/service/DeviceService.java
@@ -0,0 +1,23 @@
+package com.example.device.service;
+
+import com.example.device.entity.Device;
+import com.example.device.mapper.DeviceMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @author mengff
+ * @Date 2024/01/04
+ */
+@Service
+public class DeviceService {
+ @Autowired
+ DeviceMapper deviceMapper;
+
+ public List SelectList() {
+ return deviceMapper.SelectList();
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/device/service/StatusService.java b/src/main/java/com/example/device/service/StatusService.java
new file mode 100644
index 0000000..daae780
--- /dev/null
+++ b/src/main/java/com/example/device/service/StatusService.java
@@ -0,0 +1,28 @@
+package com.example.device.service;
+
+import com.example.device.entity.Status;
+import com.example.device.mapper.StatusMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author mengff
+ * @Date 2024/01/04
+ */
+@Service
+public class StatusService {
+ @Autowired
+ StatusMapper statusMapper;
+
+
+ public String Add(Status status) {
+ int a = statusMapper.Add(status);
+ if (a == 1) {
+ return "添加成功";
+ } else {
+ return "添加失败";
+ }
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
new file mode 100644
index 0000000..9bc7660
--- /dev/null
+++ b/src/main/resources/application.yml
@@ -0,0 +1,19 @@
+server:
+ port: 8089
+
+spring:
+ datasource:
+ username: root
+ password: root
+ url: jdbc:mysql://39.106.31.193:3307/device?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
+ driver-class-name: com.mysql.cj.jdbc.Driver
+
+mybatis:
+ mapper-locations: classpath:mapping/*Mapper.xml
+ type-aliases-package: com.example.entity.demo
+
+logging:
+ level:
+ com:
+ example:
+ mapper : debug
\ No newline at end of file
diff --git a/src/main/resources/mapping/DeviceMapper.xml b/src/main/resources/mapping/DeviceMapper.xml
new file mode 100644
index 0000000..0421ef8
--- /dev/null
+++ b/src/main/resources/mapping/DeviceMapper.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/resources/mapping/StatusMapper.xml b/src/main/resources/mapping/StatusMapper.xml
new file mode 100644
index 0000000..eee43fc
--- /dev/null
+++ b/src/main/resources/mapping/StatusMapper.xml
@@ -0,0 +1,46 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ INSERT INTO status
+
+
+ device_no,
+
+
+ device_name,
+
+
+ device_status,
+
+
+ time,
+
+
+
+
+ #{status.deviceNo,jdbcType=VARCHAR},
+
+
+ #{status.deviceName,jdbcType=VARCHAR},
+
+
+ #{status.deviceStatus,jdbcType=INTEGER},
+
+
+ #{status.time,jdbcType=DATE},
+
+
+
+
+
\ No newline at end of file