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: add aws Integration #10

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
129 changes: 129 additions & 0 deletions integrations/aws-iot-integration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?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">
<parent>
<artifactId>beaver-iot-integrations</artifactId>
<groupId>com.milesight.beaveriot</groupId>
<version>1.0.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>aws-iot-integration</artifactId>

<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>
<dependency>
<groupId>com.milesight.beaveriot</groupId>
<artifactId>parser-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- AWS iot device sdk -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-iot-device-sdk-java</artifactId>
<version>1.3.10</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cognitoidentity</artifactId>
<version>2.17.230</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>iotwireless</artifactId>
<version>2.17.230</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>iot</artifactId>
<version>2.17.230</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.milesight.beaveriot</groupId>
<artifactId>entity-service</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version>
</dependency>

</dependencies>

<build>
<plugins>
<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,42 @@
package com.milesight.beaveriot.integration.aws;

import com.milesight.beaveriot.context.integration.bootstrap.IntegrationBootstrap;
import com.milesight.beaveriot.context.integration.bootstrap.IntegrationBootstrapManager;
import com.milesight.beaveriot.context.integration.model.Integration;
import com.milesight.beaveriot.integration.aws.sdk.config.DestinationProperties;
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 AwsIntegrationBootstrap implements IntegrationBootstrap {

@Autowired
private IntegrationBootstrapManager integrationBootstrapManager;

@Override
public void onPrepared(Integration integrationConfig) {

}

@Override
public void onStarted(Integration integrationConfig) {
// Binder.get(integrationBootstrapManager.getIntegrationContext().getIntegrationEnvironment(AwsIntegrationConstants.INTEGRATION_IDENTIFIER)).bind("sdk.aws", AwsIotProperties.class);
// Binder.get(integrationBootstrapManager.getIntegrationContext().getIntegrationEnvironment(AwsIntegrationConstants.INTEGRATION_IDENTIFIER)).bind("sdk.aws.config.destination", DestinationProperties.class);
// Binder.get(integrationBootstrapManager.getIntegrationContext().getIntegrationEnvironment(AwsIntegrationConstants.INTEGRATION_IDENTIFIER)).bind("sdk.aws.config.device-profile", DeviceProfileProperties.class);
// Binder.get(integrationBootstrapManager.getIntegrationContext().getIntegrationEnvironment(AwsIntegrationConstants.INTEGRATION_IDENTIFIER)).bind("sdk.aws.config.service-profile", ServiceProfileProperties.class);
log.info("AWS integration starting");
}

@Override
public void onDestroy(Integration integrationConfig) {
log.info("AWS integration stopping");
}

@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,43 @@
package com.milesight.beaveriot.integration.aws.constant;

import com.milesight.beaveriot.base.utils.StringUtils;

public interface AwsIntegrationConstants {

String INTEGRATION_IDENTIFIER = "aws-iot-integration";

public static String getKey(String propertyKey) {
return AwsIntegrationConstants.INTEGRATION_IDENTIFIER + ".integration." + StringUtils.toSnakeCase(propertyKey);
}

public static String getSnKey() {
return AwsIntegrationConstants.getKey("add_device." + AwsIntegrationConstants.DeviceAdditionalDataName.DEVICE_SN);
}

interface DeviceAdditionalDataName {

String DEVICE_SN = "sn";

}

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,34 @@
package com.milesight.beaveriot.integration.aws.controller;

import com.milesight.beaveriot.base.utils.JsonUtils;
import com.milesight.beaveriot.parser.model.LnsAwsPayload;
import com.milesight.beaveriot.integration.aws.service.AwsWebhookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
* webhook
*/
@Slf4j
@RestController
@RequestMapping("/public/integration/aws")
public class AwsIntegrationPublicController {

@Autowired
private AwsWebhookService awsWebhookService;

@PostMapping("/webhook")
public String webhook(@RequestBody String payload) {
log.info("Received webhook data: {}", payload);
try {
// LORA设备接收值
LnsAwsPayload lnsAwsPayload = JsonUtils.fromJSON(payload, LnsAwsPayload.class);
awsWebhookService.handleWebhookData(lnsAwsPayload);
} catch (Exception e) {
log.error("Received webhook data error: {}", e.getMessage());
}
return "success";
}

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

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.milesight.beaveriot.base.response.ResponseBody;
import com.milesight.beaveriot.base.response.ResponseBuilder;
import com.milesight.beaveriot.integration.aws.model.parser.ParserRequest;
import com.milesight.beaveriot.integration.aws.service.ParserService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
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 static com.milesight.beaveriot.integration.aws.util.ParserUtils.isJsonValid;

/**
* 解析器控制器
*/
@Slf4j
@RestController
@RequestMapping("/parser")
public class ParserController {

@Autowired
private ParserService parserService;

@PostMapping
public ResponseBody<Object> parser(@RequestBody ParserRequest parserRequest) {
if (StrUtil.isEmpty(parserRequest.getInput())) {
return ResponseBuilder.success("parameter cannot be empty");
}
if("encode".equals(parserRequest.getType())) {
// 校验parserRequest.getInput()是否是json字符串
boolean isValidJson = isJsonValid(parserRequest.getInput());
if (Boolean.FALSE.equals(isValidJson)) {
return ResponseBuilder.success("invalid json");
}
}
String output = parserService.parser(parserRequest);
if (CharSequenceUtil.isEmpty(output)) {
return ResponseBuilder.success("parser not found");
}
return ResponseBuilder.success(output);
}

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

import com.milesight.beaveriot.base.response.ResponseBody;
import com.milesight.beaveriot.base.response.ResponseBuilder;
import com.milesight.beaveriot.integration.aws.model.parser.PluginResponseData;
import com.milesight.beaveriot.integration.aws.model.parser.SearchPluginRequest;
import com.milesight.beaveriot.integration.aws.model.parser.BatchDeletePluginRequest;
import com.milesight.beaveriot.integration.aws.model.parser.PluginRequest;
import com.milesight.beaveriot.integration.aws.service.PluginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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;


/**
* 插件控制器
*/
@Slf4j
@RestController
@RequestMapping("/plugin")
public class PluginController {

@Autowired
private PluginService pluginService;

@PostMapping
public ResponseBody<Object> createPlugin(@RequestBody PluginRequest pluginRequest) {
Boolean result = pluginService.createPlugin(pluginRequest);
if (Boolean.FALSE.equals(result)) {
return ResponseBuilder.fail("Failed to create product", "Failed to create product");
}
return ResponseBuilder.success();
}


@PostMapping("/search")
public ResponseBody<Page<PluginResponseData>> search(@RequestBody SearchPluginRequest searchPluginRequest) {
return ResponseBuilder.success(pluginService.searchPlugin(searchPluginRequest));
}

@PostMapping("/batch-delete")
public ResponseBody<Void> batchDelete(@RequestBody BatchDeletePluginRequest batchDeletePluginRequest) {
pluginService.batchDeletePlugins(batchDeletePluginRequest.getPluginIdList());
return ResponseBuilder.success();
}

}
Loading