Browse Source

物联网平台LoRa数据接收分发模块

pengdi@zoniot.com 4 years ago
parent
commit
f6308f3f59

+ 8 - 0
smart-city-dispatcher/README.md

@@ -0,0 +1,8 @@
+README
+===========================
+智慧城市数据分发模块,主要接收NS的数据后进入到消息队列进行消费
+- UAT环境http://129.204.175.72:8081/data/api/hlwx/
+- SIT环境http://129.204.175.72:8091/data/api/hlwx/
+- DEV环境http://129.204.175.72:8071/data/api/hlwx/
+- 生产环境http://47.112.16.203:8091/data/api/hlwx/
+- UAT设备状态接收接口 http://129.204.175.72:8081/data/api/status/

+ 122 - 0
smart-city-dispatcher/pom.xml

@@ -0,0 +1,122 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<groupId>com.zcxk.smartcity</groupId>
+	<artifactId>smart-city-dispatcher</artifactId>
+	<version>1.0</version>
+	<packaging>jar</packaging>
+
+	<name>smart-city-dispatcher</name>
+	<description>中城信科智慧城市数据分发平台</description>
+
+	<parent>
+		<groupId>org.springframework.boot</groupId>
+		<artifactId>spring-boot-starter-parent</artifactId>
+		<version>2.1.0.RELEASE</version>
+	</parent>
+
+	<properties>
+		<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+		<java.version>1.8</java.version>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-data-rest</artifactId>
+		</dependency>
+
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-test</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.projectlombok</groupId>
+			<artifactId>lombok</artifactId>
+		</dependency>
+		<!-- 构建Restful API -->
+		<dependency>
+			<groupId>io.springfox</groupId>
+			<artifactId>springfox-swagger2</artifactId>
+			<version>2.7.0</version>
+		</dependency>
+		<dependency>
+			<groupId>io.springfox</groupId>
+			<artifactId>springfox-swagger-ui</artifactId>
+			<version>2.7.0</version>
+		</dependency>
+		<!-- 
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-data-mongodb</artifactId>
+		</dependency>
+		 -->
+		<dependency>
+            <groupId>org.springframework.kafka</groupId>
+            <artifactId>spring-kafka</artifactId>
+        </dependency>
+	</dependencies>
+
+	<build>
+		<finalName>
+		  ${project.artifactId}-${project.version}_${maven.build.timestamp}
+		</finalName>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+			</plugin>
+
+			<!-- 1、设置jar的入口类 -->
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-jar-plugin</artifactId>
+				<configuration>
+					<archive>
+						<manifest>
+							<addClasspath>true</addClasspath>
+							<classpathPrefix>lib/</classpathPrefix>
+							<mainClass>com.zcxk.smartcity.data.dispatcher.Application</mainClass>
+						</manifest>
+					</archive>
+				</configuration>
+			</plugin>
+
+
+			<!--2、把附属的jar打到jar内部的lib目录中 -->
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-dependency-plugin</artifactId>
+				<executions>
+					<execution>
+						<id>copy-dependencies</id>
+						<phase>package</phase>
+						<goals>
+							<goal>copy-dependencies</goal>
+						</goals>
+						<configuration>
+							<outputDirectory>${project.build.directory}/lib</outputDirectory>
+						</configuration>
+					</execution>
+				</executions>
+			</plugin>
+
+
+			<!-- 3、打包过程忽略Junit测试 -->
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-surefire-plugin</artifactId>
+				<configuration>
+					<skip>true</skip>
+				</configuration>
+			</plugin>
+
+		</plugins>
+	</build>
+</project>

+ 12 - 0
smart-city-dispatcher/src/main/java/com/zcxk/smartcity/data/dispatcher/Application.java

@@ -0,0 +1,12 @@
+package com.zcxk.smartcity.data.dispatcher;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+
+	public static void main(String[] args) {
+		SpringApplication.run(Application.class, args);
+	}
+}

+ 27 - 0
smart-city-dispatcher/src/main/java/com/zcxk/smartcity/data/dispatcher/api/SwaggerConfig.java

@@ -0,0 +1,27 @@
+package com.zcxk.smartcity.data.dispatcher.api;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import springfox.documentation.builders.ApiInfoBuilder;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.service.Contact;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.swagger2.annotations.EnableSwagger2;
+@Configuration
+@EnableSwagger2
+public class SwaggerConfig {
+	@Bean
+	public Docket userApi() {
+		return new Docket(DocumentationType.SWAGGER_2).groupName("数据接入管理").apiInfo(apiInfo()).select()
+				.apis(RequestHandlerSelectors.basePackage("com.zcxk.smartcity.data.dispatcher.web")).paths(PathSelectors.any()).build();
+	}
+	// 预览地址:swagger-ui.html
+	private ApiInfo apiInfo() {
+		return new ApiInfoBuilder().title("Spring 中使用Swagger2构建文档").termsOfServiceUrl("http://39.108.172.131")
+				.contact(new Contact("中城信科物联云平台 ", "http://39.108.172.131", "506665698@qq.com")).version("1.1").build();
+	}
+}

+ 24 - 0
smart-city-dispatcher/src/main/java/com/zcxk/smartcity/data/dispatcher/vo/CommonResponseVO.java

@@ -0,0 +1,24 @@
+package com.zcxk.smartcity.data.dispatcher.vo;
+
+/**
+ * 
+ * @author wilian.peng
+ *
+ */
+public class CommonResponseVO {
+	private Integer code = 0;
+	private String msg = "success" ;
+	public Integer getCode() {
+		return code;
+	}
+	public void setCode(Integer code) {
+		this.code = code;
+	}
+	public String getMsg() {
+		return msg;
+	}
+	public void setMsg(String msg) {
+		this.msg = msg;
+	}
+	
+}

+ 71 - 0
smart-city-dispatcher/src/main/java/com/zcxk/smartcity/data/dispatcher/web/DataAccessaController.java

@@ -0,0 +1,71 @@
+package com.zcxk.smartcity.data.dispatcher.web;
+
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.JsonNodeType;
+import com.zcxk.smartcity.data.dispatcher.vo.CommonResponseVO;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+
+@Slf4j
+@Api(tags = "物联云数据接入管理")
+@RestController
+@RequestMapping("/data/api/")
+public class DataAccessaController {
+	@Autowired
+	private  KafkaTemplate<String, Object> kafkaTemplate;
+	
+	@Value("${com.zcxk.kafka.single.data.topic}")
+	private String singleDataTopic; // 单条数据接收主题
+	
+	@Value("${com.zcxk.kafka.multi.data.topic}")
+	private String multiDataTopic; // 多条数据接收主题
+	
+	
+	
+    /**
+         * 收到数据后直接入消息队列
+     * @param org
+     * @param json
+     * @return
+     */
+    @ApiOperation(value = "接收数据")
+    @PostMapping("/{org}/")
+    public CommonResponseVO receiveData(@PathVariable String org, @RequestBody String json) {
+        if (log.isTraceEnabled()) {
+            log.trace("数据提供方:" + org + ",数据内容:" + json);
+        }
+        log.info("----------------------------revice msg," + json);
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode root = null;
+        try {
+            root = mapper.readTree(json);
+            if(root.getNodeType() == JsonNodeType.OBJECT){
+                //是对象发送到redis设备数据消息队列
+            	log.info("sending message to {} ",singleDataTopic);
+            	kafkaTemplate.send(singleDataTopic,json);
+            	log.info("sending message to {} over",singleDataTopic);
+            }else if(root.getNodeType() == JsonNodeType.ARRAY){
+                //是数组发送到redis设备状态消息队列,水务优化版本中取消此接口数据 add by pengdi
+                // publisherService.sendMsg(CommonConstant.DEVICE_STATUS_CHANNEL, json);
+            }
+        } catch (Exception e) {
+        	log.error("Parse JSON String Error,json = {}",json);
+        }
+        CommonResponseVO rsp = new CommonResponseVO();
+        return rsp;
+    }
+}

+ 14 - 0
smart-city-dispatcher/src/main/resources/application-dev.properties

@@ -0,0 +1,14 @@
+server.port=8071
+
+#日志配置
+logging.level.root=info
+logging.path=c:/tmp
+#kafka配置
+spring.kafka.bootstrap-servers=193.112.139.7:9092
+#kafka生产者配置
+spring.kafka.producer.retries=0
+spring.kafka.producer.batch-size=4096
+spring.kafka.producer.buffer-memory=40960
+# kafka topic
+com.zcxk.kafka.single.data.topic=sc_single_data_topic_dev
+com.zcxk.kafka.multi.data.topic=sc_multi_data_topic_dev

+ 19 - 0
smart-city-dispatcher/src/main/resources/application-prd.properties

@@ -0,0 +1,19 @@
+server.port=8091
+
+#log
+logging.level.root=info
+logging.path=/mnt/prd/dispatcher/logs
+
+spring.jackson.time-zone=GMT+8
+spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
+spring.jackson.joda-date-time-format: yyyy-MM-dd HH:mm:ss
+
+#kafka 
+spring.kafka.bootstrap-servers=172.18.110.178:9092
+#kafka producer config
+spring.kafka.producer.retries=0
+spring.kafka.producer.batch-size=4096
+spring.kafka.producer.buffer-memory=40960
+#kafka topic
+com.zcxk.kafka.single.data.topic=sc_single_data_topic
+com.zcxk.kafka.multi.data.topic=sc_multi_data_topic

+ 15 - 0
smart-city-dispatcher/src/main/resources/application-sit.properties

@@ -0,0 +1,15 @@
+server.port=8091
+#log
+logging.level.root=info
+logging.path=/opt/sit/dispatcher/logs
+
+
+
+#kafka 
+spring.kafka.bootstrap-servers=193.112.139.7:9092
+#kafka producer
+spring.kafka.producer.retries=0
+spring.kafka.producer.batch-size=4096
+spring.kafka.producer.buffer-memory=40960
+com.zcxk.kafka.single.data.topic=sc_single_data_topic
+com.zcxk.kafka.multi.data.topic=sc_multi_data_topic

+ 14 - 0
smart-city-dispatcher/src/main/resources/application-test.properties

@@ -0,0 +1,14 @@
+server.port=8091
+
+#日志配置
+logging.level.root=info
+logging.path=/opt/test/dispatcher/logs
+#kafka配置
+spring.kafka.bootstrap-servers=114.135.61.188:909
+#kafka生产者配置
+spring.kafka.producer.retries=0
+spring.kafka.producer.batch-size=4096
+spring.kafka.producer.buffer-memory=40960
+# kafka topic
+com.zcxk.kafka.single.data.topic=sc_single_data_topic_test
+com.zcxk.kafka.multi.data.topic=sc_multi_data_topic_test

+ 13 - 0
smart-city-dispatcher/src/main/resources/application-uat.properties

@@ -0,0 +1,13 @@
+server.port=8081
+#log
+logging.level.root=info
+logging.path=/data/uat/dispatcher/logs
+#kafka
+spring.kafka.bootstrap-servers=193.112.139.7:9092
+#kafka producer
+spring.kafka.producer.retries=0
+spring.kafka.producer.batch-size=4096
+spring.kafka.producer.buffer-memory=40960
+#kafka topic
+com.zcxk.kafka.single.data.topic=sc_single_data_topic
+com.zcxk.kafka.multi.data.topic=sc_multi_data_topic

+ 2 - 0
smart-city-dispatcher/src/main/resources/application.properties

@@ -0,0 +1,2 @@
+#开发环境:dev  测试环境:sit  线上环境:prd
+spring.profiles.active=dev

+ 23 - 0
smart-city-dispatcher/src/main/resources/logback-spring.xml

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+    <springProperty scope="context" name="LOG_PATH" source="logging.path" defaultValue="/tmp" />
+    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
+    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
+    <appender name="TIME_FILE"
+              class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <encoder>
+            <pattern>${FILE_LOG_PATTERN}</pattern>
+        </encoder>
+        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
+            <fileNamePattern>${LOG_PATH}/smart_city_dispatcher.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
+            <maxHistory>365</maxHistory>
+            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
+                <maxFileSize>100MB</maxFileSize>
+            </timeBasedFileNamingAndTriggeringPolicy>
+        </rollingPolicy>
+    </appender>
+    <root level="INFO">
+        <appender-ref ref="CONSOLE" />
+        <appender-ref ref="TIME_FILE" />
+    </root>
+</configuration>

+ 15 - 0
smart-city-dispatcher/src/main/resources/sql/DeviceDateBase.sql

@@ -0,0 +1,15 @@
+-- auto Generated on 2018-12-21
+-- DROP TABLE IF EXISTS sc_device_date_base;
+CREATE TABLE sc_device_date_base(
+	id BIGINT (20) NOT NULL AUTO_INCREMENT COMMENT 'id',
+	max VARCHAR (128) DEFAULT '' COMMENT 'max',
+	appeui VARCHAR (128) DEFAULT '' COMMENT 'appeui',
+	last_upate_time VARCHAR (128) DEFAULT '' COMMENT 'lastUpateTime',
+	data VARCHAR (128) DEFAULT '' COMMENT 'data',
+	reserver VARCHAR (512) DEFAULT '' COMMENT 'reserver',
+	data_type INT (11) DEFAULT -1 COMMENT 'dataType',
+	gateways VARCHAR (512) DEFAULT '' COMMENT 'gateways',
+	is_analyse INT (11) DEFAULT -1 COMMENT 'isAnalyse',
+	date_create TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'dateCreate',
+	PRIMARY KEY (id)
+)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '设备上报数据';