記錄-Kaa測試程序

有感於一天的折騰,總的留個紀念。

以下的內容不是我的原創,只是自己的一個記錄。


Kaa是什麼?去官網看看就知道了 ,我也沒咋細看,哈哈。


一、測試環境準備

  • Host OS:WIN7

  • Vbox:版本 5.1.14 r112924 (Qt5.6.2)

  • Sandbox:一個Kaa配置好的測試用虛擬機鏡像。這個只是用來測試或者小範圍應用的,官方也提供在AWS上直接部署,方便測試。如果是要配置集羣,you can download ready-to-use Debian or RPM packages for various Linux flavors, or build Kaa from source code.

  • 一臺安裝了Linux的設備,我用的是Raspberry Pi 3 Model B,安裝的是Raspbian Jessie系統。畢竟要做IoT的麼。

二、安裝配置

將下載的Sandbox用虛擬機直接導入,就是這樣。啓動虛擬機。哈哈,好簡單不是。啓動後的界面是正樣子的

wKiom1i-fcrgEe5DAADO7vQ96Gk279.png-wh_50

做的很貼心的哦。端口映射什麼的都給你配置好了。

wKiom1i-fh_D4UKaAACL9CB4tiE105.png-wh_50

正常啓動後,直接在瀏覽器裏訪問對應的地址就可以看到管理界面了。

記錄兩組初始的賬戶和密碼。admin/admin123,devuser/devuser123.這123的風格不錯。。

三、測試程序

測試程序我是根據官網的Guide做的,地址在這裏

To achieve this, two Kaa features will be used:

  • Data collection feature allows sending data from endpoints to the Kaa server. In this example, the Data collection feature will be used to transmit temperature values at a configured sample period.

  • Configuration feature allows broadcasting configuration parameters from the Kaa server to Kaa endpoints. In this example, the Configuration feature will be used to send the sampling period values from Kaa server to the temperature sensors.

這兩個features,一個是數據模型,一個是控制模型,目前是這麼理解的。

  • 創建一個Application。

步驟我就不寫了。忘記了可以參考官網的例子。大致是用admin登錄,在applications裏面創建一個新的程序。也就是測試程序,名字隨便。

  • 創建兩個JSON文件。

data-schema.json

 {
     "type": "record",
     "name": "DataCollection",
     "namespace": "org.kaaproject.kaa.schema.sample",
     "fields": [
         {"name": "temperature",
             "type": "int"
         }
     ]
 }

configuration-schema.json

{
     "type": "record",
     "name": "Configuration",
     "namespace": "org.kaaproject.kaa.schema.sample",
     "fields": [
         {"name": "samplePeriod",
             "type": "int",
             "by_default": 1
         }
     ]
 }

幹啥用的?其實就是To create a new CT。具體操作在Tenant CTL這個菜單裏面。如果提示重名了,記得修改一下,這裏重點說一下,對一次接觸的人這個有點坑,我折騰的大部分時間也是在這。下面是我建好的data schema,對應的,在文章最後的main.c的第21行kaa_logging_data_collection_create();就要換成kaa_logging_data_collection_demo_create();

wKioL1i-glbg8L4CAACl-KUAC-4613.png-wh_50


這個駝峯式的名字,要和後面的代碼匹配上。如果你名字隨意了。後面的code編譯一定是過不去的。

  • 換devuser用戶登錄,配置程序,生成SDK.

給程序配置log和configuration:

Click the Applications arrow to unfold the list and click the arrow of the application you created in Add application, then click Schemas > Log and click the Add schema button.添加一個log schema,然後,去configuration菜單裏配置一個configuration schema.

記得別選錯對應的schema,錯了也沒事,我一開始也錯了。記得配置SDK的時候把版本號對應上就可以。

創建log appender:

To use the data collection feature, you need to set up a Log appender. In this example, the MongoDB log appender is used. For more information, see MongoDB log appender. 創建一個log appender,可能是版本不對,發現官網例子裏的界面,和實際的不太一樣。不過影響不大。其實,這就是最後數據存儲的地方。還有其他的類型,不過沒時間搞了。

創建SDK:

在創建的程序的SDK Profiles裏面,點擊添加按鈕,默認選擇各種參數的版本號,這裏可以調整版本號,如果之前你的schema錯了,可以在這裏調整。完成後,點擊那個下載圖標,會提示選擇Target Platform。我選擇的是C ,然後下載sdk.

   

wKiom1i-hkDheUM2AAB3_fpoCpY965.png-wh_50

到這裏,sandbox服務的配置基本就OK.下面開始在客戶端操作了。

Raspberry Pi上的操作。

Pi怎麼玩,這裏就不說了。哈。

安裝cmake,

sudo apt-get install cmake


創建一個文件夾。就叫my_app吧。然後在my_app下面創建一個文件夾koa,一個CMakeLists.txt文件,一個main.c文件,目錄結構如下。

-my_app

--koa

--CMakeLists.txt

--main.c

在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)

在main.c裏面創建一個空的主函數

int main(void)
 {
	
 }

命令行切換到my_app下,執行如下命令

mkdir build
 cd build
 cmake ..
 make

如果一切正常,編譯過後,你會看到kaa_app。這個名稱是在CMakeLists.txt裏面配置的。

 替換main.c的內容爲下面的代碼。聲明:此處的代碼來自Kaa網站的例子。

#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;}

執行如下命令,重新編譯程序

 cd build
 cmake -DKAA_MAX_LOG_LEVEL=3 ..
 make

運行,如果提示權限加sudo

./kaa-app

成功運行!

wKioL1i-ij2Dh7MgAAAS95mTEqg666.png-wh_50


在sandbox裏查看數據

1、記錄下創建app的token。

2、ssh登錄,或者直接在虛擬機登錄,初始帳號密碼kaa/kaa

3、開啓mongodb,這個和你配置的log appender是對應的。

mongo kaa
db.logs_$your_application_token$.find()

通過調整configuration schema,控制data schema的採集週期。

用devuser登錄。在程序的Endpoint groups裏選擇All那條記錄。在詳細頁面中configuration裏選擇Draft標籤,看到了吧,哈,設置新的週期數值-Save-Activate.

四、總結

。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章