跳到主要内容

示例程序

示例1

#include "stdafx.h"
#include "libtriton.h"
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

int main()
{
// 获取设备 UUID
char uuid[32] = {0};
GetUUID(uuid);
printf("device uuid is %s\n", uuid);

// 获取客户端在线状态
ret = Status();
printf("returned status: %d\n", ret);

// 构建KeyValue结构体数组
KeyValue kv[5];
for (int i = 0; i < 5; i++) {
kv[i].key = new char[MAX_KEY_LENGTH];
kv[i].value = new char[MAX_KEY_LENGTH];

// 写入key
sprintf(kv[i].key, "data-%d", i);
// 写入value
sprintf(kv[i].value, "value-%d", i);
}

// 发送数据
int ret = Send(kv, 5);
printf("send returned status %d\n", ret);

// 释放KeyValue数组内存
for (int i = 0; i < 5; i++) {
delete kv[i].key;
delete kv[i].value;
}

// 接收数据
Payload *payload = Receive();

printf("payload size is %d\n", payload->size);

if (payload->size > 0) {
for (int i = 0; i < payload->size; ++i) {
printf("key %d: %s\n", i + 1, payload->keyValue[i].key);
printf("value %d: %s\n", i + 1, payload->keyValue[i].value);
}
}

// 释放内存
freePayload(payload);

return 0;
}

示例2

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
// Import the header
#include "libtriton.h"

enum
{
eLedBlink = 1,
eLedOn,
eLedOff
};

const unsigned int iMaxSize = 256;
static bool isExit = false;
static int iLedStatus = eLedBlink;

DWORD WINAPI MyReceiveThread(LPVOID lpParam);
int SendDataToServer(string led1_state,string led2_state);

void LedOn();
void LedOff();
void LedBlink();

int main()
{
DWORD dwThreadId;
HANDLE ThreadHandle;

ThreadHandle = CreateThread(NULL,0,MyReceiveThread,NULL,0,&dwThreadId);
if (ThreadHandle == NULL)
{
printf("Create thread error\n");
return -1;
}

// 获取设备 UUID
char uuid[32] = { 0 };
GetUUID(uuid);
printf("uuid %s\n", uuid);

// 获取客户端在线状态
int online = Status();
printf("returned status: %d\n", online);

if (online == 1)
{
printf("Network is connected\n");
}
else
{
printf("Network is disconnect\n");
}

while (!isExit)
{
if (iLedStatus == eLedOn)
{
LedOn();
}
else if (iLedStatus == eLedOff)
{
LedOff();
}
else if (iLedStatus == eLedBlink)
{
LedBlink();
}
Sleep(500);
// Send the LED status to server
}

system("pause");
WaitForMultipleObjects(1, &ThreadHandle, TRUE, INFINITE);
return 0;
}

DWORD WINAPI MyReceiveThread(LPVOID lpParam)
{
while (!isExit)
{
// 接收数据
Payload *payload = Receive();
// Check if data size is not zero
if (payload->size > 0)
{
for (int i = 0; i < payload->size; ++i)
{
string strKey = payload->keyValue[i].key;
string strValue = payload->keyValue[i].value;
if (strcmp(strKey.c_str(),"switch") == 0)
{
if (strcmp(strValue.c_str(),"ALL_ON") == 0)
{
iLedStatus = eLedOn;
}
else if (strcmp(strValue.c_str(), "ALL_OFF") == 0)
{
iLedStatus = eLedOff;
}
else if (strcmp(strValue.c_str(), "NORMAL") == 0)
{
iLedStatus = eLedBlink;
}
}
//printf("%d. key: %s, value: %s", i, payload->keyValue[i].key, payload->keyValue[i].value);
}
}

// 释放内存
freePayload(payload);

Sleep(1000);
}
return 0;
}

void LedOn()
{
printf("Led1 ●,Led2 ●\n");
SendDataToServer("On", "On");
}

void LedOff()
{
printf("Led1 ○,Led2 ○\n");
SendDataToServer("Off", "Off");
}

void LedBlink()
{
printf("Led1 ○,Led2 ●\n");
SendDataToServer("Off", "On");
Sleep(500);
printf("Led1 ●,Led2 ○\n");
SendDataToServer("On", "Off");
}

int SendDataToServer(string led1_state, string led2_state)
{
// 构建KeyValue结构体数组
KeyValue kv[2];
for (int i = 0; i < 2; i++) {
kv[i].key = new char[MAX_KEY_LENGTH];
kv[i].value = new char[MAX_KEY_LENGTH];
}

// 写入key和value
sprintf_s(kv[0].key, MAX_KEY_LENGTH, "led1");
sprintf_s(kv[0].value, MAX_VALUE_LENGTH, "%s",led1_state.c_str());
sprintf_s(kv[1].key, MAX_KEY_LENGTH, "led2");
sprintf_s(kv[1].value, MAX_VALUE_LENGTH, "%s", led2_state.c_str());

// 发送数据
int ret = Send(kv, 2);

// 释放KeyValue数组内存
for (int k = 0; k < 2; k++)
{
delete kv[k].key;
delete kv[k].value;
}

return ret;
}