public function run(array $id, $startTime = null, $endTime = null) { $download = \Yii::$app->request->get('download') ? true : false; $ids = is_array($id) ? $id : [$id]; /* @var Storage $storage */ $storage = Module::getInstance()->storage; $startTimestamp = Storage::timestamp($startTime); $endTimestamp = Storage::timestamp($endTime); if ($startTime === null || $startTimestamp >= $endTimestamp) { $startTimestamp = $endTimestamp - $storage->actualDaysCount * Storage::SECONDS_PER_DAY; } $tles = $storage->get($ids, $startTimestamp, $endTimestamp); $foundIds = array_keys($tles); $missedIds = array_values(array_diff($ids, $foundIds)); if (count($missedIds) > 0 && $download) { $storage->update($missedIds, $startTimestamp, $endTimestamp); $tles = $storage->get($ids, $startTimestamp, $endTimestamp); } \Yii::$app->response->format = 'json'; return $tles; }
public function actionUpdate() { /* @var Storage $storage */ $storage = Module::getInstance()->storage; if ($this->existing) { $ids = $storage->getIds(); } elseif ($this->all) { $ids = ['*']; } else { $ids = func_get_args(); } if (empty($ids)) { $this->stdout(Module::t('Satellites\' NORAD identifiers are not specified.') . "\n"); return self::EXIT_CODE_NORMAL; } $startTimestamp = Storage::timestamp($this->startTime); $endTimestamp = Storage::timestamp($this->endTime); if ($this->startTime === null || $startTimestamp >= $endTimestamp) { $startTimestamp = $endTimestamp - $storage->actualDaysCount * Storage::SECONDS_PER_DAY; } try { $this->stdout(sprintf(Module::t('Downloading TLEs for specified satellites and time range %s — %s...'), gmdate('Y-m-d H:i:s', $startTimestamp), gmdate('Y-m-d H:i:s', $endTimestamp)) . "\n"); $tles = $storage->update($ids, $startTimestamp, $endTimestamp); $this->stdout("\n" . Module::t('Downloaded TLEs:') . "\n"); foreach ($tles as $tle) { $this->stdout(str_repeat('-', Storage::TLE_LINE_LENGTH) . "\n"); $this->stdout(gmdate('Y-m-d H:i:s', Storage::getEpochTimestamp($tle)) . "\n"); foreach ($tle as $line) { $this->stdout($line . "\n"); } } } catch (Exception $e) { $this->stdout($e->getMessage() . "\n"); return self::EXIT_CODE_ERROR; } return self::EXIT_CODE_NORMAL; }
/** * Downloads TLEs from Space Track for specified time range and saves them in the storage. * * @param array $ids NORAD identifiers of satellites. * @param int $startTime Unix timestamp of range's start. * @param int $endTime Unix timestamp of range's end. * @return array * @throws Exception */ public function update(array $ids, $startTime, $endTime) { $tles = $this->download($ids, $startTime, $endTime); foreach ($tles as $tle) { if ($this->exists($tle)) { continue; } if (!$this->add($tle)) { throw new Exception(Module::t('Unable to add TLE to storage.')); } } return $tles; }