Skip to content

Commit

Permalink
kaa-1221 second review first part
Browse files Browse the repository at this point in the history
  • Loading branch information
maxml committed Jul 23, 2016
1 parent 0407a87 commit 1de9622
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 66 deletions.
127 changes: 68 additions & 59 deletions doc/Programming-guide/Your-first-Kaa-application/index.md
Expand Up @@ -438,40 +438,30 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
* Class implement functionality for First Kaa application
*
* @author Maksym Liashenko
* Class implement functionality for First Kaa application. Application send temperature data
* from the Kaa endpoint with required configured sampling period
*/
public class FirstDemo {
public class FirstKaaDemo {
private static final long DEFAULT_SAMPLE_TIME = 1000L;
private static final long DEFAULT_START_DELAY = 1000L;
private static final Logger LOG = LoggerFactory.getLogger(FirstKaaDemo.class);
private static final Logger LOG = LoggerFactory.getLogger(FirstDemo.class);
private static Timer timer = new Timer();
private static KaaClient kaaClient;
private static ScheduledFuture<?> scheduledFuture;
private static ScheduledExecutorService scheduledExecutorService;
public static void main(String[] args) {
LOG.info("Kaa First Demo app starting!");
startKaaClient();
LOG.info(FirstKaaDemo.class.getSimpleName() + " app starting!");
LOG.info("--= Press any key to exit =--");
try {
System.in.read();
} catch (IOException e) {
LOG.error("IOException has occurred: " + e.getMessage());
}
LOG.info("Stopping...");
timer.cancel();
stopKaaClient();
}
private static void startKaaClient() {
scheduledExecutorService = Executors.newScheduledThreadPool(1);
/*
* Create the Kaa desktop context for the application.
*/
Expand All @@ -481,26 +471,14 @@ public class FirstDemo {
* Create a Kaa client and add a listener which displays the Kaa client
* configuration as soon as the Kaa client is started.
*/
kaaClient = Kaa.newClient(desktopKaaPlatformContext, new SimpleKaaClientStateListener() {
@Override
public void onStarted() {
super.onStarted();
LOG.info("Kaa client started");
Configuration configuration = kaaClient.getConfiguration();
LOG.info("Default sample period: " + configuration.getSamplePeriod());
onKaaStarted(configuration.getSamplePeriod() * 1000L);
}
}, true);
kaaClient = Kaa.newClient(desktopKaaPlatformContext, new FirstKaaClientStateListener(), true);
/*
* Used by log collector on each adding of the new log record in order to check whether to send logs to server.
* Start log upload when there is records in storage.
* Start log upload when there is at least one record in storage.
*/
RecordCountLogUploadStrategy strategy = new RecordCountLogUploadStrategy(1);
strategy.setMaxParrelelUpload(1);
strategy.setMaxParallelUploads(1);
kaaClient.setLogUploadStrategy(strategy);
/*
Expand All @@ -512,28 +490,30 @@ public class FirstDemo {
kaaClient.addConfigurationListener(new ConfigurationListener() {
@Override
public void onConfigurationUpdate(Configuration configuration) {
LOG.info("Received configuration data. New sample period: " + configuration.getSamplePeriod());
onChangedConfiguration(configuration.getSamplePeriod() * 1000L);
LOG.info("Received configuration data. New sample period: {}", configuration.getSamplePeriod());
onChangedConfiguration(TimeUnit.SECONDS.toMillis(configuration.getSamplePeriod()));
}
});
/*
* Start the Kaa client and connect it to the Kaa server.
*/
kaaClient.start();
}
private static void addTemperatureRecord(DataCollection data) {
kaaClient.addLogRecord(data);
}
LOG.info("--= Press any key to exit =--");
try {
System.in.read();
} catch (IOException e) {
LOG.error("IOException has occurred: {}", e.getMessage());
}
LOG.info("Stopping...");
private static void stopKaaClient() {
scheduledExecutorService.shutdown();
kaaClient.stop();
}
/*
* Method, that emaulate getting temperature from real sensor.
* Method, that emulate getting temperature from real sensor.
* Retrieves random temperature.
*/
private static int getTemperatureRand() {
Expand All @@ -542,29 +522,58 @@ public class FirstDemo {
private static void onKaaStarted(long time) {
if (time == 0) {
time = DEFAULT_SAMPLE_TIME;
time = DEFAULT_START_DELAY;
}
timer.schedule(new TemperatureTimerTask(), DEFAULT_SAMPLE_TIME, time);
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
int temperature = getTemperatureRand();
kaaClient.addLogRecord(new DataCollection(temperature));
LOG.info("Sampled Temperature: {}", temperature);
}
}, DEFAULT_START_DELAY, time, TimeUnit.MILLISECONDS);
}
private static void onChangedConfiguration(long time) {
if (time == 0) {
time = DEFAULT_SAMPLE_TIME;
time = DEFAULT_START_DELAY;
}
timer.cancel();
scheduledFuture.cancel(false);
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(
new Runnable() {
timer = new Timer();
timer.schedule(new TemperatureTimerTask(), DEFAULT_SAMPLE_TIME, time);
@Override
public void run() {
int temperature = getTemperatureRand();
kaaClient.addLogRecord(new DataCollection(temperature));
LOG.info("Sampled Temperature: {}", temperature);
}
}, DEFAULT_START_DELAY, time, TimeUnit.MILLISECONDS);
}
private static class TemperatureTimerTask extends TimerTask {
private static class FirstKaaClientStateListener extends SimpleKaaClientStateListener {
@Override
public void run() {
int temperature = getTemperatureRand();
addTemperatureRecord(new DataCollection(temperature));
public void onStarted() {
super.onStarted();
LOG.info("Kaa client started");
Configuration configuration = kaaClient.getConfiguration();
LOG.info("Default sample period: {}", configuration.getSamplePeriod());
LOG.info("Sampled Temperature: " + temperature);
onKaaStarted(TimeUnit.SECONDS.toMillis(configuration.getSamplePeriod()));
}
@Override
public void onStopped() {
super.onStopped();
LOG.info("Kaa client stopped");
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions pom.xml
Expand Up @@ -30,6 +30,8 @@ Copyright 2014-2016 CyberVision, Inc.
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<main.dir>${basedir}</main.dir>
<avro-ui.version>0.2.1</avro-ui.version>

<akka.version>2.4.1</akka.version>
<spring.version>4.2.5.RELEASE</spring.version>
<spring.security.version>3.2.9.RELEASE</spring.security.version>
Expand Down Expand Up @@ -82,7 +84,6 @@ Copyright 2014-2016 CyberVision, Inc.
<jackson-mapper-asl.version>1.9.12</jackson-mapper-asl.version>
<jackson-core-asl.version>1.9.13</jackson-core-asl.version>
<jline.version>2.11</jline.version>
<avro-ui.version>0.2.1</avro-ui.version>

<slf4j.version>1.7.7</slf4j.version>
<slf4j-android.version>1.7.7</slf4j-android.version>
Expand Down Expand Up @@ -815,7 +816,7 @@ Copyright 2014-2016 CyberVision, Inc.
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
Expand Down Expand Up @@ -896,7 +897,7 @@ Copyright 2014-2016 CyberVision, Inc.
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>${spring.retry.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>java-client</artifactId>
Expand Down Expand Up @@ -1039,7 +1040,7 @@ Copyright 2014-2016 CyberVision, Inc.
<artifactId>kvstore</artifactId>
<version>${oracle-kvstore.version}</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
Expand Down Expand Up @@ -1368,8 +1369,8 @@ Copyright 2014-2016 CyberVision, Inc.
</tags>
</configuration>
</plugin>
<!-- uncomment when all licence headers will be added <plugin>
<groupId>org.apache.rat</groupId> <artifactId>apache-rat-plugin</artifactId>
<!-- uncomment when all licence headers will be added <plugin>
<groupId>org.apache.rat</groupId> <artifactId>apache-rat-plugin</artifactId>
<version>${rat.version}</version> </plugin> -->
</plugins>
</reporting>
Expand All @@ -1388,7 +1389,7 @@ Copyright 2014-2016 CyberVision, Inc.
<id>central</id>
<url>http://repo1.maven.org/maven2/</url>
</repository>
<repository>
<repository>
<id>twitter-twttr</id>
<url>http://maven.twttr.com/</url>
</repository>
Expand Down

0 comments on commit 1de9622

Please sign in to comment.