Пример #1
0
static void JSONSettings(const KVPairs & key_value_pairs, FILE * stream_file)
{
	ServeHeader(stream_file, 200, "OK", false, "text/plain");
	IPAddress ip;
	fprintf(stream_file, "{\n");
#ifdef ARDUINO
	ip = GetIP();
	fprintf_P(stream_file, PSTR("\t\"ip\" : \"%d.%d.%d.%d\",\n"), ip[0], ip[1], ip[2], ip[3]);
	ip = GetNetmask();
	fprintf_P(stream_file, PSTR("\t\"netmask\" : \"%d.%d.%d.%d\",\n"), ip[0], ip[1], ip[2], ip[3]);
	ip = GetGateway();
	fprintf_P(stream_file, PSTR("\t\"gateway\" : \"%d.%d.%d.%d\",\n"), ip[0], ip[1], ip[2], ip[3]);
	ip = GetNTPIP();
	fprintf_P(stream_file, PSTR("\t\"NTPip\" : \"%d.%d.%d.%d\",\n"), ip[0], ip[1], ip[2], ip[3]);
	fprintf_P(stream_file, PSTR("\t\"NTPoffset\" : \"%d\",\n"), GetNTPOffset());
#endif
	fprintf_P(stream_file, PSTR("\t\"webport\" : \"%u\",\n"), GetWebPort());
	fprintf_P(stream_file, PSTR("\t\"ot\" : \"%d\",\n"), GetOT());
	ip = GetWUIP();
	fprintf_P(stream_file, PSTR("\t\"wuip\" : \"%d.%d.%d.%d\",\n"), ip[0], ip[1], ip[2], ip[3]);
	fprintf_P(stream_file, PSTR("\t\"wutype\" : \"%s\",\n"), GetUsePWS() ? "pws" : "zip");
	fprintf_P(stream_file, PSTR("\t\"zip\" : \"%ld\",\n"), (long) GetZip());
	fprintf_P(stream_file, PSTR("\t\"sadj\" : \"%ld\",\n"), (long) GetSeasonalAdjust());
	char ak[17];
	GetApiKey(ak);
	fprintf_P(stream_file, PSTR("\t\"apikey\" : \"%s\",\n"), ak);
	GetPWS(ak);
	ak[11] = 0;
	fprintf_P(stream_file, PSTR("\t\"pws\" : \"%s\"\n"), ak);
	fprintf(stream_file, "}");
}
Пример #2
0
static void JSONwCheck(const KVPairs & key_value_pairs, FILE * stream_file)
{
	Weather w;
	ServeHeader(stream_file, 200, "OK", false, "text/plain");
	char key[17];
	GetApiKey(key);
	char pws[12] = {0};
	GetPWS(pws);
	const Weather::ReturnVals vals = w.GetVals(GetWUIP(), key, GetZip(), pws, GetUsePWS());
	const int scale = w.GetScale(vals);

	fprintf(stream_file, "{\n");
	fprintf_P(stream_file, PSTR("\t\"valid\" : \"%s\",\n"), vals.valid ? "true" : "false");
	fprintf_P(stream_file, PSTR("\t\"keynotfound\" : \"%s\",\n"), vals.keynotfound ? "true" : "false");
	fprintf_P(stream_file, PSTR("\t\"minhumidity\" : \"%d\",\n"), vals.minhumidity);
	fprintf_P(stream_file, PSTR("\t\"maxhumidity\" : \"%d\",\n"), vals.maxhumidity);
	fprintf_P(stream_file, PSTR("\t\"meantempi\" : \"%d\",\n"), vals.meantempi);
	fprintf_P(stream_file, PSTR("\t\"precip_today\" : \"%d\",\n"), vals.precip_today);
	fprintf_P(stream_file, PSTR("\t\"precip\" : \"%d\",\n"), vals.precipi);
	fprintf_P(stream_file, PSTR("\t\"wind_mph\" : \"%d\",\n"), vals.windmph);
	fprintf_P(stream_file, PSTR("\t\"UV\" : \"%d\",\n"), vals.UV);
	fprintf_P(stream_file, PSTR("\t\"scale\" : \"%d\"\n"), scale);
	fprintf(stream_file, "}");
}
Пример #3
0
DWORD ServiceExecutionThread(LPDWORD param)
{
#if _DEBUG
    ::OutputDebugString(L"ServiceExecutionThread\n");
#endif

    if (IntializeSystemInfo() == FALSE)
    {
        return IC_NO_INITIALIZE;
    }

    WORD wVersionRequested = MAKEWORD(1, 1);
    WSADATA wsaData;
    WSAStartup(wVersionRequested, &wsaData);
    SOCKET udpSocket = INVALID_SOCKET;

    int result = IC_NOERROR;
    bool showHelp = false;

    do
    {
        g_killServiceEvent = CreateEvent(0, TRUE, FALSE, 0);
        if (!g_killServiceEvent)
        {
            OutputError(L"CreateEvent fails! (%d)", GetLastError());
            result = IC_EVENT_CREATE_FAIL;
            break;
        }

        wstring apiKey;
        wstring envInfo;
        vector<int> intervalTimes;
        ConnectionInfo connection;

        g_debugMode = GetDebugMode(g_argc, g_argv);

        if (cmdOptionExists(g_argv, g_argv + g_argc, L"-h") == true
            || cmdOptionExists(g_argv, g_argv + g_argc, L"/h") == true)
        {
            showHelp = true;
            break;
        }

        if (cmdOptionExists(g_argv, g_argv + g_argc, L"-unreg") == true)
        {
            DoUnregistration();
            break;
        }

        if (cmdOptionExists(g_argv, g_argv + g_argc, L"-start") == true)
        {
            DoStartService();
            break;
        }

        if (cmdOptionExists(g_argv, g_argv + g_argc, L"-stop") == true)
        {
            DoStopService();
            break;
        }

        if (cmdOptionExists(g_argv, g_argv + g_argc, L"-update") == true)
        {
            ProcessLatestUpdate();
            break;
        }

        if (cmdOptionExists(g_argv, g_argv + g_argc, L"-restart") == true)
        {
            RestartService();
            break;
        }

        apiKey = GetApiKey(g_argc, g_argv);
        envInfo = GetEnvInfo(g_argc, g_argv);
        intervalTimes = GetIntervalTime(g_argc, g_argv);

        if (apiKey.length() == 0)
        {
            OutputError(L"NO ApiKey\n");
            result = IC_NO_APIKEY;
            showHelp = true;
            break;
        }

        if (envInfo.length() == 0)
        {
            OutputError(L"NO AgentID Info\n");
            result = IC_NO_AGENTIDINFO;
            showHelp = true;
            break;
        }

        string address = GetHostAddress(g_argc, g_argv, connection);
        struct hostent *host = gethostbyname(address.c_str());
        if (host == nullptr)
        {
            OutputError(L"Can't resolve host address: %s\n", address.c_str());
            result = IC_NO_RESOLVE_HOSTADDR;
            break;
        }

        SetupHostPort(g_argc, g_argv, connection);

        if (cmdOptionExists(g_argv, g_argv + g_argc, L"-regservice") == true)
        {
            DoRegistration(apiKey, envInfo, address, connection.Getport(), intervalTimes);
            break;
        }

        struct sockaddr_in remoteServAddr;

        remoteServAddr.sin_family = host->h_addrtype;
        memcpy((char *)&remoteServAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
        remoteServAddr.sin_port = htons((u_short)connection.Getport());

        /* socket creation */
        udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (udpSocket == INVALID_SOCKET) {
            OutputError(L"socket failed with error: %ld\n", WSAGetLastError());
            break;
        }

        wstring appVersion = GetAppVersion(g_moduleFilePath.c_str(), NULL, NULL, NULL, NULL);
        OutputConsole(L"(%s) ServiceExecutionThread - data collect thread - start\n", appVersion.c_str());

        {
            thread processCpuMemThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]()
            {
                ProcessCpuMemInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[0]);
            });

            thread processDiskThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]()
            {
                ProcessDiskInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[1]);
            });

            thread updateThread([]()
            {
                DWORD oneday = 1000 * 60 * 60 * 24;

                while (true)
                {
                    ProcessLatestUpdate();

                    if (::WaitForSingleObject(g_killServiceEvent, oneday) == WAIT_TIMEOUT)
                    {
                        continue;
                    }

                    break;
                }
            });

            if (g_isConsoleApp == TRUE)
            {
                printf("Press any key to exit...\n");
                getchar();
            }
            else
            {
#if _DEBUG
                ::OutputDebugString(L"Service thread - WaitForSingleObject...\n");
#endif
                WaitForSingleObject(g_killServiceEvent, INFINITE);
            }

#if _DEBUG
            ::OutputDebugString(L"Service thread waiting...\n");
            Sleep(1000);
#endif

            if (processCpuMemThread.joinable() == true)
            {
                processCpuMemThread.join();
            }

            if (processDiskThread.joinable() == true)
            {
                processDiskThread.join();
            }

            if (updateThread.joinable() == true)
            {
                updateThread.join();
            }

#if _DEBUG
            ::OutputDebugString(L"All Service-threads exited...\n");
#endif
        }

        ::SetEvent(g_killSafeExitEvent);

#if _DEBUG
        ::OutputDebugString(L"Service thread detached\n");
#endif

    } while (false);

#if _DEBUG
    ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ending\n");
#endif

    if (udpSocket != INVALID_SOCKET)
    {
        closesocket(udpSocket);
    }

    if (showHelp == true)
    {
        ShowHelp();
    }

    WSACleanup();

#if _DEBUG
    ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ended\n");
#endif

    return result;
}