IoTGW/WSNApiMuxSDK/SampleCode/C

From ESS-WIKI
Revision as of 09:17, 7 March 2016 by Winston (talk | contribs) (Created page with "<span style="color:#0000ff;"><nowiki>#include</nowiki></span> <span style="color:#a31515;"><stdio.h></span> <span style="color:#0000ff;"><nowiki>#include</nowiki></span> <spa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

#include <stdio.h>

#include <unistd.h>

#include <dlfcn.h>

#include "SensorNetwork_Manager_API.h"

#include "AdvAPIMuxServer.h"

#include "BasicFun_Tool.h"

 

#if defined(WIN32) //windows

#pragma comment(lib, "SNManagerAPI.lib")

SNManagerAPI_Interface           *pSNManagerAPI = NULL;

#endif

 

void show_menu(void)

{

        printf("\n");

        printf("0) Get Sensor Network\n");

        printf("1) Set Sensor Network\n");

        printf("9) Exit the demo\n");

        printf("Enter your choice: \n");

}

 

 

#define SN_MANAGER_LIB_NAME "libIoTSensorManagerMux.so"

 

static void*        g_dl_handle       = NULL;  // libAdvPlayer.so載時的 handle

 

int InitSNGWHandler()

{

        int rc = 0;

        int i = 0;

        char libpath[MAX_PATH]={0};

 

        snprintf(libpath,sizeof(libpath),"%s",SN_MANAGER_LIB_NAME);

 

        if( GetSNManagerAPILibFn( libpath,&pSNManagerAPI ) == 0 ) {

                printf("Failed: InitSNGWHandler: GetSNManagerAPILibFn\r\n");

                return rc;

        }

 

        // 2. SN_Manager_Initialize

        if( pSNManagerAPI->SN_Manager_Initialize( ) == SN_ER_FAILED ) {

                printf("Failed: InitSNGWHandler: SN_Manager_Initialize\r\n");

                return rc;

        }

        PRINTF("InitSNGWHandler OK\n\n\n");

 

        return rc;

}

 

int getText(char *variable, int size){

    if(fgets(variable, sizeof(char) * size, stdin) != NULL ) {

                sscanf(variable, "%[^\n]", variable);

                return strlen(variable);

        }

        return 0;

}

 

 

int main(int argc, char *argv[] )

{

        int rc = 0;

        int count = 0;

        int i = 0;

        int result, done, op;

        char num[12];

        char pwd[128];

        char libpath[512];

        char szRESTfulAPICmd[512]={0};

        char szValue[128]={0};

 

        InitSNGWHandler();

 

        done = 0;

        while (! done) {

                show_menu();

 

                op = -1;

                if( getText(num,12) > 0 ) {

                        if(isdigit(num[0])) {

                                op = atoi(num);                          

                        }

                }

 

                if( op == -1 ) {

                        printf("Please input a number\n");

                        continue;

                }

 

                memset(szRESTfulAPICmd,0,sizeof(szRESTfulAPICmd));

                memset(szValue,0,sizeof(szValue));

 

                switch (op) {

                case 0: // GET

                                        printf("Please input Get API Command\r\n");

                                        printf("ex: SenHub/SenData/co\n");

                                        printf("ex: SenHub/Info/Name\n");

                                        if( getText(szRESTfulAPICmd,sizeof(szRESTfulAPICmd)) <= 0 ) {

                                                result = -1;

                                                printf("Input API Command Error\n");

                                                break;

                                        }

 

                                        printf("Get RESTful API= %s  Result= %s \n", szRESTfulAPICmd, pSNManagerAPI->SN_Manager_GetData(szRESTfulAPICmd, CACHE_MODE ));

                        break;

                case 1:  // SET

                                        printf("Please input Set API Command\r\n");

                                        printf("ex: SenHub/SenData/dout\n");

                                        printf("ex: SenHub/Info/Name\n");      

                                        if( getText(szRESTfulAPICmd,sizeof(szRESTfulAPICmd)) <= 0 ) {

                                                result = -1;

                                                printf("Input API Command Error\n");

                                                break;

                                        }

                                        printf("Please input Set Value in JSON Format\r\n");

                                        printf("ex: {\"bv\":1}\n");

                                        printf("ex: {\"sv\":\"Mote1\"}\n");

                                    if( getText(szValue,sizeof(szValue)) <= 0 ) {

                                                result = -1;

                                                printf("Input setting value Error\n");

                                                break;

                                        }

                                        printf("Set RESTful API=%s  Value=%s Result=%s\r\n", szRESTfulAPICmd, szValue, pSNManagerAPI->SN_Manager_SetData( szRESTfulAPICmd, szValue, NULL));

                        break;           

                case 9:

                        done = 1;

                        break;

                case 8: // show menum again

                        continue;

                default:

                        printf("\nUnknown choice!\n\n");

                        continue;

                }

        }

 

        pSNManagerAPI->SN_Manager_Uninitialize();

 

        return 0;

}