diff --git a/main/external/mtr/arrival.h b/main/external/mtr/arrival.h new file mode 100644 index 0000000..b15ae9d --- /dev/null +++ b/main/external/mtr/arrival.h @@ -0,0 +1,55 @@ +#pragma once +#include "external/mtr/arrival.h" +#include "cJSON.h" +#include "external/mtr/mtr.h" +#include +#include + +// Forward declaration +class MTRNextTrainHandler; + +struct ArrivalInfo { +public: + // Caller transfers ownership of arrival_time to ArrivalInfo + ArrivalInfo( + const std::string& arrival_time, + const std::string& destination_name + ) : _arrival_time(arrival_time) + , _destination_name(destination_name) { } + + const char* arrival_time() const { + return _arrival_time.c_str(); + } + +private: + const std::string _arrival_time; + const std::string _destination_name; // not the code of the station +}; + + +enum StatusEnum { + SUCCESSFUL_WITHOUT_DELAY = 0, + SUCCESSFUL_WITH_DELAY = 1, + FAILED_WITH_MESSAGE = 2, + UNKNOWN_STATUS = 3 +}; + +struct StationArrivalInfo { +public: + friend class MTRNextTrainHandler; + +private: + StationArrivalInfo( + cJSON* mtr_line_station_json, + cJSON* arrival_json, + const std::string& train_line_code, + const std::string& train_station_code + ); + + StatusEnum _status; + std::string _message; // only valid if status == FAILED_WITH_MESSAGE + std::string _train_line; + std::string _train_station; + std::vector _up_arrivals; + std::vector _down_arrivals; +}; diff --git a/main/external/mtr/line_info.h b/main/external/mtr/line_info.h new file mode 100644 index 0000000..2eedb27 --- /dev/null +++ b/main/external/mtr/line_info.h @@ -0,0 +1,45 @@ +#pragma once +#include "cJSON.h" +#include "esp_log.h" +#include "external/mtr/station_info.h" +#include "external/mtr/mtr.h" +#include + +static const char* LINE_INFO_TAG = "LineInfo"; + +// Forward declaration +class MTRNextTrainHandler; +struct StationInfo; + +struct LineInfo { +public: + + // caller does not own the returned char pointers + const char* code() const { + return _code.c_str(); + } + // caller does not own the returned char pointers + const char* color() const { + return _color.c_str(); + } + size_t station_count() const { + return _stations.size(); + } + // caller does not own the returned array or StationInfo pointers + const std::vector* stations() const { + return &_stations; + } + + friend class MTRNextTrainHandler; + +private: + // Caller transfers ownership of stations array and its contents to LineInfo + LineInfo( + cJSON* line_json + ); + + std::string _code; + std::string _color; + std::vector _stations; +}; + diff --git a/main/external/mtr/mtr.h b/main/external/mtr/mtr.h new file mode 100644 index 0000000..a7492d1 --- /dev/null +++ b/main/external/mtr/mtr.h @@ -0,0 +1,56 @@ +#include "assets/mtr_line_station_json.h" +#include "cJSON.h" +#include +#include "esp_log.h" +#include "external/mtr/line_info.h" +#include +#include "network/network.h" + +// Forward declaration +struct StationArrivalInfo; +struct LineInfo; + +enum class MtrArrivalErrorCode { + NONE = 0, + LINE_NOT_FOUND = 1, + STATION_NOT_FOUND = 2, + NO_ARRIVAL_INFO = 3, + UNKNOWN = 99, +}; + +enum class Language { + EN, + TC, +}; + +class MTRNextTrainHandler { +public: + + /** + * @brief Construct a new MTR Next Train Handler object + * @param json Pointer to cJSON object containing MTR Next Train data + * + * > Caller transfers ownership of the cJSON object to MTRNextTrainHandler + * + * cJSON structure for MTR Next Train data + * This structure is used to parse and store the MTR Next Train JSON data. + * Record + */ + MTRNextTrainHandler(); + ~MTRNextTrainHandler(); + + std::vector get_lines(); + + MtrArrivalErrorCode get_next_arrival_info( + NetworkHandler* network_handler, + std::string& line_code, + std::string& station_code, + Language lang = Language::TC, + StationArrivalInfo*& out_info + ); + +private: + cJSON* mtr_data; +}; + + diff --git a/main/external/mtr/station_info.h b/main/external/mtr/station_info.h new file mode 100644 index 0000000..6c75522 --- /dev/null +++ b/main/external/mtr/station_info.h @@ -0,0 +1,26 @@ +#pragma once +#include "esp_log.h" +#include "external/mtr/line_info.h" + +static const char* STATION_INFO_TAG = "StationInfo"; + +// Forward declaration +struct LineInfo; + +struct StationInfo { +public: + StationInfo(cJSON* station_json); + + const char* name() const { return _name.c_str(); } + const char* code() const { return _code.c_str(); } + + friend class LineInfo; + +private: + // Caller transfers ownership of station_name and station_code to StationInfo + StationInfo(std::string& station_name, std::string& station_code) + : _name(station_name), _code(station_code) { } + + std::string _name; + std::string _code; +};