#include "stdafx.h"
#include "SDK.h"
#include "IRControl.h"
#ifdef __MAKE_PYD__
#include "Python.h"
#endif

static CIRControl g_IRControl;
#ifndef __MAKE_PYD__
// 定义 DLL 的导出函数和变量。
int nIRControl = 100;

DWORD IsAppRunning(LPCTSTR lpszAppDir)
{
	return CIRControl::IsAppRunning(lpszAppDir);
}

bool StartIRApp(LPCTSTR lpszAppDir, LPCTSTR lpSignalXml, DWORD dwPort)
{
	if (!CIRControl::StartApp(lpszAppDir, lpSignalXml, dwPort))
		return false;

	return true;
}

bool CloseApp()
{
	return CIRControl::CloseApp();
}

bool Connect(LPCTSTR lpServer, int port)
{
	// 创建连接;
	if (!g_IRControl.InitSocket())
	{
		Global::WriteTextLog(_T("创建TCP连接失败"));
		g_IRControl.CloseApp();
		return false;
	}

	if (!lpServer || lpServer[0] == '\0' || port < 0)
		return false;

	//if (!g_IRControl.SelectConnect(lpServer, port, 5000))
	if (!g_IRControl.Connect(lpServer, port))
	{
		Global::WriteTextLog(_T("连接服务器失败"));
		g_IRControl.CloseApp();
		return false;
	}

	return true;
}

LPCTSTR getDeviceNames()
{
	static std::string devices;
	std::vector<std::string> vtDevices;
	if ( g_IRControl.getDeviceNameList(vtDevices) )
	{
		devices.clear();
		for ( auto dv: vtDevices )
		{
			devices.append(dv + _T(";"));
		}

		return devices.c_str();
	}

	return _T("");
}

LPCTSTR getSignalsName()
{
	static std::string signals;
	signals = g_IRControl.getSignalsName();

	return signals.c_str();
}

bool loadSignalDataSet(LPCTSTR lpDatasetXml)
{
	if (!lpDatasetXml || lpDatasetXml[0] == '\0')
		return false;

	return g_IRControl.loadSignalDataSet(lpDatasetXml);
}

bool sendSignal(LPCTSTR lpSignal, int send_times, int sleep_time)
{
	if (!lpSignal || lpSignal[0] == '\0')
		return false;

	return g_IRControl.sendSignal(lpSignal, send_times, sleep_time);
}

bool sendSignals(LPCTSTR lpSignals, int sleep_time)
{
	if (!lpSignals || lpSignals[0] == '\0')
		return false;

	std::string signals = lpSignals;
	std::vector<std::string> vtSignals;
	int npos = -1;
	while (true)
	{
		npos = signals.find_first_of(';');
		if (npos == std::string::npos)
			break;

		vtSignals.push_back(signals.substr(0, npos));
		signals = signals.substr(npos + 1);
	}
	vtSignals.push_back(signals);

	return g_IRControl.sendSignals(vtSignals, sleep_time);
}

bool sendRepeatsSignal(LPCTSTR lpSignal, int repeat_time)
{
	if (!lpSignal || lpSignal[0] == '\0')
		return false;

	return g_IRControl.sendRepeatsSignal(lpSignal, repeat_time);
}
#else

static PyObject* IsAppRunning(PyObject* self, PyObject* args)
{
	// 应用程序路径;
	const char* pszExePath = NULL;
	if (!PyArg_ParseTuple(args, "s", &pszExePath))
		return NULL;

	// 返回进程Id;
	return Py_BuildValue("i", CIRControl::IsAppRunning(pszExePath));
}

static PyObject* StartApp(PyObject* self, PyObject* args)
{
	// 应用程序路径;
	const char* pszExePath = NULL;
	const char* pszDataset = NULL;
	unsigned int port = 0;
	if (!PyArg_ParseTuple(args, "ssI", &pszExePath, &pszDataset, &port))
		return NULL;

	return Py_BuildValue("b", CIRControl::StartApp(pszExePath, pszDataset, port));
}

static PyObject* StopApp(PyObject* self, PyObject* args)
{
	return Py_BuildValue("b", CIRControl::CloseApp());	// 返回None;
}

//描述：连接设备，默认连接索引为0的设备;
static PyObject* ConnectDevice(PyObject* self, PyObject* args)
{
	unsigned int port = 0;
	const char* pszServer = NULL;
	if (!PyArg_ParseTuple(args, "sI", &pszServer, &port))
		return NULL;

	// 创建连接;
	if (!g_IRControl.InitSocket())
	{
		Global::WriteTextLog(_T("创建TCP连接失败"));
		g_IRControl.CloseApp();
		return Py_BuildValue("b", false);
	}

	if (!pszServer || pszServer[0] == '\0' || port < 0)
		return Py_BuildValue("b", false);

	if (!g_IRControl.SelectConnect(pszServer, port, 5000))
	{
		Global::WriteTextLog(_T("连接服务器失败"));
		g_IRControl.CloseApp();
		return Py_BuildValue("b", false);
	}

	return Py_BuildValue("b", true);
}

static PyObject* DisconnectDevice(PyObject* self, PyObject* args)
{
	g_IRControl.DisConnect();
	return Py_BuildValue("");
}

static PyObject* getDeviceNames(PyObject* self, PyObject* args)
{
	std::string devices;
	std::vector<std::string> vtDevices;
	if (g_IRControl.getDeviceNameList(vtDevices))
	{
		for (auto dv : vtDevices)
		{
			devices.append(dv + _T(";"));
		}

		return Py_BuildValue("s", devices.c_str());
	}

	return Py_BuildValue("s", "");
}

static PyObject* loadSignalDataSet(PyObject* self, PyObject* args)
{
	const char* pszDatasetXml = NULL;
	if (!PyArg_ParseTuple(args, "s", &pszDatasetXml))
		return NULL;

	if ( !pszDatasetXml || pszDatasetXml[0] == '\0')
		return Py_BuildValue("b", false);

	if (!g_IRControl.loadSignalDataSet(std::string(pszDatasetXml)) )
	{
		Global::WriteTextLog(_T("加载信号数据集失败"));
		g_IRControl.CloseApp();
		return Py_BuildValue("b", false);
	}

	return Py_BuildValue("b", true);
}

static PyObject* getSignalsName(PyObject* self, PyObject* args)
{
	const char* pszDatasetXml = NULL;
	if (!PyArg_ParseTuple(args, "s", &pszDatasetXml))
		return NULL;

	if (!pszDatasetXml )
		return Py_BuildValue("s", "");

	std::string data = g_IRControl.getSignalsName();
	return Py_BuildValue("s", data.c_str());
}

static PyObject* sendSignal(PyObject* self, PyObject* args)
{
	const char* pszSignal = NULL;
	int send_times = 1, sleep_time = 1000;
	if (!PyArg_ParseTuple(args, "sii", &pszSignal, &send_times, &sleep_time))
		return NULL;

	if (!pszSignal || pszSignal[0] == '\0')
		return Py_BuildValue("b", false);

	return Py_BuildValue("b", g_IRControl.sendSignal(pszSignal, send_times, sleep_time));
}

static PyObject* sendSignals(PyObject* self, PyObject* args)
{
	const char* pszSignal = NULL;
	int sleep_time = 1000;
	if (!PyArg_ParseTuple(args, "si", &pszSignal, &sleep_time))
		return NULL;

	if (!pszSignal || pszSignal[0] == '\0')
		return Py_BuildValue("b", false);

	std::string signals = pszSignal;
	std::vector<std::string> vtSignals;
	int npos = -1;
	while (true)
	{
		npos = signals.find_first_of(';');
		if (npos == std::string::npos)
			break;

		vtSignals.push_back(signals.substr(0, npos));
		signals = signals.substr(npos + 1);
	}
	vtSignals.push_back(signals);

	return Py_BuildValue("b", g_IRControl.sendSignals(vtSignals, sleep_time));
}

static PyObject* sendRepeatsSignal(PyObject* self, PyObject* args)
{
	const char* pszSignal = NULL;
	int repeat_time = 2;
	if (!PyArg_ParseTuple(args, "si", &pszSignal, &repeat_time))
		return NULL;

	if (!pszSignal || pszSignal[0] == '\0')
		return Py_BuildValue("b", false);

	return Py_BuildValue("b", g_IRControl.sendRepeatsSignal(pszSignal, repeat_time));
}

static PyObject* sendDurationSignal(PyObject* self, PyObject* args)
{
	const char* pszSignal = NULL;
	int duration_time = 2;
	if (!PyArg_ParseTuple(args, "si", &pszSignal, &duration_time))
		return NULL;

	if (!pszSignal || pszSignal[0] == '\0')
		return Py_BuildValue("b", false);

	return Py_BuildValue("b", g_IRControl.sendDurationSignal(pszSignal, duration_time));
}

// 描述方法,暴露给python的函数;
static PyMethodDef IRControl_Methods[] = {
	{"IsAppRunning",IsAppRunning,METH_VARARGS,"程序是否在运行"},
	{"StartApp", StartApp, METH_VARARGS, "启动进程"},
	{"StopApp", StopApp, METH_VARARGS, "关闭进程"},
	{"ConnectDevice", ConnectDevice, METH_VARARGS, "连接设备"},
	{"DisconnectDevice", DisconnectDevice, METH_VARARGS, "断开设备"},
	{"getDeviceNames", getDeviceNames, METH_VARARGS, "获取设备列表，逗号分隔"},
	{"loadSignalDataSet", loadSignalDataSet, METH_VARARGS, "加载指定目录的信号数据集XML文件"},
	{"getSignalsName", getSignalsName, METH_VARARGS, "获取默认信号数据集信号列表"},
	{"sendSignal", sendSignal, METH_VARARGS, "发送单个信号"},
	{"sendSignals", sendSignals, METH_VARARGS, "发送多个信号，以分号为分隔符"},
	{"sendRepeatsSignal", sendRepeatsSignal, METH_VARARGS, "发送重复信号"},
	{"sendDurationSignal", sendDurationSignal, METH_VARARGS, "发送时叠信号"},
	
	{NULL,NULL}
};

// 初始模块;//格式：init<模块名称>
PyMODINIT_FUNC initIRControl()
{
	// 初始化pyd函数列表;
	PyObject* m, * d;
	m = Py_InitModule("IRControl", IRControl_Methods);
	d = PyModule_GetDict(m);
}
#endif