├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── build.gradle ├── docker ├── Dockerfile ├── Makefile ├── docker-compose.yml └── run-application.sh ├── img └── tb-gateway.png ├── license-header-template.txt ├── pom.xml └── src ├── main ├── assembly │ └── windows.xml ├── conf │ ├── logback.xml │ └── tb-gateway.conf ├── filters │ ├── unix.properties │ └── windows.properties ├── java │ └── org │ │ └── thingsboard │ │ └── gateway │ │ ├── GatewayApplication.java │ │ ├── GatewayConfiguration.java │ │ ├── extensions │ │ ├── ExtensionService.java │ │ ├── ExtensionUpdate.java │ │ ├── common │ │ │ └── conf │ │ │ │ └── mapping │ │ │ │ ├── DataTypeMapping.java │ │ │ │ └── KVMapping.java │ │ ├── file │ │ │ ├── DefaultFileTailService.java │ │ │ ├── FileMonitor.java │ │ │ └── conf │ │ │ │ ├── CsvDeviceDataConverter.java │ │ │ │ ├── FileMonitorConfiguration.java │ │ │ │ └── FileTailConfiguration.java │ │ ├── http │ │ │ ├── DefaultHttpService.java │ │ │ ├── HttpController.java │ │ │ ├── HttpService.java │ │ │ └── conf │ │ │ │ ├── HttpConfiguration.java │ │ │ │ ├── HttpConverterConfiguration.java │ │ │ │ ├── HttpRequestProcessingError.java │ │ │ │ └── mapping │ │ │ │ └── HttpDeviceDataConverter.java │ │ ├── modbus │ │ │ ├── DefaultModbusService.java │ │ │ ├── ModbusClient.java │ │ │ ├── ModbusDevice.java │ │ │ ├── ModbusDeviceAware.java │ │ │ ├── ModbusService.java │ │ │ ├── conf │ │ │ │ ├── ModbusConfiguration.java │ │ │ │ ├── ModbusExtensionConstants.java │ │ │ │ ├── ModbusServerConfiguration.java │ │ │ │ ├── mapping │ │ │ │ │ ├── DeviceMapping.java │ │ │ │ │ ├── PollingTagMapping.java │ │ │ │ │ ├── TagMapping.java │ │ │ │ │ └── TagValueMapping.java │ │ │ │ └── transport │ │ │ │ │ ├── ModbusIpTransportConfiguration.java │ │ │ │ │ ├── ModbusRtuTransportConfiguration.java │ │ │ │ │ ├── ModbusTcpTransportConfiguration.java │ │ │ │ │ ├── ModbusTransportConfiguration.java │ │ │ │ │ └── ModbusUdpTransportConfiguration.java │ │ │ ├── rpc │ │ │ │ └── RpcProcessor.java │ │ │ └── util │ │ │ │ └── ModbusUtils.java │ │ ├── mqtt │ │ │ └── client │ │ │ │ ├── DefaultMqttClientService.java │ │ │ │ ├── MqttBrokerMonitor.java │ │ │ │ ├── MqttClientService.java │ │ │ │ ├── conf │ │ │ │ ├── MqttBrokerConfiguration.java │ │ │ │ ├── MqttClientConfiguration.java │ │ │ │ ├── credentials │ │ │ │ │ ├── AnonymousCredentials.java │ │ │ │ │ ├── BasicCredentials.java │ │ │ │ │ ├── CertJKSClientCredentials.java │ │ │ │ │ ├── CertPKCS12ClientCredentials.java │ │ │ │ │ ├── CertPemClientCredentials.java │ │ │ │ │ └── MqttClientCredentials.java │ │ │ │ └── mapping │ │ │ │ │ ├── AttributeRequestsMapping.java │ │ │ │ │ ├── AttributeUpdatesMapping.java │ │ │ │ │ ├── DeviceConnectMapping.java │ │ │ │ │ ├── DeviceDisconnectMapping.java │ │ │ │ │ ├── DeviceStateChangeMapping.java │ │ │ │ │ ├── MqttDataConverter.java │ │ │ │ │ ├── MqttJsonConverter.java │ │ │ │ │ ├── MqttTopicMapping.java │ │ │ │ │ └── ServerSideRpcMapping.java │ │ │ │ └── listener │ │ │ │ ├── MqttAttributeRequestsMessageListener.java │ │ │ │ ├── MqttDeviceStateChangeMessageListener.java │ │ │ │ ├── MqttRpcResponseMessageListener.java │ │ │ │ └── MqttTelemetryMessageListener.java │ │ └── opc │ │ │ ├── DefaultOpcUaService.java │ │ │ ├── OpcUaDevice.java │ │ │ ├── OpcUaDeviceAware.java │ │ │ ├── OpcUaServerMonitor.java │ │ │ ├── OpcUaService.java │ │ │ ├── conf │ │ │ ├── OpcUaConfiguration.java │ │ │ ├── OpcUaServerConfiguration.java │ │ │ ├── identity │ │ │ │ ├── AnonymousIdentityProviderConfiguration.java │ │ │ │ ├── IdentityProviderConfiguration.java │ │ │ │ └── UsernameIdentityProviderConfiguration.java │ │ │ └── mapping │ │ │ │ ├── AttributesMapping.java │ │ │ │ ├── DeviceMapping.java │ │ │ │ └── TimeseriesMapping.java │ │ │ ├── rpc │ │ │ └── RpcProcessor.java │ │ │ ├── scan │ │ │ ├── DataValueConverter.java │ │ │ └── OpcUaNode.java │ │ │ └── util │ │ │ └── OpcUaUtils.java │ │ ├── service │ │ ├── AttributesUpdateListener.java │ │ ├── DefaultTenantManagerService.java │ │ ├── ExtensionServiceCreation.java │ │ ├── MessageFuturePair.java │ │ ├── MqttCallbackWrapper.java │ │ ├── MqttDeliveryFuture.java │ │ ├── MqttMessageReceiver.java │ │ ├── MqttMessageSender.java │ │ ├── MqttPersistentMessage.java │ │ ├── PersistentFileService.java │ │ ├── PersistentFileServiceImpl.java │ │ ├── RpcCommandListener.java │ │ ├── TenantManagerService.java │ │ ├── TenantServiceRegistry.java │ │ ├── conf │ │ │ ├── TbConnectionConfiguration.java │ │ │ ├── TbExtensionConfiguration.java │ │ │ ├── TbGatewayConfiguration.java │ │ │ ├── TbPersistenceConfiguration.java │ │ │ ├── TbReportingConfiguration.java │ │ │ └── TbTenantConfiguration.java │ │ ├── data │ │ │ ├── AttributeRequest.java │ │ │ ├── AttributeRequestKey.java │ │ │ ├── AttributeRequestListener.java │ │ │ ├── AttributeResponse.java │ │ │ ├── AttributesUpdateSubscription.java │ │ │ ├── DeviceData.java │ │ │ ├── DeviceInfo.java │ │ │ ├── RpcCommandData.java │ │ │ ├── RpcCommandResponse.java │ │ │ └── RpcCommandSubscription.java │ │ ├── gateway │ │ │ ├── GatewayService.java │ │ │ ├── MqttGatewaySecurityConfiguration.java │ │ │ └── MqttGatewayService.java │ │ └── update │ │ │ ├── DefaultUpdateService.java │ │ │ └── UpdateService.java │ │ └── util │ │ ├── CertificateInfo.java │ │ ├── ConfigurationTools.java │ │ ├── JsonTools.java │ │ ├── KeystoreConfiguration.java │ │ └── converter │ │ ├── AbstractJsonConverter.java │ │ ├── AttributesMapping.java │ │ ├── BasicJsonConverter.java │ │ ├── TimeseriesMapping.java │ │ ├── TransformerKVMapping.java │ │ └── transformer │ │ ├── AbstractDataValueTransformer.java │ │ ├── DataValueTransformer.java │ │ └── DoubleValueTransformer.java ├── resources │ ├── banner.txt │ ├── example.der │ ├── example.pfx │ ├── file-config.json │ ├── http-config.json │ ├── logback.xml │ ├── modbus-config.json │ ├── modbusTest-config.json │ ├── modbusTest.mbs │ ├── mqtt-config.json │ ├── opc-config.json │ └── tb-gateway.yml └── scripts │ ├── control │ ├── deb │ │ ├── postinst │ │ ├── postrm │ │ ├── preinst │ │ └── prerm │ ├── rpm │ │ ├── postinst │ │ ├── postrm │ │ ├── preinst │ │ └── prerm │ └── tb-gateway.service │ └── windows │ ├── install.bat │ ├── service.xml │ └── uninstall.bat └── test ├── java └── org │ └── thingsboard │ └── gateway │ ├── AbstractGatewayMqttIntegrationTest.java │ ├── HttpExtensionIntegrationTest.java │ ├── TestConfiguration.java │ ├── mqtt │ ├── MqttIntegrationTest.java │ ├── MqttTimestampFormatIntegrationTest.java │ ├── MqttTimestampIntegrationTest.java │ ├── MqttTwoFiltersIntegrationTest.java │ ├── TestMqttHandler.java │ └── simulators │ │ ├── MqttBroker.java │ │ ├── MqttTestClient.java │ │ └── TbSimulator.java │ └── util │ ├── IoUtils.java │ └── JsonUtils.java └── resources ├── 58ac4b889058c24616a43b3b.json ├── application-mqtt-timestamp-format.yml ├── application-mqtt-timestamp.yml ├── application-mqtt-two-filters.yml ├── application-mqtt.yml ├── application.yml ├── logback.xml ├── mqtt-config-timestamp-format.json ├── mqtt-config-timestamp.json ├── mqtt-config-two-filters.json ├── mqtt-config.json └── mqtt ├── connect-1110.json ├── connect-1111.json ├── connect-SN-001.json ├── mqtt-1110-publish.json ├── mqtt-1110-result.json ├── mqtt-1111-publish.json ├── mqtt-1111-result.json ├── single-value-publish.json ├── single-value-result.json ├── single-value-with-ts-format-publish.json ├── single-value-with-ts-format-result.json ├── single-value-with-ts-publish.json └── single-value-with-ts-result.json /.gitignore: -------------------------------------------------------------------------------- 1 | output/** 2 | storage/** 3 | *.class 4 | *~ 5 | *.iml 6 | */.idea/** 7 | .idea/** 8 | .idea 9 | *.log 10 | *.log.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] 11 | */.classpath 12 | .classpath 13 | */.project 14 | .project 15 | .cache/** 16 | target/ 17 | build/ 18 | tmp_deb_control/ 19 | tmp_rpm_control/ 20 | tmp_sh/ 21 | .gwt/ 22 | .settings/ 23 | /bin 24 | bin/ 25 | **/dependency-reduced-pom.xml 26 | pom.xml.versionsBackup 27 | .DS_Store 28 | **/.gradle 29 | **/local.properties 30 | **/build 31 | **/target 32 | **/Californium.properties 33 | **/.env 34 | .instance_id 35 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | before_install: 2 | - sudo rm -f /etc/mavenrc 3 | - export M2_HOME=/usr/local/maven 4 | - export MAVEN_OPTS="-Dmaven.repo.local=$HOME/.m2/repository -Xms1024m -Xmx3072m" 5 | jdk: 6 | - oraclejdk8 7 | language: java 8 | sudo: required 9 | script: mvn clean verify 10 | install: true -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | FROM openjdk:8-jre 18 | 19 | ADD run-application.sh /run-application.sh 20 | ADD tb-gateway.deb /tb-gateway.deb 21 | 22 | RUN chmod +x /run-application.sh 23 | -------------------------------------------------------------------------------- /docker/Makefile: -------------------------------------------------------------------------------- 1 | VERSION=1.2.1 2 | PROJECT=thingsboard 3 | APP=gateway 4 | 5 | build: 6 | cp ../target/tb-gateway.deb . 7 | docker build --pull -t ${PROJECT}/${APP}:${VERSION} -t ${PROJECT}/${APP}:latest . 8 | rm tb-gateway.deb 9 | 10 | push: build 11 | docker push ${PROJECT}/${APP}:${VERSION} 12 | docker push ${PROJECT}/${APP}:latest 13 | -------------------------------------------------------------------------------- /docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | version: '2' 18 | 19 | services: 20 | tb-gateway: 21 | image: "thingsboard/gateway:1.2.1" 22 | environment: 23 | - GATEWAY_ACCESS_TOKEN=${GATEWAY_ACCESS_TOKEN} 24 | - GATEWAY_HOST=${GATEWAY_HOST} 25 | ports: 26 | - "9090:9090" 27 | entrypoint: /run-application.sh 28 | -------------------------------------------------------------------------------- /docker/run-application.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Copyright © 2017 The Thingsboard Authors 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | dpkg -i /tb-gateway.deb 19 | 20 | # Copying env variables into conf files 21 | printenv | awk -F "=" '{print "export " $1 "='\''" $2 "'\''"}' >> /usr/share/tb-gateway/conf/tb-gateway.conf 22 | 23 | cat /usr/share/tb-gateway/conf/tb-gateway.conf 24 | 25 | echo "Starting 'TB-gateway' service..." 26 | service tb-gateway start 27 | 28 | # Wait until log file is created 29 | sleep 5 30 | tail -f /var/log/tb-gateway/tb-gateway.log -------------------------------------------------------------------------------- /img/tb-gateway.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guodaxia103/thingsboard-gateway/0b00554cb7c49142a1f88fefa82b1be7e846a8e1/img/tb-gateway.png -------------------------------------------------------------------------------- /license-header-template.txt: -------------------------------------------------------------------------------- 1 | Copyright © ${project.inceptionYear} ${owner} 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /src/main/assembly/windows.xml: -------------------------------------------------------------------------------- 1 | 18 | 21 | windows 22 | 23 | 24 | zip 25 | 26 | 27 | 28 | 29 | 30 | ${pkg.win.dist} 31 | logs 32 | 33 | */** 34 | 35 | 36 | 37 | ${pkg.win.dist}/conf 38 | conf 39 | windows 40 | 41 | *.der 42 | *.cer 43 | *.pfx 44 | 45 | 46 | 47 | ${pkg.win.dist}/conf 48 | conf 49 | 50 | *.der 51 | *.cer 52 | *.pfx 53 | 54 | 55 | 56 | 57 | 58 | 59 | ${project.build.directory}/${project.build.finalName}-boot.${project.packaging} 60 | lib 61 | ${pkg.name}.jar 62 | 63 | 64 | ${pkg.win.dist}/service.exe 65 | 66 | ${pkg.name}.exe 67 | 68 | 69 | ${pkg.win.dist}/service.xml 70 | 71 | ${pkg.name}.xml 72 | windows 73 | 74 | 75 | ${pkg.win.dist}/install.bat 76 | 77 | windows 78 | 79 | 80 | ${pkg.win.dist}/uninstall.bat 81 | 82 | windows 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /src/main/conf/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 24 | ${pkg.logFolder}/${pkg.name}.log 25 | 27 | ${pkg.logFolder}/${pkg.name}.%d{yyyy-MM-dd}.%i.log 28 | 100MB 29 | 30 30 | 3GB 31 | 32 | 33 | %d{ISO8601} [%thread] %-5level %logger{36} - %msg%n 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/main/conf/tb-gateway.conf: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | export JAVA_OPTS="$JAVA_OPTS -Dplatform=@pkg.platform@" 18 | export LOG_FILENAME=${pkg.name}.out 19 | export LOADER_PATH=${pkg.installFolder}/conf 20 | -------------------------------------------------------------------------------- /src/main/filters/unix.properties: -------------------------------------------------------------------------------- 1 | pkg.logFolder=${pkg.unixLogFolder} -------------------------------------------------------------------------------- /src/main/filters/windows.properties: -------------------------------------------------------------------------------- 1 | pkg.logFolder=${BASE}\\logs 2 | pkg.winWrapperLogFolder=%BASE%\\logs 3 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/GatewayApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway; 17 | 18 | import lombok.extern.slf4j.Slf4j; 19 | import org.springframework.boot.SpringApplication; 20 | import org.springframework.boot.autoconfigure.SpringBootApplication; 21 | 22 | import java.util.Arrays; 23 | 24 | @Slf4j 25 | @SpringBootApplication 26 | public class GatewayApplication { 27 | 28 | private static final String SPRING_CONFIG_NAME_KEY = "--spring.config.name"; 29 | public static final String DEFAULT_SPRING_CONFIG_PARAM = SPRING_CONFIG_NAME_KEY + "=" + "tb-gateway"; 30 | 31 | public static void main(String[] args) { 32 | try { 33 | SpringApplication.run(GatewayApplication.class, updateArguments(args)); 34 | } catch (Exception e) { 35 | log.error(e.getMessage(), e); 36 | System.exit(1); 37 | } 38 | } 39 | 40 | private static String[] updateArguments(String[] args) { 41 | if (Arrays.stream(args).noneMatch(arg -> arg.startsWith(SPRING_CONFIG_NAME_KEY))) { 42 | String[] modifiedArgs = new String[args.length + 1]; 43 | System.arraycopy(args, 0, modifiedArgs, 0, args.length); 44 | modifiedArgs[args.length] = DEFAULT_SPRING_CONFIG_PARAM; 45 | return modifiedArgs; 46 | } 47 | return args; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/ExtensionService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions; 17 | 18 | import org.thingsboard.gateway.service.conf.TbExtensionConfiguration; 19 | 20 | /** 21 | * Created by ashvayka on 29.09.17. 22 | */ 23 | public interface ExtensionService { 24 | 25 | TbExtensionConfiguration getCurrentConfiguration(); 26 | 27 | void init(TbExtensionConfiguration configuration, Boolean isRemote) throws Exception; 28 | 29 | void update(TbExtensionConfiguration configuration) throws Exception; 30 | 31 | void destroy() throws Exception; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/ExtensionUpdate.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions; 17 | 18 | import org.thingsboard.gateway.service.conf.TbExtensionConfiguration; 19 | 20 | public abstract class ExtensionUpdate implements ExtensionService { 21 | 22 | public void update (TbExtensionConfiguration configurationNode) throws Exception { 23 | destroy(); 24 | init(configurationNode, true); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/common/conf/mapping/DataTypeMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.common.conf.mapping; 17 | 18 | import com.fasterxml.jackson.annotation.JsonCreator; 19 | import lombok.AllArgsConstructor; 20 | import lombok.Data; 21 | import org.thingsboard.server.common.data.kv.DataType; 22 | 23 | /** 24 | * Created by ashvayka on 17.01.17. 25 | */ 26 | @Data 27 | @AllArgsConstructor 28 | public class DataTypeMapping { 29 | 30 | private DataType dataType; 31 | 32 | @JsonCreator 33 | public static DataTypeMapping forValue(String value) { 34 | return new DataTypeMapping(DataType.valueOf(value.toUpperCase())); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/common/conf/mapping/KVMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.common.conf.mapping; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 16.01.17. 22 | */ 23 | @Data 24 | public class KVMapping { 25 | private String key; 26 | private DataTypeMapping type; 27 | private String value; 28 | private String ts; 29 | private String tsFormat; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/file/DefaultFileTailService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.file; 17 | 18 | import lombok.extern.slf4j.Slf4j; 19 | import org.thingsboard.gateway.extensions.ExtensionUpdate; 20 | import org.thingsboard.gateway.extensions.file.conf.FileTailConfiguration; 21 | import org.thingsboard.gateway.service.conf.TbExtensionConfiguration; 22 | import org.thingsboard.gateway.service.gateway.GatewayService; 23 | import org.thingsboard.gateway.util.ConfigurationTools; 24 | 25 | import java.util.List; 26 | import java.util.stream.Collectors; 27 | 28 | /** 29 | * Created by ashvayka on 15.05.17. 30 | */ 31 | @Slf4j 32 | public class DefaultFileTailService extends ExtensionUpdate { 33 | 34 | private final GatewayService gateway; 35 | private TbExtensionConfiguration currentConfiguration; 36 | private List brokers; 37 | 38 | public DefaultFileTailService(GatewayService gateway) { 39 | this.gateway = gateway; 40 | } 41 | 42 | @Override 43 | public TbExtensionConfiguration getCurrentConfiguration() { 44 | return currentConfiguration; 45 | } 46 | 47 | @Override 48 | public void init(TbExtensionConfiguration configurationNode, Boolean isRemote) throws Exception { 49 | currentConfiguration = configurationNode; 50 | log.info("[{}] Initializing File Tail service!", gateway.getTenantLabel()); 51 | FileTailConfiguration configuration; 52 | try { 53 | if(isRemote) { 54 | configuration = ConfigurationTools.readConfiguration(configurationNode.getConfiguration(), FileTailConfiguration.class); 55 | } else { 56 | configuration = ConfigurationTools.readFileConfiguration(configurationNode.getExtensionConfiguration(), FileTailConfiguration.class); 57 | } 58 | } catch (Exception e) { 59 | log.error("[{}] File Tail service configuration failed!", gateway.getTenantLabel(), e); 60 | gateway.onConfigurationError(e, currentConfiguration); 61 | throw e; 62 | } 63 | 64 | try { 65 | brokers = configuration.getFileMonitorConfigurations().stream().map(c -> new FileMonitor(gateway, c)).collect(Collectors.toList()); 66 | brokers.forEach(FileMonitor::init); 67 | } catch (Exception e) { 68 | log.error("[{}] File Tail service initialization failed!", gateway.getTenantLabel(), e); 69 | gateway.onConfigurationError(e, currentConfiguration); 70 | throw e; 71 | } 72 | } 73 | 74 | @Override 75 | public void destroy() { 76 | if (brokers != null) { 77 | brokers.forEach(FileMonitor::stop); 78 | } 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/file/conf/CsvDeviceDataConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.file.conf; 17 | 18 | import lombok.Data; 19 | import lombok.EqualsAndHashCode; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.thingsboard.gateway.util.converter.BasicJsonConverter; 22 | 23 | @Data 24 | @EqualsAndHashCode(callSuper = true) 25 | @Slf4j 26 | public class CsvDeviceDataConverter extends BasicJsonConverter { 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/file/conf/FileMonitorConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.file.conf; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 15.05.17. 22 | */ 23 | @Data 24 | public class FileMonitorConfiguration { 25 | 26 | private String file; 27 | private int skipLines; 28 | private int updateInterval; 29 | private String[] csvColumns; 30 | private CsvDeviceDataConverter converter; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/file/conf/FileTailConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.file.conf; 17 | 18 | import lombok.Data; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * Created by ashvayka on 15.05.17. 24 | */ 25 | @Data 26 | public class FileTailConfiguration { 27 | 28 | List fileMonitorConfigurations; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/http/HttpController.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.http; 17 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.http.HttpStatus; 22 | import org.springframework.http.MediaType; 23 | import org.springframework.web.bind.annotation.*; 24 | import org.thingsboard.gateway.extensions.http.conf.HttpRequestProcessingError; 25 | import org.thingsboard.gateway.service.TenantManagerService; 26 | 27 | import javax.servlet.http.HttpServletResponse; 28 | import java.io.IOException; 29 | 30 | @RestController 31 | @Slf4j 32 | public class HttpController { 33 | private static final String TOKEN_HEADER = "Authorization"; 34 | 35 | @Autowired 36 | private TenantManagerService service; 37 | 38 | private ObjectMapper mapper = new ObjectMapper(); 39 | 40 | 41 | @RequestMapping(value = "/sigfox/{deviceTypeId}", method = RequestMethod.POST) 42 | public void handleSigfoxRequest(@PathVariable String deviceTypeId, 43 | @RequestHeader(TOKEN_HEADER) String token, 44 | @RequestBody String body) throws Exception { 45 | service.processRequest(deviceTypeId, token, body); 46 | } 47 | 48 | @RequestMapping(value = "/uplink/{converterId}", method = RequestMethod.POST) 49 | public void handleRequest(@PathVariable String converterId, 50 | @RequestBody String body) throws Exception { 51 | service.processRequest(converterId, null, body); 52 | } 53 | 54 | @ExceptionHandler(Exception.class) 55 | public void handleThingsboardException(Exception exception, HttpServletResponse response) { 56 | log.debug("Processing exception {}", exception.getMessage(), exception); 57 | if (!response.isCommitted()) { 58 | try { 59 | response.setContentType(MediaType.APPLICATION_JSON_VALUE); 60 | if (exception instanceof SecurityException) { 61 | response.setStatus(HttpStatus.FORBIDDEN.value()); 62 | mapper.writeValue(response.getWriter(), 63 | new HttpRequestProcessingError("You don't have permission to perform this operation!")); 64 | } else { 65 | response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); 66 | mapper.writeValue(response.getWriter(), new HttpRequestProcessingError(exception.getMessage())); 67 | } 68 | } catch (IOException e) { 69 | log.error("Can't handle exception", e); 70 | } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/http/HttpService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.http; 17 | 18 | import org.thingsboard.gateway.extensions.ExtensionService; 19 | 20 | public interface HttpService extends ExtensionService { 21 | 22 | void processRequest(String converterId, String token, String body) throws Exception; 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/http/conf/HttpConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.http.conf; 17 | 18 | import lombok.Data; 19 | 20 | import java.util.List; 21 | 22 | @Data 23 | public class HttpConfiguration { 24 | 25 | List converterConfigurations; 26 | List deviceTypeConfigurations; 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/http/conf/HttpConverterConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.http.conf; 17 | 18 | import lombok.Data; 19 | import org.thingsboard.gateway.extensions.http.conf.mapping.HttpDeviceDataConverter; 20 | 21 | import java.util.List; 22 | 23 | @Data 24 | public class HttpConverterConfiguration { 25 | 26 | private String converterId; 27 | private String deviceTypeId; 28 | private String token; 29 | private List converters; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/http/conf/HttpRequestProcessingError.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.http.conf; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Data; 20 | 21 | /** 22 | * Created by ashvayka on 23.03.17. 23 | */ 24 | @Data 25 | @AllArgsConstructor 26 | public class HttpRequestProcessingError { 27 | 28 | private String message; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/http/conf/mapping/HttpDeviceDataConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.http.conf.mapping; 17 | 18 | import com.jayway.jsonpath.DocumentContext; 19 | import com.jayway.jsonpath.JsonPath; 20 | import lombok.Data; 21 | import lombok.EqualsAndHashCode; 22 | import lombok.extern.slf4j.Slf4j; 23 | import org.thingsboard.gateway.service.data.DeviceData; 24 | import org.thingsboard.gateway.util.converter.BasicJsonConverter; 25 | 26 | import java.util.List; 27 | import java.util.regex.Pattern; 28 | 29 | @Data 30 | @EqualsAndHashCode(callSuper = true) 31 | @Slf4j 32 | public class HttpDeviceDataConverter extends BasicJsonConverter { 33 | 34 | public static final Pattern TAG_PATTERN = Pattern.compile("\\$\\{(.*?)\\}"); 35 | 36 | public boolean isApplicable(String body) { 37 | if (filterExpression == null || filterExpression.isEmpty()) { 38 | return true; 39 | } else { 40 | try { 41 | List jsonArray = JsonPath.parse(body).read(filterExpression); 42 | return !jsonArray.isEmpty(); 43 | } catch (RuntimeException e) { 44 | log.debug("Failed to apply filter expression: {}", filterExpression, e); 45 | throw new RuntimeException("Failed to apply filter expression " + filterExpression, e); 46 | } 47 | } 48 | } 49 | 50 | public DeviceData parseBody(String body) { 51 | try { 52 | if (filterExpression != null && !filterExpression.isEmpty()) { 53 | DocumentContext document = JsonPath.parse(body); 54 | try { 55 | log.debug("Data before filtering {}", body); 56 | List jsonArray = document.read(filterExpression); 57 | Object jsonObj = jsonArray.get(0); // take 1st element from filtered array (jayway jsonpath library limitation) 58 | document = JsonPath.parse(jsonObj); 59 | body = document.jsonString(); 60 | log.debug("Data after filtering {}", body); 61 | } catch (RuntimeException e) { 62 | log.debug("Failed to apply filter expression: {}", filterExpression, e); 63 | throw new RuntimeException("Failed to apply filter expression " + filterExpression, e); 64 | } 65 | } 66 | 67 | return parseDeviceData(JsonPath.parse(body)); 68 | } catch (Exception e) { 69 | log.error("Exception occurred while parsing json request body [{}]", body, e); 70 | throw new RuntimeException("Exception occurred while parsing json request body [" + body + "]", e); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/DefaultModbusService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.modbus; 17 | 18 | import java.util.List; 19 | import java.util.stream.Collectors; 20 | 21 | import org.thingsboard.gateway.extensions.ExtensionUpdate; 22 | import org.thingsboard.gateway.service.conf.TbExtensionConfiguration; 23 | import org.thingsboard.gateway.service.gateway.GatewayService; 24 | import org.thingsboard.gateway.util.ConfigurationTools; 25 | 26 | import lombok.extern.slf4j.Slf4j; 27 | import org.thingsboard.gateway.extensions.modbus.conf.ModbusConfiguration; 28 | 29 | @Slf4j 30 | public class DefaultModbusService extends ExtensionUpdate implements ModbusService { 31 | 32 | private final GatewayService gateway; 33 | private TbExtensionConfiguration currentConfiguration; 34 | private List clients; 35 | 36 | public DefaultModbusService(GatewayService gateway) { 37 | this.gateway = gateway; 38 | } 39 | 40 | public TbExtensionConfiguration getCurrentConfiguration() { 41 | return currentConfiguration; 42 | } 43 | 44 | public void init(TbExtensionConfiguration configurationNode, Boolean isRemote) throws Exception { 45 | currentConfiguration = configurationNode; 46 | log.info("[{}] Initializing Modbus service", gateway.getTenantLabel()); 47 | ModbusConfiguration configuration; 48 | try { 49 | if(isRemote) { 50 | configuration = ConfigurationTools.readConfiguration(configurationNode.getConfiguration(), ModbusConfiguration.class); 51 | } else { 52 | configuration = ConfigurationTools.readFileConfiguration(configurationNode.getExtensionConfiguration(), ModbusConfiguration.class); 53 | } 54 | } catch (Exception e) { 55 | log.error("[{}] Modbus service configuration failed", gateway.getTenantLabel(), e); 56 | gateway.onConfigurationError(e, currentConfiguration); 57 | throw e; 58 | } 59 | 60 | log.debug("[{}] Modbus service configuration [{}]", gateway.getTenantLabel(), configuration); 61 | 62 | try { 63 | clients = configuration.getServers().stream().map(c -> new ModbusClient(gateway, c)).collect(Collectors.toList()); 64 | clients.forEach(ModbusClient::connect); 65 | } catch (Exception e) { 66 | log.error("[{}] Modbus service initialization failed", gateway.getTenantLabel(), e); 67 | gateway.onConfigurationError(e, currentConfiguration); 68 | throw e; 69 | } 70 | } 71 | 72 | public void destroy() { 73 | if (clients != null) { 74 | clients.forEach(ModbusClient::disconnect); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/ModbusDeviceAware.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.modbus; 17 | 18 | public interface ModbusDeviceAware { 19 | ModbusDevice getDevice(String deviceName); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/ModbusService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus; 18 | 19 | import org.thingsboard.gateway.extensions.ExtensionService; 20 | 21 | public interface ModbusService extends ExtensionService { 22 | 23 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/ModbusConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf; 18 | 19 | import lombok.Data; 20 | 21 | import java.util.List; 22 | 23 | import org.thingsboard.gateway.extensions.modbus.conf.ModbusServerConfiguration; 24 | 25 | @Data 26 | public class ModbusConfiguration { 27 | private List servers; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/ModbusExtensionConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf; 18 | 19 | public class ModbusExtensionConstants { 20 | public static final int DEFAULT_MODBUS_TCP_PORT = 502; 21 | public static final int DEFAULT_SOCKET_TIMEOUT = 3000; // in milliseconds 22 | 23 | public static final int DEFAULT_POLL_PERIOD = 1000; // in milliseconds 24 | public static final int NO_POLL_PERIOD_DEFINED = 0; 25 | 26 | public static final int DEFAULT_REGISTER_COUNT = 1; 27 | 28 | public static final int DEFAULT_BIT_INDEX_FOR_BOOLEAN = 0; 29 | public static final int NO_BIT_INDEX_DEFINED = -1; 30 | public static final int DEFAULT_REGISTER_COUNT_FOR_BOOLEAN = 1; 31 | 32 | public static final int MIN_BIT_INDEX_IN_REG = 0; 33 | public static final int MAX_BIT_INDEX_IN_REG = 15; 34 | 35 | public static final String LITTLE_ENDIAN_BYTE_ORDER = "LITTLE"; 36 | public static final String BIG_ENDIAN_BYTE_ORDER = "BIG"; 37 | public static final String BIG_ENDIAN_BYTE_SWAP = "BIG_SWAP"; //2 byte and 2 byte exchange 38 | public static final String COMPLEMENT_ENDIAN_BYTE_ORDER = "COMPLEMENT"; 39 | public static final String IEEE32_ENDIAN_BYTE_ORDER = "IEEE754/32"; 40 | 41 | public static final int WORD_REGISTER_COUNT = 1; 42 | public static final int INTEGER_REGISTER_COUNT = 2; 43 | public static final int LONG_REGISTER_COUNT = 4; 44 | 45 | public static final int FLOAT_REGISTER_COUNT = 2; 46 | public static final int DOUBLE_REGISTER_COUNT = 4; 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/ModbusServerConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf; 18 | 19 | import lombok.Data; 20 | import org.thingsboard.gateway.extensions.modbus.conf.mapping.DeviceMapping; 21 | import org.thingsboard.gateway.extensions.modbus.conf.transport.ModbusTransportConfiguration; 22 | 23 | import java.util.List; 24 | 25 | @Data 26 | public class ModbusServerConfiguration { 27 | private ModbusTransportConfiguration transport; 28 | private List devices; 29 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/mapping/DeviceMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf.mapping; 18 | 19 | import lombok.Data; 20 | import org.thingsboard.gateway.extensions.modbus.conf.ModbusExtensionConstants; 21 | 22 | import java.util.Collections; 23 | import java.util.List; 24 | 25 | @Data 26 | public class DeviceMapping { 27 | private int unitId; 28 | private String deviceName; 29 | private int attributesPollPeriod = ModbusExtensionConstants.DEFAULT_POLL_PERIOD; 30 | private int timeseriesPollPeriod = ModbusExtensionConstants.DEFAULT_POLL_PERIOD; 31 | private List attributes = Collections.emptyList(); // FIXME: Is it a real case, what device is without attributes? 32 | private List timeseries = Collections.emptyList(); // FIXME: Is it a real case, what device is without timeseries? 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/mapping/PollingTagMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.modbus.conf.mapping; 17 | 18 | import lombok.Data; 19 | import lombok.ToString; 20 | import org.thingsboard.gateway.extensions.common.conf.mapping.DataTypeMapping; 21 | import org.thingsboard.gateway.extensions.modbus.conf.ModbusExtensionConstants; 22 | 23 | @Data 24 | @ToString(callSuper=true) 25 | public class PollingTagMapping extends TagMapping { 26 | private int pollPeriod = ModbusExtensionConstants.NO_POLL_PERIOD_DEFINED; 27 | private DataTypeMapping type; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/mapping/TagMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf.mapping; 18 | 19 | import lombok.Data; 20 | import org.thingsboard.gateway.extensions.common.conf.mapping.DataTypeMapping; 21 | import org.thingsboard.gateway.extensions.modbus.conf.ModbusExtensionConstants; 22 | 23 | @Data 24 | public class TagMapping { 25 | private String tag; 26 | private int functionCode; 27 | private int address; 28 | private int registerCount = ModbusExtensionConstants.DEFAULT_REGISTER_COUNT; 29 | private String byteOrder = ModbusExtensionConstants.BIG_ENDIAN_BYTE_ORDER; 30 | private int bit = ModbusExtensionConstants.NO_BIT_INDEX_DEFINED; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/mapping/TagValueMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.modbus.conf.mapping; 17 | 18 | import lombok.Data; 19 | import lombok.ToString; 20 | 21 | @Data 22 | @ToString(callSuper=true) 23 | public class TagValueMapping extends TagMapping { 24 | private Object value; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/transport/ModbusIpTransportConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf.transport; 18 | 19 | import lombok.Data; 20 | import org.thingsboard.gateway.extensions.modbus.conf.ModbusExtensionConstants; 21 | 22 | @Data 23 | public class ModbusIpTransportConfiguration implements ModbusTransportConfiguration { 24 | private String host; 25 | private int port = ModbusExtensionConstants.DEFAULT_MODBUS_TCP_PORT; 26 | private int timeout = ModbusExtensionConstants.DEFAULT_SOCKET_TIMEOUT; 27 | @Override 28 | public long getRetryInterval() { 29 | return timeout; 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/transport/ModbusRtuTransportConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf.transport; 18 | 19 | import lombok.Data; 20 | import org.thingsboard.gateway.extensions.modbus.conf.ModbusExtensionConstants; 21 | 22 | @Data 23 | public class ModbusRtuTransportConfiguration implements ModbusTransportConfiguration { 24 | private String portName; 25 | private int timeout = ModbusExtensionConstants.DEFAULT_SOCKET_TIMEOUT; 26 | private String encoding; 27 | private int baudRate; 28 | private int dataBits; 29 | private float stopBits; 30 | private String parity; 31 | @Override 32 | public long getRetryInterval() { 33 | return timeout; 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/transport/ModbusTcpTransportConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf.transport; 18 | 19 | import lombok.Data; 20 | import lombok.ToString; 21 | 22 | @Data 23 | @ToString(callSuper=true, includeFieldNames=true) 24 | public class ModbusTcpTransportConfiguration extends ModbusIpTransportConfiguration { 25 | boolean rtuOverTcp; 26 | boolean reconnect; 27 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/transport/ModbusTransportConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf.transport; 18 | 19 | import com.fasterxml.jackson.annotation.JsonSubTypes; 20 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 21 | 22 | /** 23 | * Created by ashvayka on 16.01.17. 24 | */ 25 | @JsonTypeInfo( 26 | use = JsonTypeInfo.Id.NAME, 27 | include = JsonTypeInfo.As.PROPERTY, 28 | property = "type") 29 | @JsonSubTypes({ 30 | @JsonSubTypes.Type(value = ModbusTcpTransportConfiguration.class, name = "tcp"), 31 | @JsonSubTypes.Type(value = ModbusUdpTransportConfiguration.class, name = "udp"), 32 | @JsonSubTypes.Type(value = ModbusRtuTransportConfiguration.class, name = "rtu")}) 33 | public interface ModbusTransportConfiguration { 34 | /** 35 | * Get the trial cycle time 36 | * 37 | * @return 38 | */ 39 | long getRetryInterval(); 40 | 41 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/modbus/conf/transport/ModbusUdpTransportConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.thingsboard.gateway.extensions.modbus.conf.transport; 18 | 19 | public class ModbusUdpTransportConfiguration extends ModbusIpTransportConfiguration { 20 | 21 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/MqttClientService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client; 17 | 18 | import org.thingsboard.gateway.extensions.ExtensionService; 19 | 20 | /** 21 | * Created by ashvayka on 23.01.17. 22 | */ 23 | public interface MqttClientService extends ExtensionService { 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/MqttBrokerConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf; 17 | 18 | import lombok.Data; 19 | import org.thingsboard.gateway.extensions.mqtt.client.conf.credentials.MqttClientCredentials; 20 | import org.thingsboard.gateway.extensions.mqtt.client.conf.mapping.*; 21 | 22 | import java.util.List; 23 | 24 | /** 25 | * Created by ashvayka on 23.01.17. 26 | */ 27 | @Data 28 | public class MqttBrokerConfiguration { 29 | private String host; 30 | private int port; 31 | private boolean ssl; 32 | private String clientId; 33 | private String truststore; 34 | private String truststorePassword; 35 | private long retryInterval; 36 | private MqttClientCredentials credentials; 37 | private List mapping; 38 | private List connectRequests; 39 | private List disconnectRequests; 40 | private List attributeRequests; 41 | private List attributeUpdates; 42 | private List serverSideRpc; 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/MqttClientConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf; 17 | 18 | import lombok.Data; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * Created by ashvayka on 23.01.17. 24 | */ 25 | @Data 26 | public class MqttClientConfiguration { 27 | 28 | List brokers; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/credentials/AnonymousCredentials.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.credentials; 17 | 18 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 19 | 20 | /** 21 | * Created by ashvayka on 23.01.17. 22 | */ 23 | public class AnonymousCredentials implements MqttClientCredentials { 24 | 25 | @Override 26 | public void configure(MqttConnectOptions clientOptions) { 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/credentials/BasicCredentials.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.credentials; 17 | 18 | import lombok.Data; 19 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 20 | import org.springframework.util.StringUtils; 21 | 22 | /** 23 | * Created by ashvayka on 23.01.17. 24 | */ 25 | @Data 26 | public class BasicCredentials implements MqttClientCredentials { 27 | 28 | private String username; 29 | private String password; 30 | 31 | @Override 32 | public void configure(MqttConnectOptions clientOptions) { 33 | clientOptions.setUserName(username); 34 | if (!StringUtils.isEmpty(password)) { 35 | clientOptions.setPassword(password.toCharArray()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/credentials/CertJKSClientCredentials.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.credentials; 17 | 18 | import lombok.Data; 19 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 20 | 21 | @Data 22 | public class CertJKSClientCredentials implements MqttClientCredentials { 23 | @Override 24 | public void configure(MqttConnectOptions clientOptions) { 25 | throw new RuntimeException("JKS client credentials are not supported yet!"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/credentials/CertPKCS12ClientCredentials.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.credentials; 17 | 18 | import lombok.Data; 19 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 20 | 21 | @Data 22 | public class CertPKCS12ClientCredentials implements MqttClientCredentials { 23 | @Override 24 | public void configure(MqttConnectOptions clientOptions) { 25 | throw new RuntimeException("PKCS12 client credentials are not supported yet!"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/credentials/MqttClientCredentials.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.credentials; 17 | 18 | import com.fasterxml.jackson.annotation.JsonSubTypes; 19 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 20 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 21 | 22 | /** 23 | * Created by ashvayka on 23.01.17. 24 | */ 25 | @JsonTypeInfo( 26 | use = JsonTypeInfo.Id.NAME, 27 | include = JsonTypeInfo.As.PROPERTY, 28 | property = "type") 29 | @JsonSubTypes({ 30 | @JsonSubTypes.Type(value = AnonymousCredentials.class, name = "anonymous"), 31 | @JsonSubTypes.Type(value = BasicCredentials.class, name = "basic"), 32 | @JsonSubTypes.Type(value = CertPemClientCredentials.class, name = "cert.PEM"), 33 | @JsonSubTypes.Type(value = CertJKSClientCredentials.class, name = "cert.JKS"), 34 | @JsonSubTypes.Type(value = CertPKCS12ClientCredentials.class, name = "cert.PKCS12")}) 35 | public interface MqttClientCredentials { 36 | 37 | void configure(MqttConnectOptions clientOptions); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/AttributeRequestsMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | import com.jayway.jsonpath.DocumentContext; 19 | import com.jayway.jsonpath.JsonPath; 20 | import lombok.Data; 21 | import lombok.extern.slf4j.Slf4j; 22 | import org.eclipse.paho.client.mqttv3.MqttMessage; 23 | import org.thingsboard.gateway.service.data.AttributeRequest; 24 | import org.thingsboard.gateway.util.converter.AbstractJsonConverter; 25 | 26 | import java.nio.charset.StandardCharsets; 27 | import java.util.regex.Pattern; 28 | 29 | /** 30 | * Created by ashvayka on 22.02.17. 31 | */ 32 | @Data 33 | @Slf4j 34 | public class AttributeRequestsMapping extends AbstractJsonConverter { 35 | 36 | private String topicFilter; 37 | 38 | private String deviceNameJsonExpression; 39 | private String attributeKeyJsonExpression; 40 | private String requestIdJsonExpression; 41 | 42 | private String deviceNameTopicExpression; 43 | private String attributeKeyTopicExpression; 44 | private String requestIdTopicExpression; 45 | 46 | private Pattern deviceNameTopicPattern; 47 | private Pattern attributeKeyTopicPattern; 48 | private Pattern requestIdTopicPattern; 49 | 50 | private boolean clientScope; 51 | private String responseTopicExpression; 52 | private String valueExpression; 53 | 54 | public AttributeRequest convert(String topic, MqttMessage msg) { 55 | deviceNameTopicPattern = checkAndCompile(deviceNameTopicPattern, deviceNameTopicExpression); 56 | attributeKeyTopicPattern = checkAndCompile(attributeKeyTopicPattern, attributeKeyTopicExpression); 57 | requestIdTopicPattern = checkAndCompile(requestIdTopicPattern, requestIdTopicExpression); 58 | 59 | String data = new String(msg.getPayload(), StandardCharsets.UTF_8); 60 | DocumentContext document = JsonPath.parse(data); 61 | 62 | AttributeRequest.AttributeRequestBuilder builder = AttributeRequest.builder(); 63 | builder.deviceName(eval(topic, deviceNameTopicPattern, document, deviceNameJsonExpression)); 64 | builder.attributeKey(eval(topic, attributeKeyTopicPattern, document, attributeKeyJsonExpression)); 65 | builder.requestId(Integer.parseInt(eval(topic, requestIdTopicPattern, document, requestIdJsonExpression))); 66 | 67 | builder.clientScope(this.isClientScope()); 68 | builder.topicExpression(this.getResponseTopicExpression()); 69 | builder.valueExpression(this.getValueExpression()); 70 | return builder.build(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/AttributeUpdatesMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 22.02.17. 22 | */ 23 | @Data 24 | public class AttributeUpdatesMapping { 25 | 26 | private String deviceNameFilter; 27 | private String attributeFilter; 28 | private String topicExpression; 29 | private String valueExpression; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/DeviceConnectMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | /** 19 | * Created by ashvayka on 07.03.17. 20 | */ 21 | public class DeviceConnectMapping extends DeviceStateChangeMapping { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/DeviceDisconnectMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | /** 19 | * Created by ashvayka on 07.03.17. 20 | */ 21 | public class DeviceDisconnectMapping extends DeviceStateChangeMapping { 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/DeviceStateChangeMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | import lombok.Data; 19 | import lombok.ToString; 20 | 21 | /** 22 | * Created by ashvayka on 07.03.17. 23 | */ 24 | @Data 25 | @ToString 26 | public class DeviceStateChangeMapping { 27 | private String topicFilter; 28 | private String deviceNameJsonExpression; 29 | private String deviceNameTopicExpression; 30 | 31 | private String deviceTypeJsonExpression; 32 | private String deviceTypeTopicExpression; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/MqttDataConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | import com.fasterxml.jackson.annotation.JsonSubTypes; 19 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 20 | import org.eclipse.paho.client.mqttv3.MqttMessage; 21 | import org.thingsboard.gateway.service.data.DeviceData; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Created by ashvayka on 23.01.17. 27 | */ 28 | @JsonTypeInfo( 29 | use = JsonTypeInfo.Id.NAME, 30 | include = JsonTypeInfo.As.PROPERTY, 31 | property = "type") 32 | @JsonSubTypes({ 33 | @JsonSubTypes.Type(value = MqttJsonConverter.class, name = "json") 34 | }) 35 | public interface MqttDataConverter { 36 | 37 | List convert(String topic, MqttMessage msg) throws Exception; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/MqttTopicMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 23.01.17. 22 | */ 23 | @Data 24 | public class MqttTopicMapping { 25 | 26 | private String topicFilter; 27 | private MqttDataConverter converter; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/conf/mapping/ServerSideRpcMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.conf.mapping; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 22.02.17. 22 | */ 23 | @Data 24 | public class ServerSideRpcMapping { 25 | 26 | private String deviceNameFilter; 27 | private String methodFilter; 28 | private String requestTopicExpression; 29 | private String responseTopicExpression; 30 | private long responseTimeout; 31 | private String valueExpression; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/listener/MqttAttributeRequestsMessageListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.listener; 17 | 18 | import lombok.Data; 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.eclipse.paho.client.mqttv3.IMqttMessageListener; 21 | import org.eclipse.paho.client.mqttv3.MqttMessage; 22 | import org.thingsboard.gateway.extensions.mqtt.client.conf.mapping.AttributeRequestsMapping; 23 | import org.thingsboard.gateway.service.data.AttributeRequest; 24 | 25 | import java.util.Arrays; 26 | import java.util.function.Consumer; 27 | 28 | /** 29 | * Created by ashvayka on 24.01.17. 30 | */ 31 | @Data 32 | @Slf4j 33 | public class MqttAttributeRequestsMessageListener implements IMqttMessageListener { 34 | 35 | private final Consumer consumer; 36 | private final AttributeRequestsMapping converter; 37 | 38 | @Override 39 | public void messageArrived(String topic, MqttMessage message) throws Exception { 40 | try { 41 | consumer.accept(converter.convert(topic, message)); 42 | } catch (Exception e) { 43 | log.info("[{}] Failed to decode message: {}", topic, Arrays.toString(message.getPayload()), e); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/listener/MqttRpcResponseMessageListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.listener; 17 | 18 | import lombok.Data; 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.eclipse.paho.client.mqttv3.IMqttMessageListener; 21 | import org.eclipse.paho.client.mqttv3.MqttMessage; 22 | import org.thingsboard.gateway.service.data.RpcCommandResponse; 23 | 24 | import java.nio.charset.StandardCharsets; 25 | import java.util.function.BiConsumer; 26 | 27 | /** 28 | * Created by ashvayka on 07.03.17. 29 | */ 30 | @Data 31 | @Slf4j 32 | public class MqttRpcResponseMessageListener implements IMqttMessageListener { 33 | 34 | private final int requestId; 35 | private final String deviceName; 36 | private final BiConsumer consumer; 37 | 38 | @Override 39 | public void messageArrived(String topic, MqttMessage msg) throws Exception { 40 | RpcCommandResponse response = new RpcCommandResponse(); 41 | response.setRequestId(requestId); 42 | response.setDeviceName(deviceName); 43 | response.setData(new String(msg.getPayload(), StandardCharsets.UTF_8)); 44 | consumer.accept(topic, response); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/mqtt/client/listener/MqttTelemetryMessageListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.mqtt.client.listener; 17 | 18 | import lombok.Data; 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.eclipse.paho.client.mqttv3.IMqttMessageListener; 21 | import org.eclipse.paho.client.mqttv3.MqttMessage; 22 | import org.thingsboard.gateway.extensions.mqtt.client.conf.mapping.MqttDataConverter; 23 | import org.thingsboard.gateway.service.data.DeviceData; 24 | 25 | import java.util.Arrays; 26 | import java.util.List; 27 | import java.util.function.Consumer; 28 | 29 | /** 30 | * Created by ashvayka on 24.01.17. 31 | */ 32 | @Data 33 | @Slf4j 34 | public class MqttTelemetryMessageListener implements IMqttMessageListener { 35 | 36 | private final Consumer> consumer; 37 | private final MqttDataConverter converter; 38 | 39 | @Override 40 | public void messageArrived(String topic, MqttMessage message) throws Exception { 41 | try { 42 | consumer.accept(converter.convert(topic, message)); 43 | } catch (Exception e) { 44 | log.info("[{}] Failed to decode message: {}", topic, Arrays.toString(message.getPayload()), e); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/DefaultOpcUaService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc; 17 | 18 | import lombok.extern.slf4j.Slf4j; 19 | import org.thingsboard.gateway.extensions.ExtensionUpdate; 20 | import org.thingsboard.gateway.extensions.opc.conf.OpcUaConfiguration; 21 | import org.thingsboard.gateway.service.conf.TbExtensionConfiguration; 22 | import org.thingsboard.gateway.service.gateway.GatewayService; 23 | import org.thingsboard.gateway.util.ConfigurationTools; 24 | 25 | import java.util.List; 26 | import java.util.stream.Collectors; 27 | 28 | /** 29 | * Created by ashvayka on 06.01.17. 30 | */ 31 | @Slf4j 32 | public class DefaultOpcUaService extends ExtensionUpdate implements OpcUaService { 33 | 34 | private final GatewayService gateway; 35 | private TbExtensionConfiguration currentConfiguration; 36 | private List monitors; 37 | 38 | public DefaultOpcUaService(GatewayService gateway) { 39 | this.gateway = gateway; 40 | } 41 | 42 | @Override 43 | public TbExtensionConfiguration getCurrentConfiguration() { 44 | return currentConfiguration; 45 | } 46 | 47 | @Override 48 | public void init(TbExtensionConfiguration configurationNode, Boolean isRemote) throws Exception { 49 | currentConfiguration = configurationNode; 50 | log.info("Initializing OPC-UA service!", gateway.getTenantLabel()); 51 | OpcUaConfiguration configuration; 52 | try { 53 | if(isRemote) { 54 | configuration = ConfigurationTools.readConfiguration(configurationNode.getConfiguration(), OpcUaConfiguration.class); 55 | } else { 56 | configuration = ConfigurationTools.readFileConfiguration(configurationNode.getExtensionConfiguration(), OpcUaConfiguration.class); 57 | } 58 | } catch (Exception e) { 59 | log.error("OPC-UA service configuration failed!", gateway.getTenantLabel(), e); 60 | gateway.onConfigurationError(e, currentConfiguration); 61 | throw e; 62 | } 63 | 64 | try { 65 | monitors = configuration.getServers().stream().map(c -> new OpcUaServerMonitor(gateway, c)).collect(Collectors.toList()); 66 | for (OpcUaServerMonitor monitor : monitors) { 67 | monitor.connect(isRemote); 68 | } 69 | } catch (Exception e) { 70 | log.error("OPC-UA service initialization failed!", gateway.getTenantLabel(), e); 71 | gateway.onConfigurationError(e, currentConfiguration); 72 | throw e; 73 | } 74 | } 75 | 76 | @Override 77 | public void destroy() { 78 | if (monitors != null) { 79 | monitors.forEach(OpcUaServerMonitor::disconnect); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/OpcUaDeviceAware.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc; 17 | 18 | public interface OpcUaDeviceAware { 19 | OpcUaDevice getDevice(String deviceName); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/OpcUaService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc; 17 | 18 | import org.thingsboard.gateway.extensions.ExtensionService; 19 | 20 | /** 21 | * Created by ashvayka on 16.01.17. 22 | */ 23 | public interface OpcUaService extends ExtensionService { 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/OpcUaConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf; 17 | 18 | import lombok.Data; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * Created by ashvayka on 16.01.17. 24 | */ 25 | @Data 26 | public class OpcUaConfiguration { 27 | 28 | List servers; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/OpcUaServerConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf; 17 | 18 | import lombok.Data; 19 | import org.thingsboard.gateway.extensions.opc.conf.identity.IdentityProviderConfiguration; 20 | import org.thingsboard.gateway.util.KeystoreConfiguration; 21 | import org.thingsboard.gateway.extensions.opc.conf.mapping.DeviceMapping; 22 | 23 | import java.util.List; 24 | 25 | /** 26 | * Created by ashvayka on 16.01.17. 27 | */ 28 | @Data 29 | public class OpcUaServerConfiguration { 30 | 31 | private String applicationName; 32 | private String applicationUri; 33 | private String host; 34 | private int port; 35 | private int scanPeriodInSeconds; 36 | private int timeoutInMillis; 37 | private String security; 38 | private IdentityProviderConfiguration identity; 39 | private KeystoreConfiguration keystore; 40 | private List mapping; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/identity/AnonymousIdentityProviderConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf.identity; 17 | 18 | import lombok.Data; 19 | import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider; 20 | import org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider; 21 | 22 | /** 23 | * Created by ashvayka on 16.01.17. 24 | */ 25 | @Data 26 | public class AnonymousIdentityProviderConfiguration implements IdentityProviderConfiguration { 27 | 28 | @Override 29 | public IdentityProvider toProvider() { 30 | return new AnonymousProvider(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/identity/IdentityProviderConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf.identity; 17 | 18 | import com.fasterxml.jackson.annotation.JsonSubTypes; 19 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 20 | import org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider; 21 | 22 | /** 23 | * Created by ashvayka on 16.01.17. 24 | */ 25 | @JsonTypeInfo( 26 | use = JsonTypeInfo.Id.NAME, 27 | include = JsonTypeInfo.As.PROPERTY, 28 | property = "type") 29 | @JsonSubTypes({ 30 | @JsonSubTypes.Type(value = AnonymousIdentityProviderConfiguration.class, name = "anonymous"), 31 | @JsonSubTypes.Type(value = UsernameIdentityProviderConfiguration.class, name = "username")}) 32 | public interface IdentityProviderConfiguration { 33 | 34 | IdentityProvider toProvider(); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/identity/UsernameIdentityProviderConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf.identity; 17 | 18 | import lombok.Data; 19 | import org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider; 20 | import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider; 21 | 22 | /** 23 | * Created by ashvayka on 16.01.17. 24 | */ 25 | @Data 26 | public class UsernameIdentityProviderConfiguration implements IdentityProviderConfiguration { 27 | 28 | private final String username; 29 | private final String password; 30 | 31 | @Override 32 | public IdentityProvider toProvider() { 33 | return new UsernameProvider(username, password); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/mapping/AttributesMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf.mapping; 17 | 18 | import lombok.Data; 19 | import org.thingsboard.gateway.extensions.common.conf.mapping.KVMapping; 20 | 21 | /** 22 | * Created by ashvayka on 16.01.17. 23 | */ 24 | @Data 25 | public class AttributesMapping extends KVMapping { 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/mapping/DeviceMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf.mapping; 17 | 18 | import lombok.Data; 19 | 20 | import java.util.HashSet; 21 | import java.util.List; 22 | import java.util.Set; 23 | import java.util.regex.Matcher; 24 | import java.util.regex.Pattern; 25 | 26 | /** 27 | * Created by ashvayka on 16.01.17. 28 | */ 29 | @Data 30 | public class DeviceMapping { 31 | 32 | public static final Pattern TAG_PATTERN = Pattern.compile("\\$\\{(.*?)\\}"); 33 | private final String deviceNodePattern; 34 | private final String deviceNamePattern; 35 | private final List attributes; 36 | private final List timeseries; 37 | 38 | public Set getDeviceNameTags() { 39 | Set tags = new HashSet<>(); 40 | addTags(tags, TAG_PATTERN, deviceNamePattern); 41 | return tags; 42 | } 43 | 44 | public Set getAllTags() { 45 | Set tags = new HashSet<>(); 46 | addTags(tags, TAG_PATTERN, deviceNamePattern); 47 | attributes.forEach(mapping -> addTags(tags, TAG_PATTERN, mapping.getValue())); 48 | timeseries.forEach(mapping -> addTags(tags, TAG_PATTERN, mapping.getValue())); 49 | return tags; 50 | } 51 | 52 | private void addTags(Set tags, Pattern pattern, String expression) { 53 | Matcher matcher = pattern.matcher(expression); 54 | while (matcher.find()) { 55 | String tag = matcher.group(); 56 | tags.add(tag.substring(2, tag.length() - 1)); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/conf/mapping/TimeseriesMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.conf.mapping; 17 | 18 | import lombok.Data; 19 | import org.thingsboard.gateway.extensions.common.conf.mapping.KVMapping; 20 | 21 | /** 22 | * Created by ashvayka on 16.01.17. 23 | */ 24 | @Data 25 | public class TimeseriesMapping extends KVMapping { 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/scan/DataValueConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.scan; 17 | 18 | import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue; 19 | import org.thingsboard.server.common.data.kv.KvEntry; 20 | 21 | import java.util.Optional; 22 | 23 | /** 24 | * Created by ashvayka on 17.01.17. 25 | */ 26 | public class DataValueConverter { 27 | 28 | public static Optional toKvEntry(DataValue dataValue){ 29 | return Optional.empty(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/extensions/opc/scan/OpcUaNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.extensions.opc.scan; 17 | 18 | import lombok.Data; 19 | import lombok.ToString; 20 | import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId; 21 | import org.springframework.util.StringUtils; 22 | 23 | /** 24 | * Created by ashvayka on 16.01.17. 25 | */ 26 | @Data 27 | @ToString(exclude = "parent") 28 | public class OpcUaNode { 29 | 30 | private final NodeId nodeId; 31 | private final OpcUaNode parent; 32 | private final String name; 33 | private final String fqn; 34 | 35 | public OpcUaNode(NodeId nodeId, String name) { 36 | this(null, nodeId, name); 37 | } 38 | 39 | public OpcUaNode(OpcUaNode parent, NodeId nodeId, String name) { 40 | this.parent = parent; 41 | this.nodeId = nodeId; 42 | this.name = name; 43 | this.fqn = ((parent != null && !StringUtils.isEmpty(parent.getFqn())) ? parent.getFqn() + "." : "") + name; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/AttributesUpdateListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import org.thingsboard.server.common.data.kv.KvEntry; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * Created by ashvayka on 22.02.17. 24 | */ 25 | public interface AttributesUpdateListener { 26 | 27 | void onAttributesUpdated(String deviceName, List attributes); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/ExtensionServiceCreation.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import org.thingsboard.gateway.extensions.ExtensionService; 19 | import org.thingsboard.gateway.service.gateway.GatewayService; 20 | 21 | public interface ExtensionServiceCreation { 22 | 23 | ExtensionService createExtensionServiceByType(GatewayService gateway, String type); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/MessageFuturePair.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import io.netty.util.concurrent.Future; 19 | import lombok.AllArgsConstructor; 20 | import lombok.Data; 21 | 22 | /** 23 | * Created by Valerii Sosliuk on 3/10/2018. 24 | */ 25 | @Data 26 | @AllArgsConstructor 27 | public class MessageFuturePair { 28 | 29 | Future future; 30 | MqttPersistentMessage message; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/MqttCallbackWrapper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Data; 20 | 21 | import java.util.function.Consumer; 22 | 23 | /** 24 | * Created by Valerii Sosliuk on 12/30/2017. 25 | */ 26 | @Data 27 | @AllArgsConstructor 28 | public class MqttCallbackWrapper { 29 | 30 | private Consumer successCallback; 31 | private Consumer failureCallback; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/MqttDeliveryFuture.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import java.util.concurrent.CompletableFuture; 19 | 20 | /** 21 | * Created by ashvayka on 23.03.17. 22 | */ 23 | public class MqttDeliveryFuture extends CompletableFuture { 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/MqttPersistentMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import lombok.Builder; 19 | import lombok.Data; 20 | 21 | import java.io.Serializable; 22 | import java.util.*; 23 | 24 | /** 25 | * Created by Valerii Sosliuk on 1/2/2018. 26 | */ 27 | @Data 28 | @Builder 29 | public class MqttPersistentMessage implements Serializable { 30 | 31 | private static final long serialVersionUID = -3133461476074777891L; 32 | 33 | private UUID id; 34 | private long timestamp; 35 | private String deviceId; 36 | private int messageId; 37 | private String topic; 38 | private byte[] payload; 39 | 40 | @Override 41 | public String toString() { 42 | return "{deviceId='" + deviceId + '\'' + 43 | ", payload=" + new String(payload) + 44 | ", timestamp=" + timestamp + 45 | ", topic='" + topic + '\'' + 46 | "id=" + id + 47 | ", messageId=" + messageId + 48 | '}'; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/PersistentFileService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import org.eclipse.paho.client.mqttv3.MqttMessage; 19 | import org.thingsboard.gateway.service.MqttDeliveryFuture; 20 | 21 | import java.io.IOException; 22 | import java.util.List; 23 | import java.util.Optional; 24 | import java.util.UUID; 25 | import java.util.function.Consumer; 26 | 27 | /** 28 | * Created by Valerii Sosliuk on 1/2/2018. 29 | */ 30 | public interface PersistentFileService { 31 | 32 | MqttDeliveryFuture persistMessage(String topic, int msgId, byte[] payload, String deviceId, 33 | Consumer onSuccess, Consumer onFailure) throws IOException; 34 | 35 | /** 36 | * Returns a list of the messages that are to be sent. If storage files exist, then the messages from oldest 37 | * storage file are returned and file is deleted. If no storage file exists, then returns the messages that are currently 38 | * in storage buffer and clears it 39 | * @return {@see List} of {@see MqttPersistentMessage} to be sent 40 | * @throws IOException 41 | */ 42 | List getPersistentMessages() throws IOException; 43 | 44 | /** 45 | * Returns a list of the messages that are to be re-sent 46 | * @return {@see List} of {@see MqttPersistentMessage} to be re-sent 47 | * @throws IOException 48 | */ 49 | List getResendMessages() throws IOException; 50 | 51 | void resolveFutureSuccess(UUID id); 52 | 53 | void resolveFutureFailed(UUID id, Throwable e); 54 | 55 | Optional getMqttDeliveryFuture(UUID id); 56 | 57 | boolean deleteMqttDeliveryFuture(UUID id); 58 | 59 | Optional> getSuccessCallback(UUID id); 60 | 61 | Optional> getFailureCallback(UUID id); 62 | 63 | void saveForResend(MqttPersistentMessage message) throws IOException; 64 | 65 | void saveForResend(List messages) throws IOException; 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/RpcCommandListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | import org.thingsboard.gateway.service.data.RpcCommandData; 19 | 20 | /** 21 | * Created by ashvayka on 22.02.17. 22 | */ 23 | public interface RpcCommandListener { 24 | 25 | void onRpcCommand(String deviceName, RpcCommandData command); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/TenantManagerService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service; 17 | 18 | /** 19 | * Created by ashvayka on 29.09.17. 20 | */ 21 | public interface TenantManagerService { 22 | 23 | void processRequest(String converterId, String token, String body) throws Exception; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/conf/TbConnectionConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.conf; 17 | 18 | import lombok.Data; 19 | import org.thingsboard.gateway.service.gateway.MqttGatewaySecurityConfiguration; 20 | 21 | /** 22 | * Created by ashvayka on 18.01.17. 23 | */ 24 | @Data 25 | public class TbConnectionConfiguration { 26 | 27 | private String host; 28 | private int port; 29 | private long retryInterval; 30 | private long connectionTimeout; 31 | private int maxInFlight; 32 | private int maxQueueSize; 33 | private int incomingQueueWarningThreshold; 34 | private MqttGatewaySecurityConfiguration security; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/conf/TbExtensionConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.conf; 17 | 18 | import com.fasterxml.jackson.databind.JsonNode; 19 | import com.fasterxml.jackson.databind.node.ObjectNode; 20 | import lombok.Data; 21 | 22 | /** 23 | * Created by ashvayka on 29.09.17. 24 | */ 25 | @Data 26 | public class TbExtensionConfiguration { 27 | 28 | private String id; 29 | private String type; 30 | private JsonNode configuration; 31 | private String extensionConfiguration; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/conf/TbGatewayConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.conf; 17 | 18 | import lombok.Data; 19 | import org.springframework.boot.context.properties.ConfigurationProperties; 20 | import org.springframework.context.annotation.Configuration; 21 | 22 | import java.util.List; 23 | 24 | /** 25 | * Created by ashvayka on 29.09.17. 26 | */ 27 | @Configuration 28 | @ConfigurationProperties(prefix = "gateways") 29 | @Data 30 | public class TbGatewayConfiguration { 31 | 32 | List tenants; 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/conf/TbPersistenceConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.conf; 17 | 18 | import lombok.Data; 19 | import lombok.extern.slf4j.Slf4j; 20 | import org.springframework.util.StringUtils; 21 | 22 | /** 23 | * Created by ashvayka on 24.01.17. 24 | */ 25 | @Data 26 | @Slf4j 27 | public class TbPersistenceConfiguration { 28 | 29 | private String type; 30 | private String path; 31 | private int bufferSize; 32 | private long pollingInterval; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/conf/TbReportingConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.conf; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 24.01.17. 22 | */ 23 | @Data 24 | public class TbReportingConfiguration { 25 | 26 | private long interval; 27 | private int maxErrorsPerInterval; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/conf/TbTenantConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.conf; 17 | 18 | import lombok.Data; 19 | 20 | import java.util.List; 21 | 22 | /** 23 | * Created by ashvayka on 29.09.17. 24 | */ 25 | @Data 26 | public class TbTenantConfiguration { 27 | 28 | private String label; 29 | private TbReportingConfiguration reporting; 30 | private TbPersistenceConfiguration persistence; 31 | private TbConnectionConfiguration connection; 32 | private Boolean remoteConfiguration; 33 | private List extensions; 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/AttributeRequest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.Builder; 19 | import lombok.Data; 20 | 21 | /** 22 | * Created by ashvayka on 02.03.17. 23 | */ 24 | @Data 25 | @Builder 26 | public class AttributeRequest { 27 | 28 | private final int requestId; 29 | private final String deviceName; 30 | private final String attributeKey; 31 | 32 | private final boolean clientScope; 33 | private final String topicExpression; 34 | private final String valueExpression; 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/AttributeRequestKey.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 02.03.17. 22 | */ 23 | @Data 24 | public class AttributeRequestKey { 25 | private final int requestId; 26 | private final String deviceName; 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/AttributeRequestListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.Data; 19 | 20 | import java.util.function.Consumer; 21 | 22 | /** 23 | * Created by ashvayka on 03.03.17. 24 | */ 25 | @Data 26 | public class AttributeRequestListener { 27 | private final AttributeRequest request; 28 | private final Consumer listener; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/AttributeResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.Builder; 19 | import lombok.Data; 20 | import org.thingsboard.server.common.data.kv.KvEntry; 21 | 22 | import java.util.Optional; 23 | 24 | /** 25 | * Created by ashvayka on 02.03.17. 26 | */ 27 | @Data 28 | @Builder 29 | public class AttributeResponse { 30 | 31 | private final int requestId; 32 | private final String deviceName; 33 | private final String key; 34 | private final boolean clientScope; 35 | private final Optional data; 36 | 37 | private final String topicExpression; 38 | private final String valueExpression; 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/AttributesUpdateSubscription.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Data; 20 | import org.thingsboard.gateway.service.AttributesUpdateListener; 21 | 22 | /** 23 | * Created by ashvayka on 23.02.17. 24 | */ 25 | @Data 26 | @AllArgsConstructor 27 | public class AttributesUpdateSubscription { 28 | 29 | private String deviceNameFilter; 30 | private AttributesUpdateListener listener; 31 | 32 | public boolean matches(String deviceName) { 33 | return deviceName.matches(deviceNameFilter); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/DeviceData.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Data; 20 | import lombok.RequiredArgsConstructor; 21 | import org.thingsboard.server.common.data.kv.KvEntry; 22 | import org.thingsboard.server.common.data.kv.TsKvEntry; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * Created by ashvayka on 23.01.17. 28 | */ 29 | @Data 30 | @AllArgsConstructor 31 | @RequiredArgsConstructor 32 | public class DeviceData { 33 | 34 | private final String name; 35 | private final String type; 36 | private final List attributes; 37 | private final List telemetry; 38 | private int timeout; 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/DeviceInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 20.01.17. 22 | */ 23 | @Data 24 | public class DeviceInfo { 25 | private final String name; 26 | private final String type; 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/RpcCommandData.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 22.02.17. 22 | */ 23 | @Data 24 | public class RpcCommandData { 25 | 26 | private int requestId; 27 | private String method; 28 | private String params; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/RpcCommandResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 22.02.17. 22 | */ 23 | @Data 24 | public class RpcCommandResponse { 25 | 26 | private int requestId; 27 | private String deviceName; 28 | private String data; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/data/RpcCommandSubscription.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.data; 17 | 18 | import lombok.AllArgsConstructor; 19 | import lombok.Data; 20 | import org.thingsboard.gateway.service.RpcCommandListener; 21 | 22 | /** 23 | * Created by ashvayka on 23.02.17. 24 | */ 25 | @Data 26 | @AllArgsConstructor 27 | public class RpcCommandSubscription { 28 | 29 | private String deviceNameFilter; 30 | private RpcCommandListener listener; 31 | 32 | public boolean matches(String deviceName) { 33 | return deviceName.matches(deviceNameFilter); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/service/update/UpdateService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.service.update; 17 | 18 | public interface UpdateService { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/CertificateInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util; 17 | 18 | import lombok.Data; 19 | 20 | import java.security.KeyPair; 21 | import java.security.cert.X509Certificate; 22 | 23 | /** 24 | * Created by ashvayka on 16.01.17. 25 | */ 26 | @Data 27 | public class CertificateInfo { 28 | 29 | private final X509Certificate certificate; 30 | private final KeyPair keyPair; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/KeystoreConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * Created by ashvayka on 16.01.17. 22 | */ 23 | @Data 24 | public class KeystoreConfiguration { 25 | 26 | private String type; 27 | private String location; 28 | private String fileContent; 29 | private String password; 30 | private String alias; 31 | private String keyPassword; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/converter/AbstractJsonConverter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util.converter; 17 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; 19 | import com.jayway.jsonpath.DocumentContext; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.thingsboard.gateway.extensions.opc.conf.mapping.DeviceMapping; 22 | 23 | import java.util.regex.Matcher; 24 | import java.util.regex.Pattern; 25 | 26 | /** 27 | * Created by ashvayka on 02.03.17. 28 | */ 29 | @Slf4j 30 | public abstract class AbstractJsonConverter { 31 | 32 | protected static final ObjectMapper mapper = new ObjectMapper(); 33 | 34 | protected static String eval(String topic, Pattern pattern, DocumentContext document, String expression) { 35 | if (pattern != null) { 36 | return eval(topic, pattern); 37 | } else { 38 | return eval(document, expression); 39 | } 40 | } 41 | 42 | protected static String eval(DocumentContext document, String expression) { 43 | Matcher matcher = DeviceMapping.TAG_PATTERN.matcher(expression); 44 | String result = expression; 45 | while (matcher.find()) { 46 | String tag = matcher.group(); 47 | String exp = tag.substring(2, tag.length() - 1); 48 | Object tagValue = (Object) apply(document, exp); 49 | if (tagValue != null) { 50 | String tagValueStr = ((Object) apply(document, exp)).toString(); 51 | result = result.replace(tag, tagValueStr); 52 | } else { 53 | continue; 54 | } 55 | } 56 | return result; 57 | } 58 | 59 | protected static Pattern checkAndCompile(Pattern deviceNameTopicPattern, String deviceNameTopicExpression) { 60 | if (deviceNameTopicPattern != null) { 61 | return deviceNameTopicPattern; 62 | } else if (deviceNameTopicExpression != null) { 63 | return Pattern.compile(deviceNameTopicExpression); 64 | } else { 65 | return null; 66 | } 67 | } 68 | 69 | protected static String eval(String topic, Pattern pattern) { 70 | Matcher matcher = pattern.matcher(topic); 71 | while (matcher.find()) { 72 | return matcher.group(); 73 | } 74 | return null; 75 | } 76 | 77 | protected static T apply(DocumentContext document, String expression) { 78 | try { 79 | return document.read(expression); 80 | } catch (RuntimeException e) { 81 | log.error("Failed to apply expression: {}. Reason: {}", expression, e.getMessage(), e); 82 | throw new RuntimeException("Failed to apply expression " + expression); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/converter/AttributesMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util.converter; 17 | 18 | import lombok.Data; 19 | 20 | @Data 21 | public class AttributesMapping extends TransformerKVMapping { 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/converter/TimeseriesMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util.converter; 17 | 18 | import lombok.Data; 19 | 20 | @Data 21 | public class TimeseriesMapping extends TransformerKVMapping { 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/converter/TransformerKVMapping.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util.converter; 17 | 18 | import lombok.Data; 19 | import org.thingsboard.gateway.extensions.common.conf.mapping.KVMapping; 20 | import org.thingsboard.gateway.util.converter.transformer.DataValueTransformer; 21 | 22 | @Data 23 | public class TransformerKVMapping extends KVMapping { 24 | private DataValueTransformer transformer; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/converter/transformer/AbstractDataValueTransformer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util.converter.transformer; 17 | 18 | public abstract class AbstractDataValueTransformer implements DataValueTransformer { 19 | 20 | @Override 21 | public Double transformToDouble(String strValue) { 22 | throw new UnsupportedOperationException(String.format("%s doesn't support transforming to double value", this.getClass().getSimpleName())); 23 | } 24 | 25 | @Override 26 | public Long transformToLong(String strValue) { 27 | throw new UnsupportedOperationException(String.format("%s doesn't support transforming to long value", this.getClass().getSimpleName())); 28 | } 29 | 30 | @Override 31 | public String transformToString(String strValue) { 32 | throw new UnsupportedOperationException(String.format("%s doesn't support transforming to string value", this.getClass().getSimpleName())); 33 | } 34 | 35 | @Override 36 | public Boolean transformToBoolean(String strValue) { 37 | throw new UnsupportedOperationException(String.format("%s doesn't support transforming to boolean value", this.getClass().getSimpleName())); 38 | } 39 | 40 | @Override 41 | public boolean isApplicable(String strValue) { 42 | return true; 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/converter/transformer/DataValueTransformer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util.converter.transformer; 17 | 18 | import com.fasterxml.jackson.annotation.JsonSubTypes; 19 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 20 | 21 | @JsonTypeInfo( 22 | use = JsonTypeInfo.Id.NAME, 23 | include = JsonTypeInfo.As.PROPERTY, 24 | property = "type") 25 | @JsonSubTypes({ 26 | @JsonSubTypes.Type(value = DoubleValueTransformer.class, name = DoubleValueTransformer.INT_TO_DOUBLE_TRANSFORMER_NAME) 27 | }) 28 | public interface DataValueTransformer { 29 | 30 | Double transformToDouble(String strValue); 31 | 32 | Long transformToLong(String strValue); 33 | 34 | String transformToString(String strValue); 35 | 36 | Boolean transformToBoolean(String strValue); 37 | 38 | boolean isApplicable(String strValue); 39 | 40 | } -------------------------------------------------------------------------------- /src/main/java/org/thingsboard/gateway/util/converter/transformer/DoubleValueTransformer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util.converter.transformer; 17 | 18 | 19 | public class DoubleValueTransformer extends AbstractDataValueTransformer { 20 | 21 | static final String INT_TO_DOUBLE_TRANSFORMER_NAME = "intToDouble"; 22 | 23 | private static final int MAX_DOUBLE_VALUE = 65536; 24 | private static final int DIVIDE_POWER = 10; 25 | 26 | @Override 27 | public Double transformToDouble(String strValue) { 28 | Double value = Double.valueOf(strValue); 29 | if (value <= MAX_DOUBLE_VALUE) { 30 | return value / DIVIDE_POWER; 31 | } else { 32 | return (MAX_DOUBLE_VALUE - value) / DIVIDE_POWER; 33 | } 34 | } 35 | 36 | @Override 37 | public Long transformToLong(String strValue) { 38 | return Long.valueOf(strValue); 39 | } 40 | 41 | @Override 42 | public String transformToString(String strValue) { 43 | return strValue; 44 | } 45 | 46 | @Override 47 | public Boolean transformToBoolean(String strValue) { 48 | return Boolean.valueOf(strValue); 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | =================================================== 2 | :: ${application.title} :: ${application.formatted-version} 3 | =================================================== 4 | -------------------------------------------------------------------------------- /src/main/resources/example.der: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guodaxia103/thingsboard-gateway/0b00554cb7c49142a1f88fefa82b1be7e846a8e1/src/main/resources/example.der -------------------------------------------------------------------------------- /src/main/resources/example.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guodaxia103/thingsboard-gateway/0b00554cb7c49142a1f88fefa82b1be7e846a8e1/src/main/resources/example.pfx -------------------------------------------------------------------------------- /src/main/resources/file-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "fileMonitorConfigurations": [ 3 | { 4 | "file": "/tmp/test.dat", 5 | "skipLines": 1, 6 | "updateInterval": 10000, 7 | "csvColumns": [ 8 | "ts", 9 | "device", 10 | "type", 11 | "temperature", 12 | "humidity" 13 | ], 14 | "converter": { 15 | "deviceNameJsonExpression": "${$.device}", 16 | "deviceTypeJsonExpression": "${$.type}", 17 | "attributes": [], 18 | "timeseries": [ 19 | { 20 | "type": "double", 21 | "ts": "${$.ts}", 22 | "tsFormat": "yyyy-MM-dd HH:mm:ss", 23 | "key": "temperature", 24 | "value": "${$.temperature}" 25 | }, 26 | { 27 | "type": "double", 28 | "ts": "${$.ts}", 29 | "tsFormat": "yyyy-MM-dd HH:mm:ss", 30 | "key": "humidity", 31 | "value": "${$.humidity}" 32 | } 33 | ] 34 | } 35 | } 36 | ] 37 | } -------------------------------------------------------------------------------- /src/main/resources/http-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "deviceTypeConfigurations": [ 3 | { 4 | "deviceTypeId": "YOUR_DEVICE_TYPE_ID", 5 | "token": "TOKEN", 6 | "converters": [ 7 | { 8 | "deviceNameJsonExpression": "${$.device}", 9 | "attributes": [ 10 | { 11 | "type": "string", 12 | "key": "lat", 13 | "value": "${$.lat}" 14 | }, 15 | { 16 | "type": "string", 17 | "key": "lng", 18 | "value": "${$.lng}" 19 | } 20 | ], 21 | "timeseries": [ 22 | { 23 | "type": "double", 24 | "key": "temperature", 25 | "value": "${$.data.temperature}", 26 | "transformer": { 27 | "type": "intToDouble" 28 | } 29 | }, 30 | { 31 | "type": "double", 32 | "key": "humidity", 33 | "value": "${$.data.humidity}", 34 | "transformer": { 35 | "type": "intToDouble" 36 | } 37 | } 38 | ] 39 | } 40 | ] 41 | } 42 | ] 43 | } -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/resources/modbusTest.mbs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guodaxia103/thingsboard-gateway/0b00554cb7c49142a1f88fefa82b1be7e846a8e1/src/main/resources/modbusTest.mbs -------------------------------------------------------------------------------- /src/main/resources/opc-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "servers": [ 3 | { 4 | "applicationName": "Thingsboard OPC-UA client", 5 | "applicationUri": "", 6 | "host": "localhost", 7 | "port": 49320, 8 | "scanPeriodInSeconds": 10, 9 | "timeoutInMillis": 5000, 10 | "security": "Basic128Rsa15", 11 | "identity": { 12 | "type": "anonymous" 13 | }, 14 | "keystore": { 15 | "type": "PKCS12", 16 | "location": "example.pfx", 17 | "fileContent": "", 18 | "password": "secret", 19 | "alias": "gateway", 20 | "keyPassword": "secret" 21 | }, 22 | "mapping": [ 23 | { 24 | "deviceNodePattern": "Channel1\\.Device\\d+$", 25 | "deviceNamePattern": "Device ${_System._DeviceId}", 26 | "attributes": [ 27 | { 28 | "key": "Tag1", 29 | "type": "string", 30 | "value": "${Tag1}" 31 | } 32 | ], 33 | "timeseries": [ 34 | { 35 | "key": "Tag2", 36 | "type": "long", 37 | "value": "${Tag2}" 38 | } 39 | ] 40 | } 41 | ] 42 | } 43 | ] 44 | } -------------------------------------------------------------------------------- /src/main/resources/tb-gateway.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | server: 18 | # Server bind address 19 | address: "0.0.0.0" 20 | # Server bind port 21 | port: "9090" 22 | 23 | # Check new version updates parameters 24 | updates: 25 | # Enable/disable updates checking. 26 | enabled: "${UPDATES_ENABLED:true}" 27 | 28 | gateways: 29 | tenants: 30 | - 31 | label: "Tenant" 32 | reporting: 33 | interval: 60000 34 | persistence: 35 | type: file 36 | path: storage 37 | bufferSize: 1000 38 | connection: 39 | host: "${GATEWAY_HOST:localhost}" 40 | port: 1883 41 | retryInterval: 3000 42 | maxInFlight: 1000 43 | security: 44 | accessToken: "${GATEWAY_ACCESS_TOKEN:4BRYl1TuGKoocsSX272F}" 45 | remoteConfiguration: false 46 | extensions: 47 | - 48 | id: "modbus" 49 | type: "MODBUS" 50 | extensionConfiguration: modbusTest-config.json -------------------------------------------------------------------------------- /src/main/scripts/control/deb/postinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | chown -R ${pkg.user}: ${pkg.logFolder} 4 | chown -R ${pkg.user}: ${pkg.installFolder} 5 | update-rc.d ${pkg.name} defaults 6 | -------------------------------------------------------------------------------- /src/main/scripts/control/deb/postrm: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | update-rc.d -f ${pkg.name} remove 4 | -------------------------------------------------------------------------------- /src/main/scripts/control/deb/preinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if ! getent group ${pkg.user} >/dev/null; then 4 | addgroup --system ${pkg.user} 5 | fi 6 | 7 | if ! getent passwd ${pkg.user} >/dev/null; then 8 | adduser --quiet \ 9 | --system \ 10 | --ingroup ${pkg.user} \ 11 | --quiet \ 12 | --disabled-login \ 13 | --disabled-password \ 14 | --home ${pkg.installFolder} \ 15 | --no-create-home \ 16 | -gecos "Thingsboard application" \ 17 | ${pkg.user} 18 | fi 19 | -------------------------------------------------------------------------------- /src/main/scripts/control/deb/prerm: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -e /var/run/${pkg.name}/${pkg.name}.pid ]; then 4 | service ${pkg.name} stop 5 | fi 6 | -------------------------------------------------------------------------------- /src/main/scripts/control/rpm/postinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | chown -R ${pkg.user}: ${pkg.logFolder} 4 | chown -R ${pkg.user}: ${pkg.installFolder} 5 | 6 | if [ $1 -eq 1 ] ; then 7 | # Initial installation 8 | systemctl --no-reload enable ${pkg.name}.service >/dev/null 2>&1 || : 9 | fi 10 | -------------------------------------------------------------------------------- /src/main/scripts/control/rpm/postrm: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ $1 -ge 1 ] ; then 4 | # Package upgrade, not uninstall 5 | systemctl try-restart ${pkg.name}.service >/dev/null 2>&1 || : 6 | fi 7 | -------------------------------------------------------------------------------- /src/main/scripts/control/rpm/preinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | getent group ${pkg.user} >/dev/null || groupadd -r ${pkg.user} 4 | getent passwd ${pkg.user} >/dev/null || \ 5 | useradd -d ${pkg.installFolder} -g ${pkg.user} -M -r ${pkg.user} -s /sbin/nologin \ 6 | -c "Thingsboard application" 7 | -------------------------------------------------------------------------------- /src/main/scripts/control/rpm/prerm: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ $1 -eq 0 ] ; then 4 | # Package removal, not upgrade 5 | systemctl --no-reload disable --now ${pkg.name}.service > /dev/null 2>&1 || : 6 | fi 7 | -------------------------------------------------------------------------------- /src/main/scripts/control/tb-gateway.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=${pkg.name} 3 | After=syslog.target 4 | 5 | [Service] 6 | User=${pkg.user} 7 | ExecStart=${pkg.installFolder}/bin/${pkg.name}.jar 8 | SuccessExitStatus=143 9 | 10 | [Install] 11 | WantedBy=multi-user.target 12 | -------------------------------------------------------------------------------- /src/main/scripts/windows/install.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | setlocal ENABLEEXTENSIONS 4 | 5 | @ECHO Detecting Java version installed. 6 | :CHECK_JAVA_64 7 | @ECHO Detecting if it is 64 bit machine 8 | set KEY_NAME="HKEY_LOCAL_MACHINE\Software\Wow6432Node\JavaSoft\Java Runtime Environment" 9 | set VALUE_NAME=CurrentVersion 10 | 11 | FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( 12 | set ValueName=%%A 13 | set ValueType=%%B 14 | set ValueValue=%%C 15 | ) 16 | @ECHO CurrentVersion %ValueValue% 17 | 18 | SET KEY_NAME="%KEY_NAME:~1,-1%\%ValueValue%" 19 | SET VALUE_NAME=JavaHome 20 | 21 | if defined ValueName ( 22 | FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( 23 | set ValueName2=%%A 24 | set ValueType2=%%B 25 | set JRE_PATH2=%%C 26 | 27 | if defined ValueName2 ( 28 | set ValueName = %ValueName2% 29 | set ValueType = %ValueType2% 30 | set ValueValue = %JRE_PATH2% 31 | ) 32 | ) 33 | ) 34 | 35 | IF NOT "%JRE_PATH2%" == "" GOTO JAVA_INSTALLED 36 | 37 | :CHECK_JAVA_32 38 | @ECHO Detecting if it is 32 bit machine 39 | set KEY_NAME="HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment" 40 | set VALUE_NAME=CurrentVersion 41 | 42 | FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( 43 | set ValueName=%%A 44 | set ValueType=%%B 45 | set ValueValue=%%C 46 | ) 47 | @ECHO CurrentVersion %ValueValue% 48 | 49 | SET KEY_NAME="%KEY_NAME:~1,-1%\%ValueValue%" 50 | SET VALUE_NAME=JavaHome 51 | 52 | if defined ValueName ( 53 | FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( 54 | set ValueName2=%%A 55 | set ValueType2=%%B 56 | set JRE_PATH2=%%C 57 | 58 | if defined ValueName2 ( 59 | set ValueName = %ValueName2% 60 | set ValueType = %ValueType2% 61 | set ValueValue = %JRE_PATH2% 62 | ) 63 | ) 64 | ) 65 | 66 | IF "%JRE_PATH2%" == "" GOTO JAVA_NOT_INSTALLED 67 | 68 | :JAVA_INSTALLED 69 | 70 | @ECHO Java 1.8 found! 71 | @ECHO Installing ${pkg.name} ... 72 | %~dp0${pkg.name}.exe install 73 | 74 | @ECHO DONE. 75 | 76 | GOTO END 77 | 78 | :JAVA_NOT_INSTALLED 79 | @ECHO Java 1.8 or above is not installed 80 | @ECHO Please go to https://java.com/ and install Java. Then retry installation. 81 | PAUSE 82 | GOTO END 83 | 84 | :END 85 | 86 | 87 | -------------------------------------------------------------------------------- /src/main/scripts/windows/service.xml: -------------------------------------------------------------------------------- 1 | 2 | ${pkg.name} 3 | ${project.name} 4 | ${project.description} 5 | %BASE%\conf 6 | ${pkg.winWrapperLogFolder} 7 | rotate 8 | 9 | java 10 | -Dplatform=windows 11 | -jar 12 | %BASE%\lib\${pkg.name}.jar 13 | 14 | -------------------------------------------------------------------------------- /src/main/scripts/windows/uninstall.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | @ECHO Stopping ${pkg.name} ... 4 | net stop ${pkg.name} 5 | 6 | @ECHO Uninstalling ${pkg.name} ... 7 | %~dp0${pkg.name}.exe uninstall 8 | 9 | @ECHO DONE. -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/AbstractGatewayMqttIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway; 17 | 18 | import lombok.extern.slf4j.Slf4j; 19 | import org.junit.After; 20 | import org.junit.Rule; 21 | import org.junit.runner.RunWith; 22 | import org.springframework.boot.test.context.SpringBootTest; 23 | import org.springframework.test.annotation.DirtiesContext; 24 | import org.springframework.test.context.ContextConfiguration; 25 | import org.springframework.test.context.junit4.SpringRunner; 26 | 27 | import java.lang.reflect.Field; 28 | import java.lang.reflect.InvocationTargetException; 29 | import java.util.Arrays; 30 | import java.util.HashSet; 31 | import java.util.Set; 32 | 33 | /** 34 | * Created by Valerii Sosliuk on 5/9/2018. 35 | */ 36 | @Slf4j 37 | @RunWith(SpringRunner.class) 38 | @SpringBootTest() 39 | @ContextConfiguration(classes = {TestConfiguration.class, GatewayConfiguration.class}) 40 | @DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS) 41 | public abstract class AbstractGatewayMqttIntegrationTest { 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/HttpExtensionIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway; 17 | 18 | import org.springframework.http.HttpEntity; 19 | import org.springframework.http.HttpHeaders; 20 | import org.springframework.http.HttpMethod; 21 | import org.springframework.http.MediaType; 22 | import org.springframework.web.client.RestTemplate; 23 | 24 | import java.io.IOException; 25 | import java.nio.file.Files; 26 | import java.nio.file.Paths; 27 | 28 | public class HttpExtensionIntegrationTest { 29 | 30 | private static final String GATEWAY_URL = "http://localhost:9090/sigfox/%s/"; 31 | 32 | public static void main(String[] args) throws IOException { 33 | doPost("YOUR_DEVICE_TYPE_ID", "TOKEN"); 34 | } 35 | 36 | private static void doPost(String deviceTypeId, String token) throws IOException { 37 | HttpHeaders headers = new HttpHeaders(); 38 | headers.add("Authorization", token); 39 | headers.setContentType(MediaType.APPLICATION_JSON); 40 | 41 | String postJson = new String(Files.readAllBytes(Paths.get(String.format("src/test/resources/%s.json", deviceTypeId)))); 42 | new RestTemplate().exchange(String.format(GATEWAY_URL, deviceTypeId), HttpMethod.POST, new HttpEntity<>(postJson, headers), String.class); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/TestConfiguration.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway; 17 | 18 | import org.springframework.context.annotation.Bean; 19 | import org.springframework.context.annotation.ComponentScan; 20 | import org.springframework.context.annotation.Configuration; 21 | import org.thingsboard.gateway.mqtt.TestMqttHandler; 22 | 23 | import java.util.TimeZone; 24 | 25 | /** 26 | * Created by Valerii Sosliuk on 6/12/2018. 27 | */ 28 | @Configuration 29 | @ComponentScan("org.thingsboard.gateway") 30 | public class TestConfiguration { 31 | 32 | @Bean 33 | public TimeZone timeZone(){ 34 | TimeZone defaultTimeZone = TimeZone.getTimeZone("UTC"); 35 | TimeZone.setDefault(defaultTimeZone); 36 | return defaultTimeZone; 37 | } 38 | 39 | @Bean 40 | public TestMqttHandler getMqttHandler() { 41 | return new TestMqttHandler(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/MqttIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt; 17 | 18 | import org.json.JSONException; 19 | import org.junit.After; 20 | import org.junit.Test; 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.thingsboard.gateway.AbstractGatewayMqttIntegrationTest; 23 | import org.thingsboard.gateway.mqtt.simulators.MqttTestClient; 24 | import org.thingsboard.gateway.util.IoUtils; 25 | import org.thingsboard.gateway.util.JsonUtils; 26 | 27 | import java.io.IOException; 28 | import java.util.List; 29 | 30 | import static org.junit.Assert.assertEquals; 31 | import static org.junit.Assert.assertNotNull; 32 | 33 | public class MqttIntegrationTest extends AbstractGatewayMqttIntegrationTest { 34 | 35 | @Autowired 36 | private MqttTestClient deviceSimulator; 37 | 38 | @Autowired 39 | private TestMqttHandler testMqttHandler; 40 | 41 | @Test 42 | public void testSendAndReceiveSimpleJson() throws IOException, InterruptedException, JSONException { 43 | 44 | try { 45 | deviceSimulator.publish("sensor/SN-001/temperature", 46 | IoUtils.getResourceAsString("mqtt/single-value-publish.json").getBytes(), 0); 47 | } catch (Throwable e) { 48 | e.printStackTrace(); 49 | } 50 | Thread.sleep(10000); 51 | List receivedConnectMessage = testMqttHandler.getValues("v1/gateway/connect"); 52 | assertNotNull("receivedConnectMessage was expected to be non-null", receivedConnectMessage); 53 | assertEquals("Only one connect message was expected", 1, receivedConnectMessage.size()); 54 | assertEquals(IoUtils.getResourceAsString("mqtt/connect-SN-001.json"), receivedConnectMessage.get(0)); 55 | 56 | List receivedTelemetryMessage = testMqttHandler.getValues("v1/gateway/telemetry"); 57 | assertNotNull("receivedTelemetryMessage was expected to be non-null", receivedTelemetryMessage); 58 | assertEquals("Only one telemetry message was expected", 1, receivedTelemetryMessage.size()); 59 | 60 | String expectedTelemetryJson = IoUtils.getResourceAsString("mqtt/single-value-result.json"); 61 | String actualTelemetryJson = receivedTelemetryMessage.get(0); 62 | 63 | JsonUtils.assertWithoutTimestamp("SN-001", expectedTelemetryJson, actualTelemetryJson); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/MqttTimestampFormatIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt; 17 | 18 | import org.json.JSONException; 19 | import org.junit.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.ActiveProfiles; 22 | import org.thingsboard.gateway.AbstractGatewayMqttIntegrationTest; 23 | import org.thingsboard.gateway.mqtt.simulators.MqttTestClient; 24 | import org.thingsboard.gateway.util.IoUtils; 25 | import org.thingsboard.gateway.util.JsonUtils; 26 | 27 | import java.io.IOException; 28 | import java.util.List; 29 | 30 | import static org.junit.Assert.assertEquals; 31 | import static org.junit.Assert.assertNotNull; 32 | 33 | @ActiveProfiles("mqtt-timestamp-format") 34 | public class MqttTimestampFormatIntegrationTest extends AbstractGatewayMqttIntegrationTest { 35 | 36 | @Autowired 37 | private MqttTestClient deviceSimulator; 38 | 39 | @Autowired 40 | private TestMqttHandler testMqttHandler; 41 | 42 | @Test 43 | public void testPublishWithTimestampFormat() throws IOException, InterruptedException, JSONException { 44 | try { 45 | deviceSimulator.publish("sensor/SN-001/humidity", 46 | IoUtils.getResourceAsString("mqtt/single-value-with-ts-format-publish.json").getBytes(), 0); 47 | } catch (Throwable e) { 48 | e.printStackTrace(); 49 | } 50 | Thread.sleep(10000); 51 | List receivedConnectMessage = testMqttHandler.getValues("v1/gateway/connect"); 52 | assertNotNull("receivedConnectMessage was expected to be non-null", receivedConnectMessage); 53 | assertEquals("Only one connect message was expected", 1, receivedConnectMessage.size()); 54 | assertEquals(IoUtils.getResourceAsString("mqtt/connect-SN-001.json"), receivedConnectMessage.get(0)); 55 | 56 | List receivedTelemetryMessage = testMqttHandler.getValues("v1/gateway/telemetry"); 57 | assertNotNull("receivedTelemetryMessage was expected to be non-null", receivedTelemetryMessage); 58 | assertEquals("Only one telemetry message was expected", 1, receivedTelemetryMessage.size()); 59 | 60 | String expectedTelemetryJson = IoUtils.getResourceAsString("mqtt/single-value-with-ts-format-result.json"); 61 | String actualTelemetryJson = receivedTelemetryMessage.get(0); 62 | 63 | JsonUtils.assertEquals(expectedTelemetryJson, actualTelemetryJson); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/MqttTimestampIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt; 17 | 18 | import org.json.JSONException; 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | import org.springframework.beans.factory.annotation.Autowired; 22 | import org.springframework.boot.test.context.SpringBootTest; 23 | import org.springframework.test.context.ActiveProfiles; 24 | import org.springframework.test.context.junit4.SpringRunner; 25 | import org.thingsboard.gateway.AbstractGatewayMqttIntegrationTest; 26 | import org.thingsboard.gateway.mqtt.simulators.MqttTestClient; 27 | import org.thingsboard.gateway.util.IoUtils; 28 | import org.thingsboard.gateway.util.JsonUtils; 29 | 30 | import java.io.IOException; 31 | import java.util.List; 32 | 33 | import static org.junit.Assert.assertEquals; 34 | import static org.junit.Assert.assertNotNull; 35 | 36 | @RunWith(SpringRunner.class) 37 | @SpringBootTest() 38 | @ActiveProfiles("mqtt-timestamp") 39 | public class MqttTimestampIntegrationTest extends AbstractGatewayMqttIntegrationTest { 40 | 41 | @Autowired 42 | private MqttTestClient deviceSimulator; 43 | 44 | @Autowired 45 | private TestMqttHandler testMqttHandler; 46 | 47 | @Test 48 | public void testPublishWithTimestamp() throws IOException, InterruptedException, JSONException { 49 | try { 50 | deviceSimulator.publish("sensor/SN-001/temperature", 51 | IoUtils.getResourceAsString("mqtt/single-value-with-ts-publish.json").getBytes(), 0); 52 | } catch (Throwable e) { 53 | e.printStackTrace(); 54 | } 55 | Thread.sleep(10000); 56 | List receivedConnectMessage = testMqttHandler.getValues("v1/gateway/connect"); 57 | assertNotNull("receivedConnectMessage was expected to be non-null", receivedConnectMessage); 58 | assertEquals("Only one connect message was expected", 1, receivedConnectMessage.size()); 59 | assertEquals(IoUtils.getResourceAsString("mqtt/connect-SN-001.json"), receivedConnectMessage.get(0)); 60 | 61 | List receivedTelemetryMessage = testMqttHandler.getValues("v1/gateway/telemetry"); 62 | assertNotNull("receivedTelemetryMessage was expected to be non-null", receivedTelemetryMessage); 63 | assertEquals("Only one telemetry message was expected", 1, receivedTelemetryMessage.size()); 64 | 65 | String expectedTelemetryJson = IoUtils.getResourceAsString("mqtt/single-value-with-ts-result.json"); 66 | String actualTelemetryJson = receivedTelemetryMessage.get(0); 67 | 68 | JsonUtils.assertEquals(expectedTelemetryJson, actualTelemetryJson); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/MqttTwoFiltersIntegrationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt; 17 | 18 | import org.junit.Test; 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | import org.springframework.test.context.ActiveProfiles; 21 | import org.thingsboard.gateway.AbstractGatewayMqttIntegrationTest; 22 | import org.thingsboard.gateway.mqtt.simulators.MqttTestClient; 23 | import org.thingsboard.gateway.util.IoUtils; 24 | import org.thingsboard.gateway.util.JsonUtils; 25 | 26 | import java.util.List; 27 | 28 | import static org.junit.Assert.assertEquals; 29 | import static org.junit.Assert.assertNotNull; 30 | 31 | @ActiveProfiles("mqtt-two-filters") 32 | public class MqttTwoFiltersIntegrationTest extends AbstractGatewayMqttIntegrationTest { 33 | 34 | @Autowired 35 | private MqttTestClient deviceSimulator; 36 | 37 | @Autowired 38 | private TestMqttHandler testMqttHandler; 39 | 40 | @Test 41 | public void testMqttTwoFiltersOnSameTopic() throws Exception { 42 | deviceSimulator.publish("sensor/1110", IoUtils.getResourceAsString("mqtt/mqtt-1110-publish.json").getBytes(), 0); 43 | deviceSimulator.publish("sensor/1111", IoUtils.getResourceAsString("mqtt/mqtt-1111-publish.json").getBytes(), 0); 44 | 45 | Thread.sleep(10000); 46 | List receivedConnectMessages = testMqttHandler.getValues("v1/gateway/connect"); 47 | assertNotNull("recievedConnectMessage was expected to be non-null", receivedConnectMessages); 48 | 49 | assertEquals("Exactly 2 connect messages were expected", 2, receivedConnectMessages.size()); 50 | assertEquals(IoUtils.getResourceAsString("mqtt/connect-1110.json"), receivedConnectMessages.get(0)); 51 | assertEquals(IoUtils.getResourceAsString("mqtt/connect-1111.json"), receivedConnectMessages.get(1)); 52 | 53 | List recievedTelemetryMessages = testMqttHandler.getValues("v1/gateway/telemetry"); 54 | assertNotNull("recievedTelemetryMessage was expected to be non-null", recievedTelemetryMessages); 55 | assertEquals("Exactly 2 telemetry message were expected", 2, recievedTelemetryMessages.size()); 56 | 57 | String expectedTelemetry1110 = IoUtils.getResourceAsString("mqtt/mqtt-1110-result.json"); 58 | String actualTelemetry1110 = recievedTelemetryMessages.get(0); 59 | 60 | JsonUtils.assertWithoutTimestamp("Device 1110", expectedTelemetry1110, actualTelemetry1110); 61 | 62 | String expectedTelemetry1111 = IoUtils.getResourceAsString("mqtt/mqtt-1111-result.json"); 63 | String actualTelemetry1111 = recievedTelemetryMessages.get(1); 64 | 65 | JsonUtils.assertWithoutTimestamp("Device 1111", expectedTelemetry1111, actualTelemetry1111); 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/TestMqttHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt; 17 | 18 | import io.netty.buffer.ByteBuf; 19 | import org.thingsboard.mqtt.MqttHandler; 20 | 21 | import java.nio.charset.StandardCharsets; 22 | import java.util.ArrayList; 23 | import java.util.HashMap; 24 | import java.util.List; 25 | import java.util.Map; 26 | 27 | /** 28 | * Created by Valerii Sosliuk on 5/9/2018. 29 | */ 30 | public class TestMqttHandler implements MqttHandler { 31 | 32 | private Map> payloads; 33 | 34 | public TestMqttHandler() { 35 | payloads = new HashMap<>(); 36 | } 37 | 38 | public void cleanup() { 39 | payloads.clear(); 40 | } 41 | 42 | @Override 43 | public void onMessage(String topic, ByteBuf payload) { 44 | List payloads = this.payloads.get(topic); 45 | if (payloads == null) { 46 | payloads = new ArrayList<>(); 47 | this.payloads.put(topic, payloads); 48 | } 49 | payloads.add(payload.toString(StandardCharsets.UTF_8)); 50 | } 51 | 52 | public List getValues(String topic) { 53 | return payloads.get(topic); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/simulators/MqttBroker.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt.simulators; 17 | 18 | import io.moquette.BrokerConstants; 19 | import io.moquette.server.Server; 20 | import io.moquette.server.config.MemoryConfig; 21 | import lombok.Data; 22 | import org.junit.After; 23 | import org.springframework.beans.factory.annotation.Value; 24 | import org.springframework.core.Ordered; 25 | import org.springframework.core.annotation.Order; 26 | import org.springframework.stereotype.Component; 27 | 28 | import javax.annotation.PostConstruct; 29 | import javax.annotation.PreDestroy; 30 | import java.io.IOException; 31 | import java.util.Properties; 32 | 33 | /** 34 | * Created by Valerii Sosliuk on 5/9/2018. 35 | */ 36 | @Component 37 | @Order(Ordered.HIGHEST_PRECEDENCE) 38 | @Data 39 | public class MqttBroker { 40 | 41 | 42 | @Value("${test.host:localhost}") 43 | private String host; 44 | @Value("${mqtt.broker.external.port:7883}") 45 | private int port; 46 | @Value("${mqtt.broker.external.allowAnonymous:true}") 47 | private boolean allowAnonymous; 48 | 49 | private Server server; 50 | 51 | public MqttBroker() { 52 | 53 | } 54 | 55 | @PostConstruct 56 | public void init() { 57 | server = new Server(); 58 | final Properties configProps = new Properties(); 59 | configProps.put(BrokerConstants.PORT_PROPERTY_NAME, Integer.toString(port)); 60 | configProps.put(BrokerConstants.HOST_PROPERTY_NAME, host); 61 | configProps.put(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, allowAnonymous); 62 | 63 | try { 64 | server.startServer(new MemoryConfig(configProps)); 65 | } catch (IOException e) { 66 | throw new RuntimeException("Unable to initialize MQTT Broker on port [" + port + "]. Reason: " + e.getMessage(), e); 67 | } 68 | } 69 | 70 | @PreDestroy 71 | public void tearDown() { 72 | server.stopServer(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/simulators/MqttTestClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt.simulators; 17 | 18 | import io.netty.buffer.Unpooled; 19 | import io.netty.channel.nio.NioEventLoopGroup; 20 | import io.netty.handler.codec.mqtt.MqttQoS; 21 | import lombok.Data; 22 | import lombok.extern.slf4j.Slf4j; 23 | import org.springframework.beans.factory.annotation.Value; 24 | import org.springframework.core.Ordered; 25 | import org.springframework.core.annotation.Order; 26 | import org.springframework.stereotype.Component; 27 | import org.thingsboard.mqtt.MqttClient; 28 | import org.thingsboard.mqtt.MqttClientConfig; 29 | import org.thingsboard.mqtt.MqttHandler; 30 | 31 | import javax.annotation.PostConstruct; 32 | import javax.annotation.PreDestroy; 33 | import java.util.concurrent.TimeUnit; 34 | 35 | /** 36 | * Created by Valerii Sosliuk on 5/9/2018. 37 | */ 38 | @Slf4j 39 | @Component 40 | @Order(Ordered.HIGHEST_PRECEDENCE + 1) 41 | @Data 42 | public class MqttTestClient { 43 | 44 | private MqttClient mqttClient; 45 | private MqttHandler defaultHandler; 46 | 47 | @Value("${test.host:localhost}") 48 | private String host; 49 | @Value("${mqtt.broker.external.port:7883}") 50 | private int port; 51 | @Value("${mqtt.timeout:10000}") 52 | private long timeout; 53 | 54 | public MqttTestClient(MqttHandler defaultHandler) { 55 | this.defaultHandler = defaultHandler; 56 | } 57 | 58 | @PostConstruct 59 | public void init() { 60 | MqttClientConfig mqttClientConfig = new MqttClientConfig(); 61 | mqttClient = MqttClient.create(mqttClientConfig, defaultHandler); 62 | mqttClient.setEventLoop(new NioEventLoopGroup()); 63 | try { 64 | mqttClient.connect(host, port).get(timeout, TimeUnit.MILLISECONDS); 65 | } catch (Exception e) { 66 | log.error(e.getMessage(), e); 67 | } 68 | } 69 | 70 | public void subscribe(String topic, MqttHandler handler) { 71 | try { 72 | mqttClient.on(topic, handler).get(timeout, TimeUnit.MILLISECONDS); 73 | } catch (Exception e) { 74 | throw new RuntimeException(e); 75 | } 76 | } 77 | 78 | public void publish(String topic, byte[] payload, int qos) { 79 | mqttClient.publish(topic, Unpooled.wrappedBuffer(payload), MqttQoS.valueOf(qos)); 80 | } 81 | 82 | @PreDestroy 83 | public void tearDown() { 84 | mqttClient.disconnect(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/mqtt/simulators/TbSimulator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.mqtt.simulators; 17 | 18 | import org.springframework.beans.factory.annotation.Autowired; 19 | import org.springframework.beans.factory.annotation.Value; 20 | import org.springframework.core.Ordered; 21 | import org.springframework.core.annotation.Order; 22 | import org.springframework.stereotype.Component; 23 | import org.thingsboard.gateway.mqtt.TestMqttHandler; 24 | 25 | import javax.annotation.PostConstruct; 26 | import javax.annotation.PreDestroy; 27 | import java.util.List; 28 | 29 | /** 30 | * Created by Valerii Sosliuk on 6/9/2018. 31 | */ 32 | @Component 33 | @Order(Ordered.HIGHEST_PRECEDENCE + 2) 34 | public class TbSimulator { 35 | 36 | private MqttBroker brokerRule; 37 | private MqttTestClient clientRule; 38 | 39 | 40 | @Value("${test.host:localhost}") 41 | private String host; 42 | @Value("${mqtt.broker.tb.port:7884}") 43 | private int port; 44 | @Value("${mqtt.broker.tb.allowAnonymous:true}") 45 | private boolean allowAnonymous; 46 | @Value("${mqtt.timeout:10000}") 47 | private long timeout; 48 | 49 | @Autowired 50 | private TestMqttHandler mqttHandler; 51 | 52 | public TbSimulator() { 53 | 54 | } 55 | 56 | @PostConstruct 57 | public void init() { 58 | // TODO: replace with bean injection 59 | brokerRule = new MqttBroker(); 60 | brokerRule.setHost(host); 61 | brokerRule.setPort(port); 62 | brokerRule.setAllowAnonymous(true); 63 | brokerRule.init(); 64 | clientRule = new MqttTestClient(mqttHandler); 65 | clientRule.setHost(host); 66 | clientRule.setPort(port); 67 | clientRule.setTimeout(timeout); 68 | clientRule.init(); 69 | clientRule.subscribe("v1/devices/me/attributes/request/1", mqttHandler); 70 | clientRule.subscribe("v1/devices/me/telemetry", mqttHandler); 71 | clientRule.subscribe("v1/gateway/connect", mqttHandler); 72 | clientRule.subscribe("v1/gateway/telemetry", mqttHandler); 73 | } 74 | 75 | @PreDestroy 76 | public void tearDown() { 77 | clientRule.tearDown(); 78 | brokerRule.tearDown(); 79 | } 80 | 81 | public List getValues(String topic) { 82 | return mqttHandler.getValues(topic); 83 | } 84 | 85 | public TestMqttHandler getMqttHandler() { 86 | return mqttHandler; 87 | } 88 | 89 | public void setMqttHandler(TestMqttHandler mqttHandler) { 90 | this.mqttHandler = mqttHandler; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/util/IoUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util; 17 | 18 | import com.google.common.base.Charsets; 19 | import com.google.common.io.Resources; 20 | 21 | import java.io.IOException; 22 | import java.net.URL; 23 | 24 | /** 25 | * Created by Valerii Sosliuk on 5/26/2018. 26 | */ 27 | public class IoUtils { 28 | 29 | public static String getResourceAsString(String path) throws IOException { 30 | URL url = Resources.getResource(path); 31 | return Resources.toString(url, Charsets.UTF_8); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/thingsboard/gateway/util/JsonUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright © 2017 The Thingsboard Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.thingsboard.gateway.util; 17 | 18 | import com.fasterxml.jackson.databind.JsonNode; 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | import org.json.JSONException; 21 | import org.skyscreamer.jsonassert.Customization; 22 | import org.skyscreamer.jsonassert.JSONAssert; 23 | import org.skyscreamer.jsonassert.JSONCompareMode; 24 | import org.skyscreamer.jsonassert.RegularExpressionValueMatcher; 25 | import org.skyscreamer.jsonassert.comparator.CustomComparator; 26 | 27 | import java.io.IOException; 28 | 29 | /** 30 | * Created by Valerii Sosliuk on 6/8/2018. 31 | */ 32 | public class JsonUtils { 33 | 34 | public static JsonNode fromString(String json) throws IOException { 35 | ObjectMapper mapper = new ObjectMapper(); 36 | return mapper.readTree(json); 37 | } 38 | 39 | public static void assertWithoutTimestamp(String deviceName, String expected, String actual) throws JSONException, IOException { 40 | JSONAssert.assertEquals(expected, actual, 41 | new CustomComparator(JSONCompareMode.STRICT, new Customization(deviceName + "[0].ts", 42 | new RegularExpressionValueMatcher<>("\\d+")))); 43 | } 44 | 45 | public static void assertEquals(String expected, String actual) throws JSONException { 46 | JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/resources/58ac4b889058c24616a43b3b.json: -------------------------------------------------------------------------------- 1 | { 2 | "device": "F37D45", 3 | "lat": "20.210656", 4 | "lng": "17.226563", 5 | "time": "123124123512", 6 | "data": { 7 | "temperature": "231", 8 | "humidity": "326" 9 | } 10 | } -------------------------------------------------------------------------------- /src/test/resources/application-mqtt-timestamp-format.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | server: 18 | # Server bind address 19 | address: "0.0.0.0" 20 | # Server bind port 21 | port: "9090" 22 | 23 | # Check new version updates parameters 24 | updates: 25 | # Enable/disable updates checking. 26 | enabled: "${UPDATES_ENABLED:true}" 27 | 28 | gateways: 29 | tenants: 30 | - 31 | label: "Tenant" 32 | reporting: 33 | interval: 60000 34 | persistence: 35 | type: file 36 | path: storage 37 | bufferSize: 1000 38 | connection: 39 | host: "${GATEWAY_HOST:localhost}" 40 | port: 7884 41 | retryInterval: 3000 42 | maxInFlight: 1000 43 | security: 44 | accessToken: "${GATEWAY_ACCESS_TOKEN:YOUR_TOKEN}" 45 | remoteConfiguration: false 46 | extensions: 47 | - 48 | id: "mqtt" 49 | type: "MQTT" 50 | extensionConfiguration: "${MQTT_CONFIG:mqtt-config-timestamp-format.json}" 51 | -------------------------------------------------------------------------------- /src/test/resources/application-mqtt-timestamp.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | server: 18 | # Server bind address 19 | address: "0.0.0.0" 20 | # Server bind port 21 | port: "9090" 22 | 23 | # Check new version updates parameters 24 | updates: 25 | # Enable/disable updates checking. 26 | enabled: "${UPDATES_ENABLED:true}" 27 | 28 | gateways: 29 | tenants: 30 | - 31 | label: "Tenant" 32 | reporting: 33 | interval: 60000 34 | persistence: 35 | type: file 36 | path: storage 37 | bufferSize: 1000 38 | connection: 39 | host: "${GATEWAY_HOST:localhost}" 40 | port: 7884 41 | retryInterval: 3000 42 | maxInFlight: 1000 43 | security: 44 | accessToken: "${GATEWAY_ACCESS_TOKEN:YOUR_TOKEN}" 45 | remoteConfiguration: false 46 | extensions: 47 | - 48 | id: "mqtt" 49 | type: "MQTT" 50 | extensionConfiguration: "${MQTT_CONFIG:mqtt-config-timestamp.json}" 51 | -------------------------------------------------------------------------------- /src/test/resources/application-mqtt-two-filters.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | server: 18 | # Server bind address 19 | address: "0.0.0.0" 20 | # Server bind port 21 | port: "9090" 22 | 23 | # Check new version updates parameters 24 | updates: 25 | # Enable/disable updates checking. 26 | enabled: "${UPDATES_ENABLED:true}" 27 | 28 | gateways: 29 | tenants: 30 | - 31 | label: "Tenant" 32 | reporting: 33 | interval: 60000 34 | persistence: 35 | type: file 36 | path: storage 37 | bufferSize: 1000 38 | connection: 39 | host: "${GATEWAY_HOST:localhost}" 40 | port: 7884 41 | retryInterval: 3000 42 | maxInFlight: 1000 43 | security: 44 | accessToken: "${GATEWAY_ACCESS_TOKEN:YOUR_TOKEN}" 45 | remoteConfiguration: false 46 | extensions: 47 | - 48 | id: "mqtt" 49 | type: "MQTT" 50 | extensionConfiguration: "${MQTT_CONFIG:mqtt-config-two-filters.json}" 51 | -------------------------------------------------------------------------------- /src/test/resources/application-mqtt.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | server: 18 | # Server bind address 19 | address: "0.0.0.0" 20 | # Server bind port 21 | port: "9090" 22 | 23 | # Check new version updates parameters 24 | updates: 25 | # Enable/disable updates checking. 26 | enabled: "${UPDATES_ENABLED:true}" 27 | 28 | gateways: 29 | tenants: 30 | - 31 | label: "Tenant" 32 | reporting: 33 | interval: 60000 34 | persistence: 35 | type: file 36 | path: storage 37 | bufferSize: 1000 38 | connection: 39 | host: "${GATEWAY_HOST:localhost}" 40 | port: 7884 41 | retryInterval: 3000 42 | maxInFlight: 1000 43 | security: 44 | accessToken: "${GATEWAY_ACCESS_TOKEN:YOUR_TOKEN}" 45 | remoteConfiguration: false 46 | extensions: 47 | - 48 | id: "mqtt" 49 | type: "MQTT" 50 | extensionConfiguration: "${MQTT_CONFIG:mqtt-config.json}" 51 | -------------------------------------------------------------------------------- /src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright © 2017 The Thingsboard Authors 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # 16 | 17 | server: 18 | # Server bind address 19 | address: "0.0.0.0" 20 | # Server bind port 21 | port: "9090" 22 | 23 | # Check new version updates parameters 24 | updates: 25 | # Enable/disable updates checking. 26 | enabled: "${UPDATES_ENABLED:true}" 27 | 28 | gateways: 29 | tenants: 30 | - 31 | label: "Tenant" 32 | reporting: 33 | interval: 60000 34 | persistence: 35 | type: file 36 | path: storage 37 | bufferSize: 1000 38 | connection: 39 | host: "${GATEWAY_HOST:localhost}" 40 | port: 7884 41 | retryInterval: 3000 42 | maxInFlight: 1000 43 | security: 44 | accessToken: "${GATEWAY_ACCESS_TOKEN:YOUR_TOKEN}" 45 | remoteConfiguration: false 46 | extensions: 47 | - 48 | id: "mqtt" 49 | type: "MQTT" 50 | extensionConfiguration: "${MQTT_CONFIG:mqtt-config.json}" 51 | 52 | test: 53 | host: localhost 54 | mqtt: 55 | broker: 56 | external: 57 | port: 7883 58 | allowAnonymous: true 59 | tb: 60 | port: 7884 61 | allowAnonymous: true 62 | timeout: 10000 -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | 21 | 22 | 23 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/test/resources/mqtt-config-timestamp-format.json: -------------------------------------------------------------------------------- 1 | { 2 | "brokers": [ 3 | { 4 | "host": "localhost", 5 | "port": 7883, 6 | "ssl": false, 7 | "retryInterval": 3000, 8 | "credentials": { 9 | "type": "anonymous" 10 | }, 11 | "mapping": [ 12 | { 13 | "topicFilter": "sensor/+/temperature", 14 | "converter": { 15 | "type": "json", 16 | "filterExpression": "", 17 | "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/temperature)", 18 | "timeout": 60000, 19 | "timeseries": [ 20 | { 21 | "type": "double", 22 | "key": "temperature", 23 | "value": "${$.value}", 24 | "ts": "${$.timestamp}" 25 | } 26 | ] 27 | } 28 | }, 29 | { 30 | "topicFilter": "sensor/+/humidity", 31 | "converter": { 32 | "type": "json", 33 | "filterExpression": "", 34 | "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/humidity)", 35 | "timeout": 60000, 36 | "timeseries": [ 37 | { 38 | "type": "double", 39 | "key": "humidity", 40 | "value": "${$.value}", 41 | "ts": "${$.timestamp}", 42 | "tsFormat": "yyyy-MM-dd HH:mm:ss.SSS" 43 | } 44 | ] 45 | } 46 | } 47 | ] 48 | } 49 | ] 50 | } -------------------------------------------------------------------------------- /src/test/resources/mqtt-config-timestamp.json: -------------------------------------------------------------------------------- 1 | { 2 | "brokers": [ 3 | { 4 | "host": "localhost", 5 | "port": 7883, 6 | "ssl": false, 7 | "retryInterval": 3000, 8 | "credentials": { 9 | "type": "anonymous" 10 | }, 11 | "mapping": [ 12 | { 13 | "topicFilter": "sensor/+/temperature", 14 | "converter": { 15 | "type": "json", 16 | "filterExpression": "", 17 | "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/temperature)", 18 | "timeout": 60000, 19 | "timeseries": [ 20 | { 21 | "type": "double", 22 | "key": "temperature", 23 | "value": "${$.value}", 24 | "ts": "${$.timestamp}" 25 | } 26 | ] 27 | } 28 | }, 29 | { 30 | "topicFilter": "sensor/+/humidity", 31 | "converter": { 32 | "type": "json", 33 | "filterExpression": "", 34 | "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/humidity)", 35 | "timeout": 60000, 36 | "timeseries": [ 37 | { 38 | "type": "double", 39 | "key": "humidity", 40 | "value": "${$.value}", 41 | "ts": "${$.timestamp}", 42 | "tsFormat": "yyyy-MM-dd HH:mm:ss.SSS" 43 | } 44 | ] 45 | } 46 | } 47 | ] 48 | } 49 | ] 50 | } -------------------------------------------------------------------------------- /src/test/resources/mqtt-config-two-filters.json: -------------------------------------------------------------------------------- 1 | { 2 | "brokers": [ 3 | { 4 | "host": "localhost", 5 | "port": 7883, 6 | "ssl": false, 7 | "retryInterval": 3000, 8 | "credentials": { 9 | "type": "anonymous" 10 | }, 11 | "mapping": [ 12 | { 13 | "topicFilter": "sensor/1111", 14 | "converter": { 15 | "type": "json", 16 | "filterExpression": "[?(@.tsmId == 1111)]", 17 | "deviceNameJsonExpression": "${$.tsmTuid}", 18 | "attributes": [ 19 | { 20 | "type": "string", 21 | "key": "Gateway", 22 | "value": "${$.tsmGw}" 23 | } 24 | ], 25 | "timeseries": [ 26 | { 27 | "type": "double", 28 | "key": "tsmId", 29 | "value": "${$.tsmId}" 30 | }, 31 | { 32 | "type": "double", 33 | "key": "lght", 34 | "value": "${$.lght}" 35 | }, 36 | { 37 | "type": "double", 38 | "key": "humd", 39 | "value": "${$.humd}" 40 | }, 41 | { 42 | "type": "double", 43 | "key": "airp", 44 | "value": "${$.airp}" 45 | }, 46 | { 47 | "type": "double", 48 | "key": "temp", 49 | "value": "${$.temp}" 50 | } 51 | ] 52 | } 53 | }, 54 | { 55 | "topicFilter": "sensor/1110", 56 | "converter": { 57 | "type": "json", 58 | "filterExpression": "[?(@.tsmId == 1110)]", 59 | "deviceNameJsonExpression": "${$.tsmTuid}", 60 | "attributes": [ 61 | { 62 | "type": "string", 63 | "key": "Gateway", 64 | "value": "${$.tsmGw}" 65 | } 66 | ], 67 | "timeseries": [ 68 | { 69 | "type": "double", 70 | "key": "Activity level", 71 | "value": "${$.activityLevel}" 72 | }, 73 | { 74 | "type": "double", 75 | "key": "Energy level", 76 | "value": "${$.energyLevel}" 77 | }, 78 | { 79 | "type": "double", 80 | "key": "Battery level", 81 | "value": "${$.batl}" 82 | } 83 | ] 84 | } 85 | } 86 | ] 87 | } 88 | ] 89 | } -------------------------------------------------------------------------------- /src/test/resources/mqtt-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "brokers": [ 3 | { 4 | "host": "localhost", 5 | "port": 7883, 6 | "ssl": false, 7 | "retryInterval": 3000, 8 | "credentials": { 9 | "type": "anonymous" 10 | }, 11 | "mapping": [ 12 | { 13 | "topicFilter": "sensor/+/temperature", 14 | "converter": { 15 | "type": "json", 16 | "filterExpression": "", 17 | "deviceNameTopicExpression": "(?<=sensor\/)(.*?)(?=\/temperature)", 18 | "timeout": 60000, 19 | "timeseries": [ 20 | { 21 | "type": "double", 22 | "key": "temperature", 23 | "value": "${$.value}" 24 | } 25 | ] 26 | } 27 | } 28 | ] 29 | } 30 | ] 31 | } -------------------------------------------------------------------------------- /src/test/resources/mqtt/connect-1110.json: -------------------------------------------------------------------------------- 1 | {"device":"Device 1110","type":null} -------------------------------------------------------------------------------- /src/test/resources/mqtt/connect-1111.json: -------------------------------------------------------------------------------- 1 | {"device":"Device 1111","type":null} -------------------------------------------------------------------------------- /src/test/resources/mqtt/connect-SN-001.json: -------------------------------------------------------------------------------- 1 | {"device":"SN-001","type":null} -------------------------------------------------------------------------------- /src/test/resources/mqtt/mqtt-1110-publish.json: -------------------------------------------------------------------------------- 1 | {"tsmId":1110, "tsmTuid": "Device 1110", "activityLevel":2, "energyLevel":3, "batl": 4} -------------------------------------------------------------------------------- /src/test/resources/mqtt/mqtt-1110-result.json: -------------------------------------------------------------------------------- 1 | {"Device 1110":[{"ts":1,"values":{"Activity level":2.0,"Energy level":3.0,"Battery level":4.0}}]} -------------------------------------------------------------------------------- /src/test/resources/mqtt/mqtt-1111-publish.json: -------------------------------------------------------------------------------- 1 | {"tsmId":1111, "tsmTuid": "Device 1111", "lght":100.1, "humd":98.5, "airp": 100, "temp":25} -------------------------------------------------------------------------------- /src/test/resources/mqtt/mqtt-1111-result.json: -------------------------------------------------------------------------------- 1 | {"Device 1111":[{"ts":1528603026257,"values":{"tsmId":1111.0,"lght":100.1,"humd":98.5,"airp":100.0,"temp":25.0}}]} -------------------------------------------------------------------------------- /src/test/resources/mqtt/single-value-publish.json: -------------------------------------------------------------------------------- 1 | {"value":"36.6"} -------------------------------------------------------------------------------- /src/test/resources/mqtt/single-value-result.json: -------------------------------------------------------------------------------- 1 | {"SN-001":[{"ts":1,"values":{"temperature":36.6}}]} -------------------------------------------------------------------------------- /src/test/resources/mqtt/single-value-with-ts-format-publish.json: -------------------------------------------------------------------------------- 1 | {"timestamp":"2018-06-10 13:17:37.789", "value":"0.68"} -------------------------------------------------------------------------------- /src/test/resources/mqtt/single-value-with-ts-format-result.json: -------------------------------------------------------------------------------- 1 | {"SN-001":[{"ts":1528636657789,"values":{"humidity":0.68}}]} -------------------------------------------------------------------------------- /src/test/resources/mqtt/single-value-with-ts-publish.json: -------------------------------------------------------------------------------- 1 | {"timestamp":"1528647824155", "value":"36.6"} -------------------------------------------------------------------------------- /src/test/resources/mqtt/single-value-with-ts-result.json: -------------------------------------------------------------------------------- 1 | {"SN-001":[{"ts":1528647824155,"values":{"temperature":36.6}}]} --------------------------------------------------------------------------------