Ver Fonte

bingshui 数据解析

jianghouwei há 6 anos atrás
pai
commit
f915491c72

+ 126 - 107
pom.xml

@@ -1,29 +1,29 @@
 <?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>
+         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.tidecloud</groupId>
-	<artifactId>data-acceptance</artifactId>
-	<version>0.0.1-SNAPSHOT</version>
-	<packaging>jar</packaging>
+    <groupId>com.tidecloud</groupId>
+    <artifactId>data-acceptance</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
 
-	<name>data-acceptance</name>
-	<description>Demo project for Spring Boot</description>
+    <name>data-acceptance</name>
+    <description>Demo project for Spring Boot</description>
 
-	<parent>
-		<groupId>org.springframework.boot</groupId>
-		<artifactId>spring-boot-starter-parent</artifactId>
-		<version>1.5.2.RELEASE</version>
-		<relativePath/> <!-- lookup parent from repository -->
-	</parent>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>1.5.2.RELEASE</version>
+        <relativePath/> <!-- lookup parent from repository -->
+    </parent>
 
-	<properties>
-		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-		<java.version>1.8</java.version>
-	</properties>
-	<profiles>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <java.version>1.8</java.version>
+    </properties>
+    <profiles>
         <profile>
             <id>dev</id>
             <properties>
@@ -46,75 +46,94 @@
             </properties>
         </profile>
     </profiles>
-	<dependencies>
-		<dependency>
-			<groupId>org.springframework.boot</groupId>
-			<artifactId>spring-boot-starter</artifactId>
-		</dependency>
-		<dependency>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+        <dependency>
             <groupId>io.netty</groupId>
             <artifactId>netty-all</artifactId>
             <version>4.1.15.Final</version>
         </dependency>
-        <dependency>  
-            <groupId>org.springframework.boot</groupId>  
-            <artifactId>spring-boot-starter-web</artifactId>  
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
-		<dependency>
-			<groupId>org.springframework.boot</groupId>
-			<artifactId>spring-boot-starter-test</artifactId>
-			<scope>test</scope>
-		</dependency>
-		<dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
-         <dependency>
-			<groupId>com.tidecloud</groupId>
-			<artifactId>data-common</artifactId>
-			<version>0.0.1-SNAPSHOT</version>
-		</dependency>
-	    <!-- <dependency>
-	        <groupId>io.springfox</groupId>
-	    	<artifactId>springfox-swagger2</artifactId>
-	    	    <version>2.2.2</version>
-		</dependency>
-		<dependency>
-	    	<groupId>io.springfox</groupId>
-	    	<artifactId>springfox-swagger-ui</artifactId>
-	    	    <version>2.2.2</version>
-		</dependency> -->
-		<dependency>
-			<groupId>com.tidecloud</groupId>
-			<artifactId>common-util</artifactId>
-			<version>1.0-SNAPSHOT</version>
-		</dependency>
-		<dependency>
-			<groupId>com.tidecloud</groupId>
-			<artifactId>common-general</artifactId>
-			<version>1.0-SNAPSHOT</version>
-		</dependency>
-		<dependency> 
-			<groupId>commons-lang</groupId>
-  			<artifactId>commons-lang</artifactId>
-  			<version>2.6</version>
-  		</dependency>
-  		<dependency>
+        <dependency>
+            <groupId>com.tidecloud</groupId>
+            <artifactId>data-common</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+        </dependency>
+        <!-- <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger2</artifactId>
+                <version>2.2.2</version>
+        </dependency>
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger-ui</artifactId>
+                <version>2.2.2</version>
+        </dependency> -->
+        <dependency>
+            <groupId>com.tidecloud</groupId>
+            <artifactId>common-util</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>com.tidecloud</groupId>
+            <artifactId>common-general</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>commons-lang</groupId>
+            <artifactId>commons-lang</artifactId>
+            <version>2.6</version>
+        </dependency>
+        <dependency>
             <groupId>org.springframework.kafka</groupId>
             <artifactId>spring-kafka</artifactId>
             <version>1.2.0.RELEASE</version>
         </dependency>
-         <dependency>
-			<groupId>com.alibaba</groupId>
-			<artifactId>fastjson</artifactId>
-			<version>1.2.8</version>
-		</dependency>
-	</dependencies>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.8</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-netflix-core</artifactId>
+            <version>1.3.0.RELEASE</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-eureka</artifactId>
+            <version>1.3.0.RELEASE</version>
+        </dependency>
+        <!--<dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
+            <version>1.3.0.RELEASE</version>
+        </dependency>-->
+        <dependency>
+            <groupId>com.tidecloud</groupId>
+            <artifactId>service-accept-client</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
 
-	
-	<build>
-		<finalName>data-acceptance</finalName>
-		<resources>
+    <build>
+        <finalName>data-acceptance</finalName>
+        <resources>
             <resource>
                 <directory>${project.basedir}/src/main/resources</directory>
                 <excludes>
@@ -129,36 +148,36 @@
                 <filtering>false</filtering>
             </resource>
         </resources>
-		<plugins>
-			<plugin>
-				<groupId>org.springframework.boot</groupId>
-				<artifactId>spring-boot-maven-plugin</artifactId>
-			</plugin>
-			<plugin>
-				<artifactId>maven-resources-plugin</artifactId>
-				<version>2.5</version>
-				<configuration>
-					<encoding>UTF-8</encoding>
-				</configuration>
-				<executions>
-					<execution>
-						<id>copy-bin</id>
-						<phase>package</phase>
-						<goals>
-							<goal>copy-resources</goal>
-						</goals>
-						<configuration>
-							<outputDirectory>${project.build.directory}</outputDirectory>
-							<resources>
-								<resource>
-									<directory>src/bin</directory>
-								</resource>
-							</resources>
-						</configuration>
-					</execution>
-				</executions>
-			</plugin>
-		</plugins>
-	</build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <artifactId>maven-resources-plugin</artifactId>
+                <version>2.5</version>
+                <configuration>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>copy-bin</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>copy-resources</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>${project.build.directory}</outputDirectory>
+                            <resources>
+                                <resource>
+                                    <directory>src/bin</directory>
+                                </resource>
+                            </resources>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 
 </project>

+ 4 - 0
src/main/java/com/tidecloud/dataacceptance/DataAcceptanceApplication.java

@@ -2,8 +2,12 @@ package com.tidecloud.dataacceptance;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
+import org.springframework.cloud.netflix.feign.EnableFeignClients;
 
 @SpringBootApplication
+@EnableEurekaClient
+@EnableFeignClients(basePackages = {"com.accept.client"})
 public class DataAcceptanceApplication {
 
 	public static void main(String[] args) {

+ 55 - 13
src/main/java/com/tidecloud/dataacceptance/service/impl/BingShuiGpsServerHandler.java

@@ -4,9 +4,14 @@ import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
+import com.accept.client.DeviceReplyClient;
+import com.accept.model.DeviceReplyDto;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;
 
@@ -28,10 +33,17 @@ import io.netty.util.concurrent.GenericFutureListener;
 @ChannelHandler.Sharable
 public class BingShuiGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter {
 
+	private static final String FACTORY = "bingshui";
+
 	private static final Logger logger = LoggerFactory.getLogger(BingShuiGpsServerHandler.class);
 
+	private static ExecutorService executorService = Executors.newSingleThreadExecutor();
+	@Autowired
+	private DeviceReplyClient deviceReplyClient;
+
+
 	public void handle(String oringalData, Channel channel) {
-		
+
 	}
 
 	@Override
@@ -40,18 +52,17 @@ public class BingShuiGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter
 		String oringalData = byteBufferToString(byteBuf.nioBuffer());
 		logger.info("接入数据:{}", oringalData);
 		Map<String, String> dataMap = new HashMap<>();
-
-		String[] dataArray = oringalData.split("\\s+");
-		String phone = dataArray[0];
+		String[] dataArray = oringalData.split("\\[");
+		String phone = dataArray[0].trim();
 		String deviceData = dataArray[1];
-		if (deviceData.endsWith("]") && deviceData.startsWith("[")) {
-			String subDeviceData = deviceData.substring(1, deviceData.length() - 1);
+		if (deviceData.endsWith("]")) {
+			String subDeviceData = deviceData.substring(0, deviceData.length() - 1);
 			String[] subDataArray = subDeviceData.split(";");
 			try {
 				for (int i = 0; i < subDataArray.length; i++) {
 					String subData = subDataArray[i];
 					String[] datas = subData.split(":");
-					if (datas[0].contains("S") && !datas[0].contains("G")) {
+					if (i == 0) {
 						dataMap.put("S", datas[1]);
 					} else {
 						dataMap.put(datas[0], datas[1]);
@@ -61,28 +72,59 @@ public class BingShuiGpsServerHandler extends HexBinaryAcceptanceHandlerAdapter
 				logger.error(e.getMessage(), e);
 			}
 		}
-		
+
 		byte[] dataByteArray = new byte[in.readableBytes()];
 		in.readBytes(dataByteArray);
-		sendMsg2Kafka(dataByteArray,phone,channel);
-
-
+		sendMsg2Kafka(dataByteArray, phone, channel);
 		String serialStr = dataMap.get("S");
 		Integer serialId = Integer.valueOf(serialStr) + 1;
 		Date now = new Date();
 		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
 		String dataStr = simpleDateFormat.format(now);
+		String msg = replyMsg(dataMap, phone);
 		String replyMsg = "[" + "S:" + serialId + ";R" + "1" + ";" + "ST:" + dataStr + "]";
+		if (msg != null) {
+			replyMsg = "[" + "S:" + serialId + ";R" + "1" + ";" + msg + "ST:" + dataStr + "]";
+		}
 		ByteBuf buffer = Unpooled.buffer(replyMsg.getBytes().length);
 		buffer.writeBytes(replyMsg.getBytes());
-
 		ChannelFuture channelFuture = channel.writeAndFlush(buffer);
+		String finalReplyMsg = replyMsg;
 		channelFuture.addListener(new GenericFutureListener<Future<? super Void>>() {
 			@Override
 			public void operationComplete(Future<? super Void> future) throws Exception {
-				logger.info("回复设备: [{}] 成功", replyMsg);
+				logger.info("回复设备: [{}] 成功", finalReplyMsg);
 			}
 		});
 	}
 
+	/**
+	 * 获取回复内容
+	 *
+	 * @return
+	 */
+	private String replyMsg(Map<String, String> dataMap, String deviceId) {
+		try{
+			if (deviceId != null) {
+				if (dataMap.containsKey("SET") && "1".equals(dataMap.get("SET"))) {
+					// 更新配置状态为true;
+					logger.warn("设备:" + deviceId + "设置成功");
+					deviceReplyClient.updateReplyActiveByDeviceIdAndFactory(deviceId, FACTORY);
+				} else {
+					DeviceReplyDto dto = deviceReplyClient.queryDeviceReplyByDeviceIdAndFactory(deviceId, FACTORY);
+					if (dto.getActive() != null && !dto.getActive()) {
+						// 设置不成功
+						logger.warn("设备:" + deviceId + "设置参数:" + dto.getReplyInstructions());
+						return dto.getReplyInstructions();
+					}
+				}
+			}
+		}catch (Exception ex){
+			logger.error("获取设备"+deviceId+"参数设置失败 "+ex.getMessage(),ex);
+		}
+
+		return null;
+	}
+
+
 }

+ 2 - 2
src/main/resources/dev/application.yml

@@ -38,10 +38,10 @@ acceptance:
     name: bingshui
     topic: device-bingshui
     ip: 10.27.118.76
-    port: 7510
+    port: 6001
     dataFileDir: /home/service/collector_7510/rawdata/
     handlerClass: com.tidecloud.dataacceptance.service.impl.BingShuiGpsServerHandler
-    enable: false
+    enable: true
     
    -
     name: bsj

+ 2 - 2
src/main/resources/prod/application.yml

@@ -46,10 +46,10 @@ acceptance:
     name: bingshui
     topic: device-bingshui
     ip: 10.27.118.76
-    port: 7510
+    port: 6001
     dataFileDir: /home/service/collector_7510/rawdata/
     handlerClass: com.tidecloud.dataacceptance.service.impl.BingShuiGpsServerHandler
-    enable: false
+    enable: true
     
    -
     name: bsj