diff --git a/DAPNETGateway.cpp b/DAPNETGateway.cpp index f6e1af4..07140bc 100644 --- a/DAPNETGateway.cpp +++ b/DAPNETGateway.cpp @@ -22,8 +22,9 @@ #include "Version.h" #include "Thread.h" #include "Timer.h" -#include "Log.h" #include "REGEX.h" +#include "Utils.h" +#include "Log.h" #include "GitVersion.h" #include @@ -260,8 +261,6 @@ int CDAPNETGateway::run() ret = m_pocsagNetwork->open(); if (!ret) { ::LogError("Cannot open the repeater network port"); - ::LogFinalise(); - return 1; } @@ -272,8 +271,6 @@ int CDAPNETGateway::run() if (dapnetAuthKey.length() == 0 || dapnetAuthKey == "TOPSECRET") { ::LogError("AuthKey not set or invalid"); - ::LogFinalise(); - return 1; } @@ -283,10 +280,7 @@ int CDAPNETGateway::run() m_pocsagNetwork->close(); delete m_pocsagNetwork; delete m_dapnetNetwork; - ::LogError("Cannot open the DAPNET network port"); - ::LogFinalise(); - return 1; } @@ -296,10 +290,7 @@ int CDAPNETGateway::run() m_dapnetNetwork->close(); delete m_pocsagNetwork; delete m_dapnetNetwork; - ::LogError("Cannot login to the DAPNET network"); - ::LogFinalise(); - return 1; } @@ -319,8 +310,10 @@ int CDAPNETGateway::run() if (m_regexWhitelist->load()) regexWhitelist = m_regexWhitelist->get(); - LogMessage("DAPNETGateway-%s is starting", VERSION); - LogMessage("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion); + LogInfo("DAPNETGateway-%s is starting", VERSION); + LogInfo("Built %s %s (GitID #%.7s)", __TIME__, __DATE__, gitversion); + + writeJSONStatus("DAPNETGateway is starting"); for (;;) { unsigned char buffer[200U]; @@ -431,14 +424,15 @@ int CDAPNETGateway::run() CThread::sleep(10U); } + LogInfo("DAPNETGateway is stopping"); + writeJSONStatus("DAPNETGateway is stopping"); + m_pocsagNetwork->close(); delete m_pocsagNetwork; m_dapnetNetwork->close(); delete m_dapnetNetwork; - ::LogFinalise(); - return 0; } @@ -624,3 +618,14 @@ bool CDAPNETGateway::sendMessage(CPOCSAGMessage* message) const return true; } } + +void CDAPNETGateway::writeJSONStatus(const std::string& status) +{ + nlohmann::json json; + + json["timestamp"] = CUtils::createTimestamp(); + json["message"] = status; + + WriteJSON("status", json); +} + diff --git a/DAPNETGateway.h b/DAPNETGateway.h index 28ed57a..55d16fd 100644 --- a/DAPNETGateway.h +++ b/DAPNETGateway.h @@ -1,5 +1,5 @@ /* -* Copyright (C) 2018 by Jonathan Naylor G4KLX +* Copyright (C) 2018,2023 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -60,6 +60,8 @@ private: unsigned int calculateCodewords(const CPOCSAGMessage* message) const; void loadSchedule(); bool sendMessage(CPOCSAGMessage* message) const; + + void writeJSONStatus(const std::string& status); }; #endif diff --git a/Utils.cpp b/Utils.cpp index 7276d60..bbecbfd 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -17,6 +17,13 @@ #include #include +#if defined(_WIN32) || defined(_WIN64) +#include +#else +#include +#include +#endif + void CUtils::dump(const std::string& title, const unsigned char* data, unsigned int length) { assert(data != NULL); @@ -70,3 +77,24 @@ void CUtils::dump(int level, const std::string& title, const unsigned char* data } } +std::string CUtils::createTimestamp() +{ + char buffer[100U]; + +#if defined(_WIN32) || defined(_WIN64) + SYSTEMTIME st; + ::GetSystemTime(&st); + + ::sprintf(buffer, "%04u-%02u-%02u %02u:%02u:%02u.%03u", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); +#else + struct timeval now; + ::gettimeofday(&now, NULL); + + struct tm* tm = ::gmtime(&now.tv_sec); + + ::sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d.%03lld", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000LL); +#endif + + return buffer; +} + diff --git a/Utils.h b/Utils.h index 752d4dc..17c1a35 100644 --- a/Utils.h +++ b/Utils.h @@ -21,6 +21,8 @@ public: static void dump(const std::string& title, const unsigned char* data, unsigned int length); static void dump(int level, const std::string& title, const unsigned char* data, unsigned int length); + static std::string createTimestamp(); + private: }; diff --git a/schema.json b/schema.json new file mode 100644 index 0000000..58be79f --- /dev/null +++ b/schema.json @@ -0,0 +1,13 @@ +{ + "$defs": { + "timestamp": {"type": "string"} + }, + + "status": { + "type": "object", + "timestamp": {"$ref": "#/$defs/timestamp"}, + "message": {"type": "string"}, + "required": ["timestamp", "message"] + } +} +