src/utilities/bcl/RemoteBCL.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #ifndef UTILITIES_BCL_REMOTEBCL_HPP | ||
| 7 | #define UTILITIES_BCL_REMOTEBCL_HPP | ||
| 8 | |||
| 9 | #include "BCL.hpp" | ||
| 10 | #include "../core/Path.hpp" | ||
| 11 | #include "../core/Deprecated.hpp" | ||
| 12 | |||
| 13 | #include <nano/nano_signal_slot.hpp> | ||
| 14 | #include <pugixml.hpp> | ||
| 15 | |||
| 16 | #include <memory> | ||
| 17 | |||
| 18 | namespace openstudio { | ||
| 19 | |||
| 20 | /// This class is used to capture the xml response of a query and store it for later processing. | ||
| 21 | class UTILITIES_API RemoteQueryResponse | ||
| 22 | { | ||
| 23 | public: | ||
| 24 | RemoteQueryResponse(std::shared_ptr<pugi::xml_document>& domDocument); | ||
| 25 | |||
| 26 | pugi::xml_node root() const; | ||
| 27 | |||
| 28 | private: | ||
| 29 | std::shared_ptr<pugi::xml_document> m_domDocument; | ||
| 30 | }; | ||
| 31 | |||
| 32 | struct TaxonomyItem | ||
| 33 | { | ||
| 34 | std::string full_name; | ||
| 35 | int tid; | ||
| 36 | std::string name; | ||
| 37 | |||
| 38 | // index into the static vectors | ||
| 39 | size_t parent; | ||
| 40 | std::vector<size_t> children; | ||
| 41 | }; | ||
| 42 | |||
| 43 | /// Class for accessing the remote BCL. | ||
| 44 | class UTILITIES_API RemoteBCL : public BCL | ||
| 45 | { | ||
| 46 | public: | ||
| 47 | /** @name Constructor */ | ||
| 48 | //@{ | ||
| 49 | |||
| 50 | /// Default constructor | ||
| 51 | RemoteBCL(); | ||
| 52 | |||
| 53 | //@} | ||
| 54 | /** @name Destructor */ | ||
| 55 | //@{ | ||
| 56 | |||
| 57 | /// Virtual destructor | ||
| 58 | 7 | virtual ~RemoteBCL() override = default; | |
| 59 | |||
| 60 | //@} | ||
| 61 | /** @name Inherited members */ | ||
| 62 | //@{ | ||
| 63 | |||
| 64 | /// Get the component by uid | ||
| 65 | virtual boost::optional<BCLComponent> getComponent(const std::string& uid, const std::string& versionId = "") const override; | ||
| 66 | |||
| 67 | /// Get the measure by uid | ||
| 68 | virtual boost::optional<BCLMeasure> getMeasure(const std::string& uid, const std::string& versionId = "") const override; | ||
| 69 | |||
| 70 | /// Perform a meta search on the library to identify number and types of results available. | ||
| 71 | /// The total number of search results available can be used in the search method which requires a page number. | ||
| 72 | boost::optional<BCLMetaSearchResult> metaSearchComponentLibrary(const std::string& searchTerm, const std::string& componentType, | ||
| 73 | const std::string& filterType = "component") const; | ||
| 74 | boost::optional<BCLMetaSearchResult> metaSearchComponentLibrary(const std::string& searchTerm, const unsigned componentTypeTID, | ||
| 75 | const std::string& filterType = "component") const; | ||
| 76 | |||
| 77 | /// Perform a component search of the library, results are returned in 'pages', | ||
| 78 | /// the number of results per page is configurable and a metasearch should be performed | ||
| 79 | /// to determine the total number of results pages available | ||
| 80 | std::vector<BCLSearchResult> searchComponentLibrary(const std::string& searchTerm, const std::string& componentType, const unsigned page = 0) const; | ||
| 81 | std::vector<BCLSearchResult> searchComponentLibrary(const std::string& searchTerm, const unsigned componentTypeTID, const unsigned page = 0) const; | ||
| 82 | |||
| 83 | /// Perform a measure search of the library, results are returned in 'pages', | ||
| 84 | /// the number of results per page is configurable and a metasearch should be performed | ||
| 85 | /// to determine the total number of results pages available | ||
| 86 | std::vector<BCLSearchResult> searchMeasureLibrary(const std::string& searchTerm, const std::string& componentType, const unsigned page = 0) const; | ||
| 87 | std::vector<BCLSearchResult> searchMeasureLibrary(const std::string& searchTerm, const unsigned componentTypeTID, const unsigned page = 0) const; | ||
| 88 | |||
| 89 | /// Compare the versionIds of all downloaded components/measures to the published version online | ||
| 90 | /// and return the number that have updates | ||
| 91 | int checkForComponentUpdates(); | ||
| 92 | int checkForMeasureUpdates(); | ||
| 93 | |||
| 94 | /// Return the uids for components and measures respectively that have updates available | ||
| 95 | std::vector<BCLSearchResult> componentsWithUpdates() const; | ||
| 96 | std::vector<BCLSearchResult> measuresWithUpdates() const; | ||
| 97 | |||
| 98 | /// Replace local components and measures with the latest versions | ||
| 99 | void updateComponents(); | ||
| 100 | void updateMeasures(); | ||
| 101 | |||
| 102 | //@} | ||
| 103 | /** @name Blocking class members */ | ||
| 104 | //@{ | ||
| 105 | |||
| 106 | /// Returns true if there is an internet connection | ||
| 107 | static bool isOnline(); | ||
| 108 | |||
| 109 | /// Returns the last downloaded component if there is one | ||
| 110 | boost::optional<BCLComponent> lastComponentDownload() const; | ||
| 111 | |||
| 112 | /// Returns the last downloaded measure if there is one | ||
| 113 | boost::optional<BCLMeasure> lastMeasureDownload() const; | ||
| 114 | |||
| 115 | /// Returns the last meta search result if there is one | ||
| 116 | boost::optional<BCLMetaSearchResult> lastMetaSearch() const; | ||
| 117 | |||
| 118 | /// Returns the last search results if there are any | ||
| 119 | std::vector<BCLSearchResult> lastSearch() const; | ||
| 120 | |||
| 121 | /// Get the current remote url | ||
| 122 | std::string remoteUrl() const; | ||
| 123 | |||
| 124 | /// Get the remote production url | ||
| 125 | static std::string remoteProductionUrl(); | ||
| 126 | |||
| 127 | /// Get the remote development url | ||
| 128 | static std::string remoteDevelopmentUrl(); | ||
| 129 | |||
| 130 | /// Use the development production server url | ||
| 131 | void useRemoteDevelopmentUrl(); | ||
| 132 | |||
| 133 | /// Use the development production server url | ||
| 134 | void useRemoteProductionUrl(); | ||
| 135 | |||
| 136 | /// Get the current OAuth key | ||
| 137 | std::string authKey() const; | ||
| 138 | |||
| 139 | /// Return production OAuth key | ||
| 140 | std::string prodAuthKey() const; | ||
| 141 | |||
| 142 | /// Set the production OAuth key | ||
| 143 | bool setProdAuthKey(const std::string& prodAuthKey); | ||
| 144 | |||
| 145 | /// Return development OAuth key | ||
| 146 | std::string devAuthKey() const; | ||
| 147 | |||
| 148 | /// Set the development OAuth key | ||
| 149 | bool setDevAuthKey(const std::string& devAuthKey); | ||
| 150 | |||
| 151 | /// Return the number of results per query | ||
| 152 | int resultsPerQuery() const; | ||
| 153 | |||
| 154 | /// Return the total number of results | ||
| 155 | int lastTotalResults() const; | ||
| 156 | |||
| 157 | /// Return the number of pages of results | ||
| 158 | int numResultPages() const; | ||
| 159 | |||
| 160 | unsigned timeOutSeconds() const; | ||
| 161 | bool setTimeOutSeconds(unsigned timeOutSeconds); | ||
| 162 | |||
| 163 | /// Wait number of milliseconds for download to complete | ||
| 164 | /// Returns the download if it completed in the allowable time | ||
| 165 | boost::optional<BCLComponent> waitForComponentDownload() const; | ||
| 166 | OS_DEPRECATED(3, 2, 1) boost::optional<BCLComponent> waitForComponentDownload(int) const; | ||
| 167 | |||
| 168 | /// Wait number of milliseconds for download to complete | ||
| 169 | /// Returns the download if it completed in the allowable time | ||
| 170 | boost::optional<BCLMeasure> waitForMeasureDownload() const; | ||
| 171 | OS_DEPRECATED(3, 2, 1) boost::optional<BCLMeasure> waitForMeasureDownload(int) const; | ||
| 172 | |||
| 173 | /// Wait number of milliseconds for download to complete | ||
| 174 | /// Returns the download if it completed in the allowable time | ||
| 175 | boost::optional<BCLMetaSearchResult> waitForMetaSearch() const; | ||
| 176 | OS_DEPRECATED(3, 2, 1) boost::optional<BCLMetaSearchResult> waitForMetaSearch(int) const; | ||
| 177 | |||
| 178 | /// Wait number of milliseconds for download to complete | ||
| 179 | /// Returns the download if it completed in the allowable time | ||
| 180 | std::vector<BCLSearchResult> waitForSearch() const; | ||
| 181 | OS_DEPRECATED(3, 2, 1) std::vector<BCLSearchResult> waitForSearch(int) const; | ||
| 182 | |||
| 183 | //@} | ||
| 184 | /** @name Non-blocking class members */ | ||
| 185 | //@{ | ||
| 186 | |||
| 187 | /// Starts downloading an individual component by uid, if successful this will start a download | ||
| 188 | bool downloadComponent(const std::string& uid); | ||
| 189 | |||
| 190 | /// Starts downloading an individual measure by uid, if successful this will start a download | ||
| 191 | bool downloadMeasure(const std::string& uid); | ||
| 192 | |||
| 193 | /// Start a meta search, if successful this will start a download | ||
| 194 | bool startComponentLibraryMetaSearch(const std::string& searchTerm, const std::string& componentType, const std::string& filterType); | ||
| 195 | bool startComponentLibraryMetaSearch(const std::string& searchTerm, const unsigned componentTypeTID, const std::string& filterType); | ||
| 196 | |||
| 197 | /// Start a search, if successful this will start a download | ||
| 198 | bool startComponentLibrarySearch(const std::string& searchTerm, const std::string& componentType, const std::string& filterType, | ||
| 199 | const unsigned page = 0); | ||
| 200 | bool startComponentLibrarySearch(const std::string& searchTerm, const unsigned componentTypeTID, const std::string& filterType, | ||
| 201 | const unsigned page = 0); | ||
| 202 | |||
| 203 | //@} | ||
| 204 | // signals: | ||
| 205 | |||
| 206 | /// Emitted when a component download completes | ||
| 207 | Nano::Signal<void(const std::string& uid, const boost::optional<BCLComponent>& component)> componentDownloaded; | ||
| 208 | |||
| 209 | /// Emitted when a measure download completes | ||
| 210 | Nano::Signal<void(const std::string& uid, const boost::optional<BCLMeasure>& measure)> measureDownloaded; | ||
| 211 | |||
| 212 | static const std::vector<TaxonomyItem>& measureTaxonomy(); | ||
| 213 | static const std::vector<TaxonomyItem>& componentTaxonomy(); | ||
| 214 | |||
| 215 | private: | ||
| 216 | − | REGISTER_LOGGER("openstudio.RemoteBCL"); | |
| 217 | |||
| 218 | // explicitly deleted copy constructor | ||
| 219 | RemoteBCL(const RemoteBCL& other) = delete; | ||
| 220 | |||
| 221 | // DLM: once this actually uses the website it will need a non-blocking implementation | ||
| 222 | /// Validate an OAuth key | ||
| 223 | bool validateAuthKey(const std::string& authKey, const std::string& remoteUrl); | ||
| 224 | |||
| 225 | bool waitForLock() const; | ||
| 226 | |||
| 227 | boost::optional<RemoteQueryResponse> processReply(const std::string& reply); | ||
| 228 | |||
| 229 | static boost::optional<BCLMetaSearchResult> processMetaSearchResponse(const RemoteQueryResponse& remoteQueryResponse); | ||
| 230 | |||
| 231 | static std::vector<BCLSearchResult> processSearchResponse(const RemoteQueryResponse& remoteQueryResponse); | ||
| 232 | |||
| 233 | void onDownloadComplete(); | ||
| 234 | |||
| 235 | int setResultsPerQuery(const int numResults); | ||
| 236 | |||
| 237 | void setLastTotalResults(const int lastTotalResults); | ||
| 238 | |||
| 239 | // members | ||
| 240 | |||
| 241 | bool m_lastRequestSuccess = false; | ||
| 242 | |||
| 243 | struct DownloadFile | ||
| 244 | { | ||
| 245 | DownloadFile(const DownloadFile&) = delete; | ||
| 246 | explicit DownloadFile(openstudio::path); | ||
| 247 | void flush(); | ||
| 248 | void close(); | ||
| 249 | const openstudio::path& fileName() const noexcept; | ||
| 250 | void write(const std::string& data); | ||
| 251 | bool open(); | ||
| 252 | |||
| 253 | private: | ||
| 254 | openstudio::path m_fileName; | ||
| 255 | std::ofstream m_ofs; | ||
| 256 | }; | ||
| 257 | |||
| 258 | std::unique_ptr<DownloadFile> m_downloadFile; | ||
| 259 | |||
| 260 | std::string m_downloadUid; | ||
| 261 | |||
| 262 | boost::optional<BCLComponent> m_lastComponentDownload; | ||
| 263 | |||
| 264 | boost::optional<BCLMeasure> m_lastMeasureDownload; | ||
| 265 | |||
| 266 | boost::optional<BCLMetaSearchResult> m_lastMetaSearch; | ||
| 267 | |||
| 268 | std::vector<BCLSearchResult> m_lastSearch; | ||
| 269 | |||
| 270 | std::string m_authKey; | ||
| 271 | |||
| 272 | std::string m_prodAuthKey; | ||
| 273 | |||
| 274 | std::string m_devAuthKey; | ||
| 275 | |||
| 276 | bool m_useRemoteDevelopmentUrl; | ||
| 277 | |||
| 278 | std::string m_remoteUrl; | ||
| 279 | |||
| 280 | int m_numResultsPerQuery; | ||
| 281 | |||
| 282 | int m_lastTotalResults; | ||
| 283 | |||
| 284 | bool validProdAuthKey; | ||
| 285 | |||
| 286 | bool validDevAuthKey; | ||
| 287 | |||
| 288 | std::vector<BCLSearchResult> m_componentsWithUpdates; | ||
| 289 | |||
| 290 | std::vector<BCLSearchResult> m_measuresWithUpdates; | ||
| 291 | |||
| 292 | unsigned m_timeOutSeconds; | ||
| 293 | }; | ||
| 294 | |||
| 295 | } // namespace openstudio | ||
| 296 | |||
| 297 | #endif // UTILITIES_BCL_REMOTEBCL_HPP | ||
| 298 |