C/C++_Ubuntu
<p>以下代码示范了初始化,搜索设备,登录设备,设置音量和获取音量API的使用,设备返回信息见:<a href="https://www.showdoc.cc/ViplexCore?page_id=4745574396569976">运行结果</a>
<font color='red'> 注意:
</font><strong>1、如果JSON参数中涉及到中文,请确保调用处是<em>UTF-8</em>编码,如:</strong>
<code>#pragma execution_character_set("utf-8")</code></p>
<p><strong>2、<code>LOADING_FUNCTION_WITH_DYNAMIC</code>开启此宏代表动态加载(建议使用静态加载)</strong></p>
<pre><code>#示例 qmake:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG += qt
SOURCES += \
main.cpp
unix:{
QMAKE_LFLAGS += "-Wl,-rpath,\'\$$ORIGIN\'"
}
DEFINES -= LOADING_FUNCTION_WITH_DYNAMIC #是否动态加载
if(contains(DEFINES,LOADING_FUNCTION_WITH_DYNAMIC))
{
LIBS +=-ldl -lpthread
}
else()
{
LIBS +=-ldl -lpthread -L/lviplexcore库路径/ -lviplexcore
INCLUDEPATH+=/头文件路径/include
}</code></pre>
<pre><code>#include <dlfcn.h>
#include <iostream>
#include <functional>
#include <atomic>
#include <thread>
#include <chrono>
#include <string>
//#include <QDebug>
#pragma execution_character_set("utf-8")
using namespace std;
typedef void(*ExportViplexCallback)(const uint16_t, const char *);
#ifndef LOADING_FUNCTION_WITH_DYNAMIC
#include "exportviplexcoreasync.h"
#else
typedef int(*nvInitFunc)(const char*, const int, const int);
typedef void(*nvSerchTerminalAsyncFunc)(ExportViplexCallback);
typedef void(*nvLoginAsyncFunc)(const char*, ExportViplexCallback);
typedef void(*nvSetVolumeAsyncFunc)(const char*, ExportViplexCallback);
typedef void(*nvGetVolumeAsyncFunc)(const char*, ExportViplexCallback);
nvInitFunc nvInit;
nvSerchTerminalAsyncFunc nvSerchTerminalAsync;
nvLoginAsyncFunc nvLoginAsync;
nvSetVolumeAsyncFunc nvSetVolumeAsync;
nvGetVolumeAsyncFunc nvGetVolumeAsync;
void loadingFunctionWithDynamic()
{
void* handle = dlopen("/home/qht/viplexcore/RelWithDebInfo/bin/libviplexcore.so", RTLD_LAZY);
if(!handle)
{
printf("ERROR, Message(%s).\n", dlerror());
return ;
}
nvInit = (nvInitFunc)dlsym(handle, "nvInit");
char* szError = dlerror();
if(szError != NULL)
{
printf("ERROR, Message(%s).\n", szError);
dlclose(handle);
return ;
}
nvSerchTerminalAsync = (nvSerchTerminalAsyncFunc)dlsym(handle, "nvSerchTerminalAsync");
char* szError2 = dlerror();
if(szError2 != NULL)
{
printf("ERROR, Message(%s).\n", szError2);
dlclose(handle);
return ;
}
nvLoginAsync = (nvLoginAsyncFunc)dlsym(handle, "nvLoginAsync");
char* szError3 = dlerror();
if(szError3 != NULL)
{
printf("ERROR, Message(%s).\n", szError3);
dlclose(handle);
return ;
}
nvSetVolumeAsync = (nvSetVolumeAsyncFunc)dlsym(handle, "nvSetVolumeAsync");
char* szError4 = dlerror();
if(szError4 != NULL)
{
printf("ERROR, Message(%s).\n", szError4);
dlclose(handle);
return ;
}
nvGetVolumeAsync = (nvGetVolumeAsyncFunc)dlsym(handle, "nvGetVolumeAsync");
char* szError5 = dlerror();
if(szError5 != NULL)
{
printf("ERROR, Message(%s).\n", szError5);
dlclose(handle);
return ;
}
}
#endif
atomic_bool g_bAPIReturn(false);
ExportViplexCallback callback = [](const uint16_t code, const char *data)
{
std::string msgCode = std::string("ViplexCore Demo code:") + to_string(code);
std::string msgData = std::string("ViplexCore Demo data:") + data;
std::cout << msgCode.c_str();
std::cout << msgData.c_str();
g_bAPIReturn = true;
};
void waitAPIReturn()
{
while (!g_bAPIReturn)
{
this_thread::sleep_for(chrono::seconds(1));
}
g_bAPIReturn = false;
}
void APITester()
{
cout << "nvInit:" << nvInit("D:\\test", 1, 1) << endl;
std::cout << ("ViplexCore Demo nvSerchTerminalAsync begin... ");
nvSerchTerminalAsync(callback);
this_thread::sleep_for(chrono::seconds(5));
g_bAPIReturn = false;
std::cout << ("ViplexCore Demo nvLoginAsync begin... ");
nvLoginAsync("{\"sn\":\"BZSA07194A0049999716\",\"username\":\"admin\",\"password\":\"123456\",\"loginType\":0,\"rememberPwd\":0}", callback);
waitAPIReturn();
std::cout << ("ViplexCore Demo nvSetVolumeAsync begin... ");
nvSetVolumeAsync("{\"sn\":\"BZSA07194A0049999716\",\"volumeInfo\":{\"ratio\":60.0}}", callback);
waitAPIReturn();
std::cout << ("ViplexCore Demo nvGetVolumeAsync begin... ");
nvGetVolumeAsync("{\"sn\":\"BZSA07194A0049999716\"}", callback);
waitAPIReturn();
}
int main()
{
#ifdef LOADING_FUNCTION_WITH_DYNAMIC
loadingFunctionWithDynamic();
#endif
APITester();
return 0;
}</code></pre>