// SATService.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "SATService.h"
#include "MainProcess.h"
#include "SATExecutor.h"
#include "SATDevices.h"
#include "SATTCPServer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// 唯一的应用程序对象

CWinApp theApp;

using namespace std;

extern int test();

// 工作启动回调函数;
void CALLBACK WorkStart()
{
	WindowsService::GetDebugPriv();
	// 启用SAT执行器;
	CSATExecutor::GetInstance()->StartWork();
	// 启动SAT TCP服务端;
	CSATTCPServer::GetInstance()->LoadSocketLib();
	CSATTCPServer::GetInstance()->Start(GLOBAL::g_stSATConfig.dwTCPSvrPort);
	// 启动设备扫描器;
	CSATDevices::GetInstance()->StartWork();
}

// 工作结束回调函数;
void CALLBACK WorkEndof()
{
	GLOBAL::WriteTextLog("SATService goto end");
	CSATTCPServer::GetInstance()->Stop();
	CSATTCPServer::GetInstance()->UnloadSocketLib();
	CMainProcess::GetInstance()->EndofWork();
	GLOBAL::WriteTextLog("SATService work end!");
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// 直接获得前景窗口的句柄;
	HWND hwnd = GetForegroundWindow(); 
	SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
	HMODULE hModule = ::GetModuleHandle(NULL);

	// 初始化 MFC 并在失败时显示错误
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: 更改错误代码以符合您的需要
		_tprintf(_T("错误: MFC 初始化失败\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: 在此处为应用程序的行为编写代码。
		GLOBAL::GetIniInfo();
		GLOBAL::GetMacAddress();
		GLOBAL::GetLocalAddress();
		GLOBAL::WriteTextLog("SATService goto start");
#ifdef _DEBUG
		// 启动执行器;
		CSATExecutor::GetInstance()->StartWork();
		// 启动SAT TCP服务端;
		CSATTCPServer::GetInstance()->LoadSocketLib();
		CSATTCPServer::GetInstance()->Start(GLOBAL::g_stSATConfig.dwTCPSvrPort);
		// 启动设备管理;
		CSATDevices::GetInstance()->StartWork();

		// 指定时间停止;
		ULONGLONG ulTickCount = GetTickCount64();
		//while (GetTickCount64() - ulTickCount < 1800000 )
		while(1)
		{
			//printf("倒计时：%ld\n",GetTickCount64() - ulTickCount);
			Sleep(1000);
		}

		// 关闭TCP服务;
		CSATTCPServer::GetInstance()->Stop();		
		return 0;
#endif
		// 进程存在,直接退出;
		TCHAR szExeName[MAX_PATH] = {0};
		TCHAR szDrive[_MAX_DRIVE] = { 0 };
		TCHAR szDir[_MAX_DIR] = { 0 };
		TCHAR szFna[_MAX_FNAME] = { 0 };
		TCHAR szExt[_MAX_EXT] = { 0 };
		_tsplitpath_s(GLOBAL::g_szCurModuleFileName, szDrive, szDir, szFna, szExt);
		_stprintf_s(szExeName, _T("%s%s"), szFna, szExt);
		if ( GLOBAL::FindProcessCount(szExeName) > 1)
		{// 进程计数大于1,退出;
			return 0;
		}

		// 设置服务名和描述;
		_stprintf_s(WindowsService::g_szSvcName, _T("SAT Service"));
		_stprintf_s(WindowsService::g_szlpSvrDescription, _T("SAT自动化服务"));

		// 1.先判断服务是否存在;
		if ( WindowsService::IsSvcInstalled())
		{
			// 卸载服务;
			if ( argc > 1)
			{
				if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
					WindowsService::DoDeleteSvc(WindowsService::g_szSvcName);
				else
					_tprintf_s(_T("未有卸载服务参数行\n"));

				return 0;
			}
			else 
			{
				WindowsService::InitSvcVar();
				WindowsService::SetCallBack(WorkStart, WorkEndof);
				SERVICE_TABLE_ENTRY st[] =
				{
					{ WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
					{ NULL, NULL }
				};

				if (!::StartServiceCtrlDispatcher(st))	// 在直接双击.exe时,这步会运行失败;
				{
					//LOG4C((LOG_NOTICE,"服务已安装,须系统运行控制"));
					WindowsService::SvcReportEvent(_T("Register Service Main Function Error!"));
					return 0;
				}
			}
		}

		// 2.不存在,安装服务;
		WindowsService::InitSvcVar();
		WindowsService::SetCallBack(WorkStart, WorkEndof);
		SERVICE_TABLE_ENTRY st[] =
		{
			{ WindowsService::g_szSvcName, (LPSERVICE_MAIN_FUNCTION)WindowsService::ServiceMain },
			{ NULL, NULL }
		};

		WindowsService::SvcInstall();
	}

	return nRetCode;
}
