我已经创建了Kaa平台的一个节点,创建了我的第一个Kaa应用程序,链接如下:"https://kaaproject.github.io/kaa/docs/v0.10.0/Programming-guide/Your-first-Kaa-application/“现在在eclipse中,我导入了生成的https://kaaproject.github.io/kaa/docs/v0.10.0/Programming-guide/Your-first-Kaa-application/并创建了一个应用程序,如下所示:
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和配置类方面遇到了一些问题。我甚至导入了DataCollection和配置的.jar文件,该文件是从CTL=>Export=>Java库下载的,但没有任何更改。例如,其中一个错误类似于:“未为类型配置定义getSamplePeriod()方法”
我应该怎么做才能让我的项目识别DataCollection和Configuration类?任何快速的帮助都很感谢谢谢
发布于 2018-05-29 19:09:34
我已经浏览了链接并在java中创建了FirstKaaDemo。
右键单击FirstKaaDemo.java>Build path>configure build path>library> add external jar,然后添加已下载的KaaSDK<....>.jar和slf4j.jar,单击ok,然后解决所有错误,然后运行应用程序。它运行成功。
https://stackoverflow.com/questions/48496446
复制相似问题