Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Integrate with cloud ecosystem platform to develop complex and … #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions application-dev/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@
<groupId>com.milesight.beaveriot</groupId>
<artifactId>dashboard-service</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.milesight.beaveriot</groupId>-->
<!-- <artifactId>authentication-service</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.milesight.beaveriot</groupId>
<artifactId>authentication-service</artifactId>
</dependency>
<dependency>
<groupId>com.milesight.beaveriot</groupId>
<artifactId>user-service</artifactId>
Expand Down Expand Up @@ -144,6 +144,11 @@
<artifactId>msc-integration</artifactId>
<version>${project.version}</version>
</dependency>-->
<dependency>
<groupId>com.milesight.beaveriot</groupId>
<artifactId>lavatory-integration</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
3 changes: 3 additions & 0 deletions application-dev/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ spring:
enabled: ${SPRING_H2_CONSOLE_ENABLED:true}
path: /public/h2-console

file:
upload-dir: C://file

camel:
springboot:
routes-include-pattern: classpath:routes/*.yaml
Expand Down
101 changes: 101 additions & 0 deletions integrations/lavatory-integration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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>
<parent>
<groupId>com.milesight.beaveriot</groupId>
<artifactId>integrations</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>lavatory-integration</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.milesight.beaveriot</groupId>
<artifactId>context</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.milesight</groupId>
<artifactId>msc-java-sdk</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<!-- unit test -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version> <!-- 确保使用适当的版本 -->
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<!-- unit test -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.milesight.beaveriot.integration.lavatory;

import com.milesight.beaveriot.context.integration.bootstrap.IntegrationBootstrap;
import com.milesight.beaveriot.context.integration.model.Integration;
import com.milesight.beaveriot.integration.lavatory.service.LavatoryConnectionService;
import com.milesight.beaveriot.integration.lavatory.service.LavatoryDataSyncService;
import com.milesight.beaveriot.integration.lavatory.service.LavatoryWebhookService;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LavatoryIntegrationBootstrap implements IntegrationBootstrap {

@Autowired
private LavatoryConnectionService lavatoryConnectionService;

@Autowired
private LavatoryDataSyncService lavatoryDataFetchingService;

@Autowired
private LavatoryWebhookService lavatoryWebhookService;


@Override
public void onPrepared(Integration integrationConfig) {

}

@Override
public void onStarted(Integration integrationConfig) {
log.info("Lavatory integration starting");
lavatoryConnectionService.init();
lavatoryDataFetchingService.init();
lavatoryWebhookService.init();
log.info("Lavatory integration started");
}

@Override
public void onDestroy(Integration integrationConfig) {
log.info("Lavatory integration stopping");
lavatoryDataFetchingService.stop();
log.info("Lavatory integration stopped");
}

@Override
public void customizeRoute(CamelContext context) throws Exception {
IntegrationBootstrap.super.customizeRoute(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.milesight.beaveriot.integration.lavatory.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 允许所有路径
.allowedOrigins("*") // 允许所有来源
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的HTTP方法
.allowedHeaders("*") // 允许的请求头
.allowCredentials(true) // 允许发送Cookie信息
.maxAge(3600); // 预检请求的缓存时间(秒)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.milesight.beaveriot.integration.lavatory.constant;

public interface LavatoryIntegrationConstants {

String INTEGRATION_IDENTIFIER = "lavatory";

interface DeviceAdditionalDataName {

String DEVICE_ID = "device_id";
String FLOOR = "floor";
String SEX = "sex";
String COMPARTMENT = "compartment";

}

interface InternalPropertyIdentifier {

interface Pattern {
String PREFIX = "_#";
String SUFFIX = "#_";
String TEMPLATE = "_#%s#_";

static boolean match(String key) {
return key.startsWith(PREFIX) && key.endsWith(SUFFIX);
}
}

String LAST_SYNC_TIME = "_#last_sync_time#_";

static String getLastSyncTimeKey(String deviceKey) {
return String.format("%s.%s", deviceKey, LAST_SYNC_TIME);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.milesight.beaveriot.integration.lavatory.controller;

import com.milesight.beaveriot.base.response.ResponseBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@RestController
@RequestMapping("/public/integration/lavatory/file")
public class FileUploadController {

@Value("${file.upload-dir}")
private String uploadDir;
@PostMapping("/upload")
public com.milesight.beaveriot.base.response.ResponseBody<Object> uploadFile(@RequestParam("file") MultipartFile file) {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());

try {
// 检查上传目录是否存在,不存在则创建
Path uploadPath = Paths.get(uploadDir);
if (!Files.exists(uploadPath)) {
Files.createDirectories(uploadPath);
}

// 检查文件名是否包含无效字符
if (fileName.contains("..")) {
return ResponseBuilder.fail("500", "Filename contains invalid path sequence " + fileName);
}

// 保存文件
Path targetLocation = uploadPath.resolve(fileName);
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);

// 构建文件访问URL
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/public/integration/lavatory/file/images/")
.path(fileName)
.toUriString();
return ResponseBuilder.success(fileDownloadUri);
} catch (IOException ex) {
ex.printStackTrace(); // 打印堆栈跟踪以便调试
return ResponseBuilder.fail("500", "Could not store file " + fileName + ". Please try again!");
}
}

@GetMapping("/images/{fileName:.+}")
@ResponseBody
public ResponseEntity<Resource> getImage(@PathVariable String fileName) {
try {
Path filePath = Paths.get(uploadDir).resolve(fileName).normalize();
Resource resource = new UrlResource(filePath.toUri());

if (resource.exists() || resource.isReadable()) {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
} catch (MalformedURLException ex) {
ex.printStackTrace(); // 打印堆栈跟踪以便调试
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//package com.milesight.beaveriot.integration.lavatory.controller;
//
//import com.milesight.beaveriot.integration.lavatory.model.request.LavatoryRequest;
//import com.milesight.beaveriot.integration.lavatory.service.LavatoryDeviceService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//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 java.util.List;
//
///**
// *
// */
//@Slf4j
//@RestController
//@RequestMapping("/public/integration/lavatory/device-data")
//public class LavatoryIntegrationDeviceDataController {
//
// @Autowired
// private LavatoryDeviceService lavatoryDeviceService;
//
// @PostMapping("/list")
// public void list(@RequestBody List<LavatoryRequest> requests) {
// lavatoryDeviceService.list(requests);
// }
//
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.milesight.beaveriot.integration.lavatory.controller;

import com.milesight.beaveriot.integration.lavatory.model.WebhookPayload;
import com.milesight.beaveriot.integration.lavatory.service.LavatoryWebhookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
*
*/
@Slf4j
@RestController
@RequestMapping("/public/integration/lavatory")
public class LavatoryIntegrationPublicController {

@Autowired
private LavatoryWebhookService lavatoryWebhookService;

@PostMapping("/webhook")
public String webhook(@RequestHeader(name = "x-msc-request-signature") String signature,
@RequestHeader(name = "x-msc-webhook-uuid") String webhookUuid,
@RequestHeader(name = "x-msc-request-timestamp") String requestTimestamp,
@RequestHeader(name = "x-msc-request-nonce") String requestNonce,
@RequestBody List<WebhookPayload> webhookPayloads) {
lavatoryWebhookService.handleWebhookData(signature, webhookUuid, requestTimestamp, requestNonce, webhookPayloads);
return "success";
}

}
Loading