|
| 1 | +#include "bart_station_model.h" |
| 2 | +#include "util.h" |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <cstdio> |
| 6 | +#include <cstring> |
| 7 | + |
| 8 | +BartStationModel::Platform::Platform(const String& platformId) |
| 9 | + : platformId_(platformId) {} |
| 10 | + |
| 11 | +void BartStationModel::Platform::update(const JsonObject& root) { |
| 12 | + if (platformId_.isEmpty()) return; |
| 13 | + |
| 14 | + ETDBatch batch; |
| 15 | + const char* dateStr = root["date"] | ""; |
| 16 | + const char* timeStr = root["time"] | ""; |
| 17 | + batch.apiTs = parseHeaderTimestamp(dateStr, timeStr); |
| 18 | + batch.ourTs = bartdepart::util::time_now(); |
| 19 | + |
| 20 | + if (root["station"].is<JsonArray>()) { |
| 21 | + for (JsonObject station : root["station"].as<JsonArray>()) { |
| 22 | + if (!station["etd"].is<JsonArray>()) continue; |
| 23 | + for (JsonObject etd : station["etd"].as<JsonArray>()) { |
| 24 | + if (!etd["estimate"].is<JsonArray>()) continue; |
| 25 | + bool matches = false; |
| 26 | + for (JsonObject est : etd["estimate"].as<JsonArray>()) { |
| 27 | + if (String(est["platform"] | "0") != platformId_) continue; |
| 28 | + matches = true; |
| 29 | + int mins = atoi(est["minutes"] | "0"); |
| 30 | + time_t dep = batch.apiTs + mins * 60; |
| 31 | + TrainColor col = parseTrainColor(est["color"] | ""); |
| 32 | + batch.etds.push_back(ETD{dep, col}); |
| 33 | + } |
| 34 | + if (matches) { |
| 35 | + String dest = etd["destination"] | ""; |
| 36 | + if (!dest.isEmpty()) { |
| 37 | + auto it = std::find(destinations_.begin(), destinations_.end(), dest); |
| 38 | + if (it == destinations_.end()) destinations_.push_back(dest); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + std::sort(batch.etds.begin(), batch.etds.end(), |
| 46 | + [](const ETD& a, const ETD& b){ return a.estDep < b.estDep; }); |
| 47 | + |
| 48 | + history_.push_back(std::move(batch)); |
| 49 | + while (history_.size() > 5) { |
| 50 | + history_.pop_front(); |
| 51 | + } |
| 52 | + |
| 53 | + DEBUG_PRINTF("BartDepart::update platform %s: %s\n", |
| 54 | + platformId_.c_str(), toString().c_str()); |
| 55 | +} |
| 56 | + |
| 57 | +void BartStationModel::Platform::merge(const Platform& other) { |
| 58 | + for (auto const& b : other.history_) { |
| 59 | + history_.push_back(b); |
| 60 | + if (history_.size() > 5) history_.pop_front(); |
| 61 | + } |
| 62 | + |
| 63 | + for (auto const& d : other.destinations_) { |
| 64 | + auto it = std::find(destinations_.begin(), destinations_.end(), d); |
| 65 | + if (it == destinations_.end()) destinations_.push_back(d); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +time_t BartStationModel::Platform::oldest() const { |
| 70 | + if (history_.empty()) return 0; |
| 71 | + return history_.front().ourTs; |
| 72 | +} |
| 73 | + |
| 74 | +const String& BartStationModel::Platform::platformId() const { |
| 75 | + return platformId_; |
| 76 | +} |
| 77 | + |
| 78 | +const std::deque<BartStationModel::Platform::ETDBatch>& |
| 79 | +BartStationModel::Platform::history() const { |
| 80 | + return history_; |
| 81 | +} |
| 82 | + |
| 83 | +const std::vector<String>& BartStationModel::Platform::destinations() const { |
| 84 | + return destinations_; |
| 85 | +} |
| 86 | + |
| 87 | +String BartStationModel::Platform::toString() const { |
| 88 | + if (history_.empty()) return String(); |
| 89 | + |
| 90 | + const ETDBatch& batch = history_.back(); |
| 91 | + const auto& etds = batch.etds; |
| 92 | + if (etds.empty()) return String(); |
| 93 | + |
| 94 | + char nowBuf[20]; |
| 95 | + bartdepart::util::fmt_local(nowBuf, sizeof(nowBuf), batch.ourTs, "%H:%M:%S"); |
| 96 | + int lagSecs = batch.ourTs - batch.apiTs; |
| 97 | + |
| 98 | + String out; |
| 99 | + out += nowBuf; |
| 100 | + out += ": lag "; |
| 101 | + out += lagSecs; |
| 102 | + out += ":"; |
| 103 | + |
| 104 | + time_t prevTs = batch.ourTs; |
| 105 | + |
| 106 | + for (const auto& e : etds) { |
| 107 | + out += " +"; |
| 108 | + int diff = e.estDep - prevTs; |
| 109 | + out += diff / 60; |
| 110 | + out += " ("; |
| 111 | + prevTs = e.estDep; |
| 112 | + |
| 113 | + char depBuf[20]; |
| 114 | + bartdepart::util::fmt_local(depBuf, sizeof(depBuf), e.estDep, "%H:%M:%S"); |
| 115 | + out += depBuf; |
| 116 | + out += ":"; |
| 117 | + out += ::toString(e.color); |
| 118 | + out += ")"; |
| 119 | + } |
| 120 | + return out; |
| 121 | +} |
| 122 | + |
| 123 | +time_t BartStationModel::Platform::parseHeaderTimestamp(const char* dateStr, |
| 124 | + const char* timeStr) const { |
| 125 | + int month=0, day=0, year=0; |
| 126 | + int hour=0, min=0, sec=0; |
| 127 | + char ampm[3] = {0}; |
| 128 | + sscanf(dateStr, "%d/%d/%d", &month, &day, &year); |
| 129 | + sscanf(timeStr, "%d:%d:%d %2s", &hour, &min, &sec, ampm); |
| 130 | + if (strcasecmp(ampm, "PM") == 0 && hour < 12) hour += 12; |
| 131 | + if (strcasecmp(ampm, "AM") == 0 && hour == 12) hour = 0; |
| 132 | + struct tm tm{}; |
| 133 | + tm.tm_year = year - 1900; |
| 134 | + tm.tm_mon = month - 1; |
| 135 | + tm.tm_mday = day; |
| 136 | + tm.tm_hour = hour; |
| 137 | + tm.tm_min = min; |
| 138 | + tm.tm_sec = sec; |
| 139 | + return mktime(&tm) - bartdepart::util::current_offset(); // return UTC |
| 140 | +} |
| 141 | + |
| 142 | +void BartStationModel::update(std::time_t now, BartStationModel&& delta) { |
| 143 | + for (auto &p : delta.platforms) { |
| 144 | + auto it = std::find_if(platforms.begin(), platforms.end(), |
| 145 | + [&](const Platform& x){ return x.platformId() == p.platformId(); }); |
| 146 | + if (it != platforms.end()) { |
| 147 | + it->merge(p); |
| 148 | + } else { |
| 149 | + platforms.push_back(std::move(p)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +time_t BartStationModel::oldest() const { |
| 155 | + time_t oldest = 0; |
| 156 | + for (auto const& p : platforms) { |
| 157 | + time_t o = p.oldest(); |
| 158 | + if (!oldest || (o && o < oldest)) oldest = o; |
| 159 | + } |
| 160 | + return oldest; |
| 161 | +} |
| 162 | + |
| 163 | +std::vector<String> BartStationModel::destinationsForPlatform(const String& platformId) const { |
| 164 | + for (auto const& p : platforms) { |
| 165 | + if (p.platformId() == platformId) { |
| 166 | + return p.destinations(); |
| 167 | + } |
| 168 | + } |
| 169 | + return {}; |
| 170 | +} |
0 commit comments