第二天:通过kaa采集数据

今天是收获满满的一天,学到的东西特别的多。在讲解今天的内容之前,我们先来熟悉几个最简单的ubuntu命令。
1、cd 目录名 //进入指定目录下
2、cd … //返回上一级目录
3、mkdir 文件名 //建立文件
4、vim 文件名 //进入vim模式
5、ls //查看当前目录
6、mv 源文件 目标目录 //把源文件移动到目标目录里面
7、rm 文件名**//删除文件**
8、rm -rf 目录名 //删除目录
当然除了这些还有一些最基本的命令,不过这些命令网上都是可以找到的,大家需要什么命令直接搜索就可以了,非常简单。
接着昨天的内容。
1、安装CMake,为此,请在终端中运行以下命令。
sudo apt-get install cmake
2、在创建一个名为app的文件。
mkdir app
3、在app 目录下创建名为kaa的文件,并且把之前下载的SDK文件解压到里面。
4、在app目录下创建一个名为CMakeLists.txt的文件。应该包含以下文件
cmake_minimum_required(VERSION 2.8.12)
project(kaa-application C)

set(CMAKE_C_FLAGS “${CMAKE_C_FLAGS} -std=gnu99 -g -Wall -Wextra”)

add_subdirectory(kaa)

add_executable(kaa-app main.c)

target_link_libraries(kaa-app kaac)
创建CMakeLists.txt的命令为:vim CMakeLists.txt
5、在app下面创建一个main.c包含空主例程的源文件(现在)。
int main(void)
{

}
6、在app目录下面创建一个名为build的目录文件。
7、在app目录下面创建一个名为src的目录。
8、在src目录下面创建一个名为kaa-application.c的文件,并且包含以下内容。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <kaa.h>
#include <platform/kaa_client.h>
#include <kaa_error.h>
#include <kaa_configuration_manager.h>
#include <kaa_logging.h>
#include <gen/kaa_logging_gen.h>
#include <platform/kaa_client.h>
#include <utilities/kaa_log.h>
#include <platform-impl/common/ext_log_upload_strategies.h>

static int32_t sample_period;
static time_t last_sample_time;
extern kaa_error_t ext_unlimited_log_storage_create(void **log_storage_context_p, kaa_logger_t logger);
/
Retrieves current temperature. /
static int32_t get_temperature_sample(void)
{
/
For the sake of example, random data is used /
return rand() % 10 + 25;
}
/
Periodically called by Kaa SDK. */
static void example_callback(void context)
{
time_t current_time = time(NULL);
/
Respect sample period */
if (difftime(current_time, last_sample_time) >= sample_period) {
int32_t temperature = get_temperature_sample();
printf(“Sampled temperature: %i\n”, temperature);
last_sample_time = current_time;
kaa_user_log_record_t log_record = kaa_logging_data_collection_create();
log_record->temperature = temperature;
kaa_logging_add_record(kaa_client_get_context(context)->log_collector, log_record, NULL);
}
}
/
Receives new configuration data. */
static kaa_error_t on_configuration_updated(void *context, const kaa_root_configuration_t conf)
{
(void) context;
printf(“Received configuration data. New sample period: %i seconds\n”, conf->sample_period);
sample_period = conf->sample_period;
return KAA_ERR_NONE;
}
int main(void)
{
/
Init random generator used to generate temperature /
srand(time(NULL));
/
Prepare Kaa client. */
kaa_client_t kaa_client = NULL;
kaa_error_t error = kaa_client_create(&kaa_client, NULL);
if (error) {
return EXIT_FAILURE;
}
/
Configure notification manager. /
kaa_configuration_root_receiver_t receiver = {
.context = NULL,
.on_configuration_updated = on_configuration_updated
};
error = kaa_configuration_manager_set_root_receiver(
kaa_client_get_context(kaa_client)->configuration_manager,
&receiver);
if (error) {
return EXIT_FAILURE;
}
/
Obtain default configuration shipped within SDK. */
const kaa_root_configuration_t *dflt = kaa_configuration_manager_get_configuration(
kaa_client_get_context(kaa_client)->configuration_manager);
printf(“Default sample period: %i seconds\n”, dflt->sample_period);
sample_period = dflt->sample_period;

/* Configure data collection. */
void *log_storage_context         = NULL;
void *log_upload_strategy_context = NULL;
/* The internal memory log storage distributed with Kaa SDK. */
error = ext_unlimited_log_storage_create(&log_storage_context,
    kaa_client_get_context(kaa_client)->logger);
if (error) {
    return EXIT_FAILURE;
}
/* Create a strategy based on timeout. */
error = ext_log_upload_strategy_create(
    kaa_client_get_context(kaa_client), &log_upload_strategy_context,
    KAA_LOG_UPLOAD_BY_TIMEOUT_STRATEGY);
if (error) {
    return EXIT_FAILURE;
}
/* Strategy will upload logs every 5 seconds. */
error = ext_log_upload_strategy_set_upload_timeout(log_upload_strategy_context, 5);
if (error) {
    return EXIT_FAILURE;
}
/* Specify log bucket size constraints. */
kaa_log_bucket_constraints_t bucket_sizes = {
     .max_bucket_size       = 32,   /* Bucket size in bytes. */
     .max_bucket_log_count  = 2,    /* Maximum log count in one bucket. */
};
/* Initialize the log storage and strategy (by default, they are not set). */
error = kaa_logging_init(kaa_client_get_context(kaa_client)->log_collector,
    log_storage_context, log_upload_strategy_context, &bucket_sizes);
if (error) {
    return EXIT_FAILURE;
}

/* Start Kaa SDK's main loop. example_callback is called once per second. */
error = kaa_client_start(kaa_client, example_callback, kaa_client, 1);
/* Should get here only after Kaa stops. */
kaa_client_destroy(kaa_client);

if (error) {
    return EXIT_FAILURE;
}
return EXIT_SUCCESS;

}
9、定位到build目录下。
cd build
10、在build目录下执行
cmake -DKAA_MAX_LOG_LEVEL=3 -DBUILD_TESTING=OFF …
make
11、启动可执行文件。
./kaa-app
注:如果没有反应的话请切换到app目录下,用vim命令进入CMakeLists.txt里面。
把里面的
kaa -app main.c修改成kaa -app src/kaa-application.c。
然后在到build里面执行./kaa-app

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哈喽朝龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值