#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
#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;
}
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);
}
system("pause");
WaitForMultipleObjects(1, &ThreadHandle, TRUE, INFINITE);
return 0;
}
DWORD WINAPI MyReceiveThread(LPVOID lpParam)
{
while (!isExit)
{
Payload *payload = Receive();
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;
}
}
}
}
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 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];
}
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);
for (int k = 0; k < 2; k++)
{
delete kv[k].key;
delete kv[k].value;
}
return ret;
}