我创建了Kaa平台的单个节点,在此链接后创建了我的第一个Kaa应用程序: " https://kaaproject.github.io/kaa/docs/v0.10.0/Programming-guide/Your-first-Kaa-application/" 现在在eclipse中,我用Java导入生成的SDK并创建了一个类似的应用程序:
import org.kaaproject.kaa.client.DesktopKaaPlatformContext;
import org.kaaproject.kaa.client.Kaa;
import org.kaaproject.kaa.client.KaaClient;
import org.kaaproject.kaa.client.SimpleKaaClientStateListener;
import org.kaaproject.kaa.client.configuration.base.ConfigurationListener;
import org.kaaproject.kaa.client.configuration.base.SimpleConfigurationStorage;
import org.kaaproject.kaa.client.logging.strategies.RecordCountLogUploadStrategy;
import org.kaaproject.kaa.schema.sample.Configuration;
import org.kaaproject.kaa.schema.sample.DataCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Random;
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. Application send temperature data
* from the Kaa endpoint with required configured sampling period
*/
public class FirstKaaDemo {
private static final long DEFAULT_START_DELAY = 1000L;
private static final Logger LOG = LoggerFactory.getLogger(FirstKaaDemo.class);
private static KaaClient kaaClient;
private static ScheduledFuture<?> scheduledFuture;
private static ScheduledExecutorService scheduledExecutorService;
public static void main(String[] args) {
LOG.info(FirstKaaDemo.class.getSimpleName() + " app starting!");
scheduledExecutorService = Executors.newScheduledThreadPool(1);
//Create the Kaa desktop context for the application.
DesktopKaaPlatformContext desktopKaaPlatformContext = new DesktopKaaPlatformContext();
/*
* 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 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 at least one record in storage.
*/
RecordCountLogUploadStrategy strategy = new RecordCountLogUploadStrategy(1);
strategy.setMaxParallelUploads(1);
kaaClient.setLogUploadStrategy(strategy);
/*
* Persist configuration in a local storage to avoid downloading it each
* time the Kaa client is started.
*/
kaaClient.setConfigurationStorage(new SimpleConfigurationStorage(desktopKaaPlatformContext, "saved_config.cfg"));
kaaClient.addConfigurationListener(new ConfigurationListener() {
@Override
public void onConfigurationUpdate(Configuration configuration) {
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();
LOG.info("--= Press any key to exit =--");
try {
System.in.read();
} catch (IOException e) {
LOG.error("IOException has occurred: {}", e.getMessage());
}
LOG.info("Stopping...");
scheduledExecutorService.shutdown();
kaaClient.stop();
}
/*
* Method, that emulate getting temperature from real sensor.
* Retrieves random temperature.
*/
private static int getTemperatureRand() {
return new Random().nextInt(10) + 25;
}
private static void onKaaStarted(long time) {
if (time <= 0) {
LOG.error("Wrong time is used. Please, check your configuration!");
kaaClient.stop();
System.exit(0);
}
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
int temperature = getTemperatureRand();
kaaClient.addLogRecord(new DataCollection(temperature));
LOG.info("Sampled Temperature: {}", temperature);
}
}, 0, time, TimeUnit.MILLISECONDS);
}
private static void onChangedConfiguration(long time) {
if (time == 0) {
time = DEFAULT_START_DELAY;
}
scheduledFuture.cancel(false);
scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
int temperature = getTemperatureRand();
kaaClient.addLogRecord(new DataCollection(temperature));
LOG.info("Sampled Temperature: {}", temperature);
}
}, 0, time, TimeUnit.MILLISECONDS);
}
private static class FirstKaaClientStateListener extends SimpleKaaClientStateListener {
@Override
public void onStarted() {
super.onStarted();
LOG.info("Kaa client started");
Configuration configuration = kaaClient.getConfiguration();
LOG.info("Default sample period: {}", configuration.getSamplePeriod());
onKaaStarted(TimeUnit.SECONDS.toMillis(configuration.getSamplePeriod()));
}
@Override
public void onStopped() {
super.onStopped();
LOG.info("Kaa client stopped");
}
}
}
现在它在DataCollection和Configuration类中遇到了一些问题。我甚至导入了从CTL =&gt; Export =&gt; Java Library下载的DataCollection和Configuration的.jar文件,但没有任何改变。 例如,其中一个错误就像: &#34;方法getSamplePeriod()未定义类型Configuration&#34;
我应该怎么做才能让我的项目识别DataCollection和Configuration类? 任何快速的帮助表示赞赏 谢谢
答案 0 :(得分:0)
我已经浏览了链接并在java中创建了FirstKaaDemo
。
我在eclipse中创建了新项目,然后在新包中创建了新类FirstKaaDemo.java
并复制粘贴在页面上的代码。
右键点击FirstKaaDemo.java>Build path>configure build path>library> add external jar
然后添加Downloaded KaaSDK&lt; ....&gt; .jar和slf4j.jar点击确定
然后所有错误都解决了,我运行了应用程序。它运行成功。