Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Browan codec example #55

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
114 changes: 114 additions & 0 deletions lora-codec-browan/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?xml version="1.0"?>
<!--
~ Copyright (c) 2012-2020 Cumulocity GmbH
~ Copyright (c) 2022 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA,
~ and/or its subsidiaries and/or its affiliates and/or their licensors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>c8y.example</groupId>
<artifactId>c8y-examples</artifactId>
<version>${revision}${changelist}</version>
</parent>
<artifactId>lora-codec-browan</artifactId>
<name>lora-codec-browan</name>
<url>http://maven.apache.org</url>
<properties>
<spring-boot-dependencies.version>2.3.3.RELEASE</spring-boot-dependencies.version>
<microservice.name>lora-codec-browan</microservice.name>
<main.class>lora.Application</main.class>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>microservice-dependencies</artifactId>
<version>${revision}${changelist}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>lpwan-custom-codec</artifactId>
<version>${c8y.version}</version>
</dependency>
<dependency>
<groupId>com.nsn.cumulocity.model</groupId>
<artifactId>device-capability-model</artifactId>
<version>${c8y.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-dependencies.version}</version>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.nsn.cumulocity.clients-java</groupId>
<artifactId>microservice-package-maven-plugin</artifactId>
<version>${c8y.version}</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>package</goal>
</goals>
<configuration>
<name>${microservice.name}</name>
<image>${microservice.name}</image>
<encoding>UTF-8</encoding>
<perm>
<min>92m</min>
<max>172m</max>
</perm>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>cumulocity</id>
<layout>default</layout>
<url>http://download.cumulocity.com/maven/repository</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<url>http://download.cumulocity.com/maven/repository</url>
</pluginRepository>
</pluginRepositories>
</project>
13 changes: 13 additions & 0 deletions lora-codec-browan/src/main/configuration/cumulocity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"apiVersion":"1",
"version":"@project.version@",
"contextPath": "lora-codec-browan",
"provider": {
"name":"Software AG"
},
"isolation":"MULTI_TENANT",
"requiredRoles": [
"ROLE_INVENTORY_ADMIN",
"ROLE_INVENTORY_READ"
]
}
29 changes: 29 additions & 0 deletions lora-codec-browan/src/main/java/lora/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2012-2020 Cumulocity GmbH
* Copyright (c) 2022 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA,
* and/or its subsidiaries and/or its affiliates and/or their licensors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package lora;

import com.cumulocity.microservice.lpwan.codec.annotation.CodecMicroserviceApplication;
import org.springframework.boot.SpringApplication;

@CodecMicroserviceApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
25 changes: 25 additions & 0 deletions lora-codec-browan/src/main/java/lora/codec/browan/BrowanCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lora.codec.browan;

import java.util.Collections;
import java.util.Set;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

import com.cumulocity.microservice.lpwan.codec.Codec;
import com.cumulocity.microservice.lpwan.codec.model.DeviceInfo;

@Component
public class BrowanCodec implements Codec {

@Override
public @NotNull @NotEmpty Set<DeviceInfo> supportsDevices() {

DeviceInfo airQualitySensor = new DeviceInfo("Browan", "TBHV110");
return Collections.singleton(airQualitySensor);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package lora.codec.browan.decoder;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.stereotype.Component;

import com.cumulocity.microservice.customdecoders.api.exception.DecoderServiceException;
import com.cumulocity.microservice.customdecoders.api.model.DecoderResult;
import com.cumulocity.microservice.customdecoders.api.model.MeasurementDto;
import com.cumulocity.microservice.customdecoders.api.model.MeasurementValueDto;
import com.cumulocity.microservice.customdecoders.api.service.DecoderService;
import com.cumulocity.microservice.lpwan.codec.decoder.model.LpwanDecoderInputData;
import com.cumulocity.model.event.CumulocitySeverities;
import com.cumulocity.model.idtype.GId;
import com.cumulocity.rest.representation.alarm.AlarmRepresentation;
import com.cumulocity.rest.representation.inventory.ManagedObjects;
import com.google.common.io.BaseEncoding;

@Component
public class BrowanDecoder implements DecoderService {

@Override
public DecoderResult decode(String inputData, GId deviceId, Map<String, String> args) throws DecoderServiceException {
LpwanDecoderInputData decoderInput = new LpwanDecoderInputData(inputData, deviceId, args);
ByteBuffer buffer = ByteBuffer.wrap(BaseEncoding.base16().decode(inputData.toUpperCase()));
DecoderResult result = new DecoderResult();

if(decoderInput.getFport() == 103) {
buffer.get();
buffer.get();
int pcbTemperature = Byte.toUnsignedInt(buffer.get()) - 32;
result.addMeasurement(measurement(decoderInput, "pcb_Temperature", "T", pcbTemperature, "C"));
int humidity = Byte.toUnsignedInt(buffer.get());
result.addMeasurement(measurement(decoderInput, "c8y_Humidity", "H", humidity, "%"));
int co2 = Short.toUnsignedInt(Short.reverseBytes(buffer.getShort()));
result.addMeasurement(measurement(decoderInput, "CO2", "C", co2, "ppm"));
int voc = Short.toUnsignedInt(Short.reverseBytes(buffer.getShort()));
result.addMeasurement(measurement(decoderInput, "VOC", "V", voc, "ppm"));
int iaq = Short.toUnsignedInt(Short.reverseBytes(buffer.getShort()));
result.addMeasurement(measurement(decoderInput, "IAQ", "Q", iaq, "Q"));
int envTemperature = Byte.toUnsignedInt(buffer.get()) - 32;
result.addMeasurement(measurement(decoderInput, "c8y_Temperature", "T", envTemperature, "C"));

if(iaq > 100) {
result.addAlarm(alarm(deviceId, "AirQualityWarning", CumulocitySeverities.WARNING,
String.format("Air quality indicator > 100. Actual value: %d. Please open window", iaq)), true);
}
} else {
result.addAlarm(alarm(deviceId, "DecoderError", CumulocitySeverities.CRITICAL, String.format("Unknown port: %s", decoderInput.getFport())), true);
}

return result;
}

private AlarmRepresentation alarm(GId deviceId, String alarmType, CumulocitySeverities severity, String alarmText) {
AlarmRepresentation alarm = new AlarmRepresentation();
alarm.setSource(ManagedObjects.asManagedObject(deviceId));
alarm.setType(alarmType);
alarm.setSeverity(severity.name());
alarm.setText(alarmText);
alarm.setDateTime(DateTime.now());
return alarm;
}

private MeasurementDto measurement(LpwanDecoderInputData decoderInput, String fragmentName, String seriesName, long v, String unit) {
MeasurementDto m = new MeasurementDto();
m.setType(fragmentName);
m.setSeries(fragmentName);
MeasurementValueDto value = new MeasurementValueDto();
value.setSeriesName(seriesName);
value.setValue(BigDecimal.valueOf(v));
value.setUnit(unit);
m.setValues(Collections.singletonList(value));
m.setTime(new DateTime(decoderInput.getUpdateTime() != null?decoderInput.getUpdateTime() : System.currentTimeMillis()));
return m;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lora.codec.browan.encoder;

import org.springframework.stereotype.Component;

import com.cumulocity.microservice.customencoders.api.exception.EncoderServiceException;
import com.cumulocity.microservice.customencoders.api.model.EncoderInputData;
import com.cumulocity.microservice.customencoders.api.model.EncoderResult;
import com.cumulocity.microservice.customencoders.api.service.EncoderService;
import com.cumulocity.microservice.lpwan.codec.encoder.model.LpwanEncoderResult;

@Component
public class BrowanEncoder implements EncoderService {

@Override
public EncoderResult encode(EncoderInputData arg0) throws EncoderServiceException {
LpwanEncoderResult encoderResult = new LpwanEncoderResult();
encoderResult.setSuccess(false);
encoderResult.setMessage("Encoding Payload Failed");
return encoderResult;
}

}
18 changes: 18 additions & 0 deletions lora-codec-browan/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2012-2020 Cumulocity GmbH
# Copyright (c) 2022 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA,
# and/or its subsidiaries and/or its affiliates and/or their licensors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
application.name=lora-codec-browan
53 changes: 53 additions & 0 deletions lora-codec-browan/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2012-2020 Cumulocity GmbH
~ Copyright (c) 2022 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA,
~ and/or its subsidiaries and/or its affiliates and/or their licensors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

<appender name="ROLLING"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>

<!-- keep 30 days' worth of history capped at 1GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>

<logger name="com.cumulocity.microservice.security.filter.provider.SpringSecurityContextProvider" level="ERROR"/>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.apache.commons.httpclient" level="INFO"/>
<logger name="httpclient.wire" level="WARN"/>
<logger name="com.softwareag.dtintegration" level="INFO"/>
<logger name="com.cumulocity" level="INFO"/>

<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING"/>
</root>

</configuration>